-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.py
executable file
·129 lines (113 loc) · 3.42 KB
/
analyze.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
#!/usr/bin/python
import sys
import r2pipe
def usage():
print("Dipendency analyzer")
print("usage: {} BIN START_FUNCTION".format(sys.argv[0]))
print("\tBIN is analysis target")
print("\tSTART_FUNCTION is a function to start symbolic exection")
exit(1)
if len(sys.argv) < 3:
usage()
BIN = sys.argv[1]
bin_file_name = BIN.split('/')[-1]
START_FUNC = sys.argv[2]
if not START_FUNC.startswith('sym.'):
START_FUNC = "sym." + START_FUNC
r2 = r2pipe.open(BIN)
r2.cmd("aaa")
cfg = r2.cmdj("agCj")
calee = set()
xrefs = {}
def add_calle(cfg, func):
global calee
for x in cfg:
if x['name'] == func:
for f in x['imports']:
if 'sym.imp.' in f:
continue
if f not in calee:
calee.add(f)
add_calle(cfg, f) # add callee functions of f
### dependent functions
add_calle(cfg, START_FUNC)
print(calee)
ret = r2.cmdj("aflj")
symbols_lookup_table = {}
for x in ret:
if 'name' in x:
if x['name'] in calee:
offset = x['offset']
size = x['size']
symbols_lookup_table[(offset, offset + size)] = x['name']
# print(symbols_lookup_table)
### dependent objects
def translate(addr):
global symbols_lookup_table
for x in symbols_lookup_table.keys():
begin, end = x
if begin <= addr and addr < end:
return symbols_lookup_table[x]
return None
dependent_objs = set()
res = r2.cmd("ax | grep 'data mem'")
for x in res.split('\n'):
x = x.strip().split('->')
ref_from, ref_to = x[0].strip(), x[2].strip()
if 'str.' in ref_from:
continue
if 'reloc.' in ref_from:
continue
# if 'obj.' in ref_from:
if True:
for y in calee:
ref_to_addr = int(ref_to.split(' ')[0], 16)
if y in ref_to or (y == translate(ref_to_addr)):
objname = ref_from.split(' ')[0]
dependent_objs.add(objname)
# dependent_objs.add(ref_from)
print(dependent_objs)
### gather dependent symbols information
symbols = {}
isj_ret = r2.cmdj("isj")
for x in isj_ret:
flagname = x['flagname']
# if flagname in calee or flagname in dependent_objs:
if flagname in dependent_objs:
symbols[flagname] = x
# print(symbols)
# for x in symbols.values():
# print("%s\t%#x" % (x['flagname'], x['vaddr']))
# fdump = open("fetch-memory.py", "w")
# fdump.write("""### NOTE: THIS IS A GENERATED SCRIPT BY analyze.py
# ### NOTE: run this script in gdb, NOT shell.
# import gdb
# import json
# def do_dump(addr, size):
# if size % 4:
# size += 4 - (size % 4)
# addr = '\\'' + addr + '\\''
# if not addr.isdigit():
# addr = '&' + addr
# o = gdb.execute('x/{size:d}wx {addr!s}'.format(addr=addr, size=int(size / 4)), to_string=True)
# o = o.strip()
# vals = []
# for x in o.split('\\n'):
# v = x.split(':')[1].strip().split('\t')
# for y in v:
# vals.append(int(y, 16))
# return vals
# dump = {}
# """)
# for x in dependent_objs:
# name = symbols[x]['name']
# addr = symbols[x]['name']
# size = symbols[x]['size']
# fdump.write("dump['{name}'] = do_dump('{addr}', {size})\n".format(name=name, addr=addr, size=size))
# fdump.write("""
# print(dump)
# with open("{dump_name}.dump", "w") as f:
# json.dump(dump, f)
# print('[*] memory dump done! Go on your analysis!')
# """.format(dump_name=BIN))
# fdump.close()