-
Notifications
You must be signed in to change notification settings - Fork 20
/
idapythonrc.py
288 lines (246 loc) · 8.07 KB
/
idapythonrc.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# C:\Users\[USERNAME]\AppData\Roaming\Hex-Rays\IDA Pro\idapythonrc.py
from idaapi import *
import idc
import zlib
import traceback
import webbrowser
import datetime
from codemap import codemap
__version__ = '2.0'
codemap = codemap.Codemap()
def IDA_State():
if get_root_filename() is None:
return 'empty'
try:
a = idc.GetRegValue('esp')
return 'running'
except:
return 'static'
# batch function break point script - daehee
def Functions(start=None, end=None):
if not start:
start = cvar.inf.minEA
if not end:
end = cvar.inf.maxEA
chunk = get_fchunk(start)
if not chunk:
chunk = get_next_fchunk(start)
while chunk and chunk.startEA < end and (chunk.flags & FUNC_TAIL) != 0:
chunk = get_next_fchunk(chunk.startEA)
func = chunk
while func and func.startEA < end:
yield (func)
func = get_next_func(func.startEA)
def FuncItems(start):
func = get_func(start)
if not func:
return
fii = func_item_iterator_t()
ok = fii.set(func)
while ok:
yield fii.current()
ok = fii.next_code()
'''
Returns a list of module objects with name,size,base and the rebase_to
'''
def Modules():
mod = idaapi.module_info_t()
result = idaapi.get_first_module(mod)
while result:
yield idaapi.object_t(name=mod.name, size=mod.size, base=mod.base, rebase_to=mod.rebase_to)
result = idaapi.get_next_module(mod)
# print slows IDA down
class IDAHook(DBG_Hooks):
global codemap
def dbg_process_exit(self, pid, tid, ea, code):
codemap.db_insert()
codemap.init_codemap()
print("Process exited pid=%d tid=%d ea=0x%x code=%d" %
(pid, tid, ea, code))
def dbg_bpt(self, tid, ea):
if codemap.pause is True:
return 0 # stop visualizing
codemap.set_data()
codemap.db_insert_queue()
continue_process() # continue
return 0 # no warning
def hook_ida():
global debughook
# Remove an existing debug hook
try:
if debughook:
print("Removing previous hook ...")
debughook.unhook()
except:
pass
# Install the debug hook
debughook = IDAHook()
debughook.hook()
debughook.steps = 0
'''
- SetRangeBP -
Get address range from user and setup bp to all instruction in that range.
'''
def SetRangeBP():
if IDA_State() == 'empty':
print 'no program loaded'
return
start_addr = AskStr('', 'Start Addr? (e.g. 0x8000) : ')
end_addr = AskStr('', 'End Addr? (e.g. 0xC000) : ')
start_addr = int(start_addr.replace('0x', ''), 16)
end_addr = int(end_addr.replace('0x', ''), 16)
for e in Heads(start_addr, end_addr):
if get_bpt(e, bpt_t()) is False:
add_bpt(e, 0, BPT_SOFT)
else:
del_bpt(e)
'''
- SetFunctionBP -
put cursor inside the IDA-recognized function then call this.
bp will be set to all instructions of function
'''
def SetFunctionBP():
if IDA_State() == 'empty':
print 'no program loaded'
return
ea = ScreenEA()
target = 0
for e in Functions():
if e.startEA <= ea and ea <= e.endEA:
target = e.startEA
if target != 0:
for e in FuncItems(target):
if get_bpt(e, bpt_t()) is False:
add_bpt(e, 0, BPT_SOFT)
else:
del_bpt(e)
else:
Warning('put cursor in the function body')
'''
- Start Trace -
setup all bp's you want(maybe using GETBPLIST or SetFunctionBP or manually)
then execute this script in order to create dygraph trace.
'''
def StartTracing():
global codemap
if codemap.start is False and IDA_State() != 'running':
print 'IDA debugger not running'
return
if codemap.start is True:
codemap.pause = not codemap.pause
print 'Codemap Paused? : ', codemap.pause
if codemap.pause is False: # resume tracing
continue_process()
else:
codemap.db_insert()
suspend_process()
return
# set current uid. if there is existing codemap instance, save it to prev_uids
if codemap.uid != None:
codemap.prev_uids.append( codemap.uid )
codemap.uid = datetime.datetime.fromtimestamp(
time.time()).strftime('%Y%m%d%H%M%S')
codemap.init_arch()
hook_ida()
print 'hook ida done.'
print 'homedir : ', codemap.homedir
print 'making table...'
# initialize sqlite3 db
print codemap.db_create()
# set default SQL
if codemap.arch.name == 'x86':
codemap.query = "select eip from trace{0}".format(codemap.uid)
if codemap.arch.name == 'x64':
codemap.query = "select rip from trace{0}".format(codemap.uid)
print 'start HTTP server'
# start HTTP server
codemap.start_webserver()
# fire up chrome!
result = 'http://{0}:{1}/{2}'.format(codemap.server,
codemap.port,
codemap.uid)
webbrowser.open(result)
print 'start tracing...'
codemap.start = True
continue_process()
'''
- SaveModuleBP -
open a dll file with IDA and execute this script after IDA analysis is done.
the function offset information of dll will be saved to file inside Codemap directory
'''
def SaveModuleBP():
global codemap
try:
modname = AskStr('', 'module name : ')
bpo = ''
for e in Functions():
func = e.startEA
length = e.endEA - e.startEA
if length < codemap.func_min_size:
continue
offset = func - get_imagebase()
bpo += str(offset) + '\n'
print 'bp offset generation complete! ' + str(len(bpo))
payload = bpo
with open(codemap.homedir + modname + '.bpo', 'wb') as f:
f.write(zlib.compress(payload))
except:
traceback.print_exc(file=sys.stdout)
'''
- LoadModuleBP -
while debugging the target app, put cursor somewhere inside the target module code.
execute the script and bp will be set for all functions specified in .bpo file
'''
def LoadModuleBP():
global codemap
try:
# get current cursor
cur = get_screen_ea()
baseaddr = 0
modname = ''
# what module is my cursor pointing?
for i in Modules():
if cur > i.base and cur < i.base + i.size:
modname = i.name.split('\x00')[0]
modname = modname.split('\\')[-1:][0]
baseaddr = i.base
codemap.base = baseaddr # this is needed.
modname = AskStr('', 'module name : ')
payload = ''
with open(codemap.homedir + modname + '.bpo', 'rb') as f:
payload = zlib.decompress(f.read())
bps = payload.split()
code = bytearray()
for bp in bps:
code += 'add_bpt({0}, 0, BPT_SOFT);'.format(baseaddr + int(bp))
print 'setting breakpoints...'
# set bp!
exec(str(code))
except:
traceback.print_exc(file=sys.stdout)
def SetModuleBP():
global codemap
if codemap.start is False and IDA_State() is 'static':
SaveModuleBP()
if codemap.start is False and IDA_State() is 'running':
LoadModuleBP()
def ListenCodemap():
global codemap
codemap.start_websocketserver()
print "Listning to codemap connection..."
CompileLine('static key_1() { RunPythonStatement("StartTracing()"); }')
CompileLine('static key_2() { RunPythonStatement("SetFunctionBP()"); }')
CompileLine('static key_3() { RunPythonStatement("SetRangeBP()"); }')
CompileLine('static key_4() { RunPythonStatement("SetModuleBP()"); }')
CompileLine('static key_5() { RunPythonStatement("ListenCodemap()"); }')
AddHotkey('Alt-1', 'key_1')
AddHotkey('Alt-2', 'key_2')
AddHotkey('Alt-3', 'key_3')
AddHotkey('Alt-4', 'key_4')
AddHotkey('Alt-5', 'key_5')
print 'ALT-1 : Start(Resume)/Pause Codemap'
print 'ALT-2 : Set Function BP'
print 'ALT-3 : Set Range BP'
print 'ALT-4 : Create/Setup Module BP'
print 'ALT-5 : Connect Codemap Graph with IDA'
print 'Codemap Python Plugin is ready. enjoy. - by daehee'