Files
Neutron/tools/binary/default_raw_carrier.py
T

60 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Map verified original sections into a static Ghidra carrier at DEFAULT.
This is analysis input, not a runnable executable or a loaded-process image.
It preserves initialized section bytes and zero-fills virtual gaps/tails.
"""
import argparse
import hashlib
import json
from pathlib import Path
from checkpoint_script_state import EXE_SHA256, find_case
from pe_inspect import PE
START = 0x447CF0
def generate(original, output):
original, output = Path(original).resolve(), Path(output).resolve()
if output.is_relative_to(original) or original.is_relative_to(output):
raise ValueError('Output must be separate from originals')
pe = PE(find_case(original, 'Neutron.exe'))
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
raise ValueError('Unexpected original executable')
end = max(pe.base+s['rva']+max(s['virtual_size'], s['raw_size'])
for s in pe.sections)
if not START < end <= START+16*1024*1024:
raise ValueError('Unexpected source image extent')
carrier = bytearray(end-START)
ranges = []
for section in pe.sections:
a = max(START, pe.base+section['rva'])
b = min(end, pe.base+section['rva']+section['raw_size'])
if a >= b:
continue
offset = pe.offset(a-pe.base)
source = pe.data[offset:offset+b-a]
if len(source) != b-a:
raise ValueError('Truncated source section')
carrier[a-START:b-START] = source
ranges.append(dict(source_offset=offset, va=a, length=b-a,
carrier_offset=a-START))
result = dict(source_sha256=EXE_SHA256,
carrier_sha256=hashlib.sha256(carrier).hexdigest(),
start=hex(START), end=hex(end), ranges=ranges,
scope='Static raw analysis carrier with original byte mappings '
'and zero virtual tails, not an executable or runtime capture.')
output.mkdir(parents=True, exist_ok=True)
(output/'default.bin').write_bytes(carrier)
(output/'mapping.json').write_text(json.dumps(result, indent=2)+'\n')
return result
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('original', type=Path)
parser.add_argument('--output', required=True, type=Path)
args = parser.parse_args()
print(json.dumps(generate(args.original, args.output), indent=2))