51 lines
2.3 KiB
Python
51 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Reproduce static analysis of the original Windows SpeedTreeRT DLL."""
|
|
import argparse
|
|
import hashlib
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
|
|
def main():
|
|
root = Path(__file__).resolve().parents[2]
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--ghidra-home', type=Path, required=True)
|
|
parser.add_argument('--java-bin', type=Path, help='Directory containing a compatible java executable')
|
|
parser.add_argument('--dll', type=Path, default=root.parent / 'AVA2/1. Client DEV/SpeedTreeRT.dll')
|
|
parser.add_argument('--output', type=Path, default=root / 'build/verification/speedtree-decompile')
|
|
parser.add_argument('--objdump', default='objdump')
|
|
args = parser.parse_args()
|
|
dll, out = args.dll.resolve(), args.output.resolve()
|
|
if not dll.is_file():
|
|
parser.error(f'DLL missing: {dll}')
|
|
headless = args.ghidra_home.resolve() / 'support/analyzeHeadless'
|
|
if not headless.is_file():
|
|
parser.error(f'Ghidra headless launcher missing: {headless}')
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
projects = out / 'ghidra-project'
|
|
projects.mkdir(exist_ok=True)
|
|
(out / 'dll-sha256.txt').write_text(hashlib.sha256(dll.read_bytes()).hexdigest() + ' ' + str(dll) + '\n')
|
|
for filename, options in [('pe-headers.txt', ['-p']), ('disassembly.txt', ['-d', '--demangle'])]:
|
|
with (out / filename).open('w') as f:
|
|
subprocess.run([args.objdump, *options, str(dll)], stdout=f, check=True)
|
|
env = os.environ.copy()
|
|
if args.java_bin:
|
|
env['PATH'] = str(args.java_bin.resolve()) + os.pathsep + env.get('PATH', '')
|
|
command = [str(headless), str(projects), 'SpeedTreeRT', '-import', str(dll), '-overwrite',
|
|
'-analysisTimeoutPerFile', '600', '-max-cpu', '4',
|
|
'-scriptPath', str(Path(__file__).resolve().parent),
|
|
'-postScript', 'ExportSpeedTree.java', str(out), '-log', str(out / 'analysis.log')]
|
|
# Avoid treating stale outputs from an earlier run as a successful export.
|
|
summary = out / 'summary.txt'
|
|
if summary.exists():
|
|
summary.unlink()
|
|
subprocess.run(command, env=env, check=True)
|
|
if not summary.is_file():
|
|
raise RuntimeError('Ghidra did not complete the export; inspect analysis.log')
|
|
print(summary.read_text())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|