diff --git a/tools/repin_scan.py b/tools/repin_scan.py new file mode 100644 index 0000000..bb7bd75 --- /dev/null +++ b/tools/repin_scan.py @@ -0,0 +1,87 @@ +"""Read-only: find the pin values of build_profile.hpp in a new PBZ.exe. + +Uses PBZModLoader's validator patterns (its signatures.hpp) to find the byte and text +validators, then follows their calls to the lookup and disassembles it: its first call +is the path normalizer, its last call the free helper, its `lea r13, [rip+...]` the +record table and its `mov edi, ` the record count. Prints the .pdata ranges and the +SHA-256 of their bytes, as build_profile.hpp pins them. + +Usage: python -I tools/repin_scan.py [ ] +""" +import hashlib +import struct +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).parent / 'python')) +import pefile # noqa: E402 +from capstone import Cs, CS_ARCH_X86, CS_MODE_64 # noqa: E402 + +BYTES = [0x20,0x48,0x8b,0xda,0xe8,-1,-1,-1,-1,0x48,0x85,0xc0,0x74,0x22,0x48,0x63,0x53,0x08,0x48,0x3b,0x50,0x08,0x75,0x18,0x48,0x8b,0x0b,0x4c,0x8d,0x40,0x18,0xe8,-1,-1,-1,-1,0x84,0xc0,0x74,0x08,0xb0,0x01,0x48,0x83,0xc4,0x20,0x5b,0xc3,0x32,0xc0,0x48,0x83,0xc4,0x20,0x5b,0xc3] +TEXT = [0x55,0x56,0x57,0x41,0x54,0x41,0x55,0x41,0x56,0x41,0x57,0x48,0x81,0xec,0xf0,0x01,0,0,0x48,0x8b,0x05,-1,-1,-1,-1,0x48,0x33,0xc4,0x48,0x89,0x84,0x24,0xe0,0x01,0,0,0x4c,0x8b,0xf2,0xe8,-1,-1,-1,-1,0x48,0x8b,0xf0,0x48,0x85,0xc0,0x0f,0x84,-1,-1,-1,-1,0x41,0x8b,0x4e,0x08,0x8d,0x59,0xff] + + +def find(data, sec_off, sec_len, pat): + first = bytes([pat[0]]) + hits, at, end = [], sec_off, sec_off + sec_len - len(pat) + 1 + while True: + at = data.find(first, at, end) + if at < 0: + return hits + if all(p < 0 or data[at + j] == p for j, p in enumerate(pat)): + hits.append(at) + at += 1 + + +def main(): + exe = Path(sys.argv[1]) + data = exe.read_bytes() + print("sha256", hashlib.sha256(data).hexdigest()) + pe = pefile.PE(data=data, fast_load=True) + print(f"image_size 0x{pe.OPTIONAL_HEADER.SizeOfImage:x} timestamp 0x{pe.FILE_HEADER.TimeDateStamp:x}") + base = pe.OPTIONAL_HEADER.ImageBase + d = pe.OPTIONAL_HEADER.DATA_DIRECTORY[3] + pdata = sorted((s, e) for s, e, _ in struct.iter_unpack("