Repository navigation
Expand file tree
/
Copy pathrecon.py
More file actions
402 lines (341 loc) · 13.3 KB
/
Copy pathrecon.py
File metadata and controls
402 lines (341 loc) · 13.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python3
"""
recon.py — php2shell target fingerprinting (phase 0)
Pure HTTP probes. No exploitation, no file writes on target.
Detects: PHP version, OS, SAPI (mod_php/fpm), MPM (prefork/worker/event),
session.upload_progress status, and whether target is in vulnerable range.
Usage standalone:
python3 recon.py http://target:port/index.php
Usage from exploit.py:
from recon import fingerprint
info = fingerprint("127.0.0.1", 8088, "/index.php")
if not info["vulnerable"]:
sys.exit("target not vulnerable")
"""
from __future__ import annotations
import http.client
import re
import socket
import ssl
import sys
import urllib.parse
from dataclasses import dataclass, field
PATCHED_VERSIONS = {
(8, 2): (8, 2, 35),
(8, 3): (8, 3, 36),
(8, 4): (8, 4, 27),
(8, 5): (8, 5, 12),
}
BOUNDARY = "----recon0xdeadbeef"
@dataclass
class TargetInfo:
url: str = ""
reachable: bool = False
php_version: str = ""
php_version_tuple: tuple = ()
os_hint: str = ""
server: str = ""
sapi: str = ""
mpm: str = ""
upload_progress: str = ""
vulnerable: bool = False
vulns: list = field(default_factory=list)
notes: list = field(default_factory=list)
raw_headers: dict = field(default_factory=dict)
def summary(self) -> str:
lines = []
lines.append(f"Target : {self.url}")
lines.append(f"Reachable : {'yes' if self.reachable else 'NO'}")
if not self.reachable:
return "\n".join(lines)
lines.append(f"PHP Version : {self.php_version or 'hidden'}")
lines.append(f"OS Hint : {self.os_hint or 'unknown'}")
lines.append(f"Server : {self.server or 'hidden'}")
lines.append(f"SAPI : {self.sapi or 'unknown'}")
lines.append(f"MPM : {self.mpm or 'unknown'}")
lines.append(f"UploadProg : {self.upload_progress or 'unknown'}")
lines.append(f"Vulnerable : {'YES' if self.vulnerable else 'no'}")
if self.vulns:
for v in self.vulns:
lines.append(f" [!] {v}")
if self.notes:
for n in self.notes:
lines.append(f" [i] {n}")
return "\n".join(lines)
def _conn(host: str, port: int, use_tls: bool, timeout: float = 10):
if use_tls:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
return http.client.HTTPSConnection(host, port, timeout=timeout, context=ctx)
return http.client.HTTPConnection(host, port, timeout=timeout)
def _get(host: str, port: int, path: str, tls: bool, timeout: float = 10,
extra_headers: dict | None = None) -> tuple[int, dict, bytes]:
conn = _conn(host, port, tls, timeout)
hdrs = {"Host": host, "Connection": "close", "User-Agent": "Mozilla/5.0"}
if extra_headers:
hdrs.update(extra_headers)
try:
conn.request("GET", path, headers=hdrs)
r = conn.getresponse()
body = r.read(1 << 20)
headers = {k.lower(): v for k, v in r.getheaders()}
return r.status, headers, body
except Exception:
return 0, {}, b""
finally:
conn.close()
def _post_multipart(host: str, port: int, path: str, tls: bool,
fields: list[tuple[str, str, bytes | None]],
timeout: float = 10) -> tuple[int, dict, bytes]:
parts = []
for name, value, filename_data in fields:
if filename_data is not None:
parts.append(
f"--{BOUNDARY}\r\nContent-Disposition: form-data; "
f'name="{name}"; filename="probe.txt"\r\n'
f"Content-Type: application/octet-stream\r\n\r\n".encode()
+ filename_data + b"\r\n"
)
else:
parts.append(
f"--{BOUNDARY}\r\nContent-Disposition: form-data; "
f'name="{name}"\r\n\r\n{value}\r\n'.encode()
)
body = b"".join(parts) + f"--{BOUNDARY}--\r\n".encode()
conn = _conn(host, port, tls, timeout)
try:
conn.request("POST", path, body=body, headers={
"Host": host,
"Connection": "close",
"Content-Type": f"multipart/form-data; boundary={BOUNDARY}",
"Content-Length": str(len(body)),
"User-Agent": "Mozilla/5.0",
})
r = conn.getresponse()
resp_body = r.read(1 << 20)
headers = {k.lower(): v for k, v in r.getheaders()}
return r.status, headers, resp_body
except Exception:
return 0, {}, b""
finally:
conn.close()
def _parse_php_version(s: str) -> tuple:
m = re.search(r"(\d+)\.(\d+)\.(\d+)", s)
if m:
return tuple(int(x) for x in m.groups())
return ()
def _detect_os(server: str, headers: dict, body: bytes) -> str:
combined = (server + " " + str(headers) + " " + body.decode("latin1", "replace")).lower()
if "debian" in combined or "deb" in combined:
return "Debian/Ubuntu"
if "ubuntu" in combined:
return "Ubuntu"
if "centos" in combined or "el7" in combined or "el8" in combined or "el9" in combined:
return "CentOS/RHEL"
if "red hat" in combined or "rhel" in combined:
return "RHEL"
if "alpine" in combined:
return "Alpine"
if "win" in server.lower() or "iis" in server.lower():
return "Windows"
if "unix" in combined or "linux" in combined:
return "Linux"
return ""
def _detect_sapi(server: str, headers: dict) -> str:
s = server.lower()
if "php/" in s:
return "mod_php"
via = headers.get("via", "").lower()
if "fastcgi" in via or "fpm" in via:
return "php-fpm"
xpb = headers.get("x-powered-by", "").lower()
if "php-fpm" in xpb:
return "php-fpm"
if "php/" in xpb and ("apache" in s or "nginx" in s):
if "apache" in s:
return "mod_php (probable)"
return "php-fpm (probable)"
return ""
def _detect_mpm(host: str, port: int, path: str, tls: bool) -> str:
try:
conns = []
for _ in range(4):
c = _conn(host, port, tls, timeout=5)
c.request("GET", path, headers={
"Host": host, "Connection": "keep-alive",
"User-Agent": "Mozilla/5.0",
})
conns.append(c)
pids = set()
for c in conns:
try:
r = c.getresponse()
r.read()
pid_hdr = {k.lower(): v for k, v in r.getheaders()}.get("x-process-id", "")
if pid_hdr:
pids.add(pid_hdr)
except Exception:
pass
finally:
c.close()
if len(pids) > 1:
return "prefork (multiple PIDs)"
except Exception:
pass
# heuristic: server header sometimes reveals MPM
status, headers, _ = _get(host, port, path, tls)
server = headers.get("server", "")
if "prefork" in server.lower():
return "prefork"
if "event" in server.lower():
return "event"
if "worker" in server.lower():
return "worker"
# timing heuristic: send slow requests in parallel
# prefork with MaxRequestWorkers=1 will serialize them
try:
import time
t0 = time.monotonic()
socks = []
for _ in range(3):
s = socket.create_connection((host, port), timeout=5)
req = f"GET {path} HTTP/1.0\r\nHost: {host}\r\n\r\n".encode()
s.sendall(req)
socks.append(s)
for s in socks:
s.recv(4096)
s.close()
elapsed = time.monotonic() - t0
if elapsed > 2.0:
return "prefork (single-worker behavior)"
except Exception:
pass
return ""
def _probe_upload_progress(host: str, port: int, path: str, tls: bool) -> str:
status, headers, body = _post_multipart(host, port, path, tls, [
("PHP_SESSION_UPLOAD_PROGRESS", "probe_recon", None),
("upload", "", b"X" * 256),
])
if status == 0:
return "unreachable"
cookies = headers.get("set-cookie", "")
if "PHPSESSID" in cookies:
return "ACTIVE (session cookie set on upload progress probe)"
# try with explicit session id
status2, headers2, body2 = _post_multipart(host, port, path, tls, [
("PHPSESSID", "reconprobe000000000000000000000000", None),
("PHP_SESSION_UPLOAD_PROGRESS", "probe_recon", None),
("upload", "", b"X" * 256),
])
cookies2 = headers2.get("set-cookie", "")
if "PHPSESSID" in cookies2:
return "ACTIVE (session started via form field + progress)"
return "inactive or hidden"
def fingerprint(host: str, port: int, path: str = "/",
tls: bool = False, timeout: float = 10) -> TargetInfo:
info = TargetInfo(url=f"{'https' if tls else 'http'}://{host}:{port}{path}")
# basic reachability + header grab
status, headers, body = _get(host, port, path, tls, timeout)
if status == 0:
return info
info.reachable = True
info.raw_headers = headers
# PHP version
xpb = headers.get("x-powered-by", "")
server = headers.get("server", "")
info.server = server
php_str = ""
if "PHP/" in xpb:
php_str = xpb
elif "PHP/" in server:
php_str = server
if php_str:
info.php_version = re.search(r"PHP/[\d.]+", php_str).group() if re.search(r"PHP/[\d.]+", php_str) else php_str
info.php_version_tuple = _parse_php_version(php_str)
else:
info.notes.append("PHP version hidden (X-Powered-By/Server stripped)")
# try triggering error
st2, hd2, bd2 = _get(host, port, path + "?=" + "A" * 2000, tls, timeout)
for src in [hd2.get("x-powered-by", ""), bd2.decode("latin1", "replace")]:
if "PHP/" in src:
info.php_version = re.search(r"PHP/[\d.]+", src).group()
info.php_version_tuple = _parse_php_version(src)
break
# OS detection
info.os_hint = _detect_os(server, headers, body)
# SAPI detection
info.sapi = _detect_sapi(server, headers)
# MPM detection (only if Apache)
if "apache" in server.lower() or "mod_php" in info.sapi:
info.mpm = _detect_mpm(host, port, path, tls)
# upload_progress probe
info.upload_progress = _probe_upload_progress(host, port, path, tls)
# vulnerability assessment
if info.php_version_tuple:
major_minor = info.php_version_tuple[:2]
patch_ver = PATCHED_VERSIONS.get(major_minor)
if major_minor < (8, 2):
info.vulnerable = True
info.vulns.append(f"PHP {info.php_version} — EOL, no patch available")
elif patch_ver and info.php_version_tuple < patch_ver:
info.vulnerable = True
pv = ".".join(str(x) for x in patch_ver)
info.vulns.append(f"PHP {info.php_version} < {pv} — CVE-2026-109618/619 affected")
elif patch_ver and info.php_version_tuple >= patch_ver:
info.notes.append(f"PHP {info.php_version} — patched")
else:
info.notes.append(f"PHP {info.php_version} — unknown patch status")
if "ACTIVE" in info.upload_progress:
info.vulns.append("session.upload_progress.enabled = On (attack vector active)")
elif info.upload_progress == "inactive or hidden":
info.notes.append("upload_progress appears disabled or not triggerable")
if "mod_php" in info.sapi:
info.vulns.append(f"SAPI: {info.sapi} — direct exploit path (no FPM boundary)")
elif "fpm" in info.sapi.lower():
info.notes.append(f"SAPI: {info.sapi} — FPM makes exploitation harder but not impossible")
if "prefork" in info.mpm.lower():
info.vulns.append(f"MPM: {info.mpm} — single-process model, heap state predictable")
if not info.vulnerable and not info.php_version_tuple:
info.notes.append("Cannot confirm version — manual check recommended")
return info
BANNER = r"""
php2shell recon — CVE-2026-109618/619
Author: agrresore (https://github.com/agrresore)
"""
def main() -> int:
print(BANNER)
if len(sys.argv) < 2:
print("usage: python3 recon.py <url> [<url> ...]")
print(" e.g. python3 recon.py http://target:8088/index.php")
return 1
for url in sys.argv[1:]:
parsed = urllib.parse.urlparse(url)
host = parsed.hostname or "127.0.0.1"
tls = parsed.scheme == "https"
port = parsed.port or (443 if tls else 80)
path = parsed.path or "/"
print(f"\n{'='*60}")
info = fingerprint(host, port, path, tls)
print(info.summary())
if info.vulnerable:
score = 0
if info.php_version_tuple:
score += 3
if "ACTIVE" in info.upload_progress:
score += 3
if "mod_php" in info.sapi:
score += 2
if "prefork" in info.mpm.lower():
score += 2
print(f"\n Risk Score : {score}/10")
if score >= 8:
print(" Assessment : CRITICAL — all conditions met for CVE-2026-109618/619")
elif score >= 5:
print(" Assessment : HIGH — likely exploitable, some conditions unconfirmed")
else:
print(" Assessment : MEDIUM — version vulnerable but attack surface unclear")
print(f"{'='*60}")
return 0
if __name__ == "__main__":
sys.exit(main())