-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.py
245 lines (163 loc) · 6.95 KB
/
find.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
#!/usr/bin/env python3
import sys
import binaryninja
import logging
import traceback
logging.basicConfig(level=logging.DEBUG)
import queue as Queue
def get_callstack(function, nb_iter=0):
callers = function.callers
if len(callers) == 0:
return ""
if nb_iter > 10:
return ""
paths = []
for caller in callers:
if caller == function:
continue
tmp_path = str(caller) + " -> " + str(function)
all_paths = get_callstack(caller, nb_iter+1)
if len(all_paths) == 0:
paths += [tmp_path]
for path in all_paths:
paths += [path + " -> " + str(function)]
return paths
def backward_slice(xref, op, param_number):
ml = xref.function.mlil
instr = ml[ml.get_instruction_start(op.address)].ssa_form
var_s = instr.vars_read
q = Queue.Queue()
q.put(var_s[param_number])
visited = []
while not q.empty():
var = q.get()
visited += [var]
if type(var) == binaryninja.mediumlevelil.SSAVariable:
temp = [ml.get_ssa_var_definition(var)]
else:
temp = ml.get_var_definitions(var)
for t in temp:
if t is not None:
print(f"Var {t.dest} reads from {t.src}")
if t.src.operation == binaryninja.MediumLevelILOperation.MLIL_LOAD and hasattr(t.src.src, "constant"):
param_ptr = t.src.src.constant
read_string_argument(param_ptr)
assert(len(t.ssa_form.src.vars_read) == 0)
return True
data = t.ssa_form.vars_read
for j in data:
if j not in visited:
q.put(j)
return False
def explore_children(op):
operands = Queue.Queue()
operands.put(*op.operands)
# the call is apparently located in a child operation
while not operands.empty():
o = operands.get()
operands.put(*o.operands)
if hasattr(o, "operation") and o.operation == binaryninja.HighLevelILOperation.HLIL_CALL:
op = o
break
else:
raise Exception("Cannot find the function call O.o")
return op
def is_relevant_bb(basic_block, function_name):
for line in basic_block.disassembly_text:
if function_name in str(line):
logging.debug(str(line))
return True
return False
def is_relevant_line(instruction, function_name):
for line in instruction.lines:
if function_name.replace("sub_", "") in str(line):
return True
return False
def read_string_argument(param_ptr):
string_value = bv.get_string_at(param_ptr)
if string_value is not None:
logging.info(
f"LoadLibrary param is {string_value.value.encode('utf-8', errors='ignore')}")
elif param_ptr != 0:
a = bv.get_strings(param_ptr)
weird_string = bv.read(param_ptr, 1).decode('utf-8') + a[0].value
logging.info(f"LoadLibrary param is {weird_string}")
else:
raise Exception(f"Exception: String value is None @ {hex(param_ptr)}")
return True
def find_param_to_fn_call_xref(xref, param_number=0, recursion_level=0, function_name="LoadLibrary"):
logging.debug(f"Analyzing {xref.function}, call stack depth={recursion_level}")
blocks = list(xref.function.high_level_il)
found_instruction = False
for basic_block in blocks:
if not is_relevant_bb(basic_block, function_name):
continue
for x in basic_block:
if found_instruction:
break
if not is_relevant_line(x, function_name):
continue
try:
for op in x.operands:
if found_instruction:
break
# handle function call in "if" statements or other similar cases
if not hasattr(op, "operation") or op.operation != binaryninja.HighLevelILOperation.HLIL_CALL:
if not hasattr(op, "lines") or not function_name.replace("sub_", "") in str(op.lines[0]):
continue
# function call is located further down the AST
op = explore_children(op)
found_instruction = True
# The function argument is a variable
if len(op.params) > param_number and not hasattr(op.params[param_number], "constant"):
# The variable is set by another function, climb up the call stack
if hasattr(op.params[0], "var") and "arg" in op.params[param_number].var.name:
nb_param = int(op.params[param_number].var.name.split("arg")[1]) - 1
logging.debug(f"Look for param number {nb_param} in any caller function.")
for xrf in bv.get_callers(xref.function.start):
logging.debug(f"That function is called at @ {hex(xrf.address)}")
found_instruction |= find_param_to_fn_call_xref(xrf, nb_param, recursion_level+1, xref.function.name)
if found_instruction:
break
found_instruction |= backward_slice(xref, op, param_number)
# The function argument is a constant string
elif hasattr(op.params[param_number], "constant"):
param_ptr = op.params[param_number].constant
found_instruction |= read_string_argument(param_ptr)
except Exception as e:
traceback.print_exc()
logging.error("Exception: " + str(e))
if found_instruction:
break
return found_instruction
if __name__ == "__main__":
if len(sys.argv) < 2:
logging.error("Usage: find.py /path/to/bin")
target = sys.argv[1]
bv = binaryninja.BinaryViewType.get_view_of_file(target, update_analysis=True)
#a = bv.get_symbols_by_name("LoadLibraryA")
a = bv.get_symbols()
addresses = []
success = 0
errors = 0
for symbol in a:
if symbol.type == binaryninja.SymbolType.ImportAddressSymbol:
if "LoadLibrary" in symbol.name:
logging.info(f"Found {symbol.name} @ {hex(symbol.address)}")
addresses += [symbol.address]
for addr in addresses:
xrefs = bv.get_code_refs(addr)
logging.debug(xrefs)
for xref in xrefs:
res = find_param_to_fn_call_xref(xref)
if res:
cs = get_callstack(xref.function)
if len(cs) == 0:
errors += 1
continue
success += 1
logging.info("Success. Here is the callstack:")
logging.info(cs)
else:
errors += 1
logging.info(f"Done. {success} successes, {errors} errors.")