Advertisement
joxeankoret

Untitled

Dec 2nd, 2013
3,907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. IDAPython script for IDA 6.X to create structures from VTables.
  6.  
  7. Usage
  8. -----
  9.  
  10. Either create an array of functions by pressing '*' in the start of the VTable and setting the number of elements in the array or just select the whole VTable and execute this script. The generated output structure will be printed out to the output window.
  11.  
  12. Common problems
  13. ---------------
  14.  
  15. If some function doesn't have a prototype and IDA fails to guess the prototype you will find functions in the output like this:
  16.  
  17. (...)
  18.  int();
  19. (...)
  20.  
  21.  Change the prototype for such functions, select the area and execute again the script.
  22. """
  23.  
  24. import os
  25. import time
  26. import idaapi
  27.  
  28. def log_msg(amsg):
  29.   print ("[%s] %s" % (time.asctime(), amsg))
  30.  
  31. def get_true_name(f):
  32.   name = GetFunctionName(f)
  33.   if name is None or name == "":
  34.     d = dict(Names())
  35.     if d.has_key(f):
  36.       name = d[f]
  37.   return name
  38.  
  39. def get_demangled_name(name):
  40.   name = get_true_name(name)
  41.   demangled = Demangle(name, INF_SHORT_DN)
  42.   # Is the function name mangled?
  43.   if demangled is not None:
  44.     name = demangled.replace("::", "_").replace("`", "").replace("'", "").replace(" ", "_")
  45.   return name
  46.  
  47. def is64():
  48.   return BADADDR != 0xFFFFFFFF
  49.  
  50. class CVTableStructGenerator(object):
  51.   def __init__(self, ea, end_ea = BADADDR):
  52.     self.ea = ea
  53.     self.end_ea = end_ea
  54.     self._debug = False
  55.  
  56.   def debug(self, msg):
  57.     if self._debug:
  58.       log_msg(msg)
  59.  
  60.   def get_struct(self):
  61.     struct_name = get_demangled_name(self.ea)
  62.     if self.end_ea != BADADDR:
  63.       size = self.end_ea - self.ea
  64.     else:
  65.       size = ItemSize(self.ea)
  66.     if is64():
  67.       step_size = 8
  68.     else:
  69.       step_size = 4
  70.  
  71.     ret = ["struct %s" % struct_name]
  72.     ret.append("{")
  73.     ea = self.ea
  74.     i = 0
  75.     while ea < self.ea + size and ea != BADADDR:
  76.       func = Dword(ea)
  77.       self.debug("Function 0x%x" % func)
  78.       func_type = GetType(func)
  79.       if func_type is None:
  80.         func_type = GetType(func)
  81.         if not func_type:
  82.           func_type = GuessType(func)
  83.       func_name = get_demangled_name(func)
  84.       if func_type is not None:
  85.         if func_type.find("__cdecl") > -1:
  86.           func_type = func_type.replace("__cdecl", "(__cdecl *%s)" % func_name)
  87.         elif func_type.find("__stdcall") > -1:
  88.           func_type = func_type.replace("__stdcall", "(__stdcall *%s)" % func_name)
  89.         elif func_type.find("__fastcall") > -1:
  90.           func_type = func_type.replace("__fastcall", "(__fastcall *%s)" % func_name)
  91.         elif func_type.find("__thiscall") > -1:
  92.           func_type = func_type.replace("__thiscall", "(__thiscall *%s)" % func_name)
  93.         elif func_type.find("__usercall") > -1:
  94.           func_type = func_type.replace("__usercall", "(__usercall *%s)" % func_name)
  95.         elif func_type.find("__userpurge") > -1:
  96.           func_type = func_type.replace("__userpurge", "(__userpurge *%s)" % func_name)
  97.         elif func_type.find("[") > -1:
  98.           func_type = func_type.replace("[", " %s[" % get_demangled_name(func))
  99.       else:
  100.         if func_name is None or func_name == "":
  101.           func_type = "_DWORD dword%x" % (i * 4)
  102.         else:
  103.           func_type = "_DWORD %s%x" % (func_name, i * 4)
  104.       ret.append("  %s;" % func_type)
  105.       ea += step_size
  106.       i += 1
  107.     ret.append("};")
  108.     return "\n".join(ret)
  109.  
  110. def main():
  111.   if SelStart() != BADADDR:
  112.     tsgen = CVTableStructGenerator(SelStart(), SelEnd())
  113.   else:
  114.     tsgen = CVTableStructGenerator(here())
  115.   print tsgen.get_struct()
  116.  
  117. if __name__ == "__main__":
  118.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement