def rewrite_libraries(libpath, paths, arch): rewriter = Rewriter(True, False, False) for path in paths: (base, fname) = os.path.split(path) libname = os.path.join(libpath, fname) shutil.copy(path, libname) rewriter.rewrite(libname, arch) os.remove(libname) shutil.move(libname + '-r', libname) shutil.move(libname + '-r-map.json', libname + '-map.json') shutil.move(libname + '-r-stat.json', libname + '-stat.json')
''' # If this address is not in the guidance then return no instrumentation if not inst.address in guidance: return None inc = increment_template % (global_addr, global_addr + 4) return _asm(inc) if __name__ == '__main__': if len(sys.argv) < 2: print "Error: must pass executable filename.\nCorrect usage: %s <filename>" % sys.argv[ 0] sys.exit() with open("units_guidance.txt") as f: for line in f: guidance[int(line, 16)] = True f = open(sys.argv[1]) e = ELFFile(f) entry_point = e.header.e_entry f.close() rewriter = Rewriter(False, True, False) global_addr = rewriter.alloc_globals(8, 'x86') #8 bytes print("Count Address: " + str(hex(global_addr))) rewriter.set_before_inst_callback(count_instruction) rewriter.rewrite(sys.argv[1], 'x86')
print 'Getting required libraries for %s' % fpath paths = extract_libraries(fpath) (base, fname) = os.path.split(fpath) libpath = os.path.join(base, fname + '-libs-r') if not os.path.exists(libpath): os.makedirs(libpath) print 'Getting dynamic libraries' paths.extend(extract_dynamic_libraries(fname, libpath)) print 'Rewriting libraries' print paths rewrite_libraries(libpath, paths, arch) if not dynamic_only: print 'Rewriting main binary' rewriter = Rewriter(False, False, False) rewriter.rewrite(fpath, arch) print 'Writing runnable .sh' with open(fpath + '-r.sh', 'w') as f: ld_preload = '' for path in extract_dynamic_libraries(fname, libpath): (lbase, lname) = os.path.split(path) ld_preload += os.path.join(libpath, lname) + ' ' f.write( '#!/bin/bash\nLD_LIBRARY_PATH=./%s LD_BIND_NOW=1 LD_PRELOAD="%s" ./%s' % (fname + '-libs-r', ld_preload, fname + '-r')) else: print "Error: must pass executable filename.\nCorrect usage: %s [-d -64] <filename>\nUse -d flag to rewrite only dynamic libaries.\nUse -64 flag to rewrite 64-bit binaries." % sys.argv[ 0]
#!/usr/bin/python import sys from elftools.elf.elffile import ELFFile from multiverse import Rewriter from x64_assembler import _asm def count_instruction(inst): template = ''' nop ''' inc = template return _asm(inc) if __name__ == '__main__': if len(sys.argv) == 2: f = open(sys.argv[1]) e = ELFFile(f) entry_point = e.header.e_entry f.close() #write_so = False, exec_only = True, no_pic = True rewriter = Rewriter(False, True, False) rewriter.set_before_inst_callback(count_instruction) rewriter.rewrite(sys.argv[1], 'x86-64') else: print "Error: must pass executable filename.\nCorrect usage: %s <filename>" % sys.argv[ 0]