def _arg_out(arg): if len(arg) == 0: return () elif REIL.Arg_type(arg) == REIL.A_CONST: arg = ( REIL.Arg_type(arg), REIL.Arg_size(arg), _U64OUT(REIL.Arg_val(arg)) ) return arg
def _insn_to_item(self, insn): insn = REIL.Insn(insn) def _arg_in(arg): if arg.type == REIL.A_NONE: return () elif arg.type == REIL.A_CONST: return ( arg.type, arg.size, _U64IN(arg.val) ) else: return ( arg.type, arg.size, arg.name ) if insn.has_attr(REIL.IATTR_BIN): # JSON doesn't support binary data insn.set_attr(REIL.IATTR_BIN, base64.b64encode(insn.get_attr(REIL.IATTR_BIN))) # JSON doesn't support numeric keys attr = [ (key, val) for key, val in insn.attr.items() ] return { 'addr': _U64IN(insn.addr), 'size': insn.size, 'inum': insn.inum, 'op': insn.op, \ 'a': _arg_in(insn.a), 'b': _arg_in(insn.b), 'c': _arg_in(insn.c), \ 'attr': attr }
def test_reader(self): if os.path.isfile(self.BIN_PATH): reader = Reader(self.BIN_PATH) tr = REIL.CodeStorageTranslator(reader) print tr.get_func(self.PROC_ADDR)
def read(self, addr, size): if addr < self.pe.OPTIONAL_HEADER.ImageBase or \ addr > self.pe.OPTIONAL_HEADER.ImageBase + self.pe.OPTIONAL_HEADER.SizeOfImage: # invalid VA print 'Reader.read(): Address 0x%x is outside of executable image' % addr raise REIL.ReadError(addr) # convert VA to RVA addr -= self.pe.OPTIONAL_HEADER.ImageBase try: return self.pe.get_data(rva=addr, length=size) except e, why: print 'Reader.read(): Exception:', str(why) raise REIL.ReadError(addr)
def read(self, addr, size): for sec in self.bfd.sections.values(): # lookup for image section by address if addr >= sec.vma and addr < sec.vma + sec.size: # return section data addr -= sec.vma return sec.content[addr : addr + size] # invalid VA print 'Reader.read(): Address 0x%x is outside of executable image' % addr raise REIL.ReadError(addr)
def _del_insn(self, ir_addr): insn = self._find(ir_addr) if insn is not None: # remove item from collection self.collection.remove(self._get_key(ir_addr)) # remove item from cache try: del self.cache[ir_addr] except KeyError: pass else: raise REIL.StorageError(*ir_addr)
def _put_insn(self, insn): ir_addr = REIL.Insn_ir_addr(insn) if self._find(ir_addr) is not None: # update existing item self.collection.update(self._get_key(ir_addr), self._insn_to_item(insn)) else: # add a new item self.collection.insert(self._insn_to_item(insn)) # update cache self.cache[ir_addr] = insn
def _get_insn(self, ir_addr): # get item from cache try: return self.cache[ir_addr] except KeyError: pass # get item from collection insn = self._find(ir_addr) if insn is not None: insn = self._insn_from_item(insn) # update cache self.cache[ir_addr] = insn return insn else: raise REIL.StorageError(*ir_addr)
def __iter__(self): for item in self.collection.find().sort(self.INDEX): yield REIL.Insn(self._insn_from_item(item))