x86 Disassembly
x86 Disassembly
x86 Disassembly
Contents
0.1
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.1.1
What is Wikibooks? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.1.2
0.1.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.1.4
Wikibooks in Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.1.5
Happy Reading!
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.2
Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.3
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.3.1
0.3.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.3.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.3.4
0.3.5
What is Disassembly?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Tools
1.1
1.2
1.3
4
Assemblers and Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.1
Assemblers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.2
Assembler Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.4
1.1.5
Other Assemblers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.6
Compilers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.7
1.2.1
What is a Disassembler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2.2
x86 Disassemblers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2.3
Disassembler Issues
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
1.2.4
Decompilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
1.2.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
1.2.6
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
Disassembly Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
1.3.1
13
1.3.2
13
ii
CONTENTS
1.4
Analysis Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
1.4.1
Debuggers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
1.4.2
Hex Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15
1.4.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
1.4.4
GNU Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
1.4.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
1.4.6
XCode Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
Platforms
19
2.1
Microsoft Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
2.1.1
Microsoft Windows
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
2.1.2
Windows Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
2.1.3
Virtual Memory
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
2.1.4
System Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
2.1.5
20
2.1.6
Win32 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
2.1.7
Native API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
2.1.8
ntoskrnl.exe
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
2.1.9
Win32K.sys
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
21
21
21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
22
22
2.2.1
22
2.2.2
22
2.2.3
PE Files
23
2.2.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
2.2.5
File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
2.2.6
Code Sections
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
26
2.2.7
27
2.2.8
Exports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
27
2.2.9
Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
2.2
2.2.10 Resources
2.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
2.2.11 Relocations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30
30
30
Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
2.3.1
Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
2.3.2
System Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
CONTENTS
2.4
iii
2.3.3
Conguration Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
2.3.4
Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
2.3.5
GUIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
2.3.6
Debuggers
31
2.3.7
File Analyzers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
32
2.4.1
ELF Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
2.4.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
2.4.3
a.out Files
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
Code Patterns
33
3.1
The Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
3.1.1
The Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
3.1.2
33
3.1.3
ESP In Action
33
3.1.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
34
3.1.5
Data Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
34
34
3.2.1
34
3.2.2
34
3.2.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
35
3.2.4
35
3.2.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36
36
3.3.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36
3.3.2
36
Calling Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
3.4.1
Calling Conventions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
3.4.2
Notes on Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
3.4.3
38
3.4.4
39
3.4.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
3.4.6
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40
40
3.5.1
Microsoft C Compiler
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40
3.5.2
GNU C Compiler
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
3.5.3
43
3.5.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
3.5.5
43
3.5.6
. . . . . . . . . . . . . . . . . . . . . .
43
3.5.7
43
Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
3.2
3.3
3.4
3.5
3.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
iv
CONTENTS
3.7
3.8
3.9
3.6.1
Branching
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
3.6.2
If-Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
3.6.3
If-Then-Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
3.6.4
Switch-Case
44
3.6.5
Ternary Operator ?:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46
Branch Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46
3.7.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46
3.7.2
47
3.7.3
Example: Convert To C
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
3.8.1
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
3.8.2
Do-While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
3.8.3
While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
48
3.8.4
For Loops
48
3.8.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
48
Loop Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
49
3.9.1
49
3.9.2
49
3.9.3
49
Data Patterns
51
4.1
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
4.1.1
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
4.1.2
51
4.1.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
4.1.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
4.1.5
52
4.1.6
Floating-Point Values
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
52
4.1.7
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
52
4.1.8
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
4.1.9
Volatile memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3
53
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
54
Variable Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
54
4.2.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
54
4.2.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
54
Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
4.3.1
Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
4.3.2
Arrays
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
4.3.3
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
56
4.3.4
Advanced Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
56
4.3.5
56
CONTENTS
4.3.6
4.4
4.5
4.6
v
Linked Lists and Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
57
57
4.4.1
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
57
4.4.2
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
57
4.4.3
58
58
4.5.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
58
4.5.2
Calling Conventions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
59
4.5.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
61
4.5.4
61
61
4.6.1
61
Diculties
62
5.1
Code Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
5.1.1
Code Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
5.1.2
Stages of Optimizations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
5.1.3
Loop Unwinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
5.1.4
Inline Functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
Optimization Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
5.2.1
63
5.2.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
64
5.2.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
64
5.2.4
65
5.2.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
5.2.6
65
5.2.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
Code Obfuscation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
5.3.1
Code Obfuscation
65
5.3.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
5.3.3
Interleaving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
5.3.4
Non-Intuitive Instructions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
5.3.5
Obfuscators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
5.3.6
Code Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
5.3.7
Opaque Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
5.3.8
Code Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
Debugger Detectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
5.4.1
Detecting Debuggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
5.4.2
IsDebuggerPresent API
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
5.4.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
5.4.4
69
5.4.5
Timeouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
5.2
5.3
5.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
vi
CONTENTS
Detecting SoftICE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
5.4.7
Detecting OllyDbg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
70
6.1
Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
70
6.1.1
Wikimedia Resources
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
70
6.1.2
External Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
70
Licensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71
6.2.1
Licensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71
Manual of Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71
6.3.1
71
6.2
6.3
5.4.6
Global Stylesheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
72
7.1
Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
72
7.2
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
73
7.3
Content license . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74
0.1. PREFACE
0.1 Preface
0.1.1
What is Wikibooks?
CONTENTS
0.1.5
Happy Reading!
0.2 Cover
0.3.4 What Are The Prerequisites?
The Wikibook of
x86 Disassembly
Using C and Assembly Language
Kernel
Library
Library
Library
0.3 Introduction
Programs frequently perform tasks that need to be duplicated, or need to be made to interact with other programs. Without the source code and without adequate
documentation, these tasks can be dicult to accomplish.
This book outlines tools and techniques for attempting to
convert the raw machine code of an executable le into
0.3.1 What Is This Book About?
equivalent code in assembly language and the high-level
This book is about the disassembly of x86 machine code languages C and C++. With the high-level code to perinto human-readable assembly, and the decompilation of form a particular task, several things become possible:
0.3. INTRODUCTION
1. Programs can be ported to new computer platforms,
by compiling the source code in a dierent environment.
2. The algorithm used by a program can be determined. This allows other programs to make use of
the same algorithm, or for updated versions of a program to be rewritten without needing to track down
old copies of the source code.
3. Security holes and vulnerabilities can be identied
and patched by users without needing access to the
original source code.
4. New interfaces can be implemented for old programs. New components can be built on top of old
components to speed development time and reduce
the need to rewrite large volumes of code.
5. We can gure out what a piece of malware does. We
hope this leads us to guring out how to block its
harmful eects. Unfortunately, some malware writers use self-modifying code techniques (polymorphic camouage, XOR encryption, scrambling)[1] ,
apparently to make it dicult to even detect that
malware, much less disassemble it.
Disassembling code has a large number of practical uses.
One of the positive side eects of it is that the reader will
gain a better understanding of the relation between machine code, assembly language, and high-level languages.
Having a good knowledge of these topics will help programmers to produce code that is more ecient and more
secure.
[1] How does a crypter for bypass antivirus detection work?"
Chapter 1
Tools
1.1 Assemblers and Compilers
1.1.1
code, the instructions will be the same, but all the other
helpful information will be lost. The code will be accurate, but more dicult to read.
Compilers, as we will see later, cause even more information to be lost, and decompiling is often so dicult and
convoluted as to become nearly impossible to do accurately.
Assemblers
1.1.2
Assembler Concepts
Assemblers, on a most basic level, translate assembly instructions into machine code with a one to one correspondence. They can also translate named variables into
hard-coded memory addresses and labels into their relative code addresses.
Assemblers, in general, do not perform code optimization. The machine code that comes out of an assembler
is equivalent to the assembly instructions that go into the
assembler. Some assemblers have high-level capabilities
in the form of Macros.
MASM, contrary to popular belief, has been in constant development since 1980, and is upgraded on a
needs-basis.
MASM has always been made compatible by Microsoft to the current platform, and executable le
types.
4
MASM currently supports all Intel instruction sets, GAS is developed specically to be used as the GCC
including SSE2.
backend. Because GCC always feeds it syntactically correct code, GAS often has minimal error checking.
Many users love MASM, but many more still dislike the GAS is available as a part of either the GCC package or
fact that it isn't portable to other systems.
the GNU binutils package.
TASM
HLA
FASM, the Flat Assembler is an open source assembler Some disassemblers and debuggers can disassemble bithat supports x86, and IA-64 Intel architectures.
nary code into HLA-format, although none can faithfully
recreate the HLA macros.
1.1.4
CHAPTER 1. TOOLS
C++, and has the option to compile C++ code into MSIL
(the .NET bytecode).
Microsofts compiler only supports Windows systems,
and Intel-compatible 16/32/64 bit architectures.
The Microsoft C compiler is cl.exe and the linker is
link.exe
1.1.7
The GCC frontend program is gcc (gcc.exe on Windows) and the associated linker is ld (ld.exe on WinThe purpose of this section is to list some of the most dows). Windows cmd searches for the programs with
common C and C++ compilers in use for developing .exe extensions automatically, so you don't need to type
production-level software. There are many many C com- the lename extension.
pilers in the world, but the reverser doesn't need to consider all cases, especially when looking at professional
software. This page will discuss each compilers strengths Listing Files To produce an assembly listing le in
and weaknesses, its availability (download sites or cost GCC, use the following command line syntax:
information), and it will also discuss how to generate an gcc -S /path/to/sourcele.c
assembly listing le from each compiler.
For example, the following commandline:
Microsoft C Compiler
gcc -S test.c
will produce an assembly listing le named test.s. Assembly listing les generated by GCC will be in GAS format. On x86 you can select the syntax with -masm=intel
or -masm=att. GCC listing les are frequently not as well
commented and laid-out as are the listing les for cl.exe.
bugging symbols so you can see the line numbers in the HLA syntax for code examples, but that may change in
listing. The -fno-asynchronous-unwind-tables ag can the future.
help eliminate some macros in the listing.
Intel C Compiler
This compiler is used only for x86, x86-64, and IA-64
code. It is available for both Windows and Linux. The
Intel C compiler was written by the people who invented
the original x86 architecture: Intel. Intels development
tools generate code that is tuned to run on Intel microprocessors, and is intended to squeeze every last ounce of
speed from an application. AMD IA-32 compatible processors are not guaranteed to get the same speed boosts
because they have dierent internal architectures.
ODA is a free, web-based disassembler for a wide variety of architectures. You can use Live View to see
Metrowerks CodeWarrior
how code is disassembled in real time, one byte at
a time, or upload a le. The site is currently in beta
This compiler is commonly used for classic MacOS and
release but will hopefully only get better with time.
for embedded systems. If you try to reverse-engineer a
piece of consumer electronics, you may encounter code
http://www.onlinedisassembler.com
generated by Metrowerks CodeWarrior.
Green Hills Software Compiler
This compiler is commonly used for embedded systems. IDA Pro is a professional disassembler that is expensive,
extremely powerful, and has a whole slew of feaIf you try to reverse-engineer a piece of consumer electures. The downside to IDA Pro is that it costs $515
tronics, you may encounter code generated by Green Hills
US for the standard single-user edition. As such this
C/C++.
wikibook will not consider IDA Pro specically because the price tag is exclusionary. Freeware versions do exist; see below.
1.2.1
What is a Disassembler?
CHAPTER 1. TOOLS
http://www.simtel.net/product.php
ida37fw)
(search
for
Binary Ninja Binary Ninja is a commercial, crossplatform (Linux, OS X, Windows) reverse engineer (version 4.1) http://www.themel.com/idafree.zip
ing platform with aims to oer a similar feature set
to IDA ad a much cheaper price point. It is cur (version
4.3)
http://www.datarescue.be/
rently in a semi-private beta (anyone requesting acidafreeware/freeida43.exe
cess is allowed on the beta) and a precursor written in python is open source (https://github.com/
(version 5.0) http://www.hex-rays.com/idapro/
Vector35/deprecated-binaryninja-python). Curidadownfreeware.htm
rently advertised pricing is $99 for student/noncommercial use, and $399 for commercial use.
BORG Disassembler BORG is an excellent Win32
Disassembler with GUI.
https://binary.ninja/
http://www.caesum.com/
Commercial Freeware/Shareware Windows Disassemblers
HT Editor An analyzing disassembler for Intel x86 instructions. The latest version runs as a console GUI
OllyDbg OllyDbg is one of the most popular disassemprogram on Windows, but there are versions comblers recently. It has a large community and a wide
piled for Linux as well.
variety of plugins available. It emphasizes binary
code analysis. Supports x86 instructions only (no http://hte.sourceforge.net/
x86_64 support for now, although it is on the way).
diStorm64 diStorm is an open source highly optimized
http://www.ollydbg.de/ (ocial website)
stream disassembler library for 80x86 and AMD64.
http://www.openrce.org/downloads/browse/OllyDbg_
http://ragestorm.net/distorm/
Plugins (plugins)
http://www.ollydbg.de/odbg64.html (64 bit version)
Free Windows Disassemblers
http://sourceforge.net/projects/crudasm9/
Capstone Capstone is an open source disassembly
framework for multi-arch (including support for
x86, x86_64) & multi-platform with advanced fea- BeaEngine BeaEngine is a complete disassembler library for IA-32 and intel64 architectures (coded
tures.
in C and usable in various languages : C, Python,
http://www.capstone-engine.org/
Delphi, PureBasic, WinDev, masm, fasm, nasm,
GoAsm).
Objconv A command line disassembler supporting 16,
32, and 64 bit x86 code. Latest instruction set http://www.beaengine.org
(SSE4, AVX, XOP, FMA, etc.), several object le
formats, several assembly syntax dialects. Win- Visual DuxDebugger is a 64-bit debugger disassembler
for Windows.
dows, Linux, BSD, Mac. Intelligent analysis.
9
Objconv See above.
BugDbg is a 64-bit user-land debugger designed to de- ciasdis The ocial name of ciasdis is computer_intelligence_assembler_disassembler.
This
bug native 64-bit applications on Windows.
Forth-based tool allows to incrementally and
http://www.pespin.com/
interactively build knowledge about a code body.
It is unique that all disassembled code can be
re-assembled to the exact same code. Processors
DSMHELP Disassemble Help Library is a disasare 8080, 6809, 8086, 80386, Pentium I en DEC
sembler library with single line Epimorphic
Alpha. A scripting facility aids in analyzing Elf and
assembler.
Supported instruction sets - BaMSDOS headers and makes this tool extendable.
sic,System,SSE,SSE2,SSE3,SSSE3,SSE4,SSE4A,MMX,FPU,3DNOW,VMX,SVM,AVX,AVX2,BMI1,BMI2,F16C,FMA3,FMA
The Pentium I ciasdis is available as a binary image,
http://dsmhelp.narod.ru/ (in Russian)
others are in source form, loadable onto lina Forth,
available from the same site.
ArkDasm is a 64-bit interactive disassembler and dehttp://home.hccnet.nl/a.w.m.van.der.horst/ciasdis.html
bugger for Windows. Supported processor: x64 architecture (Intel x64 and AMD64)
objdump comes standard, and is typically used for general inspection of binaries. Pay attention to the relohttp://www.arkdasm.com/
cation option and the dynamic symbol table option.
SharpDisam is a C# port of the udis86 x86 / x86-64
disassembler
http://sharpdisasm.codeplex.com/
Unix Disassemblers
Many of the Unix disassemblers, especially the open lida linux interactive disassembler an interactive dissource ones, have been ported to other platforms, like
assembler with some special functions like a crypto
Windows (mostly using MinGW or Cygwin). Some Disanalyzer. Displays string data references, does code
assemblers like otool (OS X) are distro-specic.
ow analysis, and does not rely on objdump. Utilizes the Bastard disassembly library for decoding
single opcodes. The project was started in 2004 and
Capstone Capstone is an open source disassembly
remains dormant to this day.
framework for multi-arch (including support for
x86, x86_64) & multi-platform (including Mac
http://lida.sourceforge.net
OSX, Linux, *BSD, Android, iOS, Solaris) with advanced features.
dissy This program is a interactive disassembler that
uses objdump.
http://www.capstone-engine.org/
http://code.google.com/p/dissy/
Bastard Disassembler The Bastard disassembler is a
powerful, scriptable disassembler for Linux and EmilPRO replacement for the deprecated dissy disasFreeBSD.
sembler.
http://bastard.sourceforge.net/
http://github.com/SimonKagstrom/emilpro
ndisasm NASMs disassembler for x86 and x86-64. x86dis This program can be used to display binary
streams such as the boot sector or other unstructured
Works on DOS, Windows, Linux, Mac OS X and
binary les.
various other systems.
ldasm LDasm (Linux Disassembler) is a Perl/Tk-based
GUI for objdump/binutils that tries to imitate the
'look and feel' of W32Dasm. It searches for crosshttp://udis86.sourceforge.net/
references (e.g. strings), converts the code from
GAS to a MASM-like style, traces programs and
ZyanDisassembler Engine (Zydis) Fast
and
much more. Comes along with PTrace, a processlightweight x86/x86-64 disassembler library.
ow-logger. Last updated in 2002, available from
https://github.com/zyantific/zyan-disassembler-engine
Tucows.
udis86 Disassembler Library for x86 and x86-64
10
CHAPTER 1. TOOLS
http://www.tucows.com/preview/59983/LDasm
llvm LLVM has two interfaces to its disassembler:
that is often used is to identify the entry point of an executable, and nd all code reachable from there, recursively. This is known as code crawling.
Many interactive disassemblers will give the user the option to render segments of code as either code or data,
llvm-mc See the LLVM blog. Example usage:
but non-interactive disassemblers will make the separa$ echo '1 2' | llvm-mc -disassemble - tion automatically. Disassemblers often will provide the
triple=x86_64-apple-darwin9
instruction AND the corresponding hex data on the same
addl %eax, (%rdx)
line, shifting the burden for decisions about the nature of
$ echo '0x0f 0x1 0x9' | llvm-mc -disassemble the code to the user. Some disassemblers (e.g. ciasdis)
-triple=x86_64-apple-darwin9
will allow you to specify rules about whether to disassemsidt (%rcx)
ble as data or code and invent label names, based on the
$ echo '0x0f 0xa2' | llvm-mc -disassemble content of the object under scrutiny. Scripting your own
-triple=x86_64-apple-darwin9
crawler in this way is more ecient; for large programs
cpuid
interactive disassembling may be impractical to the point
$ echo '0xd9 0x' | llvm-mc -disassemble of being unfeasible.
-triple=i386-apple-darwin9
The general problem of separating code from data in arfcos
bitrary executable programs is equivalent to the halting
problem. As a consequence, it is not possible to write
a disassembler that will correctly separate code and data
otool OS Xs object le displaying tool.
for all possible input programs. Reverse engineering is
full of such theoretical limitations, although by Rices theorem all interesting questions about program properties
edb A cross platform x86/x86-64 debugger.
are undecidable (so compilers and many other tools that
https://github.com/eteran/edb-debugger
deal with programs in any form run into such limits as
well). In practice a combination of interactive and autox64dbg An open-source x64/x32 debugger for windows. matic analysis and perseverance can handle all but programs specically designed to thwart reverse engineerhttp://x64dbg.com
ing, like using encryption and decrypting code just prior
to use, and moving code around in memory.
llvm-objdump Mimics GNU objdump.
1.2.3
Disassembler Issues
Lost Information
As we have alluded to before, there are a number of issues
and diculties associated with the disassembly process. User dened textual identiers, such as variable names,
The two most important diculties are the division be- label names, and macros are removed by the assembly
tween code and data, and the loss of text information.
process. They may still be present in generated object
les, for use by tools like debuggers and relocating linkers, but the direct connection is lost and re-establishing
Separating Code from Data
that connection requires more than a mere disassembler.
Especially small constants may have more than one posSince data and instructions are all stored in an executable sible name. Operating system calls (like DLLs in MSas binary data, the obvious question arises: how can a dis- Windows, or syscalls in Unices) may be reconstructed, as
assembler tell code from data? Is any given byte a vari- their names appear in a separate segment or are known
able, or part of an instruction?
beforehand. Many disassemblers allow the user to attach
The problem wouldn't be as dicult if data were limited a name to a label or constant based on his understanding
to the .data section (segment) of an executable (explained of the code. These identiers, in addition to comments
in a later chapter) and if executable code were limited to in the source le, help to make the code more readable
the .code section of an executable, but this is often not to a human, and can also shed some clues on the purthe case. Data may be inserted directly into the code sec- pose of the code. Without these comments and identition (e.g. jump address tables, constant strings), and exe- ers, it is harder to understand the purpose of the source
cutable code may be stored in the data section (although code, and it can be dicult to determine the algorithm
new systems are working to prevent this for security rea- being used by that code. When you combine this problem
sons). AI programs, LISP or Forth compilers may not with the possibility that the code you are trying to read
contain .text and .data sections to help decide, and have may, in reality, be data (as outlined above), then it can
code and data interspersed in a single section that is read- be even harder to determine what is going on. Another
able, writable and executable, Boot code may even re- challenge is posed by modern optimising compilers; they
quire substantial eort to identify sections. A technique inline small subroutines, then combine instructions over
1.2.4
Decompilers
11
Intel x86, ARM, MIPS, PIC32, and PowerPC architectures and outputs C or Python-like code, plus
ow charts and control ow graphs. It puts a running
time limit on each decompilation. It produces nice
results in most cases.
https://github.com/uxmal/reko
In the face of optimizing compilers, it is not uncommon C4Decompiler C4Decompiler is an interactive, static
decompiler under development (Alpha in 2013). It
to be asked Is decompilation even possible?" To some
performs global analysis of the binary and presents
degree, it usually is. Make no mistake, however: an opthe resulting C source in a Windows GUI. Context
timizing compiler results in the irretrievable loss of inmenus support navigation, properties, cross referformation. An example is in-lining, as explained above,
ences, C/Asm mixed view and manipulation of the
where code called is combined with its surroundings, such
decompile context (function ABI).
that the places where the original subroutine is called cannot even be identied. An optimizer that reverses that http://www.c4decompiler.com
process is comparable to an articial intelligence program
that recreates a poem in a dierent language. So perfectly
Boomerang Decompiler Project Boomerang Decomoperational decompilers are a long way o. At most, curpiler is an attempt to make a powerful, retargetable
rent Decompilers can be used as simply an aid for the
decompiler. So far, it only decompiles into C with
reverse engineering process leaving lots of arduous work.
moderate success.
Common Decompilers
http://boomerang.sourceforge.net/
Hex-Rays Decompiler Hex-Rays is a commercial de- Reverse Engineering Compiler (REC) REC is a powerful decompiler that decompiles native assembly
compiler. It is made as an extension to popular
code into a C-like code representation. The code is
IDA-Pro disassembler. It is currently the only vihalf-way between assembly and C, but it is much
able commercially available decompiler which promore readable than the pure assembly is. Unfortuduces usable results. It supports both x86 and ARM
nately the program appears to be rather unstable.
architecture.
http://www.hex-rays.com/products/decompiler/index.
shtml
http://www.backerstreet.com/rec/rec.htm
ExeToC ExeToC decompiler is an interactive decompiler that boasted pretty good results in the past.
DCC DCC is likely one of the oldest decompilers in existence, dating back over 20 years. It serves as a
http://sourceforge.net/projects/exetoc
good historical and theoretical frame of reference
for the decompilation process in general (Mirrors: ). snowman Snowman is an open source native code to
As of 2015, DCC is an active project. Some of the
C/C++ decompiler. Supports ARM, x86, and x86latest changes include xes for longstanding mem64 architectures. Reads ELF, Mach-O, and PE le
ory leaks and a more modern Qt5-based front-end.
formats. Reconstructs functions, their names and
arguments, local and global variables, expressions,
RetDec The Retargetable Decompiler is a freeware web
integer, pointer and structural types, all types of
decompiler that takes in ELF/PE/COFF binaries in
control-ow structures, including switch. Has a nice
12
CHAPTER 1. TOOLS
graphical user interface with one-click navigation
between the assembler code and the reconstructed
program. Has a command-line interface for batch
processing.
https://derevenets.com
From a human disassemblers point of view, this is a
nightmare, although this is straightforward to read in the
1.2.5 A General view of Disassembling
original Assembly source code, as there is no way to decide if the db should be interpreted or not from the binary
8 bit CPU code
form, and this may contain various jumps to real executable code area, triggering analysis of code that should
Most CPUs are 8-bit CPUs.[1]
never be analysed, and interfering with the analysis of the
Normally when a subroutine is nished, it returns to ex- real code (e.g. disassembling the above code from 0000h
ecuting the next address immediately following the call or 0001h won't give the same results at all).
instruction.
However a half-decent tool with possibilities to speciy
However, assembly-language programmers occasionally rules, and heuristic means to identify texts will have little
use several dierent techniques that adjust the return ad- trouble.
dress, making disassembly more dicult:
32 bit CPU code
jump tables,
calculated jumps, and
a parameter after the call instruction.
13
For OS X/BSD systems, there is a rough equiv- Here are examples of C and C++ Hello World!" proalent called otool in the XCode kit.
grams.
Disassemblers at DMOZ lists a huge number of dis- #include <stdio.h> int main() { printf(Hello World!\n);
return 0; }
assemblers
#include <iostream> int main() { std::cout << Hello
Program transformation wiki: disassembly lists World!\n"; return 0; }
many highly recommended disassemblers
search for disassemble at SourceForge shows
many disassemblers for a variety of CPUs.
1.3.2
Hopper is a disassembler that runs on OS-X and disWrite a basic Hello World!" program (see the example
assembles 32/64-bit OS-X and windows binaries.
above). Compile the program into an executable with
The University of Queensland Binary Translator your favorite compiler, then disassemble it. How big is
(UQBT) is a reusable, component-based binary- the disassembled code le? How does it compare to the
translation framework that supports CISC, RISC, code from the listing le you generated? Can you explain
why the le is this size?
and stack-based processors.
1.2.6
Further reading
1.4.1 Debuggers
[3] Tom Krazit. ARMed for the living room. ARM licensed 1.6 billion cores [in 2005]". 2006.
the Open Source Institute occasionally has reverse tures. Debuggers often allow the user to set breakpoints
engineering challenges among its other brainteasers. on instructions, function calls, and even memory locations.
The Program Transformation wiki has a Reverse enA breakpoint is an instruction to the debugger that allows
gineering and Re-engineering Roadmap, and disprogram execution to be halted when a certain condition
cusses disassemblers, decompilers, and tools for
is met. For instance, when a program accesses a certain
translating programs from one high-level language
variable, or calls a certain API function, the debugger can
to another high-level language.
pause program execution.
Other disassemblers with multi-platform support
Windows Debuggers
1.3.1
WinDbg WinDbg is a free piece of software from Microsoft that can be used for local user-mode deWrite a simple Hello World program using C or C++
bugging, or even remote kernel-mode debugging.
and your favorite compiler. Generate a listing le from
WinDbg is not the same as the better-known Vithe compiler. Does the code look the way you expect it
sual Studio Debugger, but comes with a nifty GUI
to? Do you understand what the assembly code means?
nonetheless. Available in 32 and 64-bit versions.
14
http://www.microsoft.com/whdc/devtools/debugging/
installx86.mspx
CHAPTER 1. TOOLS
NLKD A kernel debugger.
http://forge.novell.com/modules/xfmod/project/?nlkd
Many of the open source debuggers on Linux, again, are ladebug An enhanced debugger on Tru64 Unix systems from HP (originally Digital Equipment Corcross-platform. They may be available on some other
poration) that handles advanced functionality like
Unix(-like) systems, or even Windows. Some of the dethreads better than dbx.
buggers may give you better experience than the old and
native ones on your system.
DTrace An advanced tool on Solaris that provides funcgdb The GNU debugger, comes with any normal Linux
tions like proling and many others on the entire sysinstall. It is quite powerful and even somewhat protem, including the kernel.
grammable, though the raw user interface is harsh.
lldb LLVMs debugger.
emacs The GNU editor, can be used as a front-end to
gdb. This provides a powerful hex editor and allows
full scripting in a LISP-like language.
ddd The Data Display Debugger. Its another front-end
to gdb. This provides graphical representations of
data structures. For example, a linked list will look
just like a textbook illustration.
strace, ltrace, and xtrace Lets you run a program
while watching the actions it performs. With strace,
you get a log of all the system calls being made.
With ltrace, you get a log of all the library calls being made. With xtrace, you get a log of some of the
funtion calls being made.
valgrind Executes a program under emulation, performing analysis according to one of the many plugin modules as desired. You can write your own plugin module as desired. Newer versions of valgrind
also support OS X.
15
For instance, lets say that in your program you consis- http://www.bpsoft.com/
tantly experience crashes after one particular event: immediately after closing a message box. You set break- Tiny Hexer Free and does statistics. For Windows.
points on all calls to MessageBoxA. You run your program with the breakpoints set, and it stops, ready to call http://www.mirkes.de/files/
MessageBoxA. Executing each line one-by-one thereafter
(referred to as stepping) through the code, and watching frhed - free hex editor For Windows. Free and openthe program stack, you see that a buer overow occurs
source.
soon after the call.
http://www.kibria.de/frhed.html
1.4.2
Hex Editors
Hex editors are able to directly view and edit the binary
of a source le, and are very useful for investigating the http://www.softcircuits.com/cygnus/fe/
structure of proprietary closed-format data les. There
are many hex editors in existence. This section will at- Hexprobe Hex Editor For Windows. A professional
hex editor designed to include all the power to deal
tempt to list some of the best, some of the most popular,
with hex data, particularly helpful in the areas of
or some of the most powerful.
hex-byte editing and byte-pattern analysis.
HxD (Freeware) For Windows. A fast and powerful
free hex, disk and RAM editor
http://mh-nexus.de/hxd/
Freeware Hex Editor XVI32 For Windows. A freeware hex editor.
http://www.hexprobe.com/hexprobe/index.htm
UltraEdit32 For Windows. A hex editor/text editor,
won Application of the Year at 2005 Shareware
Industry Awards Conference.
http://www.ultraedit.com/
http://www.chmaas.handshake.de/delphi/freeware/
xvi32/xvi32.htm
BreakPoint Hex Workshop For Windows. An excel- 1Fh For Windows. A free binary/hex editor which is
lent and powerful hex-editor, its usefulness is revery fast, even while working with large les. Its
stricted by the fact that it is not free like some of
the only Windows hex editor that allows you to view
the other options.
les in byte code (all 256-characters).
16
CHAPTER 1. TOOLS
Linux Hex Editors only
bvi A typical three-pane hex editor, with a vi-like interface.
emacs Along with everything else, emacs also includes
a hex editor.
joe Joes own editor now also supports hex editing.
bless A very capable gtk based hex editor.
http://www.4neurons.com/1Fh/
xxd and any text editor Produce a hex dump with xxd,
freely edit it in your favorite text editor, and then
convert it back to a binary le with your changes
included.
HexEdit For Windows (Open source) and shareware GHex Hex editor for GNOME.
versions. Powerful and easy to use binary le and
http://directory.fsf.org/All_Packages_in_Directory/
disk editor.
ghex.html
http://www.hexedit.com/
HexToolkit For Windows. A free hex viewer specically designed for reverse engineering le formats.
Allows data to be viewed in various formats and includes an expression evaluator as well as a binary le
comparison tool.
http://www.binaryearth.net/HexToolkit
http://biew.sourceforge.net/en/biew.html
Hex Fiend For MacOS. A very simple hex editor, but
incredibly powerful nonetheless. Its only 346 KB to
hview A curses based hex editor designed to work with
download and takes les as big as 116 GB.
large (600+MB) les with as quickly, and with little
http://ridiculousfish.com/hexfiend/
overhead, as possible.
17
http://web.archive.org/web/20010306001713/http:
//tdistortion.esmartdesign.com/Zips/hview.tgz
rohitab.com API Monitor API Monitor is a free software that lets you monitor and control API calls
made by applications and services. Features include
detailed parameter information, structures, unions,
HexCurse An ncurses-based hex editor written in C
enumerated/ag data types, call stack, call tree,
that currently supports hex and decimal address outbreakpoints, custom DLLs, memory editor, call lput, jumping to specied le locations, searching,
tering, COM monitoring, 64-bit. Includes deniASCII and EBCDIC output, bolded modications,
tions for over 13,000 APIs and 1,300+ COM interan undo command, quick keyboard shortcuts, etc.
faces.
http://www.jewfish.net/description.php?title=
http://www.rohitab.com/apimonitor
HexCurse
hexedit View and edit les in hexadecimal or in ASCII. PE File Header dumpers
http://rigaux.org/hexedit.html
VCHE A hex editor which lets you see all 256 characters
It is frequently useful to simply create a batch
as found in video ROM, even control and extended
le that handles this conversion:
ASCII, it uses the /dev/vcsa* devices to do it. It also
could edit non-regular les, like hard disks, oppies, ::dumpbin.bat link.exe /dump %*
CDROMs, ZIPs, RAM, and almost any device. It
All examples in this wikibook that use dumpbin will
comes with a ncurses and a raw version for people
call it in this manner.
who work under X or remotely.
http://www.grigna.com/diego/linux/vche/
dumpbin /EXPORTS displays a list of functions exDHEX DHEX is just another Hexeditor with a Diported from a library dumpbin /IMPORTS displays a
mode for ncurses. It makes heavy use of colors and
list of functions imported from other libraries dumpbin
is themeable.
/HEADERS displays PE header information for the executable
http://www.dettus.net/dhex/
1.4.3
http://msdn.microsoft.com/library/default.
asp?url=/library/en-us/vccore/html/_core_
dumpbin_reference.asp
Resource Monitors
Depends Dependency Walker is a GUI tool which will
SysInternals Freeware This page has a large number
allow you to see exports and imports of binaries. It
of excellent utilities, many of which are very useships with many Microsoft tools including MS Viful to security experts, network administrators, and
sual Studio.
(most importantly to us) reversers. Specically,
check out Process Monitor, FileMon, RegMon,
1.4.4 GNU Tools
TCPView, and Process Explorer.
http://technet.microsoft.com/sysinternals/default.aspx
API Monitors
18
CHAPTER 1. TOOLS
http://www.gnu.org/software/binutils/
objdump Dumps out information about an executable XCode contains some extra tools to be used unincluding symbols and assembly. It comes standard. der OS X with the Mach-O format. You can see
It can be made to support non-native binary formats. more of them under /Applications/Xcode.app/Contents/
Developer/usr/bin/.
objdump -p displays a list of functions imported from
other libraries, exported to and miscellaneous le header lipo Manages fat binaries with multiple architectures.
information
Its useful to check dll dependencies from command line
otool Object le displaying tool, works somehow like objdump and readelf.
1.4.5
Chapter 2
Platforms
2.1 Microsoft Windows
19
20
CHAPTER 2. PLATFORMS
more-primitive functions in GDI32. This and NTDLL GDI diverts most of its calls into WIN32K, but it does
both provide an interface to the Windows NT kernel, contain a manager for GDI objects, such as pens, brushes
NTOSKRNL (see further below).
and device contexts. The GDI object manager and the
NTOSKRNL is also partially layered on HAL (Hardware KERNEL object manager are completely separate.
Abstraction Layer), but this interaction will not be considered much in this book. The purpose of this layering is to allow processor variant issues (such as location
of resources) to be made separate from the actual kernel itself. A slightly dierent system conguration thus
requires just a dierent HAL module, rather than a completely dierent kernel module.
2.1.5
user32.dll
The USER subsystem is located in the user32.dll library
le. This subsystem controls the creation and manipulation of USER objects, which are common screen items
such as windows, menus, cursors, etc... USER will set
up the objects to be drawn, but will perform the actual
drawing by calling on GDI (which in turn will make many
calls to WIN32K) or sometimes even calling WIN32K
directly. USER utilizes the GDI Object Manager.
The native API, hereby referred to as the NTDLL subsystem, is a series of undocumented API function calls
that handle most of the work performed by KERNEL32.
Microsoft also does not guarantee that the native API will
remain the same between dierent versions, as Windows
developers modify the software. This gives the risk of native API calls being removed or changed without warning,
Both Intel and AMD provide an extra set of instructions breaking software that utilizes it.
to allow faster system calls, the SYSENTER instruction
from Intel and the SYSCALL instruction from AMD.
ntdll.dll
The NTDLL subsystem is located in ntdll.dll. This library contains many API function calls, that all follow a
particular naming scheme. Each function has a prex:
Both WinNT and Win9x systems utilize the Win32 API.
Ldr, Nt, Zw, Csr, Dbg, etc... and all the functions that
However, the WinNT version of the API has more funchave a particular prex all follow particular rules.
tionality and security constructs, as well as Unicode support. Most of the Win32 API can be broken down into 3 The ocial native API is usually limited only to functions whose prex is Nt or Zw. These calls are in fact the
separate components, each performing a separate task.
same in user-mode: the relevant Export entries map to
the same address in memory. However, in kernel-mode,
kernel32.dll
the Zw* system call stubs set the previous mode to kernelmode, ensuring that certain parameter validation routines
Kernel32.dll, home of the KERNEL subsystem, is where are not performed. The origin of the prex Zw is unnon-graphical functions are implemented. Some of the known; this prex was chosen due to its having no signifAPIs located in KERNEL are: The Heap API, the Vir- icance at all[1] .
tual Memory API, File I/O API, the Thread API, the System Object Manager, and other similar system services. In actual implementation, the system call stubs merely
Most of the functionality of kernel32.dll is implemented load two registers with values required to describe a nain ntdll.dll, but in undocumented functions. Microsoft tive API call, and then execute a software interrupt (or
prefers to publish documentation for kernel32 and guar- the sysenter instruction).
antee that these APIs will remain unchanged, and then Most of the other prexes are obscure, but the known
put most of the work in other libraries, which are then ones are:
not documented.
Rtl stands for Run Time Library, calls which help
functionality at runtime (such as RtlAllocateHeap)
gdi32.dll
Csr is for Client Server Runtime, which repregdi32.dll is the library that implements the GDI subsyssents the interface to the win32 subsystem located
tem, where primitive graphical operations are performed.
in csrss.exe
2.1.6
Win32 API
21
Dbg functions are present to enable debugging rou- Vista may be better known by its development code-name
tines and operations
Longhorn. Microsoft claims that Vista has been written largely from the ground up, and therefore it can be
Ldr provides the ability to load, manipulate and re- assumed that there are fundamental dierences between
trieve data from DLLs and other module resources the Vista API and system architecture, and the APIs and
architectures of previous Windows versions. Windows
Vista was released January 30th, 2007.
User Mode Versus Kernel Mode
Many functions, especially Run-time Library routines,
are shared between ntdll.dll and ntoskrnl.exe. Most Native API functions, as well as other kernel-mode only
functions exported from the kernel are useful for driver
writers. As such, Microsoft provides documentation on
many of the native API functions with the Microsoft
Server 2003 Platform DDK. The DDK (Driver Development Kit) is available as a free download.
2.1.8
ntoskrnl.exe
2.1.9
Recent windows service packs have attempted to implement a system known as Non-executable memory
where certain pages can be marked as being nonexecutable. The purpose of this system is to prevent
some of the most common security holes by not allowing control to pass to code inserted into a memory buer
by an attacker. For instance, a shellcode loaded into an
overowed text buer cannot be executed, stopping the
attack in its tracks. The eectiveness of this mechanism
is yet to be seen, however.
Win32K.sys
2.1.10
Win64 API
22
CHAPTER 2. PLATFORMS
data structures exported by a COM component are dicult to perceive by simply inspecting the executable le.
Matters are made worse if the creating programmer has
used a library such as ATL to simplify their programming
experience. Unfortunately for a reverse engineer, this reduces the contents of an executable into a Sea of bits,
with pointers and data structures everywhere.
oset specifying an oset into that window. The segment register would be set by DOS and the COM le
would be expected to respect this setting and not ever
change the segment registers. The oset registers, however, were fair game and served (for COM les) the same
purpose as a modern 32-bit register. The downside was
that the oset registers were only 16-bit and, therefore,
since COM les could not change the segment registers,
COM les were limited to using 64K of RAM. The good
thing about this approach, however, was that no extra
2.1.15 Remote Procedure Calls (RPC)
work was needed by DOS to load and run a COM le:
RPC is a generic term referring to techniques that allow a just load the le, set the segment register, and jump to it.
program running on one machine to make calls that actu- (The programs could perform 'near' jumps by just giving
ally execute on another machine. Typically, this is done an oset to jump to.)
by marshalling all of the data needed for the procedure in- COM les are loaded into RAM at oset $100. The
cluding any state information stored on the rst machine, space before that would be used for passing data to and
and building it into a single data structure, which is then from DOS (for example, the contents of the command
transmitted over some communications method to a sec- line used to invoke the program).
ond machine. This second machine then performs the requested action, and returns a data packet containing any Note that COM les, by denition, cannot be 32-bit.
results and potentially changed state information to the Windows provides support for COM les via a special
CPU mode.
originating machine.
In Windows NT, RPC is typically handled by having two
libraries that are similarly named, one which generates
RPC requests and accepts RPC returns, as requested by
a user-mode program, and one which responds to RPC
requests and returns results via RPC. A classic example
is the print spooler, which consists of two pieces: the
RPC stub spoolss.dll, and the spooler proper and RPC
service provider, spoolsv.exe. In most machines, which
are stand-alone, it would seem that the use of two modules communicating by means of RPC is overkill; why
not simply roll them into a single routine? In networked
printing, though, this makes sense, as the RPC service
provider can be resident physically on a distant machine,
with the remote printer, and the local machine can control the printer on the remote machine in exactly the same
way that it controls printers on the local machine.
[1] https://msdn.microsoft.com/en-us/library/windows/
hardware/ff565646(v=vs.85).aspx
2.2.1
23
2.2.3
PE Files
2.2.4
2.2.5
File Format
What is this?
24
CHAPTER 2. PLATFORMS
The DOS header is also known by some as the EXE The rst big chunk of information lies in the COFF
header. Here is the DOS header presented as a C data header, directly after the PE signature.
structure:
struct DOS_Header { // short is 2 bytes, long is 4 bytes
char signature[2] = MZ"; short lastsize; short nblocks;
short nreloc; short hdrsize; short minalloc; short maxalloc; void *ss; void *sp; short checksum; void *ip; void
*cs; short relocpos; short noverlay; short reserved1[4];
short oem_id; short oem_info; short reserved2[10]; long
e_lfanew; }
COFF Header
The COFF header is present in both COFF object les
(before they are linked) and in PE les where it is known
as the File header. The COFF header has some information that is useful to an executable, and some information that is more useful to an object le.
Here is the COFF header, presented as a C data structure:
struct COFFHeader { short Machine; short NumberOfSections; long TimeDateStamp; long PointerToSymbolTable; long NumberOfSymbols; short
SizeOfOptionalHeader; short Characteristics; }
PE Header
25
AddressOfEntryPoint A pointer to the entry point
function, relative to the image base address. For executable les, this is the starting address. For device
drivers, this is the address of the initialization function. The entry point function is optional for DLLs.
When no entry point is present, this member is zero.
Signature Contains a signature that identies the im- MajorSubsystemVersion The major version number
age.
of the subsystem.
MajorLinkerVersion The major version number of the MinorSubsystemVersion The minor version number
of the subsystem.
linker.
MinorLinkerVersion The minor version number of Win32VersionValue This member is reserved and
must be 0.
the linker.
SizeOfCode The size of the code section, in bytes, or SizeOfImage The size of the image, in bytes, including all headers. Must be a multiple of SectionAlignthe sum of all such sections if there are multiple code
ment.
sections.
SizeOfInitializedData The size of the initialized data SizeOfHeaders The combined size of the following
items, rounded to a multiple of the value specied
section, in bytes, or the sum of all such sections if
in the FileAlignment member.
there are multiple initialized data sections.
SizeOfUninitializedData The size of the uninitialized
data section, in bytes, or the sum of all such sections
if there are multiple uninitialized data sections.
26
size of COFFHeader
CHAPTER 2. PLATFORMS
.data 000A0000_00000000_00000000_00000000_40000000
2E627373_00000000_00200000_00300000_00000000
.bss 00000000_00000000_00000000_00000000_80000000
2.2.7
What is linking?
Whenever a developer writes a program, there are a number of subroutines and functions which are expected to be
implemented already, saving the writer the hassle of having to write out more code or work with complex data
structures. Instead, the coder need only declare one call
to the subroutine, and the linker will decide what happens
next.
There are two types of linking that can be used: static and
dynamic. Static uses a library of precompiled functions.
This precompiled code can be inserted into the nal executable to implement a function, saving the programmer
a lot of time. In contrast, dynamic linking allows subroutine code to reside in a dierent le (or module), which
27
is loaded at runtime by the operating system. This is also
known as a Dynamically linked library, or DLL. A library is a module containing a series of functions or values that can be exported. This is dierent from the term
executable, which imports things from libraries to do what
it wants. From here on, module means any le of PE
format, and a Library is any module which exports and
imports functions and values.
Dynamically linking has the following benets:
It saves disk space, if more than one executable links
to the library module
Allows instant updating of routines, without providing new executables for all applications
Can save space in memory by mapping the code of
a library into more than one process
Increases abstraction of implementation.
The
method by which an action is achieved can be modied without the need for reprogramming of applications. This is extremely useful for backward compatibility with operating systems.
This section discusses how this is achieved using the PE
le format. An important point to note at this point is that
anything can be imported or exported between modules,
including variables as well as subroutines.
Loading
The downside of dynamically linking modules together is
that, at runtime, the software which is initialising an executable must link these modules together. For various
reasons, you cannot declare that The function in this dynamic library will always exist in memory here". If that
memory address is unavailable or the library is updated,
the function will no longer exist there, and the application
trying to use it will break. Instead, each module (library
or executable) must declare what functions or values it exports to other modules, and also what it wishes to import
from other modules.
As said above, a module cannot declare where in memory
it expects a function or value to be. Instead, it declares
where in its own memory it expects to nd a pointer to
the value it wishes to import. This permits the module
to address any imported value, wherever it turns up in
memory.
2.2.8 Exports
Exports are functions and values in one module that
have been declared to be shared with other modules.
This is done through the use of the Export Directory, which is used to translate between the name of
an export (or Ordinal, see below), and a location in
28
memory where the code or data can be found. The
start of the export directory is identied by the IMAGE_DIRECTORY_ENTRY_EXPORT entry of the
resource directory. All export data must exist in the same
section. The directory is headed by the following structure:
struct IMAGE_EXPORT_DIRECTORY { long Characteristics; long TimeDateStamp; short MajorVersion;
short MinorVersion; long Name; long Base; long
NumberOfFunctions; long NumberOfNames; long
*AddressOfFunctions; long *AddressOfNames; long
*AddressOfNameOrdinals; }
The Characteristics value is generally unused, TimeDateStamp describes the time the export directory was
generated, MajorVersion and MinorVersion should describe the version details of the directory, but their nature is undened. These values have little or no impact
on the actual exports themselves. The Name value is
an RVA to a zero terminated ASCII string, the name of
this library name, or module.
CHAPTER 2. PLATFORMS
dressOfFunctions array point into the section which contains the export directory, something that normal exports should not do. At that location, there should
be a zero terminated ASCII string of format LibraryName.ExportName for the appropriate place to forward
this export to.
2.2.9 Imports
The other half of dynamic linking is importing functions
and values into an executable or other module. Before
runtime, compilers and linkers do not know where in
memory a value that needs to be imported could exist.
The import table solves this by creating an array of pointers at runtime, each one pointing to the memory location
of an imported value. This array of pointers exists inside
of the module at a dened RVA location. In this way, the
linker can use addresses inside of the module to access
values outside of it.
The Import directory
29
the AddressOfNames array; to save searching for a string, The OriginalFirstThunk for that index identies the IMthe loader rst checks the AddressOfNames entry corre- AGE_IMPORT_BY_NAME structure for a import that
sponding to Hint.
needs to be resolved, and the FirstThunk for that index is
To summarise: The import table consists of a large ar- the index of another entry that needs to be resolved. This
ray of IMAGE_IMPORT_DESCRIPTORs, terminated continues until the FirstThunk value is 1, indicating no
by an all-zero entry. These descriptors identify a library more forwarded values to import.
to import things from. There are then two parallel RVA
arrays, each pointing at IMAGE_IMPORT_BY_NAME
2.2.10
structures, which identify a specic value to be imported.
Resources
Resource structures
Imports at runtime
Using the above import directory at runtime, the loader
nds the appropriate modules, loads them into memory, and seeks the correct export. However, to be able
to use the export, a pointer to it must be stored somewhere in the importing modules memory. This is why
there are two parallel arrays, OriginalFirstThunk and
FirstThunk, identifying IMAGE_IMPORT_BY_NAME
structures. Once an imported value has been resolved,
the pointer to it is stored in the FirstThunk array. It can
then be used at runtime to address imported values.
Bound imports
The PE le format also supports a peculiar feature known
as binding. The process of loading and resolving import
addresses can be time consuming, and in some situations
this is to be avoided. If a developer is fairly certain that
a library is not going to be updated or changed, then the
addresses in memory of imported values will not change
each time the application is loaded. So, the import address can be precomputed and stored in the FirstThunk
array before runtime, allowing the loader to skip resolving the imports - the imports are bound to a particular memory location. However, if the versions numbers
between modules do not match, or the imported library
needs to be relocated, the loader will assume the bound
addresses are invalid, and resolve the imports anyway.
The TimeDateStamp member of the import directory
entry for a module controls binding; if it is set to zero,
then the import directory is not bound. If it is non-zero,
then it is bound to another module. However, the TimeDateStamp in the import table must match the TimeDateStamp in the bound modules FileHeader, otherwise the
bound values will be discarded by the loader.
Forwarding and binding Binding can of course be a
problem if the bound library / module forwards its exports
to another module. In these cases, the non-forwarded imports can be bound, but the values which get forwarded
must be identied so the loader can resolve them. This
is done through the ForwarderChain member of the import descriptor. The value of ForwarderChain is an index into the FirstThunk and OriginalFirstThunk arrays.
Resources are data items in modules which are dicult to be stored or described using the chosen programming language. This requires a separate compiler or resource builder, allowing insertion of dialog
boxes, icons, menus, images, and other types of resources, including arbitrary binary data. A number of
API calls can then be used to retrieve resources from
the module. The base of resource data is pointed to by
the IMAGE_DIRECTORY_ENTRY_RESOURCE entry of the data directory, and at that location there is an
IMAGE_RESOURCE_DIRECTORY structure:
struct IMAGE_RESOURCE_DIRECTORY { long
Characteristics; long TimeDateStamp; short MajorVersion; short MinorVersion; short NumberOfNamedEntries; short NumberOfIdEntries; }
Characteristics is unused, and TimeDateStamp is normally the time of creation, although it doesn't matter
if its set or not. MajorVersion and MinorVersion relate to the versioning info of the resources: the elds
have no dened values. Immediately following the IMAGE_RESOURCE_DIRECTORY structure is a series
of IMAGE_RESOURCE_DIRECTORY_ENTRYs, the
number of which are dened by the total of NumberOfNamedEntries and NumberOfIdEntries. The rst portion of these entries are for named resources, the latter
for ID resources, depending on the values in the IMAGE_RESOURCE_DIRECTORY struct. The actual
shape of the resource entry structure is as follows:
struct IMAGE_RESOURCE_DIRECTORY_ENTRY {
long NameId; long *Data; }
The NameId value has dual purpose: if the most significant bit (or sign bit) is clear, then the lower 16 bits are
an ID number of the resource. Alternatly, if the top bit
is set, then the lower 31 bits make up an oset from the
start of the resource data to the name string of this particular resource. The Data value also has a dual purpose: if
the most signicant bit is set, the remaining 31 bits form
an oset from the start of the resource data to another
IMAGE_RESOURCE_DIRECTORY (i.e. this entry is
an interior node of the resource tree). Otherwise, this is
a leaf node, and Data contains the oset from the start
of the resource data to a structure which describes the
specics of the resource data itself (which can be consid-
30
CHAPTER 2. PLATFORMS
Function Exports
2.2.11
Relocations
2.2.12
2.2.13
Function Imports
In a similar manner to function exports, a program may
import a function from an external DLL le. The dll le
will load into the process memory when the program is
started, and the function will be used like a local function. DLL imports need to be prototyped in the following
manner, for the compiler and linker to recognize that the
function is coming from an external library:
__declspec(dllimport) void MyFunction();
You can also use depends.exe to list imported and exDLLs may be loaded in one of two ways, a) at load-time, ported functions. Depends is a a GUI tool and comes
or b) by calling the LoadModule() Win32 API function. with Microsoft Platform SDK.
2.3. LINUX
2.3 Linux
31
Bash An acronym for Bourne Again SHell.
Bourne A precursor to Bash.
Csh C Shell
Ksh Korn Shell
2.3.1
Linux
2.3.2
System Architecture
2.3.6 Debuggers
gdb The GNU Debugger. It comes pre-installed on
most Linux distributions and is primarily used to debug ELF executables. manpage
winedbg A debugger for Wine, used to debug Win32
executables under Linux. manpage
edb A fully featured plugin-based debugger inspired by
the famous OllyDbg. Project page
2.3.4
Shells
32
CHAPTER 2. PLATFORMS
2.4.1
ELF Files
.text
.rodata
The ELF le format (short for Executable and Linking Format) was developed by Unix System Laboratories to be a successor to previous le formats such as
COFF and a.out. In many respects, the ELF format is
more powerful and versatile than previous formats, and
has widely become the standard on Linux, Solaris, IRIX,
and FreeBSD (although the FreeBSD-derived Mac OS
X uses the Mach-O format instead). ELF has also been
adopted by OpenVMS for Itanium and BeOS for x86.
...
.data
Section header table
Historically, Linux has not always used ELF; Red Hat An ELF le has two views: the program header shows the segLinux 4 was the rst time that distribution used ELF; pre- ments used at run-time, while the section header lists the set of
sections of the binary.
vious versions had used the a.out format.
ELF Objects are broken down into dierent segments
and/or sections. These can be located by using the ELF
header found at the rst byte of the object. The ELF
header provides the location for both the program header
and the section header. Using these data structures the
rest of the ELF objects contents can be found, this includes .text and .data segments which contain code and
data respectively.
a.out Files
File Format
Chapter 3
Code Patterns
3.1 The Stack
3.1.1
because other functions may overwrite these values without your knowledge.
The Stack
Push
Pop
34
3.1.4
3.1.5
Data Allocation
This all seems well and good, but what is the purpose of
ebp in this setup? Why save the old value of ebp and then
point ebp to the top of the stack, only to change the value
of esp with the next instruction? The answer is function
As we shall see, explicitly declared variables are allocated parameters.
on the stack. Stack variables are nite in number, and
have a denite size. Heap variables can be variable in Consider the following C function declaration:
number and in size. We will discuss these topics in more void MyFunction2(int x, int y, int z) { ... }
detail later.
35
36
static Functions
_Question1: push ebp mov ebp, esp sub esp, 4 mov eax,
[ebp + 8] mov ecx, 2 mul ecx mov [esp + 0], eax mov
eax, [ebp + 12] mov edx, [esp + 0] add eax, edx mov
esp, ebp pop ebp ret
37
Although an optimizing compiler has chosen to take a few will generate the following code if passed Left-to-Right:
shortcuts.
push a push b call _MyFunction
3.4.1
Calling Conventions
Calling conventions are a standardized method for functions to be implemented and called by the machine. A
calling convention species the method that a compiler
sets up to access a subroutine. In theory, code from any
compiler can be interfaced together, so long as the functions all have the same calling conventions. In practice Cleaning the stack When arguments are pushed onto
however, this is not always the case.
the stack, eventually they must be popped back o
again. Whichever function, the caller or the callee,
Calling conventions specify how arguments are passed to
is responsible for cleaning the stack must reset the
a function, how return values are passed back out of a
stack pointer to eliminate the passed arguments.
function, how the function is called, and how the function
manages the stack and its stack frame. In short, the calling convention species how a function call in C or C++ Calling function (the caller) The parent function
is converted into assembly language. Needless to say,
that calls the subroutine. Execution resumes in the
there are many ways for this translation to occur, which
calling function directly after the subroutine call,
is why its so important to specify certain standard methunless the program terminates inside the subroutine.
ods. If these standard conventions did not exist, it would
be nearly impossible for programs created using dierent
compilers to communicate and interact with one another. Called function (the callee) The child function that
gets called by the parent.
There are three major calling conventions that are used
with the C language: STDCALL, CDECL, and FASTName Decoration When C code is translated to assemCALL. In addition, there is another calling convenbly code, the compiler will often decorate the
tion typically used with C++: THISCALL. There are
function name by adding extra information that the
other calling conventions as well, including PASCAL and
linker will use to nd and link to the correct funcFORTRAN conventions, among others. We will not contions. For most calling conventions, the decoration
sider those conventions in this book.
is very simple (often only an extra symbol or two
to denote the calling convention), but in some extreme cases (notably C++ thiscall convention), the
3.4.2 Notes on Terminology
names are mangled severely.
There are a few terms that we are going to be using in this
chapter, which are mostly common sense, but which are Entry sequence (the function prologue) a few inworthy of stating directly:
structions at the beginning of a function, which
prepare the stack and registers for use within the
function.
Passing arguments passing arguments is a way of
saying that the calling function is writing data in the
place where the called function will look for them. Exit sequence (the function epilogue) a few instrucArguments are passed before the call instruction is
tions at the end of a function, which restore the stack
executed.
and registers to the state expected by the caller, and
return to the caller. Some calling conventions clean
the stack in the exit sequence.
Right-to-Left and Left-to-Right These describe the
manner that arguments are passed to the subroutine,
in terms of the High-level code. For instance, the Call sequence a few instructions in the middle of a funcfollowing C function call:
tion (the caller) which pass the arguments and call
the called function. After the called function has
MyFunction1(a, b);
returned, some calling conventions have one more
instruction in the call sequence to clean the stack.
38
3.4.3
CDECL
In the CDECL calling convention the following holds:
STDCALL
STDCALL, also known as WINAPI (and a few other
names, depending on where you are reading it) is used almost exclusively by Microsoft as the standard calling convention for the Win32 API. Since STDCALL is strictly
dened by Microsoft, all compilers that implement it do
it the same way.
STDCALL passes arguments right-to-left, and returns the value in eax. (The Microsoft documentation erroneously claimed that arguments are passed
left-to-right, but this is not the case.)
The called function cleans the stack, unlike CDECL.
This means that STDCALL doesn't allow variablelength argument lists.
Consider the following C function:
_stdcall int MyFunction2(int a, int b) { return a + b; }
Variadic functions usually have special entry code, gen- There are a few important points to note here:
erated by the va_start(), va_arg() C pseudo-functions.
Consider the following C instructions:
_cdecl int MyFunction1(int a, int b) { return a + b; }
and the following function call:
x = MyFunction1(2, 3);
These would produce the following assembly listings, respectively:
1. In the function body, the ret instruction has an (optional) argument that indicates how many bytes to
pop o the stack when the function returns.
2. STDCALL functions are name-decorated with a
leading underscore, followed by an @, and then the
number (in bytes) of arguments passed on the stack.
This number will always be a multiple of 4, on a 32bit aligned machine.
_MyFunction1: push ebp mov ebp, esp mov eax, [ebp + FASTCALL
8] mov edx, [ebp + 12] add eax, edx pop ebp ret
The FASTCALL calling convention is not completely
standard across all compilers, so it should be used with
and
caution. In FASTCALL, the rst 2 or 3 32-bit (or
push 3 push 2 call _MyFunction1 add esp, 8
smaller) arguments are passed in registers, with the most
commonly used registers being edx, eax, and ecx. AdWhen translated to assembly code, CDECL functions ditional arguments, or arguments larger than 4-bytes are
are almost always prepended with an underscore (thats passed on the stack, often in Right-to-Left order (simiwhy all previous examples have used "_ in the assembly lar to CDECL). The calling function most frequently is
code).
responsible for cleaning the stack, if needed.
39
and
;the calling function mov eax, 2 mov edx, 3 call @MyFunction3@8
The name decoration for FASTCALL prepends an @ to
the function name, and follows the function name with
@x, where x is the number (in bytes) of arguments passed
to the function.
They are recognizable on sight because of their complexity when compared to CDECL, FASTCALL,
and STDCALL function name decorations
They sometimes include the name of that functions
class.
3.4.4
a);
My-
C++ requires that non-static methods of a class be called And here is the resultant mangled name:
by an instance of the class. Therefore it uses its own stan- ?MyFunction@MyClass@@QAEHH@Z
dard calling convention to ensure that pointers to the object are passed to the function: THISCALL.
Extern C
In a C++ source le, functions placed in an extern C
block are guaranteed not to be mangled. This is done frequently when libraries are written in C++, and the funcIn THISCALL, the pointer to the class object is passed in tions need to be exported without being mangled. Even
ecx, the arguments are passed Right-to-Left on the stack, though the program is written in C++ and compiled with
and the return value is passed in eax.
a C++ compiler, some of the functions might therefore
not be mangled and will use one of the ordinary C calling
For instance, the following C++ instruction:
conventions (typically CDECL).
MyObj.MyMethod(a, b, c);
THISCALL
40
readable identiers, and replaces them with the binary lo- CDECL
cations instead. Function names really only appear in two
places:
int MyFunction(int x, int y) { return (x * 2) + (y * 3); }
1. Listing les produced during compilation
2. In export tables, if functions are exported
When disassembling raw machine code, there will be no
function names and no name decorations to examine. For
this reason, you will need to pay more attention to the way
parameters are passed, the way the stack is cleaned, and
other similar details.
While we haven't covered optimizations yet, suce it to
say that optimizing compilers can even make a mess out
of these details. Functions which are not exported do not
necessarily need to maintain standard interfaces, and if it
is determined that a particular function does not need to
follow a standard convention, some of the details will be
optimized away. In these cases, it can be dicult to determine what calling conventions were used (if any), and it
is even dicult to determine where a function begins and
ends. This book cannot account for all possibilities, so
we try to show as much information as possible, with the
knowledge that much of the information provided here
will not be available in a true disassembly situation.
3.4.6
Further reading
becomes:
PUBLIC _MyFunction _TEXT SEGMENT _x$ = 8
; size = 4 _y$ = 12 ; size = 4 _MyFunction PROC
NEAR ; Line 4 push ebp mov ebp, esp ; Line 5 mov
eax, _y$[ebp] imul eax, 3 mov ecx, _x$[ebp] lea eax,
[eax+ecx*2] ; Line 6 pop ebp ret 0 _MyFunction ENDP
_TEXT ENDS END
On entry of a function, ESP points to the return address
pushed on the stack by the call instruction (that is, previous contents of EIP). Any argument in stack of higher
address than entry ESP is pushed by caller before the call
is made; in this example, the rst argument is at oset
+4 from ESP (EIP is 4 bytes wide), plus 4 more bytes
once the EBP is pushed on the stack. Thus, at line 5, ESP
points to the saved frame pointer EBP, and arguments are
located at addresses ESP+8 (x) and ESP+12 (y).
For CDECL, caller pushes arguments into stack in a right
to left order. Because ret 0 is used, it must be the caller
who cleans up the stack.
As a point of interest, notice how lea is used in this function to simultaneously perform the multiplication (ecx *
2), and the addition of that quantity to eax. Unintuitive
instructions like this will be explored further in the chapter on unintuitive instructions.
FASTCALL
3.5.1
Microsoft C Compiler
because the compiler wants a consistent way to use all arguments via stack access, not only one compiler does like
that.
There is no argument is accessed with positive oset to
entry SP, it seems caller doesnt pushed in them, thus it
can use ret 0. Lets do further investigation:
41
int FastTest(int x, int y, int z, int a, int b, int c) { return x ; size = 4 _STDCALLTest@24 PROC NEAR ; Line
* y * z * a * b * c; }
2 push ebp mov ebp, esp ; Line 3 mov eax, _x$[ebp]
imul eax, DWORD PTR _y$[ebp] imul eax, DWORD
PTR _z$[ebp] imul eax, DWORD PTR _a$[ebp] imul
and the corresponding listing:
eax, DWORD PTR _b$[ebp] imul eax, DWORD
PUBLIC @FastTest@24 _TEXT SEGMENT _y$ = PTR _c$[ebp] ; Line 4 pop ebp ret 24 ; 00000018H
8 ; size = 4 _x$ = 4 ; size = 4 _z$ = 8 ; size = _STDCALLTest@24 ENDP _TEXT ENDS END
4 _a$ = 12 ; size = 4 _b$ = 16 ; size = 4 _c$ = 20 ;
size = 4 @FastTest@24 PROC NEAR ; _x$ = ecx ;
_y$ = edx ; Line 2 push ebp mov ebp, esp sub esp, 8 Yes the only dierence between STDCALL and CDECL
mov _y$[ebp], edx mov _x$[ebp], ecx ; Line 3 mov is that the former does stack clean up in callee, the later
eax, _x$[ebp] imul eax, DWORD PTR _y$[ebp] imul in caller. This saves a little bit in X86 due to its ret n.
eax, DWORD PTR _z$[ebp] imul eax, DWORD PTR
_a$[ebp] imul eax, DWORD PTR _b$[ebp] imul eax,
3.5.2 GNU C Compiler
DWORD PTR _c$[ebp] ; Line 4 mov esp, ebp pop ebp
ret 16 ; 00000010H
We will be using 2 example C functions to demonstrate
how GCC implements calling conventions:
Now we have 6 arguments, four are pushed in by caller
int MyFunction1(int x, int y) { return (x * 2) + (y * 3); }
from right to left, and last two are passed again in cx/dx,
and processed the same way as previous example. Stack
cleanup is done by ret 16, which corresponding to 4 ar- and
guments pushed before call executed.
int MyFunction2(int x, int y, int z, int a, int b, int c) {
For FASTCALL, compiler will try to pass arguments in
registers, if not enough caller will pushed them into stack
still in an order from right to left. Stack cleanup is done
by callee. It is called FASTCALL because if arguments
can be passed in registers (for 64bit CPU the maximum
number is 6), no stack push/clean up is needed.
The name-decoration scheme of the function: @MyFunction@n, here n is stack size needed for all arguments.
return x * y * (z + 1) * (a + 2) * (b + 3) * (c + 4); }
GCC does not have commandline arguments to force the
default calling convention to change from CDECL (for
C), so they will be manually dened in the text with the
directives: __cdecl, __fastcall, and __stdcall.
CDECL
STDCALL
becomes:
PUBLIC _MyFunction@8 _TEXT SEGMENT _x$ =
8 ; size = 4 _y$ = 12 ; size = 4 _MyFunction@8 PROC
NEAR ; Line 4 push ebp mov ebp, esp ; Line 5 mov
eax, _y$[ebp] imul eax, 3 mov ecx, _x$[ebp] lea eax,
[eax+ecx*2] ; Line 6 pop ebp ret 8 _MyFunction@8
ENDP _TEXT ENDS END
42
With c being pushed rst, and x being pushed last. Therefore, these parameters are also pushed in right-to-left order. This function then also cleans 24 bytes o the stack
with the ret 24 instruction.
3.6. BRANCHES
3.5.3
43
3.5.4
The function includes the decorated name of an STDCALL function, and cleans up its own stack. It is therefore an STDCALL function.
Assuming that Load is the function name and Container is the class name (it could just as easily be the
other way around), here is our function denition:
3.5.5
This code snippet is the entire body of an unnamed assembly function. Identify the calling convention of this
function.
3.6 Branches
push ebp mov ebp, esp add eax, edx pop ebp ret
The function sets up a stack frame, so we know the compiler hasnt done anything funny to it. It accesses regis- 3.6.1 Branching
ters which arent initialized yet, in the edx and eax registers. It is therefore a FASTCALL function.
Computer science professors tell their students to avoid
jumps and goto instructions, to avoid the proverbial
spaghetti code. Unfortunately, assembly only has jump
3.5.6 Example: Another Unnamed As- instructions to control program ow. This chapter will exsembly Function
plore the subject that many people avoid like the plague,
and will attempt to show how the spaghetti of assembly
push ebp mov ebp, esp mov eax, [ebp + 8] pop ebp ret can be translated into the more familiar control structures
16
of high-level language. Specically, this chapter will focus on If-Then-Else and Switch branching instructions.
The function has a standard stack frame, and the ret instruction has a parameter to clean its own stack. Also,
3.6.2 If-Then
it accesses a parameter from the stack. It is therefore an
STDCALL function.
Lets consider a generic if statement in pseudo-code followed by its equivalent form using jumps:
3.5.7
What does this code do? In English, the code checks the
condition and performs a jump only if it is false. With
that in mind, lets compare some actual C code and its
What can we tell about the following function call?
mov ecx, x push eax mov eax, ss:[ebp - 4] push eax mov Assembly translation:
al, ss:[ebp - 3] call @__Load?$Container__XXXY_?Fcii Note that when we translate to assembly, we need to
negate the condition of the jump because--like we said
44
3.6.3
If-Then-Else
Let us now look at a more complicated case: the If-Then- And when we compile this with cl.exe, we can generate
Else instruction.
the following listing le:
tv64 = 4 ; size = 4 _argc$ = 8 ; size = 4 _argv$ = 12 ;
size = 4 _main PROC NEAR ; Line 10 push ebp mov
ebp, esp push ecx ; Line 11 mov eax, DWORD PTR
_argc$[ebp] mov DWORD PTR tv64[ebp], eax mov
ecx, DWORD PTR tv64[ebp] sub ecx, 1 mov DWORD
PTR tv64[ebp], ecx cmp DWORD PTR tv64[ebp], 3 ja
Now, here is an example of a real C If-Then-Else:
SHORT $L810 mov edx, DWORD PTR tv64[ebp] jmp
if(x == 10) { x = 0; } else { x++; }
DWORD PTR $L818[edx*4] $L806: ; Line 14 push
1 call _MyFunction add esp, 4 ; Line 15 jmp SHORT
Which gets translated into the following assembly code: $L803 $L807: ; Line 17 push 2 call _MyFunction
add esp, 4 ; Line 18 jmp SHORT $L803 $L808: ;
mov eax, $x cmp eax, 0x0A ;0x0A = 10 jne else mov Line 19 push 3 call _MyFunction add esp, 4 ; Line
eax, 0 jmp end else: inc eax end: mov $x, eax
20 jmp SHORT $L803 $L809: ; Line 22 push 4 call
_MyFunction add esp, 4 ; Line 23 jmp SHORT $L803
As you can see, the addition of a single unconditional $L810: ; Line 25 push 5 call _MyFunction add esp, 4
jump can add an entire extra option to our conditional.
$L803: ; Line 27 xor eax, eax ; Line 28 mov esp, ebp
Now, what happens here? Like before, the if statement
only jumps to the else clause when the condition is false.
However, we must also install an unconditional jump at
the end of the then clause, so we don't perform the else
clause directly afterwards.
3.6. BRANCHES
45
pop ebp ret 0 $L818: DD $L806 DD $L807 DD $L808 sub ecx, 1 mov DWORD PTR tv64[ebp], ecx
DD $L809 _main ENDP
Lets work our way through this. First, we see that line
10 sets up our standard stack frame, and it also saves ecx.
Why does it save ecx? Scanning through the function,
we never see a corresponding pop ecx instruction, so
it seems that the value is never restored at all. In fact,
the compiler isn't saving ecx at all, but is instead simply
reserving space on the stack: its creating a local variable. The original C code didn't have any local variables,
however, so perhaps the compiler just needed some extra
scratch space to store intermediate values. Why doesn't
the compiler execute the more familiar sub esp, 4 command to create the local variable? push ecx is just a faster
instruction that does the same thing. This scratch space
is being referenced by a negative oset from ebp. tv64
was dened in the beginning of the listing as having the
value 4, so every call to tv64[ebp]" is a call to this
scratch space.
This sequence performs the following pseudo-C opera- edx gets the value of the scratch pad (argc - 1), and then
tion:
there is a very weird jump that takes place: execution
if( argc - 1 >= 4 ) { goto $L810; /* the default */ } label jumps to a location pointed to by the value (edx * 4 +
*L818[] = { $L806, $L807, $L808, $L809 }; /* dene a $L818). What is $L818? We will examine that right now.
table of jumps, one per each case */ // goto L818[argc 1]; /* use the address from the table to jump to the correct
The Switch Table
case */
Heres why...
46
In this jump, $L818 isn't the oset, its the base, edx*4 is
the oset. As we said earlier, edx contains the value of
(argc - 1). If argc == 1, we jump to [$L818 + 0] which
is $L806. If argc == 2, we jump to [$L818 + 4], which
is $L807. Get the picture? A quick look at labels $L806,
$L807, $L808, and $L809 shows us exactly what we expect to see: the bodies of the case statements from the
original C code, above. Each one of the case statements
calls the function MyFunction, then breaks, and then
jumps to the end of the switch block.
3.6.5
Ternary Operator ?:
In x86 processors, negative numbers are stored in twoscomplement format. For instance, lets look at the fol- This function accesses parameters on the stack at [ebp
lowing C code:
+ 8] and [ebp + 12]. Both of these values are loaded
into ecx, and we can therefore assume they are 4-byte
BYTE x; x = 1;
values. This function doesn't clean its own stack, and the
values aren't passed in registers, so we know the function
At the end of this C code, x will have the value 11111111: is CDECL. The return value in eax is a 4-byte value, and
all ones!
we are told to assume that all the values are unsigned.
When argc is greater than 1, setle sets al to zero. Decre- Putting all this together, we can construct the function
3.8. LOOPS
47
prototype:
3.7.2
Example:
tures
3.8.1 Loops
So we can rename _Label_1 to something else descrip- To complete repetitive tasks, programmers often impletive, such as Else_1. The rest of the code block, after ment loops. There are many sorts of loops, but they can
Branch_1_End (_Label_2) is as follows:
all be boiled down to a few similar formats in assembly
mov ecx, [ebp + 12] cmp ecx, 0 jne _Label_3 inc eax : code. This chapter will discuss loops, how to identify
them, and how to decompile them back into high-level
_Label_3
representations.
We can see immediately that _Label_3 is the end of
this branch structure, so we can immediately call it
Branch_2_End, or something else. Here, we are again
comparing ecx to 0, and if it is not equal, we jump to the
end of the block. If it is equal to zero, however, we increment eax, and then fall out the bottom of the branch. We
can see that there is no else block in this branch structure,
48
mov eax, $x beginning: inc eax cmp eax, 0x0A ;0x0A = What is a For-Loop? In essence, its a While-Loop with
an initial state, a condition, and an iterative instruction.
10 jne beginning mov $x, eax
For instance, the following generic For-Loop:
gets translated into the following pseudocode while-loop:
3.8.3
While Loops
While loops look almost as simple as a Do-While loop, Which in turn gets translated into the following Do-While
but in reality they aren't as simple at all. Lets examine a Loop:
generic while-loop:
initialization; if(condition) { do { action; increment; }
while(x) { //loop body }
while(condition); }
What does this loop do? First, the loop checks to make
sure that x is true. If x is not true, the loop is skipped. The
loop body is then executed, followed by another check: is
x still true? If x is still true, execution jumps back to the
top of the loop, and execution continues. Keep in mind
that there needs to be a jump at the bottom of the loop
(to get back up to the top), but it makes no sense to jump
back to the top, retest the conditional, and then jump back
to the bottom of the loop if the conditional is found to be
false. The while-loop then, performs the following steps:
4. if the condition is not true, fall-through the end of C only has Do-While, While, and For Loops, but some
the loop.
other languages may very well implement their own types.
Also, a good C-Programmer could easily home brew a
new type of loop using a series of good macros, so they
Here is a while-loop in C code:
bear some consideration:
while(x <= 10) { x++; }
And here then is that same loop translated into assembly:
mov eax, $x cmp eax, 0x0A jg end beginning: inc eax
cmp eax, 0x0A jle beginning end:
Do-Until Loop
A common Do-Until Loop will take the following form:
If we were to translate that assembly code back into C, do { //loop body } until(x);
we would get the following code:
if(x <= 10) //remember: in If statements, we reverse which essentially becomes the following Do-While loop:
the condition from the asm { do { x++; } while(x <= 10) } do { //loop body } while(!x);
49
Until Loop
Do-Forever Loop
push ebp mov ebp, esp mov esi, [ebp + 8] mov ebx, 0
mov eax, 0 mov ecx, 0 _Label_1: mov ecx, [esi + ebx *
4] add eax, ecx inc ebx cmp ebx, 100 jne _Label_1 mov
esp, ebp pop ebp ret 4
3.9.1
push ebp mov ebp, esp mov esi, [ebp + 8] mov ebx, 0
mov eax, 0 mov ecx, 0 _Label_1: mov ecx, [esi + ebx *
4] add eax, ecx inc ebx cmp ebx, 100 jne _Label_1 mov
esp, ebp pop ebp ret 4
Starting with the function prototype above, and the description of what this function does, we can start to write
the C code for this function. We know that this function
initializes eax, ebx, and ecx before the loop. However,
we can see that ecx is being used as simply an intermediate storage location, receiving successive values from the
array, and then being added to eax.
push ebp mov ebp, esp mov esi, [ebp + 8] mov ebx, 0
mov eax, 0 mov ecx, 0 _Label_1: mov ecx, [esi + ebx *
4] add eax, ecx inc ebx cmp ebx, 100 jne _Label_1 mov We will create two unsigned integer values, a (for eax)
esp, ebp pop ebp ret 4
and b (for ebx). We will dene both a and b with the
register qualier, so that we can instruct the compiler
This function loops through an array of 4 byte integer val- not to create space for them on the stack. For each loop
ues, pointed to by esi, and adds each entry. It returns the iteration, we are adding the value of the array, at location
50
ebx*4 to the running sum, eax. Converting this to our a
and b variables, and using C syntax, we see:
a = a + array[b];
The loop could be either a for loop, or a while loop. We
see that the loop control variable, b, is initialized to 0 before the loop, and is incremented by 1 each loop iteration.
The loop tests b against 100, after it gets incremented, so
we know that b never equals 100 inside the loop body.
Using these simple facts, we will write the loop in 3 different ways:
First, with a while loop.
unsigned int STDCALL MyFunction(unsigned int
*array) { register unsigned int b = 0; register unsigned int
a = 0; while(b != 100) { a = a + array[b]; b++; } return a; }
Or, with a for loop:
unsigned int STDCALL MyFunction(unsigned int
*array) { register unsigned int b; register unsigned int a =
0; for(b = 0; b != 100; b++) { a = a + array[b]; } return a; }
And nally, with a do-while loop:
unsigned int STDCALL MyFunction(unsigned int *array) { register unsigned int b = 0; register unsigned int a =
0; do { a = a + array[b]; b++; }while(b != 100); return a; }
Chapter 4
Data Patterns
4.1 Variables
4.1.1
In the last example, the value of ecx is calculated at runtime, whereas in the rst 2 examples, the value is the same
every time. RVAs are considered hard-coded addresses,
even though the loader needs to x them up to point to
the correct locations.
Variables
We've already seen some mechanisms to create local storage on the stack. This chapter will talk about some other
variables, including global variables, static variables,
variables labled "const, "register, and "volatile. It will
also consider some general techniques concerning variables, including accessor and setter methods (to borrow
from OO terminology). This section may also talk about
setting memory breakpoints in a debugger to track memory I/O on a variable.
4.1.2
52
FLAT:$SG797 call _printf add esp, 12 ; 0000000cH ; 4.1.5 Signed and Unsigned Variables
Line 8 pop ebp ret 0 _MyFunction ENDP _TEXT ENDS
Integer formatted variables, such as int, char, short and
Normally when assembly listings are posted in this wiki- long may be declared signed or unsigned variables in the
book, most of the code gibberish is discarded to aid read- C source code. There are two dierences in how these
ability, but in this instance, the gibberish contains the variables are treated:
answer we are looking for. As can be clearly seen, this
function creates a standard stack frame, and it doesn't create any local variables on the stack. In the interests of
being complete, we will take baby-steps here, and work
to the conclusion logically.
Global variables do not have a limited scope like lexical variables do inside a function body. Since the notion
of lexical scope implies the use of the system stack, and
since global variables are not lexical in nature, they are
typically not found on the stack. Global variables tend
_BSS SEGMENT ?x@?1??MyFunction@@9@9 DD to exist in the program as a hard-coded memory address,
01H DUP (?) _BSS ENDS
a location which never changes throughout program execution. These could exist in the DATA segment of the
This shows that the Microsoft C compiler creates static executable, or anywhere else that a hard-coded memory
variables in the .bss section. This might not be the same address can be used to store data.
for all compilers, but the lesson is the same: local static In C, global variables are dened outside the body of any
variables are created and used in a very similar, if not function. There is no global keyword. Any variable
the exact same, manner as global values. In fact, as far which is not dened inside a function is global. In C howas the reverser is concerned, the two are usually inter- ever, a variable which is not dened inside a function is
changeable. Remember, the only real dierence between only global to the particular source code le in which it is
static variables and global variables is the idea of scope, dened. For example, we have two les Foo.c and Bar.c,
which is only used by the compiler.
and a global variable MyGlobalVar:
4.1. VARIABLES
In the example above, the variable MyGlobalVar is visible
inside the le Foo.c, but is not visible inside the le Bar.c.
To make MyGlobalVar visible inside all project les, we
need to use the extern keyword, which we will discuss
below.
static Variables
53
4.1.8 Constants
Variables qualied with the const keyword (in C) are frequently stored in the .data section of the executable. Constant values can be distinguished because they are initialized at the beginning of the program, and are never
modied by the program itself. For this reasons, some
compilers may chose to store constant variables (especially strings) in the .text section of the executable, thus
allowing the sharing of these variables across multiple instances of the same process. This creates a big problem
for the reverser, who now has to decide whether the code
hes looking at is part of a constant variable or part of a
subroutine.
Volatile memory
Static variables in C are global variables, except the compiler takes precautions to prevent the variable from being accessed outside of the parent functions scope. Like
global variables, static variables are referenced using a
hardcoded memory address, not a location on the stack
like ordinary variables. However unlike globals, static
variables are only used inside a single function. There
is no dierence between a global variable which is only
used in a single function, and a static variable inside that
same function. However, its good programming practice
to limit the number of global variables, so when disas- It is not always possible to determine from a disassemsembling, you should prefer interpreting these variables bly listing whether a given variable is a volatile varias static instead of global.
able. However, if the variable is accessed frequently from
memory, and its value is constantly updated in memory
(especially if there are free registers available), thats a
good hint that the variable might be volatile.
extern Variables
The extern keyword is used by a C compiler to indicate
4.1.10 Simple Accessor Methods
that a particular variable is global to the entire project, not
just to a single source code le. Besides this distinction,
An Accessor Method is a tool derived from OO theand the slightly larger lexical scope of extern variables,
ory and practice. In its most simple form, an accessor
they should be treated like ordinary global variables.
method is a function that receives no parameters (or perIn static libraries, variables marked as being extern might haps simply an oset), and returns the value of a variable.
be available for use with programs which are linked to the Accessor and Setter methods are ways to restrict access to
library.
certain variables. The only standard way to get the value
of the variable is to use the Accessor.
When disassembling, a hard-coded memory address push ebp mov ebp, esp mov eax, [ecx + 8] ;THISCALL
should be considered to be an ordinary global variable function, passes this pointer in ecx mov esp, ebp pop
unless you can determine from the scope of the variable ebp ret
that it is static or extern.
54
Because they are so simple, accessor methods are fre- and we can take the element that is at oset +8 from that
quently heavily optimized (they generally don't need a pointer (value3):
stack frame), and are even occasionally inlined by the MyClass::GetValue3() { return this->value3; }
compiler.
4.1.11
push ebp mov ebp, esp cmp [ebp + 8], 0 je error mov
eax, [ebp + 8] mov [ecx + 0], eax mov eax, 1 jmp end Can you tell what the original C++ source code looks like,
:error mov eax, 0 :end mov esp, ebp pop ebp ret
in general, for the following setter method?
push ebp mov ebp, esp cmp [ebp + 8], 0 je error mov
eax, [ebp + 8] mov [ecx + 0], eax mov eax, 1 jmp end
:error mov eax, 0 :end mov esp, ebp pop ebp ret
This code looks a little complicated, but don't panic! We
will walk through it slowly. The rst two lines of code set
up the stack frame:
4.2.1
Can you tell what the original C++ source code looks like, The next two lines of code compare the value of [ebp +
8] (which we know to be the rst parameter) to zero. If
in general, for the following accessor method?
push ebp mov ebp, esp mov eax, [ecx + 8] ;THISCALL [ebp+8] is zero, the function jumps to the label error.
function, passes this pointer in ecx mov esp, ebp pop We see that the label error sets eax to 0, and returns. We
haven't seen it before, but this looks conspicuously like an
ebp ret
if statement. If the parameter is zero, return zero.
We don't know the name of the class, so we will use a
generic name MyClass (or whatever you would like to call
it). We will lay out a simple class denition, that contains
a data value at oset +8. Oset +8 is the only data value
accessed, so we don't know what the rst 8 bytes of data
looks like, but we will just assume (for our purposes) that
our class looks like this:
class MyClass { int value1; int value2; int value3; //oset If we use the same MyClass dention as in question 1,
above, we can get the following code for our function,
+8 ... }
SetValue1(int val)":
We will then create our function, which I will call GetValue3()". We know that the data value being accessed
is located at [ecx+8], (which we have dened above to
be value3). Also, we know that the data is being read
into a 4-byte register (eax), and is not truncated. We can
assume, therefore, that value3 is a 4-byte data value. We
can use the this pointer as the pointer value stored in ecx,
55
so we can't be sure. The bool is usually dened to have Which looks harmless enough. But, what if a program
a size of 1 byte, but it is often stored the same way as an inadvertantly accesses buer[4]? what about buer[5]?
int.
what about buer[8]? This is the makings of a buer
overow vulnerability, and (might) will be discussed in a
later section. However, this section won't talk about security issues, and instead will focus only on data structures.
4.3.1
Data Structures
4.3.2
Arrays
Arrays are simply a storage scheme for multiple data ob- Spotting an Array in Memory
jects of the same type. Data objects are stored sequentially, often as an oset from a pointer to the beginning Arrays in memory, such as global arrays, or arrays which
of the array. Consider the following C code:
have initial data (remember, initialized data is created in
the .data section in memory) and will be accessed as ox = array[25];
sets from a hardcoded address in memory:
Which is identical to the following asm code:
mov ebx, $array mov eax, [ebx + 25] mov $x, eax
56
4.3.3
Structures
Advanced Structures
Its called a structure (Pascal programmers may know a Lets say we have the following situation in a function:
similar concept as a record).
:MyFunction1 push ebp mov ebp, esp mov esi, [ebp + 8]
Structures may be very big or very small, and they may lea ecx, SS:[esi + 8] ...
contain all sorts of dierent data. Structures may look
very similar to arrays in memory, but a few key points what is happening here? First, esi is loaded with the value
need to be remembered: structures do not need to con- of the functions rst parameter (ebp + 8). Then, ecx is
tain data elds of all the same type, structure elds are loaded with a pointer to the oset +8 from esi. It looks
often 4-byte aligned (not sequential), and each element like we have 2 pointers accessing the same data structure!
in a structure has its own oset. It therefore makes no
sense to reference a structure element by a variable oset The function in question could easily be one of the following 2 prototypes:
from the base.
struct MyStruct1 { DWORD value1; DWORD value2;
Take a look at the following structure denition:
struct MySubStruct1 { ...
struct MyStruct2 { long value1; short value2; long struct MyStruct2 { DWORD value1; DWORD value2;
value3; }
DWORD array[LENGTH]; ...
Assuming the pointer to the base of this structure is one pointer oset from another pointer in a structure ofloaded into ebx, we can access these members in one of ten means a complex data structure. There are far too
two schemes:
many combinations of structures and arrays, however, so
The rst arrangement is the most common, but it clearly this wikibook will not spend too much time on this subleaves open an entire memory word (2 bytes) at oset +6, ject.
which is not used at all. Compilers occasionally allow the
programmer to manually specify the oset of each data
member, but this isn't always the case. The second exam- 4.3.5 Identifying Structs and Arrays
ple also has the benet that the reverser can easily identify
that each data member in the structure is a dierent size. Array elements and structure elds are both accessed as
osets from the array/structure pointer. When disassemConsider now the following function:
bling, how do we tell these data structures apart? Here
:_MyFunction push ebp mov ebp, esp lea ecx, SS:[ebp are some pointers:
+ 8] mov [ecx + 0], mov [ecx + 4], ecx mov [ecx + 8],
1. array elements are not meant to be accessed individmov esp, ebp pop ebp
ually. Array elements are typically accessed using a
variable oset
The function clearly takes a pointer to a data structure as
its rst argument. Also, each data member is the same
2. Arrays are frequently accessed in a loop. Because
size (4 bytes), so how can we tell if this is an array or a
arrays typically hold a series of similar data items,
structure? To answer that question, we need to remember
the best way to access them all is usually a loop.
one important distinction between structures and arrays:
Specically, for(x = 0; x < length_of_array; x++)
the elements in an array are all of the same type, the elstyle loops are often used to access arrays, although
ements in a structure do not need to be the same type.
there can be others.
Given that rule, it is clear that one of the elements in this
3. All the elements in an array have the same data type.
structure is a pointer (it points to the base of the structure itself!) and the other two elds are loaded with the
4. Struct elds are typically accessed using constant
hex value 0x0A (10 in decimal), which is certainly not
osets.
a valid pointer on any system I have ever used. We can
5. Struct elds are typically not accessed in order, and
then partially recreate the structure and the function code
are also not accessed using loops.
below:
4.3.6
57
When you start adding in inheritance and polymorphism,
things get a little more complicated. For the purposes
of simplicity, the structure of an object will be described
in terms of having no inheritance. At the end, however,
inheritance and polymorphism will be covered.
A:
4.4.2
Classes
A basic class that has not inherited anything can be broken into two parts, the variables and the methods. The
non-static variables are shoved into a simple data structure while the methods are compiled and called like every
other function.
58
B:
A:
4.5.2
59
Calling Conventions
aren't integers. Unfortunately, the exact format of oating point numbers is well beyond the scope of this chapWith the addition of the oating-point stack, there is an ter.
entirely new dimension for passing parameters and re- x is oset +8, y is oset +16, and z is oset +24 from
turning values. We will examine our calling conventions ebp. Therefore, z is pushed rst, x is pushed last, and the
here, and see how they are aected by the presence of parameters are passed right-to-left on the regular stack
oating-point numbers. These are the functions that we not the oating point stack. To understand how a value
will be assembling, using both GCC, and cl.exe:
is returned however, we need to understand what fmulp
__cdecl double MyFunction1(double x, double y, oat
z) { return (x + 1.0) * (y + 2.0) * (z + 3.0); } __fastcall
double MyFunction2(double x, double y, oat z) { return
(x + 1.0) * (y + 2.0) * (z + 3.0); } __stdcall double
MyFunction3(double x, double y, oat z) { return (x +
1.0) * (y + 2.0) * (z + 3.0); }
does. fmulp is the Floating-Point Multiply and Pop instruction. It performs the instructions:
ST1 := ST1 * ST0 FPU POP ST0
60
LIC
__real@30000000000000
PUBLIC
__real@4000000000000000
PUBLIC
__real@4008000000000000
EXTRN
__tused:NEAR ; COMDAT __real@30000000000000
CONST
SEGMENT
__real@30000000000000
DQ 030000000000000r ; 1 CONST ENDS ;
COMDAT
__real@4000000000000000
CONST
SEGMENT
__real@4000000000000000
DQ
04000000000000000r ; 2 CONST ENDS ; COMDAT __real@4008000000000000 CONST SEGMENT
__real@4008000000000000 DQ 04008000000000000r
; 3 CONST ENDS _TEXT SEGMENT _x$ = 8 ; size
= 8 _y$ = 16 ; size = 8 _z$ = 24 ; size = 4 @MyFunction2@20 PROC NEAR ; Line 7 push ebp mov ebp, esp
; Line 8 d QWORD PTR _x$[ebp] fadd QWORD PTR
__real@30000000000000 d QWORD PTR _y$[ebp]
fadd QWORD PTR __real@4000000000000000 fmulp
ST(1), ST(0) d DWORD PTR _z$[ebp] fadd QWORD
PTR __real@4008000000000000 fmulp ST(1), ST(0) ;
Line 9 pop ebp ret 20 ; 00000014H @MyFunction2@20
ENDP _TEXT ENDS
We can see that this function is taking 20 bytes worth of
parameters, because of the @20 decoration at the end of
the function name. This makes sense, because the function is taking two double parameters (8 bytes each), and
one oat parameter (4 bytes each). This is a grand total of 20 bytes. We can notice at a rst glance, without
having to actually analyze or understand any of the code,
that there is only one register being accessed here: ebp.
This seems strange, considering that FASTCALL passes
its regular 32-bit arguments in registers. However, that is
not the case here: all the oating-point parameters (even
z, which is a 32-bit oat) are passed on the stack. We
know this, because by looking at the code, there is no
other place where the parameters could be coming from.
Notice also that fmulp is the last instruction performed
again, as it was in the CDECL example. We can infer
then, without investigating too deeply, that the result is
passed at the top of the oating-point stack.
Notice also that x (oset [ebp + 8]), y (oset [ebp + 16])
and z (oset [ebp + 24]) are pushed in reverse order: z
is rst, x is last. This means that oating point parameters are passed in right-to-left order, on the stack. This is
exactly the same as CDECL code, although only because
we are using oating-point values.
Here is the GCC assembly listing for MyFunction2:
.align 8 LC5: .long 0 .long 1073741824 .align 8 LC6:
.long 0 .long 1074266112 .globl @MyFunction2@20
.def @MyFunction2@20; .scl 2; .type 32; .endef @MyFunction2@20: pushl %ebp movl %esp, %ebp subl
$16, %esp dl 8(%ebp) fstpl 8(%ebp) dl 16(%ebp)
fstpl 16(%ebp) dl 8(%ebp) d1 faddp %st, %st(1)
dl 16(%ebp) dl LC5 faddp %st, %st(1) fmulp %st,
%st(1) ds 24(%ebp) dl LC6 faddp %st, %st(1) fmulp
%st, %st(1) leave ret $20
STDCALL
Here is the cl.exe listing for MyFunction3:
PUBLIC
_MyFunction3@20
PUBLIC
__real@30000000000000
PUBLIC
__real@4000000000000000
PUBLIC
__real@4008000000000000
EXTRN
__tused:NEAR ; COMDAT __real@30000000000000
CONST
SEGMENT
__real@30000000000000
DQ 030000000000000r ; 1 CONST ENDS ;
COMDAT
__real@4000000000000000
CONST
SEGMENT
__real@4000000000000000
DQ
04000000000000000r ; 2 CONST ENDS ; COMDAT __real@4008000000000000 CONST SEGMENT
__real@4008000000000000 DQ 04008000000000000r
; 3 CONST ENDS _TEXT SEGMENT _x$ = 8 ; size = 8
_y$ = 16 ; size = 8 _z$ = 24 ; size = 4 _MyFunction3@20
PROC NEAR ; Line 12 push ebp mov ebp, esp ; Line
13 d QWORD PTR _x$[ebp] fadd QWORD PTR
__real@30000000000000 d QWORD PTR _y$[ebp]
fadd QWORD PTR __real@4000000000000000 fmulp
ST(1), ST(0) d DWORD PTR _z$[ebp] fadd QWORD
PTR __real@4008000000000000 fmulp ST(1), ST(0) ;
Line 14 pop ebp ret 20 ; 00000014H _MyFunction3@20
ENDP _TEXT ENDS END
x is the highest on the stack, and z is the lowest, therefore these parameters are passed from right-to-left. We
can tell this because x has the smallest oset (oset [ebp
+ 8]), while z has the largest oset (oset [ebp + 24]).
We see also from the nal fmulp instruction that the return value is passed on the FPU stack. This function also
cleans the stack itself, as noticed by the call 'ret 20. It
4.5.3
4.5.4
4.6.1
Here is the C source code, and the GCC assembly listing of a simple C language function that performs simple
oating-point arithmetic. Can you determine what the
numerical values of LC5 and LC6 are?
__fastcall double MyFunction2(double x, double y, oat
61
z) { return (x + 1.0) * (y + 2.0) * (z + 3.0); }
.align 8 LC5: .long 0 .long 1073741824 .align 8 LC6:
.long 0 .long 1074266112 .globl @MyFunction2@20
.def @MyFunction2@20; .scl 2; .type 32; .endef @MyFunction2@20: pushl %ebp movl %esp, %ebp subl
$16, %esp dl 8(%ebp) fstpl 8(%ebp) dl 16(%ebp)
fstpl 16(%ebp) dl 8(%ebp) d1 faddp %st, %st(1)
dl 16(%ebp) dl LC5 faddp %st, %st(1) fmulp %st,
%st(1) ds 24(%ebp) dl LC6 faddp %st, %st(1) fmulp
%st, %st(1) leave ret $20
For this, we don't even need a oating-point number calculator, although you are free to use one if you wish (and
if you can nd a good one). LC5 is added to [ebp - 16],
which we know to be y, and LC6 is added to [ebp - 24],
which we know to be z. Therefore, LC5 is the number
2.0, and LC6 is the number 3.0. Notice that the d1
instruction automatically loads the top of the oatingpoint stack with the constant value 1.0.
Chapter 5
Diculties
5.1 Code Optimization
Another set of optimization which can be performed either at the intermediate or at the code generation level are
An optimizing compiler is perhaps one of the most com- control ow optimizations. Most of these optimizations
plicated, most powerful, and most interesting programs in deal with the elimination of useless branches. Consider
existence. This chapter will talk about optimizations, al- the following code:
though this chapter will not include a table of common if(A) { if(B) { C; } else { D; } end_B: } else { E; } end_A:
optimizations.
5.1.1
Code Optimization
5.1.2
Stages of Optimizations
Once the compiler has sifted through all the logical ineciencies in your code, the code generator takes over.
Often the code generator will replace certain slow machine instructions with faster machine instructions.
For instance, the instruction:
beginning: ... loopnz beginning
operates much slower than the equivalent instruction set:
beginning: ... dec ecx jne beginning
63
It is not necessarily possible to determine whether identical portions of code were created originally as macros,
inline functions, or were simply copy and pasted. HowWhen a loop needs to run for a small, but denite number ever, when disassembling it can make your work easier to
of iterations, it is often better to unwind the loop in or- separate these blocks out into separate inline functions, to
der to reduce the number of jump instructions performed, help keep the code straight.
and in many cases prevent the processors branch predictor from failing. Consider the following C loop, which
calls the function MyFunction() 5 times:
5.2 Optimization Examples
5.1.3
Loop Unwinding
5.2.1 Example:
Optimized
Optimized Code
vs
Non-
The following example is adapted from an algorithm preEach loop iteration requires the following operations to sented in Knuth(vol 1, chapt 1) used to nd the greatest
common denominator of 2 integers. Compare the listbe performed:
ing le of this function when compiler optimizations are
turned on and o.
1. Compare the value in eax (the variable x) to 5, and
/*line 1*/ int EuclidsGCD(int m, int n) /*we want to
jump to the end if greater then or equal
nd the GCD of m and n*/ { int q, r; /*q is the quotient,
r is the remainder*/ while(1) { q = m / n; /*nd q and
2. Increment eax
r*/ r = m % n; if(r == 0) /*if r is 0, return our n value*/
{ return n; } m = n; /*set m to the current n value*/ n
= r; /*set n to our current remainder value*/ } /*repeat*/ }
3. Jump back to the top of the loop.
Compiling with the Microsoft C compiler, we generate a
Notice that we remove all these instructions if we manu- listing le using no optimization:
ally repeat our call to MyFunction():
PUBLIC _EuclidsGCD _TEXT SEGMENT _r$ = 8
call _MyFunction call _MyFunction call _MyFunction ; size = 4 _q$ = 4 ; size = 4 _m$ = 8 ; size = 4 _n$
call _MyFunction call _MyFunction
= 12 ; size = 4 _EuclidsGCD PROC NEAR ; Line 2
push ebp mov ebp, esp sub esp, 8 $L477: ; Line 4 mov
This new version not only takes up less disk space be- eax, 1 test eax, eax je SHORT $L473 ; Line 6 mov
cause it uses fewer instructions, but also runs faster be- eax, DWORD PTR _m$[ebp] cdq idiv DWORD PTR
cause fewer instructions are executed. This process is _n$[ebp] mov DWORD PTR _q$[ebp], eax ; Line 7 mov
called Loop Unwinding.
eax, DWORD PTR _m$[ebp] cdq idiv DWORD PTR
64
_n$[ebp] mov DWORD PTR _r$[ebp], edx ; Line 8 cmp
DWORD PTR _r$[ebp], 0 jne SHORT $L479 ; Line 10
mov eax, DWORD PTR _n$[ebp] jmp SHORT $L473
$L479: ; Line 12 mov ecx, DWORD PTR _n$[ebp]
mov DWORD PTR _m$[ebp], ecx ; Line 13 mov edx,
DWORD PTR _r$[ebp] mov DWORD PTR _n$[ebp],
edx ; Line 14 jmp SHORT $L477 $L473: ; Line 15 mov
esp, ebp pop ebp ret 0 _EuclidsGCD ENDP _TEXT
ENDS END
CHAPTER 5. DIFFICULTIES
assigns storage in the function, and readily discards
values that are not needed.
5.2.4
Example:
Code
65
and eax, edx ret
This is an example of using various arithmetic instruc-
Decompile Optimized tions to avoid branching. The neg instruction sets the
Below is the optimized listing le of the EuclidGCD function, presented in the examples above. Can you decompile this assembly code listing into equivalent optimized
C code? How is the optimized version dierent in structure from the non-optimized version?
PUBLIC _EuclidsGCD _TEXT SEGMENT _m$ = 8
; size = 4 _n$ = 12 ; size = 4 _EuclidsGCD PROC
NEAR ; Line 7 mov eax, DWORD PTR _m$[esp-4]
push esi mov esi, DWORD PTR _n$[esp] cdq idiv
esi mov ecx, edx ; Line 8 test ecx, ecx je SHORT
$L563 $L547: ; Line 12 mov eax, esi cdq idiv ecx ;
Line 13 mov esi, ecx mov ecx, edx test ecx, ecx jne
SHORT $L547 $L563: ; Line 10 mov eax, esi pop esi
; Line 15 ret 0 _EuclidsGCD ENDP _TEXT ENDS END
Altering the conditions to maintain the same structure
gives us:
66
CHAPTER 5. DIFFICULTIES
of obfuscations. Notice that many code optimizations Port0 Double-speed integer arithmetic, oating point
(discussed in the previous chapter) have the side-eect
load, memory store
of making code more dicult to read, and therefore optimizations act as obfuscations.
Port1 Double-speed integer arithmetic, oating point
arithmetic
5.3.2
Code instructions that are put in a hard-to read or- Notice however that writing to memory is particularly
slow (requiring the address to be sent by Port3, and the
der.
data itself to be written by Port0). Floating point num Code instructions which are used in a non-obvious bers need to be loaded to the FPU before they can be
operated on, so a oating point load and a oating point
way.
arithmetic instruction cannot operate on a single value in
a single instruction cycle. Therefore, it is not uncommon
This chapter will try to examine some common methods
to see oating point values loaded, integer values be maof obfuscating code, but will not necessarily delve into
nipulated, and then the oating point value be operated
methods to break the obfuscation.
on.
5.3.3
Interleaving
Optimizing Compilers will engage in a process called in- Optimizing compilers frequently will use instructions that
terleaving to try and maximize parallelism in pipelined are not intuitive. Some instructions can perform tasks for
processors. This technique is based on two premises:
which they were not designed, typically as a helpful side
eect. Sometimes, one instruction can perform a task
1. That certain instructions can be executed out of or- more quickly than other specialized instructions can.
der and still maintain the correct output
The only way to know that one instruction is faster than
another is to consult the processor documentation. How2. That processors can perform certain pairs of tasks
ever, knowing some of the most common substitutions is
simultaneously.
very useful to the reverser.
x86 NetBurst Architecture
The Intel NetBurst Architecture divides an x86 processor into 2 distinct parts: the supporting hardware, and the
primitive core processor. The primitive core of a processor contains the ability to perform some calculations
blindingly fast, but not the instructions that you or I am
familiar with. The processor rst converts the code instructions into a form called micro-ops that are then
handled by the primitive core processor.
Here are some examples. The code in the rst box operates more quickly than the one in the second, but performs exactly the same tasks.
Example 1
Fast
xor eax, eax
Slow
mov eax, 0
The processor can also be broken down into 4 components, or modules, each of which is capable of perform- Example 2
ing certain tasks. Since each module can operate separately, up to 4 separate tasks can be handled simultane- Fast
ously by the processor core, so long as those tasks can be shl eax, 3
performed by each of the 4 modules:
67
or operation on two operands. Consider then, the
following example:
mov al, 0xAA xor al, al
5.3.5 Obfuscators
There are a number of tools on the market that will automate the process of code obfuscation. These products
will use a number of transformations to turn a code snippet into a less-readable form, although it will not aect
the program ow itself (although the transformations may
xor The xor instruction performs the bit-wise exclusive- increase code size or execution time).
68
5.3.6
CHAPTER 5. DIFFICULTIES
Code Transformations
Code transformations are a way of reordering code so that To disassemble an encrypted executable, you must rst
it performs exactly the same task but becomes more dif- determine how the code is being decrypted. Code can be
cult to trace and disassemble. We can best demonstrate decrypted in one of two primary ways:
this technique by example. Lets say that we have 2 functions, FunctionA and FunctionB. Both of these two func1. All at once. The entire code portion is decrypted in
tions are comprised of 3 separate parts, which are pera single pass, and left decrypted during execution.
formed in order. We can break this down as such:
Using a debugger, allow the decryption routine to
FunctionA() { FuncAPart1(); FuncAPart2(); FuncArun completely, and then dump the decrypted code
Part3(); } FunctionB() { FuncBPart1(); FuncBPart2();
into a le for further analysis.
FuncBPart3(); }
2. By Block. The code is encrypted in separate blocks,
where each block may have a separate encryption
And we have our main program, that executes the two
key. Blocks may be decrypted before use, and refunctions:
encrypted again after use. Using a debugger, you
main() { FunctionA(); FunctionB(); }
can attempt to capture all the decryption keys and
then use those keys to decrypt the entire program
at once later, or you can wait for the blocks to be
Now, we can rearrange these snippets to a form that is
decrypted, and then dump the blocks individually to
much more complicated (in assembly):
a separate le for analysis.
main: jmp FAP1 FBP3: call FuncBPart3 jmp end FBP1:
call FuncBPart1 jmp FBP2 FAP2: call FuncAPart2 jmp
FAP3 FBP2: call FuncBPart2 jmp FBP3 FAP1: call
FuncAPart1 jmp FAP2 FAP3: call FuncAPart3 jmp 5.4 Debugger Detectors
FBP1 end:
As you can see, this is much harder to read, although it
perfectly preserves the program ow of the original code.
This code is much harder for a human to read, although 5.4.1 Detecting Debuggers
it isn't hard at all for an automated debugging tool (such
as IDA Pro) to read.
It may come as a surprise that a running program can
actually detect the presence of an attached user-mode
debugger. Also, there are methods available to detect
5.3.7 Opaque Predicates
kernel-mode debuggers, although the methods used depend in large part on which debugger is trying to be deAn Opaque Predicate is a predicate inside the code, that tected.
cannot be evaluated during static analysis. This forces the
This subject is peripheral to the narrative of this book,
attacker to perform a dynamic analysis to understand the
and the section should be considered an optional one for
result of the line. Typically this is related to a branch
most readers.
instruction that is used to prevent in static analysis the
understanding which code path is taken.
Code Encryption
The Win32 API contains a function called IsDebuggerCode can be encrypted, just like any other type of data, Present, which will return a boolean true if the program
except that code can also work to encrypt and decrypt is being debugged. The following code snippet will detail
itself. Encrypted programs cannot be directly disassem- a general usage of this function:
bled. However, such a program can also not be run di- if(IsDebuggerPresent())
{
TerminateProrectly because the encrypted opcodes cannot be inter- cess(GetCurrentProcess(), 1); }
preted properly by the CPU. For this reason, an encrypted
program must contain some sort of method for decrypt- Of course, it is easy to spot uses of the IsDebuggerPreing itself prior to operation.
sent() function in the disassembled code, and a skilled reThe most basic method is to include a small stub program verser will simply patch the code to remove this line. For
that decrypts the remainder of the executable, and then OllyDbg, there are many plugins available which hide the
passes control to the decrypted routines.
debugger from this and many other APIs.
5.4.3
The Process Environment Block stores the value that IsDebuggerPresent queries to determine its return value.
To avoid suspicion, some programmers access the value
directly from the PEB instead of calling the API function. The following code snippet shows how to access the
value:
mov eax, [fs:0x30] mov al, [eax+2] test al, al jne
@DebuggerDetected
69
To detect SoftICE, there are a number of techniques that
can be used:
1. Search for the SoftICE install directory. If SoftICE
is installed, the user is probably a hacker or a reverser.
2. Detect the presence of int 1. SoftICE uses interrupt
1 to debug, so if interrupt 1 is installed, SoftICE is
running.
5.4.5
Timeouts
5.4.6
Detecting SoftICE
OllyDbg is a popular 32-bit usermode debugger. Unfortunately, the last few releases, including the latest version (v1.10) contain a vulnerability in the handling of the
Win32 API function OutputDebugString(). A programmer trying to prevent his program from being debugged
by OllyDbg could exploit this vulnerability in order to
make the debugger crash. The author has never released
a x, however there are unocial versions and plugins
available to protect OllyDbg from being exploited using
this vulnerability.
Chapter 6
6.1.1
http://www.opensolaris.org/os/community/
mdb/
Wikimedia Resources
Wikibooks
X86 Assembly
Subject:Assembly languages
Compiler Construction
Gareth
Williams:
//gareththegeek.ga.funpic.de/
Floating Point
C Programming
C++ Programming
Wikipedia
6.1.2
http:
External Resources
External Links
http://www.
http://www.microsoft.com/whdc/devtools/
debugging/default.mspx
70
71
Books
Yurichev, Dennis, An Introduction To Reverse This book has a global stylesheet that can be loaded
Engineering for Beginners. Online book: http: for you. Go to the Gadgets tab at Special:Preferences,
//yurichev.com/writings/RE_for_beginners-en.pdf and activate the "Per-book Javascript and Stylesheets"
gadget.
Eilam, Eldad. Reversing: Secrets of Reverse Engineering. 2005. Wiley Publishing Inc. ISBN
0764574817
Hyde, Randall. The Art of Assembly Language,
No Starch, 2003 ISBN 1886411972
Aho, Alfred V. et al. Compilers: Principles, Techniques and Tools, Addison Wesley, 1986. ISBN:
0321428900
Steven Muchnick, Advanced Compiler Design &
Implementation, Morgan Kaufmann Publishers,
1997. ISBN 1-55860-320-4
Kernighan and Ritchie, The C Programming Language, 2nd Edition, 1988, Prentice Hall.
Petzold, Charles. Programming Windows, Fifth
Edition, Microsoft Press, 1999
Hart, Johnson M. Win32 System Programming,
Second Edition, Addison Wesley, 2001
Gordon, Alan. COM and COM+ Programming
Primer, Prentice Hall, 2000
Nebbett, Gary. Windows NT/2000 Native API
Reference, Macmillan, 2000
Levine, John R. Linkers and Loaders, MorganKauman, 2000
Knuth, Donald E. The Art of Computer Programming, Vol 1, 1997, Addison Wesley.
MALWARE: Fighting Malicious Code, by Ed Skoudis; Prentice Hall, 2004
Maximum Linux Security, Second Edition, by Anonymous; Sams, 2001
6.2 Licensing
6.2.1
Licensing
Chapter 7
https://en.wikibooks.org/wiki/X86_Disassembly/Introduction?oldid=2370674 Contributors:
72
7.2. IMAGES
73
Contributors:
Source:
https://en.wikibooks.org/wiki/X86_Disassembly/Licensing?oldid=1075890
Contributors:
7.2 Images
File:1Fh_01.png Source: https://upload.wikimedia.org/wikibooks/en/a/af/1Fh_01.png License: Fair use Contributors: ? Original artist: ?
File:C_language_building_steps.png Source: https://upload.wikimedia.org/wikipedia/commons/b/b3/C_language_building_steps.png
License: CC-BY-SA-3.0 Contributors: ? Original artist: ?
File:C_language_do_while.png Source: https://upload.wikimedia.org/wikipedia/commons/2/21/C_language_do_while.png License: CC
BY 3.0 Contributors: Own work Original artist: Thedsadude
File:C_language_for.png Source: https://upload.wikimedia.org/wikipedia/commons/5/51/C_language_for.png License: CC BY 3.0 Contributors: Own work Original artist: Thedsadude
File:C_language_if.png Source: https://upload.wikimedia.org/wikipedia/commons/f/fb/C_language_if.png License: CC BY 3.0 Contributors: Own work Original artist: Thedsadude
File:C_language_if_else.png Source: https://upload.wikimedia.org/wikipedia/commons/a/ac/C_language_if_else.png License: CC BY
3.0 Contributors: Own work Original artist: Thedsadude
File:C_language_linked_list.png Source: https://upload.wikimedia.org/wikipedia/commons/1/1b/C_language_linked_list.png License:
CC BY 3.0 Contributors: Own work Original artist: Thedsadude
File:Data_stack.svg Source: https://upload.wikimedia.org/wikipedia/commons/2/29/Data_stack.svg License: Public domain Contributors: made in Inkscape, by myself User:Boivie. Based on Image:Stack-sv.png, originally uploaded to the Swedish Wikipedia in 2004 by
sv:User:Shrimp Original artist: User:Boivie
File:Elf-layout--en.svg Source: https://upload.wikimedia.org/wikipedia/commons/7/77/Elf-layout--en.svg License: CC BY-SA 3.0 Contributors: Own work Original artist: Suruea
File:Heckert_GNU_white.svg Source: https://upload.wikimedia.org/wikipedia/commons/2/22/Heckert_GNU_white.svg License: CC
BY-SA 2.0 Contributors: gnu.org Original artist: Aurelio A. Heckert <[email protected]>
74
File:Information_icon.svg Source: https://upload.wikimedia.org/wikipedia/commons/3/35/Information_icon.svg License: Public domain Contributors: en:Image:Information icon.svg Original artist: El T
File:Kernel-exo.svg Source: https://upload.wikimedia.org/wikipedia/commons/8/8f/Kernel-exo.svg License: CC-BY-SA-3.0 Contributors: self-made, based on Image:Kernel-exo.png by User:Aholstenson Original artist: Surachit
File:RevEngDosHead.JPG Source: https://upload.wikimedia.org/wikipedia/commons/2/2f/RevEngDosHead.JPG License: Public domain Contributors: Transferred from en.wikibooks to Commons by Adrignola using CommonsHelper. Original artist: Whiteknight at
English Wikibooks
File:RevEngPEFile.JPG Source: https://upload.wikimedia.org/wikipedia/commons/e/ea/RevEngPEFile.JPG License: Public domain
Contributors: Transferred from en.wikibooks to Commons by Adrignola using CommonsHelper. Original artist: Whiteknight at English
Wikibooks
File:RevEngPeSig.JPG Source: https://upload.wikimedia.org/wikipedia/commons/c/ca/RevEngPeSig.JPG License: Public domain Contributors: Transferred from en.wikibooks; transferred to Commons by User:Adrignola using CommonsHelper. Original artist: Original
uploader was Whiteknight at en.wikibooks
File:ReverseEngineeringPop.JPG Source: https://upload.wikimedia.org/wikibooks/en/8/8c/ReverseEngineeringPop.JPG License: Public domain Contributors: ? Original artist: ?
File:ReverseEngineeringPush.JPG Source: https://upload.wikimedia.org/wikibooks/en/9/98/ReverseEngineeringPush.JPG License:
Public domain Contributors: ? Original artist: ?
File:Tree-data-structure.svg Source: https://upload.wikimedia.org/wikipedia/commons/4/45/Tree-data-structure.svg License: GFDL
1.2 Contributors: ? Original artist: ?
File:Wikibooks-logo-en-noslogan.svg Source: https://upload.wikimedia.org/wikipedia/commons/d/df/Wikibooks-logo-en-noslogan.
svg License: CC BY-SA 3.0 Contributors: Own work Original artist: User:Bastique, User:Ramac et al.
File:Wikipedia-logo.png Source: https://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png License: GFDL Contributors: based on the rst version of the Wikipedia logo, by Nohat. Original artist: version 1 by Nohat (concept by Paullusmagnus);