Skip to content

Instantly share code, notes, and snippets.

@ChiChou
Last active June 27, 2020 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChiChou/cb00877c1495f3b062efb0eb66dab7c2 to your computer and use it in GitHub Desktop.
Save ChiChou/cb00877c1495f3b062efb0eb66dab7c2 to your computer and use it in GitHub Desktop.
import idc
import idautils
import idaapi
import ida_funcs
import ida_name
import ida_bytes
import ida_nalt
import ida_hexrays as hr
def cstr(ea):
b = ida_bytes.get_strlit_contents(ea, -1, ida_nalt.STRTYPE_C)
if not b:
raise ValueError('uanble to get string content from %s' % hex(ea))
return b.decode()
class Visitor(idaapi.ctree_visitor_t):
def __init__(self, target, index):
super().__init__(hr.CV_FAST)
self.target = target
self.name = None
self.index = index
def visit_expr(self, i):
if (i.op == idaapi.cot_call) and i.x.obj_ea == self.target:
s = i.a[self.index].obj_ea
self.name = cstr(s)
return 1
return 0
def guess_name_by_logger(logger, index):
visited = set()
for xref in idautils.CodeRefsTo(logger, False):
f = ida_funcs.get_func(xref)
if not f:
continue
name = ida_name.get_ea_name(f.start_ea)
if not name.startswith('sub_'):
continue
if f.start_ea in visited:
continue
cfunc = hr.decompile(f)
v = Visitor(logger, index)
v.apply_to_exprs(cfunc.body, None)
if v.name:
idc.set_name(f.start_ea, v.name)
visited.add(f.start_ea)
else:
print('unable to find', hex(f.start_ea))
guess_name_by_logger(0x1234, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment