-
Notifications
You must be signed in to change notification settings - Fork 35
/
utils.py
495 lines (397 loc) · 13.5 KB
/
utils.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import logging
import random
import ida_bytes
import ida_enum
import ida_funcs
import ida_hexrays
import ida_kernwin
import ida_lines
import ida_nalt
import ida_name
import ida_search
import ida_struct
import ida_typeinf
import ida_xref
import idaapi
import idautils
import idc
from idc import BADADDR
# WORD length in bytes
WORD_LEN = None
def update_word_len(code, old=0):
global WORD_LEN
info = idaapi.get_inf_structure()
if info.is_64bit():
logging.debug("is 32 bit")
WORD_LEN = 8
elif info.is_32bit():
logging.debug("is 32 bit")
WORD_LEN = 4
idaapi.notify_when(idaapi.NW_OPENIDB, update_word_len)
def get_word(ea):
if WORD_LEN == 4:
return idaapi.get_32bit(ea)
elif WORD_LEN == 8:
return idaapi.get_64bit(ea)
return None
def get_ptr(ea):
return get_word(ea)
def make_word(ea):
if WORD_LEN == 4:
return ida_bytes.create_dword(ea, 4)
elif WORD_LEN == 8:
return ida_bytes.create_qword(ea, 8)
return None
def make_ptr(ea):
return make_word(ea)
def is_func(ea):
func = ida_funcs.get_func(ea)
if func is not None and func.start_ea == ea:
return True
return None
def get_funcs_list():
pass
def get_drefs(ea):
xref = ida_xref.get_first_dref_to(ea)
while xref != BADADDR:
yield xref
xref = ida_xref.get_next_dref_to(ea, xref)
def get_typeinf(typestr):
tif = idaapi.tinfo_t()
tif.get_named_type(idaapi.get_idati(), typestr)
return tif
def get_typeinf_ptr(typeinf):
old_typeinf = typeinf
if isinstance(typeinf, str):
typeinf = get_typeinf(typeinf)
if typeinf is None:
logging.warning("Couldn't find typeinf %s", old_typeinf or typeinf)
return None
tif = idaapi.tinfo_t()
tif.create_ptr(typeinf)
return tif
def get_func_details(func_ea):
xfunc = ida_hexrays.decompile(func_ea)
if xfunc is None:
return None
func_details = idaapi.func_type_data_t()
xfunc.type.get_func_details(func_details)
return func_details
def update_func_details(func_ea, func_details):
function_tinfo = idaapi.tinfo_t()
function_tinfo.create_func(func_details)
if not ida_typeinf.apply_tinfo(func_ea, function_tinfo, idaapi.TINFO_DEFINITE):
return None
return function_tinfo
def add_to_struct(
struct,
member_name,
member_type=None,
offset=BADADDR,
is_offset=False,
overwrite=False,
):
mt = None
flag = idaapi.FF_DWORD
member_size = WORD_LEN
if member_type is not None and (member_type.is_struct() or member_type.is_union()):
logging.debug("Is struct!")
substruct = extract_struct_from_tinfo(member_type)
if substruct is not None:
flag = idaapi.FF_STRUCT
mt = ida_nalt.opinfo_t()
mt.tid = substruct.id
logging.debug(
f"Is struct: {ida_struct.get_struc_name(substruct.id)}/{substruct.id}"
)
member_size = ida_struct.get_struc_size(substruct.id)
elif WORD_LEN == 4:
flag = idaapi.FF_DWORD
elif WORD_LEN == 8:
flag = idaapi.FF_QWORD
if is_offset:
flag |= idaapi.FF_0OFF
mt = ida_nalt.opinfo_t()
r = ida_nalt.refinfo_t()
r.init(ida_nalt.get_reftype_by_size(WORD_LEN) | ida_nalt.REFINFO_NOBASE)
mt.ri = r
new_member_name = member_name
member_ptr = ida_struct.get_member(struct, offset)
if overwrite and member_ptr:
if ida_struct.get_member_name(member_ptr.id) != member_name:
logging.debug("Overwriting!")
ret_val = ida_struct.set_member_name(struct, offset, member_name)
i = 0
while ret_val == ida_struct.STRUC_ERROR_MEMBER_NAME:
new_member_name = "%s_%d" % (member_name, i)
i += 1
if i > 250:
logging.debug("failed change name")
return
ret_val = ida_struct.set_member_name(struct, offset, new_member_name)
else:
ret_val = ida_struct.add_struc_member(
struct, new_member_name, offset, flag, mt, member_size
)
i = 0
while ret_val == ida_struct.STRUC_ERROR_MEMBER_NAME:
new_member_name = "%s_%d" % (member_name, i)
i += 1
if i > 250:
return
ret_val = ida_struct.add_struc_member(
struct, new_member_name, offset, flag, mt, member_size
)
if ret_val != 0:
logging.debug(f"ret_val: {ret_val}")
member_ptr = ida_struct.get_member_by_name(struct, new_member_name)
if member_type is not None and member_ptr is not None:
ida_struct.set_member_tinfo(
struct, member_ptr, 0, member_type, idaapi.TINFO_DEFINITE
)
return member_ptr
def set_func_name(func_ea, func_name):
counter = 0
new_name = func_name
while not ida_name.set_name(func_ea, new_name):
new_name = func_name + "_%d" % counter
counter += 1
return new_name
def deref_tinfo(tinfo):
pointed_obj = None
if tinfo.is_ptr():
pointed_obj = tinfo.get_pointed_object()
return pointed_obj
def get_struc_from_tinfo(struct_tinfo):
if ida_hexrays.init_hexrays_plugin() and (
not (struct_tinfo.is_struct() or struct_tinfo.is_union())
):
return None
struct_id = ida_struct.get_struc_id(struct_tinfo.get_type_name())
if struct_id == BADADDR:
return None
struct = ida_struct.get_struc(struct_id)
return struct
def deref_struct_from_tinfo(tinfo):
struct_tinfo = deref_tinfo(tinfo)
if struct_tinfo is None:
return None
return get_struc_from_tinfo(struct_tinfo)
def extract_struct_from_tinfo(tinfo):
struct = get_struc_from_tinfo(tinfo)
if struct is None:
struct = deref_struct_from_tinfo(tinfo)
return struct
def get_member_tinfo(member, member_typeinf=None):
if member_typeinf is None:
member_typeinf = idaapi.tinfo_t()
ida_struct.get_member_tinfo(member_typeinf, member)
return member_typeinf
def get_sptr_by_name(struct_name):
s_id = ida_struct.get_struc_id(struct_name)
return ida_struct.get_struc(s_id)
def get_member_substruct(member):
member_type = get_member_tinfo(member)
if member_type is not None and member_type.is_struct():
current_struct_id = ida_struct.get_struc_id(member_type.get_type_name())
return ida_struct.get_struc(current_struct_id)
elif member.flag & idaapi.FF_STRUCT == idaapi.FF_STRUCT:
return ida_struct.get_sptr(member)
return None
def set_member_name(struct, offset, new_name):
i = 0
ret_val = ida_struct.set_member_name(struct, offset, new_name)
while not ret_val:
formatted_new_name = "%s_%d" % (new_name, i)
i += 1
if i > 250:
return False
ret_val = ida_struct.set_member_name(struct, offset, formatted_new_name)
return True
def get_or_create_struct_id(struct_name, is_union=False):
struct_id = ida_struct.get_struc_id(struct_name)
if struct_id != BADADDR:
return struct_id
struct_id = ida_struct.add_struc(BADADDR, struct_name, is_union)
return struct_id
def get_or_create_struct(struct_name):
struct_id = get_or_create_struct_id(struct_name)
return ida_struct.get_struc(struct_id)
def get_signed_int(ea):
x = idaapi.get_dword(ea)
if x & (1 << 31):
return ((1 << 32) - x) * (-1)
return x
def expand_struct(struct_id, new_size):
struct = ida_struct.get_struc(struct_id)
if struct is None:
logging.warning("Struct id 0x%x wasn't found", struct_id)
return
logging.debug(
"Expanding struc %s 0x%x -> 0x%x",
ida_struct.get_struc_name(struct_id),
ida_struct.get_struc_size(struct_id),
new_size,
)
if ida_struct.get_struc_size(struct_id) > new_size - WORD_LEN:
return
fix_list = []
xrefs = idautils.XrefsTo(struct.id)
for xref in xrefs:
if xref.type == ida_xref.dr_R and xref.user == 0 and xref.iscode == 0:
member, full_name, x_struct = ida_struct.get_member_by_id(xref.frm)
if x_struct is not None:
old_name = ida_struct.get_member_name(member.id)
offset = member.soff
marker_name = "marker_%d" % random.randint(0, 0xFFFFFF)
idc.add_struc_member(
x_struct.id,
marker_name,
member.soff + new_size,
idaapi.FF_DATA | idaapi.FF_BYTE,
-1,
0,
)
logging.debug(
"Delete member (0x%x-0x%x)", member.soff, member.soff + new_size - 1
)
ida_struct.del_struc_members(
x_struct, member.soff, member.soff + new_size - 1
)
fix_list.append(
[
x_struct.id,
old_name,
offset,
idaapi.FF_STRUCT | idaapi.FF_DATA,
struct_id,
new_size,
]
)
else:
logging.warning("Xref wasn't struct_member 0x%x", xref.frm)
ret = add_to_struct(
ida_struct.get_struc(struct_id), None, None, new_size - WORD_LEN
)
logging.debug("Now fix args:")
for fix_args in fix_list:
ret = idc.add_struc_member(*fix_args)
logging.debug("%s = %d", fix_args, ret)
x_struct_id = fix_args[0]
idc.del_struc_member(x_struct_id, ida_struct.get_struc_size(x_struct_id))
def get_curline_striped_from_viewer(viewer):
line = ida_kernwin.get_custom_viewer_curline(viewer, False)
line = ida_lines.tag_remove(line)
return line
strings = None
def refresh_strings():
global strings
strings = idautils.Strings()
def get_strings():
if strings is None:
refresh_strings()
return strings
def get_xrefs_for_string(s, filter_func=None):
"""filter_func(x,s) choose x if True for magic str (s)"""
if filter_func is None:
def filter_func(x, string):
return str(x) == string
filtered_strings = filter(lambda x: filter_func(x, s), get_strings())
strings_xrefs = []
for s in filtered_strings:
xrefs = []
xref = ida_xref.get_first_dref_to(s.ea)
while xref != BADADDR:
xrefs.append(xref)
xref = ida_xref.get_next_dref_to(s.ea, xref)
strings_xrefs.append([str(s), xrefs])
return strings_xrefs
def get_func_ea_by_name(name):
loc = idc.get_name_ea_simple(name)
func = ida_funcs.get_func(loc)
if func is None:
return BADADDR
return func.start_ea
def get_funcs_contains_string(s):
def filter_func(x, string):
return string in str(x)
strings_xrefs = get_xrefs_for_string(s, filter_func)
strings_funcs = []
for found_str, xrefs in strings_xrefs:
funcs = set()
for xref in xrefs:
contained_func = ida_funcs.get_func(xref)
if contained_func is not None:
funcs.add(contained_func)
strings_funcs.append([found_str, funcs])
return strings_funcs
def batchmode(func):
def wrapper(*args, **kwargs):
old_batch = idc.batch(1)
try:
val = func(*args, **kwargs)
except Exception:
raise
finally:
idc.batch(old_batch)
return val
return wrapper
def get_code_xrefs(ea):
xref = ida_xref.get_first_cref_to(ea)
while xref != BADADDR:
yield xref
xref = ida_xref.get_next_cref_to(ea, xref)
def get_enum_const_name(enum_name, const_val):
enum = ida_enum.get_enum(enum_name)
if enum != BADADDR:
const = ida_enum.get_const(enum, const_val, 0, BADADDR)
if const != BADADDR:
return ida_enum.get_const_name(const)
return None
def find_hex_string(start_ea, stop_ea, hex_string):
curr_ea = ida_search.find_binary(
start_ea, stop_ea, hex_string, 16, ida_search.SEARCH_DOWN
)
while curr_ea != BADADDR:
yield curr_ea
curr_ea = ida_search.find_binary(
curr_ea + len(hex_string), stop_ea, hex_string, 16, ida_search.SEARCH_DOWN
)
def force_make_struct(ea, struct_name):
sptr = get_sptr_by_name(struct_name)
if sptr == BADADDR:
return False
s_size = ida_struct.get_struc_size(sptr)
ida_bytes.del_items(ea, ida_bytes.DELIT_SIMPLE, s_size)
return ida_bytes.create_struct(ea, s_size, sptr.id)
@batchmode
def set_name_retry(ea, name, name_func=ida_name.set_name, max_attempts=100):
i = 0
suggested_name = name
while not name_func(ea, suggested_name):
suggested_name = name + "_" + str(i)
i += 1
if i == max_attempts:
return None
return suggested_name
def add_struc_retry(name, max_attempts=100):
i = 0
suggested_name = name
sid = ida_struct.add_struc(BADADDR, suggested_name)
while sid == BADADDR:
suggested_name = name + "_" + str(i)
sid = ida_struct.add_struc(BADADDR, suggested_name)
i += 1
if i == max_attempts:
return None, sid
return suggested_name, sid
def get_selected_range_or_line():
selection, startaddr, endaddr = ida_kernwin.read_range_selection(None)
if selection:
return startaddr, endaddr
else:
return ida_kernwin.get_screen_ea(), None
def refresh_struct(sptr):
# Hack: need to refresh structure so MF_BASECLASS will be updated
member_ptr = add_to_struct(sptr, "dummy")
ida_struct.del_struc_member(sptr, member_ptr.soff)