def enter(self, pc, options): options['database'].address(pc)['name'] = database.name(pc) xrefs_to = database.cxup(pc) func_top = pc for x in xrefs_to: xref_top = function.top(x) context = options['database'].c(xref_top) context.address(x).edge((func_top, func_top)) return
def cxUp(self, ea): return database.cxup(ea)
def __init__(self, options, create=True, existing=None): self.provider = ida.IDA() if create: # create the DB print "[*] db.py: Creating a new DB file" db = store.sqlite3.connect(options['full_file_name']) #db.isolation_level = self.db_obj = db store.driver.sqlite.Deploy(db).create() # mutes the pesky sqlite messages tmp = sys.stderr sys.stderr = StringIO() session = store.driver.sqlite.Session(db,0) my_store = store.Store(session) sys.stderr = tmp all_funcs = database.functions() opt = {} opt['database'] = my_store self.store = my_store proc = self.provider.getArch() if proc == "pc": # XXX: hackish way to fix a crap ton of stuff... start = self.provider.segByBase(self.provider.segByName(".text")) end = self.provider.segEnd(self.provider.segByBase(self.provider.segByName(".text"))) succeeded = 0 for instr in self.provider.iterInstructions(start, end): disasm = self.provider.getDisasm(instr) tokens = disasm.split(" ") res = [] for t in tokens: if len(t) != 0: res.append(t) prologues = [['mov', 'edi,', 'edi'], ['push', 'ebp'], ['push', 'rbp']] if res in prologues and instr not in all_funcs: try: prev_ea = self.provider.prevItem(instr, instr-0x20) if prev_ea not in all_funcs: if options['verbosity'] > 2: print "[!] Attempting to create a function at 0x%08x" % instr ret = self.provider.makeFunc(instr) else: continue if ret: if options['verbosity'] > 2: print "[*] Successfully made new function at 0x%08x" % instr succeeded += 1 except Exception as detail: print detail pass elif "dup(90h)" in disasm: if options['verbosity'] > 2: print "Found dup at 0x%08x" % instr try: next_ea = self.provider.nextItem(instr, instr+0x20) if next_ea not in all_funcs: ret = self.provider.nextItem(next_ea, 0xFFFFFFFF) else: continue if not ret and (next_ea in database.functions()) : if options['verbosity'] > 2: print "[*] Successfully made new function at 0x%08x" % next_ea succeeded += 1 except: pass if succeeded != 0: print "[*] Successfully created %d new functions" % succeeded print "[*] There are %d funtions to process" % len(all_funcs) failed = 0 succeeded = 0 for i in xrange(0, len(all_funcs)): i_actual = i+1 ea = all_funcs[i] if ((i_actual % 250 == 0) or (i == len(all_funcs)-1)): print "[*] db.py: Processing 0x%08x (%d of %d)" % (ea, i_actual, len(all_funcs)) analyza = analyze_xrefs(opt) collecta = collector(analyza, opt) try: collecta.go(ea) succeeded += 1 except ValueError as detail: failed += 1 if options['verbosity'] > 2: print "0x%08x - failed to process node, %s" % (ea, detail) opt['database'].commit() print "[*] Failed to process %d functions" % failed print "[*] Successfully processed %d functions" % succeeded # now loop imports segs = list(self.provider.getSegments()) if proc in ["arm", "ppc", "mips"]: idata = "extern" elif proc == "pc": idata = ".idata" for s in segs: if self.provider.segName(s) == idata: start = s end = self.provider.segEnd(s) for head in self.provider.iterData(start, end): opt['database'].address(head)['name'] = self.provider.getName(head) xrefs_to = database.cxup(head) for x in xrefs_to: try: xref_top = function.top(x) except ValueError: continue context = opt['database'].c(xref_top) context.address(x).edge((head, head)) self.commit() else: db = store.sqlite3.connect(options['full_file_name']) self.db_obj = db # mutes the pesky sqlite messages tmp = sys.stderr sys.stderr = StringIO() session = store.driver.sqlite.Session(db,0) sys.stderr = tmp my_store = store.Store(session) self.store = my_store