def main(argc, argv): # Check arguments if argc != 5: fatal( 'Usage: %s <symbol table> <symbol base> <new symbol base> <output linker script>' % argv[0]) sym_path = argv[1] sym_path_out = argv[4] try: sym_base = int(argv[2], 0) except: fatal('Parsing symbol base failed!') try: sym_base_new = int(argv[3], 0) except: fatal('Parsing new symbol base failed!') try: sym_table = SymbolTable.from_file(sym_path, False, sym_base - sym_base_new) except: fatal('Opening symbol table failed!') try: out_file = open(sym_path_out, "w") except: fatal('Opening output file failed!') # Create linker script with relocated symbols for sym_name, sym_address in sorted(sym_table.mangled_dict.items(), key=itemgetter(1)): sym_line = '%s = %s;\n' % (sym_name, hex(sym_address)) out_file.write(sym_line) print('Relocated %s from %s to %s into %s' % (sym_path, hex(sym_base), hex(sym_base_new), sym_path_out)) sys.exit(0)
def main(argc, argv): if argc != 5: fatal( 'Usage: %s <hooks file/directory> <symbol table> <symbol table offset> <output ips>' % argv[0]) # Parse relocate symbol table if os.path.isfile(argv[2]) == False: fatal("Symbol table file does not exist!") try: sym_offset = int(argv[3], 0) except: fatal("Symbol table offset is invalid!") try: sym_table = SymbolTable.from_file(argv[2], True, sym_offset) except: fatal('Opening symbol table failed!') # Load hooks hks_path = argv[1] hooks = [] if os.path.isfile(hks_path): hooks += load_hooks(hks_path) elif os.path.isdir(hks_path): for file in os.listdir(hks_path): hks_file = os.fsdecode(file) if hks_file.endswith(".hks"): hooks += load_hooks(os.path.join(hks_path, hks_file)) else: fatal("Hooks path is invalid!") # Apply hooks out_path = argv[4] try: out_file = open(out_path, "wb") except: fatal('Opening output file failed!') ips32.write_header(out_file) for hook in hooks: try: hook.write_ips(out_file, sym_table) except Exception as e: cprint( '%s:%d: applying hook \"%s\" failed: %s' % (hook.path, hook.line, hook.name, str(e)), 'red') ips32.write_eof(out_file) out_file.close() print('Hook IPS written to %s' % out_path)