02 CTRL Hijacking PDF
02 CTRL Hijacking PDF
Hijacking
Basic
Control
Hijacking
A5acks
Dan
Boneh
600
500
400
300
200
100
0
Source:
NVD/CVE
1995 1997 1999 2001 2003
2005
Dan Boneh
What
is
needed
Understanding
C
funcFons,
the
stack,
and
the
heap.
Know
how
system
calls
are
made
The
exec()
system
call
A5acker
needs
to
know
which
CPU
and
OS
used
on
the
target
machine:
user stack
shared
libraries
brk
Loaded
from
exec
0xC0000000
0x40000000
0x08048000
0
Dan Boneh
Stack
Frame
high
arguments
return
address
stack
frame
pointer
excepFon
handlers
local
variables
SP
Stack
Growth
low
Dan
Boneh
char
buf[128]
SP
Dan Boneh
*str
argument:
str
return
address
stack
frame
pointer
char
buf[128]
SP
Problem:
no
length
checking
in
strcpy()
Dan Boneh
high
Program P
Program
P:
exec(/bin/sh)
return address
When
func()
exits,
the
user
gets
shell
!
Note:
a5ack
code
P
runs
in
stack.
char
buf[128]
low
Dan
Boneh
high
Program P
NOP Slide
return address
char
buf[128]
low
Dan
Boneh
FuncPtr
Heap
or
stack
Dan Boneh
ptr
vtable
data
method
#1
method
#2
method
#3
NOP
slide
Object T
shell
code
vtable
data
buf[256]
ptr
object T
Dan Boneh
Control Hijacking
More
Control
Hijacking
A5acks
Dan
Boneh
c = 0
s = 0x80 + 0x80
s = 0
m = 0x80 + 0x80
m = 0
Can
this
be
exploited?
Dan Boneh
An
example
void
func(
char
*buf1,
*buf2,
unsigned
int
len1,
len2)
{
char temp[256];
if (len1 + len2 > 256) {return -1}
// length check
memcpy(temp, buf1, len1);
// cat buffers
memcpy(temp+len1, buf2, len2);
do-something(temp);
// do stuff
}
Dan Boneh
1996
1998
2000
2002
2004
2006
Source: NVD/CVE
Dan Boneh
Dan Boneh
{
fprintf( stderr, user);
Vulnerable
funcFons
Any
funcFon
using
a
format
string.
PrinFng:
prin{,
fprin{,
sprin{,
vprin{,
vfprin{,
vsprin{,
Logging:
syslog,
err,
warn
Dan
Boneh
Exploit
Dumping
arbitrary
memory:
Walk
up
stack
unFl
desired
pointer
is
found.
prin{(
%08x.%08x.%08x.%08x|%s|)
WriFng
to
arbitrary
memory:
prin{(
hello
%n,
&temp)
--
writes
6
into
temp.
prin{(
%08x.%08x.%08x.%08x.%n)
Dan
Boneh
Control Hijacking
Pla{orm Defenses
Dan Boneh
Audit
sofware
Automated
tools:
Coverity,
Prefast/Prex.
Dan Boneh
LimitaFons:
Some
apps
need
executable
heap
(e.g.
JITs).
Does
not
defend
against
`Return
Oriented
Programming
exploits
Dan
Boneh
exec()
printf()
local buf
/bin/sh
Dan Boneh
Response: randomizaFon
Dan Boneh
ASLR
Example
Booting twice loads libraries into different locations:
Dan Boneh
execute enabled
execute enabled
heap
execute enabled
shellcode
vtable
Dan Boneh
Control Hijacking
Run-Fme Defenses
Dan Boneh
Frame
2
canary
sfp ret str
Frame
1
local
canary
top
of
stack
Dan
Boneh
Canary
Types
Random
canary:
Random
string
chosen
at
program
startup.
Insert
canary
string
into
every
stack
frame.
Verify
canary
before
returning
from
funcFon.
Exit
program
if
canary
changed.
Turns
potenFal
exploit
into
DoS.
StackGuard
(Cont.)
StackGuard
implemented
as
a
GCC
patch
Dan Boneh
args
ret
addr
SFP
excepaon
handlers
Stack
Growth
CANARY
local
string
buers
local
non-buer
variables
copy
of
pointer
args
SEH
frame
next
handler
buf
ptr
to
next
next
handler
a5ack
code
SEH
frame
next
handler
high
mem
Dan
Boneh
Dan Boneh
dest
Libsafe strcpy
src
buf
sfp ret-addr
main
top
of
stack
Dan
Boneh
sfp ret-addr
dest
Libsafe strcpy
src
buf
sfp ret-addr
high
memory
main
Dan Boneh
More
methods
StackShield
At
funcFon
prologue,
copy
return
address
RET
and
SFP
to
safe
locaFon
(beginning
of
data
segment)
Upon
return,
check
that
RET
and
SFP
is
equal
to
copy.
Implemented
as
assembler
le
processor
(GCC)
Control
Flow
Integrity
(CFI)
A
combinaFon
of
staFc
and
dynamic
checking
StaFcally
determine
program
control
ow
Dynamically
enforce
control
ow
integrity
Dan
Boneh
Control Hijacking
Advanced
Hijacking
A5acks
Dan
Boneh
Dan Boneh
ptr
data
vtable
method
#1
method
#2
method
#3
Object T
vtable
data
buf[256]
ptr
object T
Dan Boneh
ptr
vtable
data
method
#1
method
#2
method
#3
Object T
shell
code
vtable
data
buf[256]
ptr
object T
Dan Boneh
A
reliable
exploit?
<SCRIPT language="text/javascript">
shellcode
=
unescape("%u4343%u4343%...");
overow-string
=
unescape(%u2332%u4276%...);
vtable
data
ptr
???
shellcode
Dan
Boneh
shellcode
heap
vtable
heap
spray
area
Dan
Boneh
Dan Boneh
(parFal)
Defenses
Protect
heap
funcFon
pointers
(e.g.
PointGuard)
Be5er
browser
architecture:
non-writable
pages
Nozzle
[RLZ08]
:
detect
sprays
by
prevalence
of
code
on
heap
Dan
Boneh
End of Segment
Dan Boneh