This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
PltResolver.py
216 lines (196 loc) · 6.05 KB
/
PltResolver.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#coding=utf8
#author: veritas501
import idc
import idaapi
import idautils
def SetFuncFlags(ea):
func_flags=idc.GetFunctionFlags(ea)
func_flags|=0x84 # FUNC_THUNK|FUNC_LIB
idc.SetFunctionFlags(ea,func_flags)
def PltResolver64():
def GetDyn():
phoff = idc.Qword(idc.MinEA()+0x20)+idc.MinEA()
phnum = idc.Word(idc.MinEA()+0x38)
phentsize = idc.Word(idc.MinEA()+0x36)
for i in range(phnum):
p_type = idc.Dword(phoff+phentsize*i)
if p_type == 2: # PY_DYNAMIC
dyn_addr = idc.Qword(phoff+phentsize*i+0x10)
return dyn_addr
def ParseDyn(dyn,tag):
idx=0
while True:
v1,v2 = idc.Qword(dyn+idx*0x10),idc.Qword(dyn+idx*0x10+8)
if v1 == 0 and v2 == 0:
return
if v1 == tag:
return v2
idx+=1
def __PltResolver(jmprel,strtab,symtab):
idx=0
while True:
r_off = idc.Qword(jmprel+0x18*idx)
r_info1 = idc.Dword(jmprel+0x18*idx+0x8)
r_info2 = idc.Dword(jmprel+0x18*idx+0xc)
r_addend = idc.Qword(jmprel+0x18*idx+0x10)
if r_off > 0x7fffffff:
return
if r_info1 == 7:
st_name = idc.Dword(symtab+r_info2*0x18)
name = idc.GetString(strtab+st_name)
# rename got
idc.set_name(r_off,name+'_ptr')
plt_func = idc.Qword(r_off)
# rename plt
idc.set_name(plt_func,'j_'+name)
SetFuncFlags(plt_func)
# rename plt.sec
for addr in idautils.DataRefsTo(r_off):
plt_sec_func = idaapi.get_func(addr)
if plt_sec_func:
plt_sec_func_addr = plt_sec_func.startEA
idc.set_name(plt_sec_func_addr,'_'+name)
SetFuncFlags(plt_sec_func_addr)
else:
print "[!] idaapi.get_func({}) failed".format(hex(addr))
idx+=1
dyn = GetDyn()
if not dyn:
print "[-] can't find symbol '_DYNAMIC'"
return
jmprel = ParseDyn(dyn,0x17)
strtab = ParseDyn(dyn,0x5)
symtab = ParseDyn(dyn,0x6)
if not jmprel:
print "[-] can't find 'DT_JMPREL' in '_DYNAMIC'"
return
if not strtab:
print "[-] can't find 'DT_STRTAB' in '_DYNAMIC'"
return
if not symtab:
print "[-] can't find 'DT_SYMTAB' in '_DYNAMIC'"
return
__PltResolver(jmprel,strtab,symtab)
def PltResolver32():
def GetDyn():
phoff = idc.Dword(idc.MinEA()+0x1c)+idc.MinEA()
phnum = idc.Word(idc.MinEA()+0x2c)
phentsize = idc.Word(idc.MinEA()+0x2a)
for i in range(phnum):
p_type = idc.Dword(phoff+phentsize*i)
if p_type == 2: # PY_DYNAMIC
dyn_addr = idc.Dword(phoff+phentsize*i+8)
return dyn_addr
def ParseDyn(dyn,tag):
idx=0
while True:
v1,v2 = idc.Dword(dyn+idx*0x8),idc.Dword(dyn+idx*0x8+4)
if v1 == 0 and v2 == 0:
return
if v1 == tag:
return v2
idx+=1
def __PltResolver(jmprel,strtab,symtab,pltgot):
seg_sec = idc.SegByName('.plt.sec')
sec_start = idc.SegByBase(seg_sec)
sec_end = idc.SegEnd(sec_start)
if sec_start == idaapi.BADADDR:
print "[-] can't find .plt.sec segment"
return
idx=0
while True:
r_off = idc.Dword(jmprel+0x8*idx)
r_info1 = idc.Byte(jmprel+0x8*idx+0x4)
r_info2 = idc.Byte(jmprel+0x8*idx+0x5)
if r_off > 0x7fffffff:
return
if r_info1 == 7:
st_name = idc.Dword(symtab+r_info2*0x10)
name = idc.GetString(strtab+st_name)
# rename got
idc.set_name(r_off,name+'_ptr')
plt_func = idc.Dword(r_off)
# rename plt
idc.set_name(plt_func,'j_'+name)
SetFuncFlags(plt_func)
# rename plt.sec
for addr in idautils.DataRefsTo(r_off):
plt_sec_func = idaapi.get_func(addr)
if plt_sec_func:
plt_sec_func_addr = plt_sec_func.startEA
idc.set_name(plt_sec_func_addr,'_'+name)
SetFuncFlags(plt_sec_func_addr)
else:
print "[!] idaapi.get_func({}) failed".format(hex(addr))
got_off = r_off-pltgot
target = '+{}h'.format(hex(got_off).lower().replace('0x','').replace('l','').rjust(2,'0'))
for func_ea in idautils.Functions(sec_start,sec_end):
func = idaapi.get_func(func_ea)
cur = func.startEA
end = func.endEA
find=False
while cur <= end:
code = idc.GetDisasm(cur).lower().replace(' ','')
if target in code:
find=True
break
cur = idc.NextHead(cur, end)
if find:
idc.set_name(func_ea,'_'+name)
SetFuncFlags(func_ea)
idx+=1
dyn = GetDyn()
if not dyn:
print "[-] can't find symbol '_DYNAMIC'"
return
jmprel = ParseDyn(dyn,0x17)
strtab = ParseDyn(dyn,0x5)
symtab = ParseDyn(dyn,0x6)
pltgot = ParseDyn(dyn,0x3)
if not jmprel:
print "[-] can't find 'DT_JMPREL' in '_DYNAMIC'"
return
if not strtab:
print "[-] can't find 'DT_STRTAB' in '_DYNAMIC'"
return
if not symtab:
print "[-] can't find 'DT_SYMTAB' in '_DYNAMIC'"
return
if not pltgot:
print "[-] can't find 'DT_PLTGOT' in '_DYNAMIC'"
return
__PltResolver(jmprel,strtab,symtab,pltgot)
class PltResolver_handler(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
arch = idc.Word(idc.MinEA()+0x12)
if arch == 0x3E: # EM_X86_64
PltResolver64()
elif arch == 0x3: # EM_386
PltResolver32()
else:
print '[-] Only support EM_X86_64 and EM_386'
return 1
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class PltResolver(idaapi.plugin_t):
flags = idaapi.PLUGIN_HIDE
comment = 'pltResolver'
help = ''
wanted_name = 'pltResolver'
wanted_hotkey = ''
def init(self):
idaapi.msg('===================================================\n')
idaapi.msg('pltResolver plugin has been loaded.\n')
idaapi.msg('Press Ctrl+Shift+J to resolve .plt.sec symbols.\n')
idaapi.msg('===================================================\n')
idaapi.register_action(idaapi.action_desc_t('pltResolver:pltResolver', 'Parse .plt.sec symbols', PltResolver_handler(), 'Ctrl+Shift+J', None, 25))#注册action
idaapi.attach_action_to_menu('Edit/pltResolver', 'pltResolver:pltResolver', idaapi.SETMENU_APP)#将action添加到menu
return idaapi.PLUGIN_KEEP
def term(self):#析构
idaapi.unregister_action('pltResolver:pltResolver')
def run(self,arg):
pass
def PLUGIN_ENTRY():
return PltResolver()