C Vulnerabilities Slides
C Vulnerabilities Slides
countermeasures
Frank Piessens
([email protected])
Heap grows
Heap (dynamic data)
up
Static Data
f1:
buffer[]
overflow()
SPARC Solaris:
char shellcode[] =
"\x2d\x0b\xd8\x9a\xac\x15\xa1\x6e\x2f\x0b\xdc\xda\x90\x0b\x80\x0e"
"\x92\x03\xa0\x08\x94\x1a\x80\x0a\x9c\x03\xa0\x10\xec\x3b\xbf\xf0"
"\xdc\x23\xbf\xf8\xc0\x23\xbf\xfc\x82\x10\x20\x3b\x91\xd0\x20\x08"
"\x90\x1b\xc0\x0f\x82\x10\x20\x01\x91\xd0\x20\x08";
Heap
KATHOLIEKE! Secappdev 2011 31
UNIVERSITEIT!
LEUVEN
Exploiting a buffer overrun
Top Heap
grows
with brk() RA Green value is written
12 bytes above where
red value points
Net result is that the
return address points to
the injected code
Stack
Heap
KATHOLIEKE! Secappdev 2011 32
UNIVERSITEIT!
LEUVEN
Indirect pointer overwrite
This technique of overwriting a pointer that is later
dereferenced for writing is called indirect pointer
overwrite
This is a broadly useful attack technique, as it allows to
selectively change memory contents
A program is vulnerable if:
It contains a bug that allows overwriting a pointer value
This pointer value is later dereferenced for writing
And the value written is under control of the attacker
f1
Params for f1
.
.
return
Return addr
f2
Params for f2
.
Return addr .
return
f3
Params for f3
return
Return addr
.
SP Return addr
.
IP return
KATHOLIEKE! Secappdev 2011 36
UNIVERSITEIT!
LEUVEN
Return-into-libc: overview
Stack Code Memory
f1
Params for f1
.
.
return
Return addr
f2
Params for f2
.
Return addr .
return
IP f3
Params for f3
SP return
Return addr
.
.
return
KATHOLIEKE! Secappdev 2011 37
UNIVERSITEIT!
LEUVEN
Return-into-libc: overview
Stack Code Memory
f1
Params for f1
.
.
return
Return addr
f2
Params for f2
.
Return addr .
return
f3
Params for f3
IP
SP return
Return addr
.
.
return
KATHOLIEKE! Secappdev 2011 38
UNIVERSITEIT!
LEUVEN
Return-into-libc: overview
Stack Code Memory
f1
Params for f1
.
.
return
Return addr
f2
Params for f2
.
Return addr .
return
f3
Params for f3
SP IP return
Return addr
.
.
return
KATHOLIEKE! Secappdev 2011 39
UNIVERSITEIT!
LEUVEN
Return-into-libc: overview
Stack Code Memory
f1
Params for f1
.
.
return
Return addr
IP f2
Params for f2
.
SP Return addr .
return
f3
return
.
.
return
KATHOLIEKE! Secappdev 2011 40
UNIVERSITEIT!
LEUVEN
Return-into-libc: overview
Stack Code Memory
f1
Params for f1
.
.
return
Return addr
f2
Params for f2
.
SP Return addr .
IP return
f3
return
.
.
return
KATHOLIEKE! Secappdev 2011 41
UNIVERSITEIT!
LEUVEN
Return-into-libc: overview
Stack Code Memory
IP f1
Params for f1
.
.
return
SP Return addr
f2
.
.
return
f3
return
.
.
return
KATHOLIEKE! Secappdev 2011 42
UNIVERSITEIT!
LEUVEN
Return-to-libc
What do we need to make this work?
Inject the fake stack
Easy: this is just data we can put in a buffer
Make the stack pointer point to the fake stack right
before a return instruction is executed
We will show an example where this is done by jumping to
a trampoline
Then we make the stack execute existing functions
to do a direct code injection
But we could do other useful stuff without direct code
injection
KATHOLIEKE! Secappdev 2011 43
UNIVERSITEIT!
LEUVEN
Vulnerable program
Trampoline code
VirtualAlloc
.
.
return
.
.
.
InterlockedEcxh
ange
return
.
.
SP
.
KATHOLIEKE! Secappdev 2011 47
UNIVERSITEIT!
LEUVEN
Unwinding the fake stack
Code Memory
IP VirtualAlloc
.
.
return
.
.
.
InterlockedEcxh
ange
return
SP .
.
.
KATHOLIEKE! Secappdev 2011 48
UNIVERSITEIT!
LEUVEN
Unwinding the fake stack
Code Memory
VirtualAlloc
.
.
IP return
.
.
.
InterlockedEcxh
ange
return
SP .
.
.
KATHOLIEKE! Secappdev 2011 49
UNIVERSITEIT!
LEUVEN
Unwinding the fake stack
Code Memory
VirtualAlloc
.
.
return
.
.
.
SP
IP InterlockedEcxh
ange
return
.
.
.
KATHOLIEKE! Secappdev 2011 50
UNIVERSITEIT!
LEUVEN
Unwinding the fake stack
Code Memory
VirtualAlloc
.
.
return
.
.
.
SP
InterlockedEcxh
ange
IP return
.
.
.
KATHOLIEKE! Secappdev 2011 51
UNIVERSITEIT!
LEUVEN
Overview
Introduction
Example attacks
Stack-based buffer overflow
Heap-based buffer overflow
Return-to-libc attacks
Data-only attacks
Example defenses
Stack canaries
Non-executable data
Control-flow integrity
Layout randomization
Conclusion
KATHOLIEKE! Secappdev 2011 52
UNIVERSITEIT!
LEUVEN
Data-only attacks
These attacks proceed by changing only data of
the program under attack
Depending on the program under attack, this can
result in interesting exploits
We discuss two examples:
The unix password attack
Overwriting the environment table
Stack
Password check in login program:
1. Read loginname
Hashed password 2. Lookup hashed password
3. Read password
password 4. Check if
hashed password = hash (password)
ATTACK: type in a password of the form pw || hash(pw)
KATHOLIEKE! Secappdev 2011 55
UNIVERSITEIT!
LEUVEN
Overwriting the environment table
f1:
buffer[]
overflow()
SP
SP