# -*- coding: utf-8 -*- # @category Decompilation # @runtime Jython from ghidra.app.decompiler import DecompInterface from ghidra.util.task import ConsoleTaskMonitor import codecs def safe_str(x): try: return str(x) except: return repr(x) def run(): args = getScriptArgs() out_path = "/data/libmsaoaidsec_pseudocode.cpp" if args and len(args) > 0: out_path = safe_str(args[0]) fm = currentProgram.getFunctionManager() funcs = list(fm.getFunctions(True)) decomp = DecompInterface() decomp.openProgram(currentProgram) print("[*] Functions found: " + str(len(funcs))) print("[*] Output: " + out_path) # IMPORTANT: force ASCII-safe writing f = codecs.open(out_path, "w", "utf-8", "ignore") f.write("// AUTO DECOMPILATION OUTPUT\n") f.write("// PROGRAM: " + safe_str(currentProgram.getName()) + "\n\n") i = 0 for func in funcs: i += 1 name = safe_str(func.getName()) print("[" + str(i) + "/" + str(len(funcs)) + "] " + name) try: res = decomp.decompileFunction(func, 60, ConsoleTaskMonitor()) if res and res.decompileCompleted(): code = res.getDecompiledFunction().getC() f.write("\n// =====================\n") f.write("// FUNCTION: " + name + "\n") f.write("// =====================\n\n") f.write(code) f.write("\n\n") else: f.write("\n// FAILED: " + name + "\n\n") except Exception as e: print("[!] Error in " + name + " -> " + str(e)) f.write("\n// ERROR FUNCTION: " + name + "\n\n") f.close() print("[+] DONE") if __name__ == "__main__": run()