def main(): parser = argparse.ArgumentParser(description="Decompile DSO files.") parser.add_argument("file", metavar='file', nargs="+", help="The DSO file to decompile.") parser.add_argument("--stdout", action="store_true", help="Dump the decompiled script to stdout.") args = parser.parse_args() for f in args.file: # Verify that the file exists. if not os.path.exists(f): print("{!] Error: could not find %s" % f, file=sys.stderr) continue # Set the output filename if args.stdout: out = sys.stdout else: if f.endswith(".cs.dso"): outfile = f[:-4] # file.cs.dso -> file.cs else: outfile = "%s.cs" % f # file -> file.cs out = open(outfile, 'w') # Create a backup of the original DSO in case the decompiled one is broken. if not os.path.exists("%s.bak" % f) and not args.stdout: shutil.copy(f, "%s.bak" % f) else: f = "%s.bak" % f # Work on the original DSO instead of possibly decompiling our own file. # Decompile the file dso = DSOFile(sys.argv[1]) try: decompile(dso, sink=out) except Exception: exc_type, exc_value, tb = sys.exc_info() if tb is not None: prev = tb curr = tb.tb_next while curr is not None: prev = curr curr = curr.tb_next if "ip" in prev.tb_frame.f_locals and "offset" in prev.tb_frame.f_locals: break if "ip" in prev.tb_frame.f_locals: ip = prev.tb_frame.f_locals["ip"] opcode = prev.tb_frame.f_locals["opcode"] print( "Error encountered at ip=%d (%s) while decompiling %s." % (ip, opcode, f), file=sys.stderr) out.close() if not args.stdout: os.remove(outfile) raise if not args.stdout: out.close() print("%s successfully decommpiled to %s." % (f, outfile))
def main(): parser = argparse.ArgumentParser(description="Decompile DSO files.") parser.add_argument("file", metavar='file', nargs="+", help="The DSO file to decompile.") parser.add_argument("--stdout", action="store_true", help="Dump the decompiled script to stdout.") args = parser.parse_args() for f in args.file: # Verify that the file exists. if not os.path.exists(f): print("{!] Error: could not find %s" % f, file=sys.stderr) continue # Set the output filename if args.stdout: out = sys.stdout else: if f.endswith(".cs.dso"): outfile = f[:-4] # file.cs.dso -> file.cs else: outfile = "%s.cs" % f # file -> file.cs out = open(outfile, 'w') # Create a backup of the original DSO in case the decompiled one is broken. if not os.path.exists("%s.bak" % f) and not args.stdout: shutil.copy(f, "%s.bak" % f) else: f = "%s.bak" % f # Work on the original DSO instead of possibly decompiling our own file. # Decompile the file dso = DSOFile(sys.argv[1]) try: decompile(dso, sink=out) except Exception: exc_type, exc_value, tb = sys.exc_info() if tb is not None: prev = tb curr = tb.tb_next while curr is not None: prev = curr curr = curr.tb_next if "ip" in prev.tb_frame.f_locals and "offset" in prev.tb_frame.f_locals: break if "ip" in prev.tb_frame.f_locals: ip = prev.tb_frame.f_locals["ip"] opcode = prev.tb_frame.f_locals["opcode"] print("Error encountered at ip=%d (%s) while decompiling %s." % (ip, opcode, f), file=sys.stderr) out.close() if not args.stdout: os.remove(outfile) raise if not args.stdout: out.close() print("%s successfully decommpiled to %s." % (f, outfile))
from __future__ import print_function import ast import sys import decompile with open(sys.argv[1], "r") as f: root = ast.parse(f.read(), sys.argv[1]) for op in sys.argv[2:]: if op == 'constprop': import constprop root = constprop.ConstPropTransformer().visit(root) elif op == 'js': import decompile_js as decompile elif op == 'js2': import decompile_js_v2 as decompile elif op == 'inline': import inlining root = inlining.InlineTransformer().visit(root) print(decompile.decompile(root))
# Grab the code code = r2.cmdj('pcj {0} @ {1}'.format(codeSize,codeAddress)) code_bin = array.array('B',code).tobytes() # Generate the binary to write binOut = MAGICVAL + code_bin # Write it out fName = os.path.join(MODULE_DIR,name + ".pyc") with open(fName,"wb") as f: f.write(binOut) write("[ Done ]\n") # Move to next in array frozenArray += offset*3 decompile.decompile(MODULE_DIR,MODULE_SOURCE_DIR)
def assert_decompiles(self, func, expected): result = decompile(func) expected = textwrap.dedent(expected).strip("\n") assert result == expected