Binary Exploitation 1�Buffer Overflows��(return-to-libc, ROP, Canaries, W^X, ASLR)
Chester Rebeiro
Indian Institute of Technology Madras
CR
Parts of Malware
Subvert execution:
change the normal execution behavior of the program
Payload:
the code which the attacker wants to execute�
2
CR
Subvert Execution
3
These do not really subvert execution,�but can lead to confidentiality attacks.
CR
Buffer Overflows in the Stack
4
http://insecure.org/stf/smashstack.html
CR
Stack in a Program�(when function is executing)
5
EBP
Parameters
for function
return Address
Locals of function
prev frame pointer
push $3
push $2
push $1
Stack
call function
push %ebp
movl %esp, %ebp
sub $20, %esp
%ebp: Frame Pointer
In main
In function
ESP
ESP
ESP
ESP
ESP
ESP
%esp : Stack Pointer
CR
Stack Usage (example)
6
Stack (top to bottom): | |
address | stored data |
1000 to 997 | 3 |
996 to 993 | 2 |
992 to 989 | 1 |
988 to 985 | return address |
984 to 981 | %ebp (stored frame pointer) |
(%ebp)980 to 976 | buffer1 |
975 to 966 | buffer2 |
(%sp) 964 | |
stack pointer
Parameters
for function
Return Address
Locals of function
prev frame pointer
frame pointer
CR
Stack Usage Contd.
7
Stack (top to bottom): | |
address | stored data |
1000 to 997 | 3 |
996 to 993 | 2 |
992 to 989 | 1 |
988 to 985 | return address |
984 to 981 | %ebp (stored frame pointer) |
(%ebp)980 to 976 | buffer1 |
975 to 966 | buffer2 |
(%sp) 964 | |
What is the output of the following?
976 🡪 buffer1
Therefore buffer2[10] = buffer1[0]
A BUFFER OVERFLOW
CR
Modifying the Return Address
buffer2[19] =
&arbitrary memory location
This causes execution of an arbitrary memory location instead of the standard return
8
Stack (top to bottom): | |
address | stored data |
1000 to 997 | 3 |
996 to 993 | 2 |
992 to 989 | 1 |
988 to 985 | |
984 to 981 | %ebp (stored frame pointer) |
(%ebp)980 to 976 | buffer1 |
976 to 966 | buffer2 |
(%sp) 964 | |
Return Address
19
Arbitrary Location
CR
Now that we seen how buffer overflows can skip an instruction,
We will see how an attacker can use it to execute his own code (exploit code)
9
Stack (top to bottom): | |
address | stored data |
1000 to 997 | 3 |
996 to 993 | 2 |
992 to 989 | 1 |
988 to 985 | ATTACKER’S code pointer |
984 to 981 | %ebp (stored frame pointer) |
(%ebp)980 to 976 | buffer1 |
976 to 966 | buffer2 |
(%sp) 964 | |
CR
Big Picture of the exploit
Fill the stack as follows
(where BA is buffer address)
10
stack pointer
Parameters
for function
Return Address
buffer
prev frame pointer
frame pointer
Exploit code
BA
BA
buffer Address
BA
BA
BA
BA
BA
BA
BA
CR
Payload
11
CR
Step 1 : Get machine codes
12
CR
Step 2: Find Buffer overflow in an application
13
O
O
O
O
o
Defined on stack
CR
Step 3 :�Put Machine Code in Large String
14
shellcode
large_string
CR
Step 3 (contd) : �Fill up Large String with BA
15
shellcode
BA
BA
BA
BA
BA
BA
BA
BA
large_string
Address of buffer is BA
CR
Final state of Stack
exploit code would be executed
16
shellcode
BA
BA
BA
BA
BA
BA
BA
BA
large_string
shellcode
BA
BA
buffer Address
BA
BA
BA
BA
BA
BA
BA
buffer
BA
CR
Putting it all together
17
bash$ gcc overflow1.c
bash$ ./a.out
$sh
CR
Buffer overflow in the Wild
18
CR
CODERED Worm
19
GET /default.ida?NNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNN%u9090%u6858%ucbd3%u7801%u9090%u6858%ucbd3%u7801%u9090%u6858%ucbd3%u7801%u9090%u9090%u8190%u00c3%u0003%u8b00%u531b%u53ff%u0078%u0000%u00=a HTTP/1.0
CR
Defenses
20
Safer programming languages; Safer libraries; hardware enhancements; � static analysis
W^X , ASLR, canaries
malware run-time detection techniques using learning techniques, ANN and malware signatures
DIFT; ensure malware does not steal sensitive information
CR
Preventing Buffer Overflows�with Canaries and W^X
21
CR
Canaries
22
Stack (top to bottom): | |
| stored data |
| 3 |
| 2 |
| 1 |
| ret addr |
| sfp (%ebp) |
| Insert canary here |
| buffer1 |
| buffer2 |
| |
Insert a canary here
check if the canary value
has got modified
CR
Canaries and gcc
23
CR
Canaries Example
24
Without canaries, the return address on stack gets overwritten resulting in a segmentation fault. With canaries, the program gets aborted due to stack smashing.
CR
Canaries Example
25
Without canaries, the return address on stack gets overwritten resulting in a segmentation fault. With canaries, the program gets aborted due to stack smashing.
CR
Canary Internals
26
Store canary onto stack
Verify if the canary has changed
Without canaries
With canaries
gs is a segment that shows thread local data; in this case it is used for picking out canaries
CR
Non Executable Stacks (W^X)
27
27
CR
Will non executable stack prevent buffer overflow attacks ?
Return – to – LibC Attacks
(Bypassing non-executable stack during exploitation using return-to-libc attacks)
28
28
https://css.csail.mit.edu/6.858/2010/readings/return-to-libc.pdf
CR
Return to Libc�(big picture)
29
29
Exploit code
BA
BA
BA
BA
BA
BA
BA
BA
buffer
This will not work if ND bit is set
Return Address
CR
Return to Libc�(replace return address to point to a function within libc)
30
30
F1 Addr
F1 Addr
F1 Addr
F1 Addr
F1 Addr
F1 Addr
F1 Addr
F1 Addr
buffer
Return Address
F1 Addr
Stack
Heap
Data
Text
Bypasses W^X since F1 is in the code segment,
And can be legally executed.
CR
F1 = system()
system(“/bin/bash”);
would create a bash shell
(there could be other options as well)
So we need to
/bin/sh
31
31
CR
The return-to-libc attack
32
32
F1ptr
F1ptr
F1ptr
F1ptr
F1ptr
Shell ptr
F1 ptr
F1ptr
buffer
F1ptr
Return Address
system()
In libc
/bin/bash
CR
Find address of system in the executable
33
33
CR
Find address of /bin/sh
34
34
CR
Finding the address of the string �/bin/sh
35
CR
The final Exploit Stack
36
xxx
xxx
xxx
0x28085260
dead
0xbfbffe25
xxx
xxx
buffer
xxx
Return Address
system()
In libc
/bin/sh
CR
A clean exit
37
xxx
xxx
xxx
0x28085260
0x281130d0
0xbfbffe25
xxx
xxx
buffer
xxx
Return Address
system()
In libc
/bin/bash
exit()
In libc
CR
Limitation of ret2libc
38
38
Limitation on what the attacker can do
(only restricted to certain functions in the library)
These functions could be removed from the library
CR
Return Oriented Programming�(ROP)
39
CR
Push and ESP
40
|
|
|
|
|
Stack Segment (top)
ESP
SS
push %EDX
CR
Call and ESP
41
|
|
|
|
|
Stack Segment (top)
ESP
SS
call Address
Instr. After call
EIP
CR
Pop and ESP
42
|
|
|
|
|
Stack Segment (top)
ESP
SS
pop %EDX
CR
Ret and ESP
43
|
|
|
|
|
Stack Segment (top)
ESP
SS
ret
Return address
CR
Return Oriented Programming Attacks
44
The Geometry of Innocent Flesh on the Bone: Return-into-libc without Function Calls (on the x86
CR
Target Payload
Lets say this is the payload needed to be executed by an attacker.
Suppose there is a function in libc, which has exactly this sequence of instructions … then we are done.. we just need to subvert execution to the function
What if such a function does not exist?�If you can’t find it then build it
45
CR
Step 1: Find Gadgets
46
useful instruction(s)
ret
CR
Step 2: Stitching
47
Program Binary
movl %esi, 0x8(%esi)
ret
G1
movb $0x0, 0x7(%esi)
ret
G2
movb $0x0, 0xc(%esi)
ret
G3
movl $0xb, %eax
ret
G4
Ret instruction has 2 steps:
CR
Step 3: Construct the Stack
48
xxx
xxx
xxx
AG1
AG2
AG3
AG4
xxx
buffer
xxx
Return Address
Program Binary
movl %esi, 0x8(%esi)
ret
G1
movb $0x0, 0x7(%esi)
ret
G2
movb $0x0, 0xc(%esi)
ret
G3
movl $0xb, %eax
ret
G4
Program Stack
AGi: Address of Gadget i
CR
Finding Gadgets
The instructions can be intended (put in by the compiler) or unintended
49
CR
Intended vs Unintended Instructions
50
F7 C7 07 00 00 00 0F 95 45 C3
Machine Code :
What the compiler intended..
What was not ntended
Highly likely to find many diverse instructions of this form in x86; not so likely to�have such diverse instructions in RISC processors
CR
Geometry
51
CR
Finding Gadgets
52
C3
00
24
37
24
46
43
16
89
94
child of
CR
Finding Gadgets
53
33 | b2 | 23 | 12 | a0 | 31 | a5 | 67 | 22 | ab | ba | 4a | 3c | c3 | ff | ee | ab | 31 | 11 | 09 |
CR
Finding Gadgets Algorithm
54
CR
Finding Gadgets Algorithm
55
is this sequence of instructions valid x86 instruction?
Boring: not interesting to look further;
Eg. pop %ebp; ret;;;; leave; ret (these are boring if we want to ignore intended instructions)
Jump out of the gadget instructions
Found 15,121 nodes in�~1MB of libc binary
CR
More about Gadgets
56
deadbeef
GadgetAdd
stack
pop %edx
ret
esp
CR
Stitch
57
pop %edx
ret
G1
mov 64(%edx), %eax
ret
G2
G2
addr
G1
stack
esp
deadbeef
addr+64
Load arbitrary data into eax register using
Gadgets G1 and G2
CR
Store Gadget
58
GadgetAddr 2
0
GadgetAddr 1
stack
pop %edx
ret
esp
mov %eax, 24(%edx)
ret
24
CR
Gadget for addition
59
addl (%edx), %eax
inc %esp
mov %edi, (%esp)
ret
Add the memory pointed�to by %edx to %eax.
The result is stored in %eax
why is this present?�…. This is unnecessary, but�this is best gadget that we can�find for addition
But can create problems!!
We need work arounds!
GadgetAddr2
GadgetAddr
stack
esp
Modified
CR
Gadget for addition�(put 0xc3 into %edi)
60
addl (%edx), %eax
inc %esp
mov %edi, (%esp)
ret
GadgetAddr3
Gadget_RET
GadgetAddr2
Gadget_RET
GadgetAddr1
stack
esp
0xc3
0xc3 is ret ; in ROP ret is equivalent to NOP v
pop %edi
ret
CR
Unconditional Branch�in ROP
61
GA
stack
esp
pop %esp
ret
CR
Conditional Branches
62
In x86 instructions conditional branches have 2 parts
eg. CMP %eax, %ebx (will set ZF if %eax = %ebx)
2. A branch instruction (eg. JZ, JCC, JNZ, etc)� which internally checks the conditional flag and� changes the EIP accordingly
In ROP conditional branches have 3 parts
eg. CMP %eax, %ebx (will set ZF if %eax = %ebx)
2. Transfer flags to a register or memory
3. Perturb %esp based on flags stored in memory
In ROP, we need flags to modify %esp register instead of EIP
Needs to be explicitly handled
CR
Step 1 : Set the flags
Find suitable ROPs that set appropriate flags
63
CMP %eax, %ebx
RET
subtraction
Affects flags CF, OF, SF, ZF, AF, PF
NEG %eax
RET
2s complement negation
Affects flags CF
CR
Step 2: Transfer flags to �memory or register
ROPs for these two not easily found.
A third way – perform an operation whose result depends on the flag contents.
64
where would one use this instruction?
CR
Step 2: Indirect way to transfer flags to memory
Several instructions operate using the contents of the flags�
65
ADC %eax, %ebx : add with carry; performs eax <- eax + ebx + CF
(if eax and ebx are 0 initially, then the result will be either 1 or 0 depending on the CF)
RCL : rotate left with carry;
RCL %eax, 1
(if eax = 0. then the result is either 0 or 1 depending on CF)
CR
Gadget to transfer flags to memory
66
%edx will have value A
%ecx will contain 0x0
A
CR
Step 3: Perturb %esp depending �on flag
67
If (CF is set){
perturb %esp
}else{
leave %esp as it is
}
What we hope to achieve
CF stored in a memory location (say X)
Current %esp
delta, how much to perturb %esp
What we have
negate X
offset = delta & X
%esp = %esp + offset
One way of achieving …
if (X = 0) 2’s complement is 000000000...
2. offset = delta if X = 1� offset = 0 if X = 0
3. %esp = %esp + offset if X = 1� %esp = %esp if X = 0
CR
Turing Complete
Eg. ROPGadget (https://github.com/JonathanSalwan/ROPgadget)
Ropper (https://github.com/sashs/Ropper)
68
CR
Address Space Layout Randomization�(ASLR)
69
CR
The Attacker’s Plan
Attacker depends upon knowning where these functions reside in memory. Assumes that many systems use the same address mapping. Therefore one exploit may spread easily
70
CR
Address Space Randomization
71
Memory layout across boots for a Windows box
CR
ASLR in the Linux Kernel
2 : (default setting) setting 1 as well as the data segment location is
randomized
72
CR
ASLR in Action
73
First Run
Another Run
CR
ASLR in the Linux Kernel
74
/etc/sysctl.conf, for example:
kernel.randomize_va_space = value
sysctl -p
CR
Internals : Making code relocatable
75
CR
Load Time Relocatable
76
1
CR
Load Time Relocatable
77
note the 0x0 here… �the actual address of mylib_int is not filled in
2
CR
Load Time Relocatable
78
Relocatable table present in the executable�that contains all references of mylib_int
3
CR
Load Time Relocatable
79
The loader fills in the actual address of mylib_int�at run time.
4
CR
Load Time Relocatable
80
Limitations
CR
PIC Internals
81
Details about PIC and GOT taken from …
http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/
CR
Global Offset Table (GOT)
82
Without GOT
With GOT
CR
Enforcing Relative Addressing�(example)
83
With load time relocatable
With PIC
CR
Enforcing Relative Addressing�(example)
84
With load time relocatable
With PIC
Get address of next instruction�to achieve relativeness
Index into GOT and get the �actual address of mylib_int into�eax
Now work with the actual �address.
CR
Advantage of the GOT
85
CR
An Example of working with GOT
86
$gcc –m32 –shared –fpic –S got.c
Besides a.out, this compilation also generates got.s
The assembly code for the program
CR
87
Data section
Text section
Fills %ecx with the eip of the next�instruction. �Why do we need this indirect way of doing this?
In this case what will %ecx contain?
The macro for the GOT is known by the linker.
%ecx will now contain the offset to GOT
Load the absolute address of myglob from the�GOT into %eax
CR
More
88
offset of myglob�in GOT
GOT it!
CR
Deep Within the Kernel �(randomizing the data section)
89
loading the executable
Check if randomize_va_space�is > 1 (it can be 1 or 2)
Compute the end of the data segment (m->brk + 0x20)
Finally Randomize
CR
Function Calls in PIC
90
CR
The PLT
91
1
Preparation of arguments for a ‘resolver’
Call to resolver function
CR
First Invocation of Func
First Invocation of fun
92
1
2
(steps 2 and 3)
On first invocation of func, PLT[n]�jumps to GOT[n], which simply jumps
back to PLT[n]
3
CR
First Invocation of Func
93
1
2
(step 4). Invoke resolver, which resolves the actual of func, �places this actual address into GOT
and then invokes func
The arguments passed to resolver, that helps to do symbol resolution
Note that the contents of GOT is now�changed to point to the actual address�of func
3
4
CR
Example of PLT
94
Compiler converts the call to set_mylib_int�into set_mylib_int@plt
CR
Example of PLT
95
ebx points to the GOT table�ebx + 0x10 is the offset corresponding�to set_mylib_int
Offset of set_mylib_int in the GOT (+0x10).
It contains the address of the next instruction (ie. 0x3c2)
CR
Example of PLT
96
Push arguments for the resolver.
Jump to the first entry of the PLT
Ie. PLT[0]
Jump to the resolver, which resolves the actual address of set_mylib_int and fills it into the GOT
CR
Subsequent invocations of Func
97
1
2
3
CR
Advantages
98
CR
Bypassing ASLR
99
CR
Safer Programming Languages,�and Compiler Techniques
100
CR
Other Precautions for buffer overflows
(_s is for secure)
101
CR
Compile Bounds Checking
102
Softbound : Highly Compatible and Complete Spatial Memory Safety for C
Santosh Nagarakatte, Jianzhou Zhao, Milo M. K. Martin, and Steve Zdancewic
CR
Softbound
These checks are automatically inserted at compile time for all pointer variables. For non-pointers, this check is not required.
103
CR
Softbound – more details
No specific checks are required, until dereferencing is done
104
CR
Storing Metadata
105
CR
Softbound – more details
Compiler modifies every function declaration to
add more arguments related to metadata
For each function parameter that is a pointer, the corresponding base
and bound values are also sent to the function
106
CR