-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconst_adder.py
52 lines (40 loc) · 1.02 KB
/
const_adder.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
import idaapi, idc, idautils
import re
def get_c_string(ea):
s = ""
while True:
if not idaapi.is_loaded(ea):
break
c = idaapi.get_byte(ea)
if c == 0 or c == 0xff:
break
s += chr(c)
ea += 1
return s
def add_consts():
r = re.compile("char\[([0-9]+)\]")
tif = idaapi.tinfo_t()
for segea in idautils.Segments():
segname = idc.get_segm_name(segea)
if segname != ".data":
print("skipping segment", segname)
continue
segstart = idc.get_segm_start(segea)
segend = idc.get_segm_end(segea)
for ea in range(segstart, segend):
# I dont know what third arg is supposed to be, 3 just works :)))
if not idaapi.get_type(ea, tif, 3):
continue
if idaapi.get_name(ea) == '':
continue
type_string = str(tif)
if type_string != "char []" and r.match(type_string):
continue
cstr = get_c_string(ea)
if len(cstr) < 3:
continue
print("found at", hex(ea), tif, cstr)
new_const_type = "const " + str(tif)
idc.SetType(ea, new_const_type)
if __name__ == "__main__":
add_consts()