An Introduction To The Eturn Riented Rogramming
An Introduction To The Eturn Riented Rogramming
• Pros
▫ Does not need executable stack
▫ Also pretty easy to understand and implement
• Cons
▫ Relies on access to library functions
▫ Can only execute sequential instructions, no
branching or fancy stuff
▫ Can only use code in .text and loaded libraries
Mi&ga&on
against
these
classical
aLacks
●
When Good Instructions Go Bad: Generalizing
Return-Oriented Programming to RISC [1] -
Buchanan, E.; Roemer, R.; Shacham, H.; Savage, S. (October 2008)
●
Return-Oriented Programming: Exploits Without
Code Injection [2] - Shacham, Hovav; Buchanan, Erik; Roemer,
Ryan; Savage, Stefan. Retrieved 2009-08-12.
ROP
defini&on
• Chain
gadgets
to
execute
malicious
code.
• A
gadget
is
a
suite
of
instruc&ons
which
end
by
the
branch
instruc&on
ret
(Intel)
or
the
equivalent
on
ARM.
●
Because x86 instructions aren't aligned, a gadget can
contain another gadget.
f7c7070000000f9545c3 → test edi, 0x7 ; setnz byte ptr [rbp-0x3d] ; c7070000000f9545c3 → mov
dword ptr [rdi], 0xf000000 ; xchg ebp, eax ; ret
●
Doesn't work on RISC architectures like ARM, MIPS,
SPARC...
Why
use
the
ROP?
● Gadgets are mainly located on segments
without ASLR and on pages marked as
executables
–
It can bypass the ASLR
– It can bypass the NX bit
Road-‐map
a ttack
●
Find the needed Gadgets
●
S tore your gadge ts a ddres s e s on the s ta ck
– You must to overwrite the saved eip with the
address of your first gadget
CALL
and
RET
seman&cs
(Intel
x86)
● CALL semantic
ESP ← ESP – 4
[ESP] ← NEXT(EIP) ; sEIP
EIP ← OPERANDE
● RET semantic
TMP ← [ESP] ; get the sEIP
ESP ← ESP + 4 ; Align stack pointer
EIP ← TMP ; restore the sEIP
ALack
proce ss
o n
x86
●
Gadget1 is executed and returns
●
Gadget2 is executed and returns
●
Gadget3 is executed and returns
●
And so on until all instructions
that you want are executed
●
So, the real execution is:
ALack
process
on
ARM
●
This is exactly the same process but this time using this
kind of gadgets:
pop {r3, pc}
mov r0, r3 ; pop {r4, r5, r6, pc}
pop {r3, r4, r5, r6, r7, pc}
●
On ARM it's possible to pop a value directly in the
program counter register (pc)
How
can
we
find
gadgets?
●
Several ways to find gadgets
Old school method : objdump and grep
• Some
gadgets
will
be
not
found:
Objdump aligns
instruc&ons
●
Step
1
-‐
Write-‐what-‐where
gadgets
–
Write
“/bin/sh”
in
memory
●
Step
2
-‐
Init
syscall
number
gadgets
–
Setup
execve
syscall
number
●
Step
3
-‐
Init
syscall
arguments
gadgets
–
Setup
execve
arguments
●
Step
4
-‐
Syscall
gadgets
–
Find
syscall
interrupt
●
Step
5
-‐
Build
the
ROP
chain
–
Build
the
python
payload
Step
1
Write-‐what-‐where
g adgets
Step
2
Init
syscall
number
gadgets
Step 3
Init
syscall
arguments
gadgets
S tep 4
S yscall gadget
Step
5
-‐
Build
the
ROP
chain
Mi&ga&on
against
the
ROP
aLack
●
Linux - Position-Independent Executable
– Applies the ASLR on the section .text
●
Can be bypassed on old specific 32bits-based Linux
distribution
– PIC (Position-Independent Code) is used for library
when a binary is compiled with PIE
●
On Windows, ASLR can include the section
.text
ASLR
–
Entropy
not
enough
on
certain
old
distribu&on
●
Tested on a ArchLinux 32 bits in 2011
– NX enable
–
ASLR enable
–
PIE enable
–
RELRO full
●
If you don't have enough gadgets :
– Choose yours in the libc Brute-force the
– base address
PIC/PIE
–
Entropy
not
enough
on
certain
old
distribu&on
●
Brute-force the base address
base_addr = 0xb770a000
p = "a" * 44
# execve /bin/sh generated by RopGadget p v3.3
p += pack("<I", base_addr + 0x000e07c1) # pop %edx | pop %ecx | pop %ebx | ret
p += pack("<I", 0x42424242) # padding
p += pack("<I", base_addr + 0x00178020) # @ .data
p += pack("<I", 0x42424242) # padding
p += pack("<I", base_addr + 0x00025baf) # pop %eax | ret
+= "/bin"
[...]
PIC/PIE
–
E ntropy
n ot
e nough
on
c ertain
old
d istribution
●
Wait for a few s e conds
$while true ; do ./main "$(./exploit.py)" ; done
Segmentation fau l t
Segmentation fau l t
Segmentation fau l t
Segmentation fau l t
Segmentation fault
[...]
Segmentation fau l t
Segmentation fau l t
Segmentation fau l t
Segmentation fau l t
Segmentation fau l t
Segmentation fau l t
Segmentation fault
sh$
ROP
variants
●
BROP deals with the ROP and “timing attack”
● Constraints:
–
The vulnerability must be a stack buffer overflow The
–
target binary (server) must restart after the crash
● Scan the memory byte-by-byte to find potential gadgets
– Try to execute the _write_ function/syscall to leak more
gadget from the .text section
Signal
Return
Oriented
Programming
●
Uses the SIGRETURN Linux signal to load
values from the stack to the registers
– Store the values on the stack then raise the
SIGRETURN syscall
●
Your registers will be initialized with the stack values
Open
Problems
&
Challenges
●
ROP chain mitigation
– Heuristic ROP detection
●
ROP chain generation via theorem solver
– Use a SAT/SMT solver to build a ROP chain
●
Gadgets finding via instruction semantics
– Looking for gadgets based on their semantics
●
LOAD/STORE, GET/PUT
Conclusion
[4]
h ttp://ropshell.com/ropem e/
[5]
h ttps://github.com/pakt/ropc
[6]
h ttps://github.com/a wailly/nrop
[7]
h ttp://shell-‐storm.org/project/ROPgadget/
[8]
h ttps://www.comp.nus.edu.sg/~liangzk/papers/asiaccs11.pdf
[9]
h ttps://www.lst.inf.ethz.ch/research/publica&ons/PPREW _2013/PPREW _2013.pdf
[10]
h ttp://www.scs.stanford.e du/brop/bittau-‐brop.pdf
[11]
https://labs.portcullis.co.uk/blog/ohm-‐2013-‐review-‐of-‐returning-‐signals-‐for-‐fun-‐and-‐profit/
[12]
h ttp://shell-‐storm.org/repo/Notepad/ROP-‐c hain-‐genera&on-‐via-‐backtracking-‐and-‐state-‐machine.txt
Acknowledgements
These
slides
contain
material
by
Jonathan
Salwan
and
ScoL
Hand