This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
demangle.py
72 lines (52 loc) · 1.73 KB
/
demangle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#-*- coding: utf-8 -*-
from idc import *
import idautils
import ctypes
class Demangle:
def __init__(self, libraryPath):
self.libRustcDemangle = ctypes.CDLL(libraryPath)
self.libRustcDemangle.argtype = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int)
self.libRustcDemangle.restype = ctypes.c_int
def setLibFunc(self, name,addr,flag=False):
libNameList = ["core::","alloc::","std::","builtins::","std_unicode::"]
if flag:
libNameList.append("sub_")
libNameList.append("_Unwind_Resume")
for chk in libNameList:
if chk in name:
flags = get_func_flags(addr)
flags = flags | FUNC_LIB
set_func_flags(addr,flags)
def _demangle(self, func_list):
ret = []
for idx, func_name in enumerate(func_list):
ret.append(func_name)
func_name = func_list[idx].encode("latin-1")
c_input_1 = ctypes.create_string_buffer(func_name ,len(func_name) + 1)
c_input_2 = ctypes.create_string_buffer(b"", 0x100)
result = self.libRustcDemangle.rustc_demangle(c_input_1, c_input_2, 0x100)
if result == 1:
ret[-1] = c_input_2.raw.split(b"\x00")[0].decode()
return ret
def demangle(self):
FuncNameList = []
demangleList = []
FuncList = []
for func in idautils.Functions():
name = get_func_name(func)
FuncList += [func]
FuncNameList += [name]
demangleList = self._demangle(FuncNameList)
for i in range(len(FuncList)):
addr = FuncList[i]
old_name = FuncNameList[i]
full_name = demangleList[i]
self.setLibFunc(full_name,addr)
if full_name != old_name :
set_name(addr, full_name, SN_NOCHECK | 0x800) #SN_FORCE
set_func_cmt(addr, full_name, 1)
def main():
demangle = Demangle("rustc_demangle.dll")
demangle.demangle()
if __name__ == "__main__":
main()