62 lines
2.3 KiB
Python
62 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""PyGhidra pass 5: create + decompile check/orchestrator functions.
|
|
|
|
Reuses /tmp/opencode/ghidra-lab/pyproj/LofiPy.
|
|
Writes /tmp/opencode/ghidra-lab/out/fn_<ADDR>.c.txt
|
|
"""
|
|
import os
|
|
|
|
OUT = "/tmp/opencode/ghidra-lab/out"
|
|
PROJ_LOC = "/tmp/opencode/ghidra-lab/pyproj/LofiPy"
|
|
PROJ_NAME = "LofiPy"
|
|
PROG = "/ais_sect1_addrC7074800.bin"
|
|
FNS = ["C709E7C0", "C7086788", "C708E4A4", "C708E790", "C708E860",
|
|
"C709E888", "C708E468", "C709E990"]
|
|
|
|
|
|
def main():
|
|
import pyghidra
|
|
|
|
pyghidra.start()
|
|
with pyghidra.open_project(PROJ_LOC, PROJ_NAME) as project:
|
|
with pyghidra.program_context(project, PROG) as program:
|
|
from ghidra.program.flatapi import FlatProgramAPI
|
|
from ghidra.app.decompiler.flatapi import FlatDecompilerAPI
|
|
|
|
flat = FlatProgramAPI(program)
|
|
with pyghidra.transaction(program, "mkfn2"):
|
|
for t in FNS:
|
|
a = flat.toAddr("0x" + t)
|
|
try:
|
|
flat.disassemble(a)
|
|
except Exception as e:
|
|
print(t + " dis: " + str(e)[:100], flush=True)
|
|
try:
|
|
if flat.getFunctionAt(a) is None:
|
|
flat.createFunction(a, "fn_" + t)
|
|
print(t + " fn created", flush=True)
|
|
except Exception as e:
|
|
print(t + " mkfn: " + str(e)[:150], flush=True)
|
|
dapi = FlatDecompilerAPI(flat)
|
|
try:
|
|
for t in FNS:
|
|
try:
|
|
fn = flat.getFunctionAt(flat.toAddr("0x" + t))
|
|
if fn is None:
|
|
print(t + " no fn", flush=True)
|
|
continue
|
|
c = dapi.decompile(fn)
|
|
open(os.path.join(OUT, "fn_" + t + ".c.txt"),
|
|
"w").write(c if c else "DECOMPILE_NULL")
|
|
print(t + " %d chars" % (len(c) if c else 0),
|
|
flush=True)
|
|
except Exception as e:
|
|
print(t + " dec: " + str(e)[:200], flush=True)
|
|
finally:
|
|
dapi.dispose()
|
|
print("FN_DONE", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|