Read-only. Finds the two validators with the published PBZModLoader patterns, follows them to the lookup and prints the ranges and digests that build_profile.hpp pins. Not part of the player or source package.
88 lines
4.0 KiB
Python
88 lines
4.0 KiB
Python
"""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, <n>` 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 <PBZ.exe> [<normalize rva> <free rva>]
|
|
"""
|
|
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("<III", pe.get_data(d.VirtualAddress, d.Size)))
|
|
|
|
def frange(rva):
|
|
for s, e in pdata:
|
|
if s <= rva < e:
|
|
return s, e
|
|
raise ValueError(f"no .pdata range for 0x{rva:x}")
|
|
|
|
hits = {"bytes": [], "text": []}
|
|
for sec in pe.sections:
|
|
if not sec.Characteristics & 0x20000000:
|
|
continue
|
|
for name, pat, entry in (("bytes", BYTES, b"\x40\x53\x48\x83\xec"), ("text", TEXT, b"\x48\x89\x5c\x24\x18")):
|
|
for o in find(data, sec.PointerToRawData, sec.SizeOfRawData, pat):
|
|
if data[o - len(entry):o] == entry:
|
|
hits[name].append(pe.get_rva_from_offset(o - len(entry)))
|
|
print("bytes validator", [hex(h) for h in hits["bytes"]])
|
|
print("text validator", [hex(h) for h in hits["text"]])
|
|
if len(hits["bytes"]) != 1 or len(hits["text"]) != 1:
|
|
sys.exit("validators missing or ambiguous")
|
|
b, t = hits["bytes"][0], hits["text"][0]
|
|
|
|
def rel(rva, disp, length):
|
|
return rva + length + struct.unpack("<i", pe.get_data(rva + disp, 4))[0]
|
|
|
|
lookup = rel(b + 9, 1, 5)
|
|
print(f"lookup from bytes 0x{lookup:x}, from text 0x{rel(t + 44, 1, 5):x}")
|
|
md = Cs(CS_ARCH_X86, CS_MODE_64)
|
|
s, e = frange(lookup)
|
|
print(f"\nLOOKUP 0x{s:x}..0x{e:x}")
|
|
for ins in md.disasm(pe.get_data(s, e - s), base + s):
|
|
print(f" {ins.address - base:08x} {ins.bytes.hex():24} {ins.mnemonic:8} {ins.op_str}")
|
|
print("\nPINS (rva, size, sha256 of the .pdata range):")
|
|
for label, rva in (("lookup", lookup), ("bytes", b), ("text", t)):
|
|
s, e = frange(rva)
|
|
print(f" {label:8} start=0x{rva:x} range=0x{s:x}+0x{e - s:x} sha256={hashlib.sha256(pe.get_data(s, e - s)).hexdigest()}")
|
|
for extra in sys.argv[2:]:
|
|
rva = int(extra, 0)
|
|
s, e = frange(rva)
|
|
print(f" extra start=0x{rva:x} range=0x{s:x}+0x{e - s:x} sha256={hashlib.sha256(pe.get_data(s, e - s)).hexdigest()}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|