Yuawn Binary exploitation WriteUp

ROP
Return Oriented Programming
Fromyuawn
NTU-Computer-Security
- week1
- week2
- week3
- PicoCTF.com
- pwn.college
- Pwnable.tw
pwntools interact with gdb
- tmux
pause()
, gdbattach pid
ROP Gadgets
Tools
base on capstone
ROP
For NX
- 在 code segment 尋找 gadgets 叠成一串 return address chain (ROP chain)
- 透過gadgets串出代碼執行,繞過 NX
- 首先,將函數返回地址彈出,將
rsp
指向第二個 Return address 並跳轉至執行第一個 Return address。接著執行 ret instruction,並在一開始放置好第二個 return address,從而實現持續 control flow。通過反覆執行此操作,達到 Return Oriented Programming attack 的目的。
Control register
Gadget - pop <reg>; ret;
Gadget 來源
不只有完整instruction可以形成Gadget,只要跳的位置合適,可能連value也可以被解析成可執行的Gadget
- 用ROP chain出 syscall
ox601000
input in rdi'/bin/sh\0'
input in rsimov qword ptr [rdi], rsi
mov rsi to rdi pointer memory space
…
總結
- Overflow control rip 後
- NX Off 的前提下可撰寫shellcode
execve("/bin/sh",0,0)
直接執行 - NX On 時透過 ROP 推疊出執行
execve("/bin/sh",0,0)
行為的 ROP chain
1 | attack = !NX ? shellcode : ROP |
DEMO
rop.c
1 |
|
Compile
gcc src/rop.c -o ./rop/share/rop -no-pie -fno-stack-protector --static
--static
: 方便練習
解法
ROPgadgget --binary ./rop
,找出你要的 gadgetgdb vmmap
找可寫位置pop rax;0x3b;syscall;
:sys_execve(%rdi,%rsi,%rdx)
- %rdx : 0
- %rdi :
bss
, pointer to memory space - %rsi :
mov [rdi], rsi
; “/bin/sh\0”, 0
1 | #!/usr/bin/env python |
ret2plt
Return to .plt
找到plt function 的位置就可以直接call
- 可以直接取代rop chain(👆) 的一長串
- system
DEMO
1 |
|
Sol
1 | #!/usr/bin/env python |
Conculsion
In the condition that without pie , cross ASLR to use lib function.
ret2libc
Bypass ASLR - 能leak就結束了
- Function .got table -> lib address
leaked_address - offset = base_address
Demo
- 再一次的連線中,需要同時leak和expolit
objdump -R <binary>
: 找 libc_start_mainreadelf -s <binary>
: 找 offset- leak 完成,跳回main再出發 overflow
- 可以從binary中搜尋
"/bin/sh"
:print '"/bin/sh" str :' , hex( l.search( '/bin/sh' ).next() )
- 可能會crash,因為需要8bytes對齊:再加上ret就可解決
“/bin/sh” 的寫入
- 使用ret2plt call
gets()
寫入 - pwntools lib.search
information leaking
- array 寫出一個區域並不會把內容清掉
- 使用leak_address和offset就可以知道 base_address
stack pivoting
stack migartion
- leave ; ret
- Overflow 時將 rbp 填成 ROP Chain 的
address -8
- return address th leave; ret gadget
- pop rsp; ret
- 手動找針對各種當下情況的 gadget
- Overflow 時將 rbp 填成 ROP Chain 的
- Title: Yuawn Binary exploitation WriteUp
- Author: Chihhh Linnn
- Created at : 2024-08-05 13:50:20
- Updated at : 2024-08-05 13:50:20
- Link: https://chihhhs.github.io/2024/08/05/pwn-2/
- License: This work is licensed under CC BY-NC-SA 4.0.