#!/usr/bin/env python3 """Sanity-check the v80 captures: byte-counts, equal-dword counts, raw PC counts.""" import struct import os here = os.path.dirname(os.path.abspath(__file__)) canary = open(os.path.join(here, "canary-v80.bin"), "rb").read() ours = open(os.path.join(here, "ours-v80.bin"), "rb").read() print(f"canary len: {len(canary)}") print(f"ours len: {len(ours)}") c_nonzero = sum(1 for b in canary if b != 0) o_nonzero = sum(1 for b in ours if b != 0) print(f"canary non-zero bytes: {c_nonzero} ({c_nonzero/len(canary)*100:.2f}%)") print(f"ours non-zero bytes: {o_nonzero} ({o_nonzero/len(ours)*100:.2f}%)") # Sliding 64KB window: byte-equal pages PG = 65536 c_pgs = sum(1 for i in range(0, len(canary), PG) if any(canary[i:i+PG])) o_pgs = sum(1 for i in range(0, len(ours), PG) if any(ours[i:i+PG])) print(f"canary 64K-pages with any non-zero: {c_pgs}") print(f"ours 64K-pages with any non-zero: {o_pgs}") # Compare per 64K page where canary has data: both, only_c, only_o, neither, equal = 0, 0, 0, 0, 0 for i in range(0, len(canary), PG): cnz = any(canary[i:i+PG]) onz = any(ours[i:i+PG]) if cnz and onz: both += 1 if canary[i:i+PG] == ours[i:i+PG]: equal += 1 elif cnz: only_c += 1 elif onz: only_o += 1 else: neither += 1 print(f"64K-page comparison: both_have_data={both} byte_equal_among_those={equal} canary_only={only_c} ours_only={only_o}") # Count PC-range dwords on each side overall PC_LO, PC_HI = 0x82000000, 0x82A00000 c_pc = sum(1 for i in range(0, len(canary), 4) if PC_LO <= struct.unpack_from(">I", canary, i)[0] < PC_HI) o_pc = sum(1 for i in range(0, len(ours), 4) if PC_LO <= struct.unpack_from(">I", ours, i)[0] < PC_HI) print(f"canary dwords in PC range: {c_pc}") print(f"ours dwords in PC range: {o_pc}")