def do_patch(self, file): file.seek(0) elfFile = elffile.open(fileobj=file) file.close() bin_patches = [] symbols = self.getSymbols(elfFile) # Disassemble core = RCore() core.config.set ("asm.arch", "arm") core.config.set ("asm.bits", "32") core.config.set ("io.va", "1") desc = core.file_open(file.name, 0, 0) file = open(file.name, 'r+b') for p in self.patches: bin_patches.extend(p.getPatches(elfFile, symbols, file, core)) core.file_close(desc) # Apply patches for offset, buf in bin_patches: file.seek(offset) file.write(buf) return file
def find_merger(file): file.seek(0) elff = elffile.open(fileobj = file) machine = elff.fileHeader.machine for m in mergers: if m.machineHandled(machine): return m raise ValueError("Can't find merger for machine %s" % (machine))
def testOpen(): for filename in glob.glob(os.path.join('testfiles', '*', '*.o')): break byname = elffile.open(name=filename) with open(filename, 'rb') as fileobj: byfileobj = elffile.open(name=filename, fileobj=fileobj) with open(filename, 'rb') as fileobj: m = mmap.mmap(fileobj.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ) bymap = elffile.open(map=m) block = m[:] byblock = elffile.open(block=block) assert_equal(byname, byfileobj) assert_equal(byfileobj, bymap) assert_equal(bymap, byblock)
def cmp(self, comparison): e = [(i.content.find(self._magic, 0, len(self._magic)) == 0) for i in comparison.pair] if not reduce(operator.iand, e): self._log_different(comparison) return Different e = [elffile.open(name=i.name, block=i.content) for i in comparison.pair] if e[0].close_enough(e[1]): self._log_same(comparison) return Same else: self._log_different(comparison) return Different
def main(argv): parser = argparse.ArgumentParser(description="vaddroffset") parser.add_argument('-o', '--offset', help="offset") parser.add_argument('-v', '--vaddr', help="vaddr") parser.add_argument('file', help="file") args = parser.parse_args(argv[1:]) elff = elffile.open(args.file) if args.offset: print("0x%x" % (elfhelper.offset_to_vaddr(elff, int(args.offset, 0)))) elif args.vaddr: print("0x%x" % (elfhelper.vaddr_to_offset(elff, int(args.vaddr, 0))))
def cmp(self, comparison): e = [(i.content.find(self._magic, 0, len(self._magic)) == 0) for i in comparison.pair] if not reduce(operator.iand, e): self._log_different(comparison) return Different e = [ elffile.open(name=i.name, block=i.content) for i in comparison.pair ] if e[0].close_enough(e[1]): self._log_same(comparison) return Same else: self._log_different(comparison) return Different
def do_patch(self, file): file.seek(0) elfFile = elffile.open(fileobj=file) file.close() bin_patches = [] symbols = self.getSymbols(elfFile) file = open(file.name, "r+b") for p in self.patches: bin_patches.extend(p.getPatches(elfFile, symbols, file)) # Apply patches for offset, buf in bin_patches: file.seek(offset) file.write(buf) return file
class ElfSymbolEntryl(ElfSymbolEntry): coder = struct.Struct(b'<IIIBBH') #subclass for little endian class ElfSymbolEntryb(ElfSymbolEntry): coder = struct.Struct(b'>IIIBBH') #------------------------------------------------------------------------------ dot = False try: src = sys.argv[1] if src == '-dot': dot = True src = sys.argv[2] elf = elffile.open(src) except Exception as e: print("ERROR : Could not open input file '", file_input,"'") print(e) sys.exit(1) rodata = None dicdata = None symbols = None strings = None for sec in elf.sectionHeaders: if sec.name == b".rodata": print(" .rodata found") rodata = sec
import yaml import elffile import sys import glob import os import itertools def filelist(which): for f in glob.iglob(os.path.join('testfiles', which, '.libs', 'hello')): yield f for f in glob.iglob(os.path.join('testfiles', which, '.libs', '*.so*')): yield f for f in glob.iglob(os.path.join('testfiles', which, '.libs', '*.o')): yield f for f in glob.iglob(os.path.join('testfiles', which, '*.o')): yield f for xname, yname in itertools.izip(filelist('one'), filelist('two')): x = elffile.open(name=xname) y = elffile.open(name=yname) print('{0}: {1}'.format(xname, x.close_enough(y)))
def __init__(self, path): self._lib = elffile.open(path) self.headers = dict((h.name, h) for h in self._lib.sectionHeaders) self.load_dynstr() self.load_dynamic()
def postprocess(name): eo = elffile.open(name=name) eo.fileHeader.entry = 0x4400 eo.fileHeader.type = 2 with open(name, 'wb') as ofile: ofile.write(eo.pack())
import glob import itertools import optparse import pprint import sys import elffile if __name__ == '__main__': progname = sys.argv[0] u = '' u += 'usage: %prog objfile1 objfile2' parser = optparse.OptionParser(usage=u) options, args = parser.parse_args() assert len(args) == 2 x = elffile.open(name=args[0]) y = elffile.open(name=args[1]) if x.close_enough(y): sys.exit() print('different') sys.exit(1)
#!/usr/bin/env python2 import os import sys import time sys.path.append('../../buildtools/binary_patcher') sys.path.append('../../buildtools/elffile') import binary_patcher from binary_patcher import * import elffile ef = elffile.open(name="patch.elf") # This function gives us the address of a function in our patch.elf file It helps to identify where functions where automatically placed by the linker def getSectionAddr(name): try: return next( (header for header in ef.sectionHeaders if header.name == name), None).addr except: return 0 patch_firmware( "../../bootimg_src/firmware/brcmfmac43430-sdio.orig.bin", "brcmfmac43430-sdio.bin", [ # The text section is always required and contains code that is called by patches and hooks but not directly placed to predefined memory locations ExternalArmPatch(getSectionAddr(".text"), "text.bin"), ExternalArmPatch(getSectionAddr(".text.wlc_ucode_download_hook"),
#!/usr/bin/env python2 import sys sys.path.append('../../../buildtools/binary_patcher') sys.path.append('../../../buildtools/elffile') import binary_patcher from binary_patcher import * import elffile ef = elffile.open(name="wlc_bmac_recv.elf") def getSectionAddr(name): return next((header for header in ef.sectionHeaders if header.name == name), None).addr FW_FILE = "../../../bootimg_src/firmware/fw_bcmdhd.orig.bin" patchfile0 = 'filter.bin' patchfile1 = 'wlc_bmac_recv.bin' # wlc_bmac_recv() detour0 = ExternalArmPatch(0x180050, patchfile0) detour1 = ExternalArmPatch(0x1AAD98, patchfile1) # sdio detour40 = BLPatch(0x182C60, getSectionAddr(".text.sdio_handler")); detour41 = GenericPatch4(0x180BB4, getSectionAddr(".text.sdio_handler")); detour15 = ExternalArmPatch(getSectionAddr(".text"), "text.bin") #2nd call of wlc_bmac_mctrl() in wlc_coreinit()
import glob import itertools import optparse import pprint import sys import elffile if __name__ == '__main__': progname = sys.argv[0] u = '' u += 'usage: %prog objfile1 objfile2' parser = optparse.OptionParser(usage = u) options, args = parser.parse_args() assert len(args) == 2 x = elffile.open(name=args[0]) y = elffile.open(name=args[1]) if x.close_enough(y): sys.exit() print('different') sys.exit(1)
__docformat__ = 'restructuredtext en' import glob import itertools import optparse import pprint import sys import elffile if __name__ == '__main__': progname = sys.argv[0] u = '' u += 'usage: %prog objfile [objfile [objfile ...]]' parser = optparse.OptionParser(usage=u) options, args = parser.parse_args() ef = elffile.open(name=args[0]) for header in ef.sectionHeaders: if header.addralign == 4: header.addralign = 2 print("header name: ", header.name, "type: ", header.type, "align: ", header.addralign) with open(args[1], 'wb') as f: f.write(ef.pack()) sys.exit()