def print_f(self, funcs): for f in funcs: print ' '.join((f.func_name, dec_hex(f.func_begin_addr), dec_hex(f.func_end_addr), str(f.is_lib).lower() ))
def add_data_label(self): dataoff = self.section_addr('.data') rodataoff = self.section_addr('.rodata') for e in self.locations: n, l = e if n == '.data': self.data_list[l] = (dec_hex(l + dataoff), self.data_list[l][1]) elif n == '.rodata': self.rodata_list[l] = (dec_hex(l + rodataoff), self.rodata_list[l][1])
def data_dump(self, funcs): t = self.label + self.export_data_dump() p = datahandler(t) p.text_sec_collect() p.set_datas(funcs) self.jmpreflist = map(lambda l: 'S_' + dec_hex(l), p.get_textlabel()) p.data_output()
def update(self): func = 'S_' + dec_hex(self.baddr) if func in func_slicer.func_set: func_slicer.func_set[func].func_begin_addr = self.baddr func_slicer.func_set[func].func_end_addr = self.eaddr func_slicer.func_set[func].is_lib = False else: f1 = Func(func, self.baddr, self.eaddr, False) func_slicer.func_set[func] = f1
def v_exp(self, e): if isinstance(e, (Types.BinOP_PLUS, Types.BinOP_MINUS)): r1, addr = e if lib32_helper.reg_to_index(r1) == lib32_helper.r: addr = -addr if isinstance(e, Types.BinOP_MINUS) else addr des = lib32_helper.rv + addr s = self.check_sec(des) if s is not None: s_label = 'S_' + dec_hex(des) + '@GOTOFF(' + p_reg( r1) + ')' self.label.insert(0, des) return Types.Label(s_label) return e
def process(self, lbs, withoff=False): ds = { '.data': self.data_list, '.rodata': self.rodata_list, '.got': self.got_list, '.bss': self.bss_list } for i in xrange(len(lbs)): n, l = lbs[i] if n in ds: if withoff: off = l - self.section_addr(n) else: off = l l += self.section_addr(n) ds[n][off] = ('S_' + dec_hex(l) + ': ', ds[n][off][1])
def process(self): # here dec_hex is 'S_' + dec_hex(v) do_update = lambda s, n: s if n in s else s + '\n' + n des1 = self.clean_sort(self.des) i = 0 j = 0 while True: if j == len(des1) or i == len(self.locs): break # if i == len(self.locs)-1 and j == len(des1)-1: # raise Exception("undefined des list") lh = self.locs[i] dh = des1[j] if dh == lh.loc_addr: lhs = 'S_' + dec_hex(lh.loc_addr) label = do_update(lh.loc_label, lhs + ' : ') self.locs[i].loc_label = label j += 1 elif dh < lh.loc_addr: j += 1 i += 1
def v_exp2(self, exp, instr, f, chk): # TODO: create subfunctions to simplify this mess # check_test_condition = lambda l,c: not (isinstance(l, Types.Normal) and c) if isinstance(exp, Types.Const): if isinstance(exp, Types.Normal) and chk: return exp l1 = self.parse_const(exp) s = self.check_sec(l1) if s is not None: s_label = self.build_symbol(exp) loc1 = get_loc(instr) if not self.has_data(l1): reassemble.data_set[l1] = '' self.label.insert(0, (s.sec_name, l1)) self.c2d_addr.insert(0, loc1.loc_addr) return Types.Label(s_label) if self.check_text(l1): s_label = self.build_symbol(exp) loc1 = get_loc(instr) if not self.has_text(l1): reassemble.text_set[l1] = '' self.deslist.insert(0, s_label) self.deslist_reloc.insert(0, loc1.loc_addr) return Types.Label(s_label) if self.check_plt(l1): return Types.Label(self.build_plt_symbol(exp)) elif isinstance(exp, Types.Symbol): if isinstance(exp, Types.JumpDes): if self.check_text(exp): s_label = 'S_' + dec_hex(exp) if not self.has_text(exp): reassemble.text_set[exp] = '' self.deslist.insert(0, s_label) return Types.Label(s_label) elif isinstance(exp, Types.StarDes): return Types.StarDes(self.v_exp2(exp.content, instr, f, chk)) elif isinstance(exp, Types.CallDes): fn = exp.func_name[2:] is_dig_loc = False try: addr = int(fn, 16) is_dig_loc = True except: self.symbol_list.insert(0, exp.func_begin_addr) if is_dig_loc: if self.check_text(addr): s_label = 'S_' + dec_hex(addr) if not self.has_text(addr): reassemble.text_set[addr] = '' self.deslist.insert(0, s_label) return Types.Label(s_label) elif isinstance(exp, Types.Ptr): if isinstance(exp, (Types.BinOP_PLUS, Types.BinOP_MINUS)): r, addr = exp s = self.check_sec(addr) if s is not None: s_label = 'S_' + dec_hex(addr) loc1 = get_loc(instr) if not self.has_data(addr): reassemble.data_set[addr] = '' self.label.insert(0, (s.sec_name, addr)) self.c2d_addr.insert(0, loc1.loc_addr) return Types.BinOP_PLUS_S((r, s_label)) \ if isinstance(exp, Types.BinOP_PLUS) \ else Types.BinOP_MINUS_S((r, s_label)) elif isinstance(exp, (Types.FourOP_PLUS, Types.FourOP_MINUS)): r1, r2, off, addr = exp s = self.check_sec(addr) if s is not None: s_label = 'S_' + dec_hex(addr) loc1 = get_loc(instr) if not self.has_data(addr): reassemble.data_set[addr] = '' self.label.insert(0, (s.sec_name, addr)) self.c2d_addr.insert(0, loc1.loc_addr) return Types.FourOP_PLUS_S((r1,r2,off,s_label)) \ if isinstance(exp, Types.FourOP_PLUS) \ else Types.FourOP_MINUS_S((r1,r2,off,s_label)) elif isinstance(exp, Types.JmpTable_PLUS): addr, r, off = exp s = self.check_sec(addr) if s is not None: s_label = 'S_' + dec_hex(addr) loc1 = get_loc(instr) if not self.has_data(addr): reassemble.data_set[addr] = '' self.label.insert(0, (s.sec_name, addr)) self.c2d_addr.insert(0, loc1.loc_addr) return Types.JmpTable_PLUS_S((s_label, r, off)) if self.check_text(addr): s_label = 'S_' + dec_hex(addr) loc1 = get_loc(instr) if not self.has_text(addr): reassemble.text_set[addr] = '' self.deslist.insert(0, s_label) self.deslist_reloc.insert(0, loc1.loc_addr) return Types.JmpTable_PLUS_S((s_label, r, off)) elif isinstance(exp, Types.JmpTable_MINUS): addr, r, off = exp s = self.check_sec(addr) if s is not None: s_label = '-S_' + dec_hex(addr) loc1 = get_loc(instr) if not self.has_data(addr): reassemble.data_set[addr] = '' self.label.insert(0, (s.sec_name, addr)) self.c2d_addr.insert(0, loc1.loc_addr) return Types.JmpTable_MINUS_S((s_label, r, off)) if self.check_text(addr): s_label = '-S_' + dec_hex(addr) loc1 = get_loc(instr) if not self.has_text(addr): reassemble.text_set[addr] = '' self.deslist.insert(0, s_label) self.deslist_reloc.insert(0, loc1.loc_addr) return Types.JmpTable_MINUS_S((s_label, r, off)) return exp
def build_symbol(self, c): if isinstance(c, Types.Point): return 'S_' + dec_hex(c) elif isinstance(c, Types.Normal): return '$S_' + dec_hex(c)
def print_cfi_specified_graph(self): self.cfi_specified_tbl() for k, v in cg.cfi_tbl.iteritems(): print k for l in v: print ' ' + dec_hex(l)
def print_cg_graph(self): for k, v in cg.cfi_tbl.iteritems(): sys.stdout.write(dec_hex(k)) for f in v: print ' ' + f.func_name
def func_info(self, l): for h in self.funcs: if h.func_begin_addr <= l.loc_addr < h.func_end_addr: return h raise Exception( dec_hex(l.loc_addr) + ': cannot find corresponding function')