-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathset_up_watch_points.py
162 lines (143 loc) · 4.05 KB
/
set_up_watch_points.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
import idautils
import idc
'''
api usage:
addrs_names_d = get_all_names()
flow_infoz = {}
for addr in addrs_names_d:
name = addrs_names_d[addr]
flow_infoz[name] = get_flow_to_name(name)
'''
def merge(d1,d2):
df = {}
if len(d2) == 0:
return d1
elif len(d1) == 0:
return d2
for i in d1:
df[i] = d1[i]
for i in d2:
df[i] = d2[i]
return df
def get_names(start, end):
names = {}
for addr in xrange(start, end):
name = idc.Name(addr)
if name != "":
names[addr] = name
return names
def get_all_segments():
segments = []
seg = idc.FirstSeg()
while seg != idc.BADADDR:
segments.append((seg, idc.SegEnd(seg)))
seg = idc.NextSeg(seg)
return segments
auto_names = set(['sub','loc','unk','dword','qword','byte','word','off',])
def filter_inclusive_names(names, filter=[]):
notfilered_names = {}
if isinstance(filter, str):
filter = [filter,]
for i in names:
if len(names[i]) == 0:
continue
name = names[i]
filter_this = True
for f in filter:
if name.find(f) > -1:
filter_this = False
break
if not filter_this:
notfilered_names[i] = names[i]
return notfilered_names
def filter_exclusive_names(names, filter=[]):
notfilered_names = {}
if isinstance(filter, str):
filter = [filter,]
for i in names:
if len(names[i]) == 0:
continue
name = names[i]
filter_this = False
for f in filter:
if name.find(f) > -1:
filter_this = True
break
if not filter_this:
notfilered_names[i] = names[i]
return notfilered_names
def get_all_names():
names = {}
segments = get_all_segments()
for i in segments:
t = get_names(*i)
names = merge(names, t)
return names
get_name_to_name = lambda x: x.strip().split('#')[1].strip()
get_addr_to_addr = lambda x: x.strip().split('#')[0].strip()
def get_name(addr):
'''
trys to get the named value by function and offset
other wise return %segment%:addr
'''
name = idc.GetFuncOffset(addr)
if not name is None:
return name
name = idc.SegName(name)
if not name is None:
return name+"%08x"%addr
return idc.BADADDR
get_import_flow_information = lambda x: [ '%s ==> %s'%(idc.GetFuncOffset(xref.frm), idc.Name(xref.to), ) for xref in idautils.XrefsTo(LocByName(x)) if xref.iscode]
get_name_flow_info = lambda x: [ '0x%08x ==> 0x%08x # %s ==> %s '%(xref.frm, xref.to, get_name(xref.frm), idc.Name(xref.to) ) for xref in idautils.XrefsTo(idc.LocByName(x)) if xref.iscode]
get_addr_flow_info = lambda x: [ '0x%08x ==> 0x%08x # %s ==> %s '%(xref.frm, xref.to, get_name(xref.frm), idc.Name(xref.to) ) for xref in idautils.XrefsTo(x) if xref.iscode]
idx = 0
def get_flow_to_name(name):
global idx
mflow_addrs = []
mflow_names = []
print "In call %x Name: %s"%(idx,name)
idx+=1
for flow in get_name_flow_info(name):
print flow
a2a = get_addr_to_addr(flow)
n2n = get_name_to_name(flow)
src = a2a.split()[0].strip()
# arrows at 1
dst = a2a.split()[2].strip()
sname = idc.GetFunctionName(int(src,16))
print "Obtaining flow information for: ", sname
flow_addrs, flow_names = get_flow_to_name(sname)
print "Obatained the following flow info for %s: %s"%(sname,str(flow_names))
if len(flow_addrs) == 0:
mflow_addrs.append(a2a)
if len(flow_names) == 0:
mflow_names.append(n2n)
for i in flow_addrs:
mflow_addrs.append(i + " ==> " + a2a)
for i in flow_names:
mflow_names.append(i + " ==> " + n2n)
idx -= 1
return [i for i in set(mflow_addrs)], [i for i in set(mflow_names)]
def merge_flows(flows):
flows.sort()
pruned_flows = set()
for flow in flows:
if not flow in pruned_flows:
cnt = 0
found = False
pflows = list(pruned_flows)
while cnt < len(pflows):
f = pflows[cnt]
if len(flow) < len(f) and f.find(flow):
found = True
break
elif len(f) < len(flow) and flow.find(f):
found = True
break
elif f == flow:
found = True
break
cnt += 1
if not found:
pruned_flows.add(flow)
return list(pruned_flows)