-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocbuster.py
More file actions
executable file
·182 lines (155 loc) · 5.22 KB
/
procbuster.py
File metadata and controls
executable file
·182 lines (155 loc) · 5.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
import argparse
import shlex
import subprocess
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
DEFAULT_MAX_PID = 65535
DEFAULT_WORKERS = 50
DEFAULT_TIMEOUT = 5 # seconds per subprocess call
COL_PID = 7
COL_USER = 20
def make_cmd_reader(cmd: str, timeout: int):
"""
Factory: returns a reader that invokes an external command for every path.
The command receives the remote path as its sole argument.
$ ./exploit.sh /proc/1/status
"""
def read_via_cmd(path: str, _timeout: int) -> str | None:
try:
parts = shlex.split(cmd) + [path]
result = subprocess.run(
parts,
capture_output=True,
timeout=timeout,
)
# Empty output (for any reason) → file missing / not found.
# Non-empty → return content as-is.
if not result.stdout.strip():
return None
return result.stdout.decode("utf-8", errors="replace")
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
return None
return read_via_cmd
def fetch_uid_map(reader, timeout: int) -> dict[str, str]:
text = reader("/etc/passwd", timeout)
if not text:
return {}
uid_map: dict[str, str] = {}
for line in text.splitlines():
# username:password:uid:gid:gecos:home:shell
parts = line.split(":")
if len(parts) >= 3:
uid_map[parts[2].strip()] = parts[0].strip()
return uid_map
def parse_cmdline(text: str) -> str:
cmd = text.replace("\x00", " ").replace("\n", " ").strip()
return cmd if cmd else ""
def probe_pid(pid: int, reader, timeout: int, uid_map: dict[str, str]) -> dict | None:
cmdline_text = reader(f"/proc/{pid}/cmdline", timeout)
if not cmdline_text:
return None
cmd = parse_cmdline(cmdline_text)
# Try status for Name + UID; if unavailable user stays "?"
user = "?"
status_text = reader(f"/proc/{pid}/status", timeout)
if status_text:
fields = {}
for line in status_text.splitlines():
if ":" in line:
key, _, val = line.partition(":")
fields[key.strip()] = val.strip()
name = fields.get("Name", "")
uid = fields.get("Uid", "").split()
user = uid_map.get(uid[0], uid[0]) if uid else "?"
cmd = cmd or f"[{name}]"
else:
cmd = cmd or "[unknown]"
return {"pid": pid, "user": user, "cmd": cmd}
def banner() -> None:
print(
f"{'PID':<{COL_PID}} {'USER':<{COL_USER}} CMD",
flush=True,
)
def print_entry(entry: dict) -> None:
print(
f"{entry['pid']:<{COL_PID}} "
f"{entry['user']:<{COL_USER}} "
f"{entry['cmd']}",
flush=True,
)
def check_reader(reader, timeout: int) -> None:
result = reader("/proc/self/cmdline", timeout)
if not result:
print(
"[!] file-read-cmd check failed: "
"no output received for /proc/self/cmdline.\n"
" Verify the command works and the target is reachable.",
file=sys.stderr,
)
sys.exit(1)
cmd = result.replace("\x00", " ").strip()
print(f"[+] file-read-cmd OK (self: {cmd})", flush=True)
def run(args: argparse.Namespace) -> None:
reader = make_cmd_reader(args.file_read_cmd, args.timeout)
check_reader(reader, args.timeout)
uid_map = fetch_uid_map(reader, args.timeout)
banner()
with ThreadPoolExecutor(max_workers=args.workers) as pool:
futures = {
pool.submit(probe_pid, pid, reader, args.timeout, uid_map): pid
for pid in range(1, args.max_pid + 1)
}
for future in as_completed(futures):
entry = future.result()
if entry:
print_entry(entry)
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description=(
"Lists processes by brute-forcing /proc PIDs and reading "
"status and cmdline."
),
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument(
"--file-read-cmd",
metavar="CMD",
required=True,
help=(
"Command used to read remote files. "
"The target path is appended as the last argument. "
"Example: ./exploit.sh"
),
)
parser.add_argument(
"--max-pid",
metavar="MAX_PID",
type=int,
default=DEFAULT_MAX_PID,
help=f"Maximum PID to check (default: {DEFAULT_MAX_PID})",
)
parser.add_argument(
"--workers",
metavar="N",
type=int,
default=DEFAULT_WORKERS,
help=f"Concurrent worker threads (default: {DEFAULT_WORKERS})",
)
parser.add_argument(
"--timeout",
metavar="S",
type=int,
default=DEFAULT_TIMEOUT,
help=f"Per-request timeout in seconds (default: {DEFAULT_TIMEOUT})",
)
return parser
if __name__ == "__main__":
parser = build_parser()
args = parser.parse_args()
try:
run(args)
except KeyboardInterrupt:
print("\n[!] Interrupted.", file=sys.stderr)
sys.exit(1)