02 CTRL Hijacking - 2
02 CTRL Hijacking - 2
02 CTRL Hijacking - 2
Control Hijacking
Basic Control
Hijacking Attacks
Dan Boneh
Control hijacking attacks
• Attacker’s goal:
Take over target machine (e.g. web server)
• Execute arbitrary code on target by hijacking
application control flow
• Examples:
– Buffer overflow and integer overflow attacks
– Format string vulnerabilities
– Use after free
Dan Boneh
First example: buffer overflows
Extremely common bug in C/C++ programs.
• First major exploit: 1988 Internet Worm. Fingerd.
Source: web.nvd.nist.gov
Dan Boneh
What is needed
• Understanding C functions, the stack, and the heap.
• Know how system calls are made
• The exec() system call
• Attacker needs to know which CPU and OS used on the target machine:
– Our examples are for x86 running Linux or Windows
– Details vary slightly between CPUs and OSs:
• Stack Frame structure (Unix vs. Windows, x86 vs. ARM)
• Little endian vs. big endian
Dan Boneh
Linux process memory layout (x86_64)
shared libraries
0x0000 7F1F6 XXXX XXXX
arguments
return address
rbp stack base pointer
exception handlers
Stack
local variables Growth
rsp callee saved registers
low
(esp in 32-bit mode)
Dan Boneh
What are buffer overflows?
Suppose a web server contains a function: void func(char *str) {
char buf[128];
After func() is called stack looks like:
strcpy(buf, str);
do-something(buf);
argument: str }
return address
stack base pointer
char buf[128]
rsp
Dan Boneh
What are buffer overflows?
What if *str is 136 bytes long? void func(char *str) {
After strcpy: char buf[128];
strcpy(buf, str);
do-something(buf);
argument: str }
return address
stack base pointer Poisoned return address!
*str
Problem:
char buf[128] no bounds checking in strcpy()
rsp
Dan Boneh
Stack
Basic stack exploit
high
Suppose *str is such that Program P
after strcpy stack looks like:
Program P: exec(“/bin/sh”)
(exact shell code by Aleph One)
return address
FP1 method #1
vptr FP2 method #2
FP3
method #3
data vtable
vptr
data
buf[256] vtable
Dan Boneh
A reliable exploit?
<SCRIPT language="text/javascript">
shellcode = unescape("%u4343%u4343%..."); // allocate in heap
overflow-string = unescape(“%u2332%u4276%...”);
cause-overflow(overflow-string ); // overflow buf[ ]
</SCRIPT>
data
buf[256] vtable
ptr
shellcode
Dan Boneh
Heap Spraying [SkyLined]
heap
vtable
Dan Boneh
Control Hijacking
More Control
Hijacking Attacks
Dan Boneh
More Hijacking Opportunities
• Integer overflows: (e.g. MS DirectX MIDI Lib)
• Double free: double free space on heap
– Can cause memory mgr to write data to specific location
– Examples: CVS server
• Use after free: using memory after it is freed
• Format string vulnerabilities
Dan Boneh
Integer Overflows (see Phrack 60)
Dan Boneh
Integer overflow exploit stats
700
600
500
400
300
200
100
0
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 ⋯ 2015 2016 2017 2018 2019 2020
Dan Boneh
Format string problem
int func(char *user) {
fprintf(stderr, user);
}
Printing:
printf, fprintf, sprintf, …
vprintf, vfprintf, vsprintf, …
Logging:
syslog, err, warn
Dan Boneh
Exploit
• Dumping arbitrary memory:
– Walk up stack until desired pointer is found.
– printf( “%08x.%08x.%08x.%08x|%s|”)
Dan Boneh
High impact security vulns. in Chrome 2015 – 2020 (C++)
document.getElementById("c1").onpropertychange = changer;
document.getElementById("form").reset();
</script> Dan Boneh
What just happened?
c1.doReset() causes changer() to be called and free object c2
Dan Boneh
What just happened?
c1.doReset() causes changer() to be called and free object c2
object c2 FP1
vptr FP2 ShellCode
FP3
data vtable
Use after free !
document.getElementById("c1").onpropertychange = changer;
document.getElementById("form").reset();
</script>
DEFENSES
Dan Boneh
THE END
Dan Boneh
References on heap spraying
[1] Heap Feng Shui in Javascript,
by A. Sotirov, Blackhat Europe 2007
Dan Boneh