-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
390 lines (311 loc) · 12.1 KB
/
util.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
from typing import List, Set, Dict, Tuple, Optional, Union
from collections import defaultdict
from androguard.core.analysis.analysis import (
Analysis,
ClassAnalysis,
ExternalMethod,
MethodAnalysis,
FieldClassAnalysis,
MethodClassAnalysis,
)
from androguard.core.bytecodes.dvm import EncodedMethod, DalvikCode, Instruction, Instruction11n, Instruction21c
import allocator_util
import networkx as nx
from callback_list import callback_list as android_callback_interfaces
EncodedClearsSets = Tuple[List[EncodedMethod], List[EncodedMethod]]
ClearsSets = Tuple[List[MethodClassAnalysis], List[MethodClassAnalysis]]
StaticFieldWriteInfo = Dict[FieldClassAnalysis, ClearsSets]
STATIC_FLAG = 0x8
SPUT_OBJECT_CODE = 105
CONST_4_CODE = 18
class JavaMethod(object):
def __init__(self, name):
self.name = name
self.args = list()
self.ret_type = None
def add_arg(self, arg):
self.args.append(arg)
def __repr__(self):
return "Method " + self.name + ", Args " + str(self.args)
class JavaInterface(object):
def __init__(self, name):
self.name = name
self.methods = list()
def add_method(self, method):
self.methods.append(method)
def __repr__(self):
return self.name + ": " + str(self.methods)
# format of class name as stored in decompilation
def format_activity_name(name):
return "L" + name.replace(".", "/") + ";"
# search AST for a specific method name
def get_method_from_ast(methodname: str, classname: str, ast: Dict):
try:
class_ast = ast[classname]
except KeyError:
return None
for method in class_ast["methods"]:
if method["triple"][1] == methodname:
return method
for interface in class_ast['interfaces']:
interface_name, _ = interface[1]
answer = get_method_from_ast(methodname, format_activity_name(interface_name), ast)
if answer:
return answer
return None
# recursive search of AST for checking the type of a symbol
def get_ast_type(ast):
if isinstance(ast, str):
return ast
try:
node_type = ast[0]
except Exception:
# we have an ill-formed AST
return None
if node_type == "MethodInvocation":
return get_ast_type(ast[2][2])
if node_type == "FieldAccess":
return get_ast_type(ast[2][2])
if node_type == "ClassInstanceCreation":
# ClassInstanceCreation has the format:
# [
# 'ClassInstanceCreation',
# arg1_ast, arg2_ast, ...,
# ['TypeName', (actual_type_string, integer)],
# ]
return ast[2][1][0]
if node_type == "Cast":
return get_ast_type(ast[1][0])
if node_type == "Local":
return "UNRESOLVED LOCAL"
if node_type == "TypeName":
return get_ast_type(ast[1][0])
if node_type == "Literal":
return get_ast_type(ast[2][0])
if node_type == "Parenthesis":
# unwrap parens
return get_ast_type(ast[1][0])
if node_type == "ExpressionStatement":
return "UNRESOLVED EXPR STMT"
return "failed, no case match"
# get the types of all args passed to a method
def get_method_arg_type(method_ast):
if len(method_ast[1]) > 1:
return list(map(get_ast_type, method_ast[1][1:]))
else:
# there's no method arguments
return "No args"
def get_method_invocations(ast):
if not isinstance(ast, list) or len(ast) == 0:
return []
ret = []
if ast[0] == "MethodInvocation":
ret.append(ast)
ret.extend(get_method_invocations(ast[1:]))
else:
for item in ast:
if isinstance(item, list):
ret.extend(get_method_invocations(item))
return ret
def get_callback_list(mca: MethodClassAnalysis, dx: Analysis, ast: Dict, callback_interfaces: List):
classname: str = mca.method.class_name
methodname: str = mca.name
method_ast = get_method_from_ast(methodname, classname, ast)
if method_ast:
method_ast_body = method_ast['body']
else:
return []
method_invocs = get_method_invocations(method_ast_body)
registered_callbacks = []
for invoc in method_invocs:
arg_types = get_method_arg_type(invoc)
for typ in arg_types:
cls: ClassAnalysis = dx.get_class_analysis(typ)
if isinstance(cls, ClassAnalysis):
for interface in cls.implements:
if interface in callback_interfaces:
print(
"Found registered resource: ", typ, "implements ", interface
)
registered_callbacks.append((typ, interface))
return registered_callbacks
def link_callbacks(mca: MethodClassAnalysis, dx: Analysis, ast: Dict, cg: nx.DiGraph):
android_api_callbacks = get_cb_methods()
invoked_callback_registers = get_callback_list(mca, dx, ast, android_callback_interfaces)
def is_android_api_callback(callback_tuple):
_, interface = callback_tuple
if interface in android_api_callbacks:
return True
return False
invoked_callback_registers = filter(is_android_api_callback, invoked_callback_registers)
found_methods = list()
for cb_typ, cb_interface in invoked_callback_registers:
java_interface: JavaInterface = android_api_callbacks[cb_interface]
interface_method: JavaMethod
# Try to find the analysis for the interface methods.
# This should be successful if an interface method is
# used by any user code, but I'll need to verify edge cases.
for interface_method in java_interface.methods:
gen = dx.find_methods(
classname=cb_typ, methodname=".*{}.*".format(interface_method.name)
)
analysis: MethodClassAnalysis
found = False
for item in gen:
analysis = item
found = True
if found:
found_methods.append(analysis.method)
for method_enc in found_methods:
print("adding edge: ", mca.name, " -> ", method_enc.name)
cg.add_edge(mca.method, method_enc)
def get_cb_methods():
interface_to_methods = {}
with open("./parseInterfaces/output/CallbackMethods.txt", "r") as f:
line: str = f.readline()
curr_interface = None
curr_method = None
while line:
line = line.strip()
if line == "INTERFACE":
interface_name = f.readline().strip()
curr_interface = JavaInterface(interface_name)
interface_to_methods[interface_name] = curr_interface
elif line == "METHOD":
curr_method = JavaMethod(f.readline().strip())
curr_interface.add_method(curr_method)
elif line == "RETURN TYPE":
curr_method.ret_type = f.readline().strip()
elif line == "ARG TYPE":
curr_method.add_arg(f.readline().strip())
line = f.readline()
return interface_to_methods
def find_method(analysis: ClassAnalysis, name: str) -> MethodClassAnalysis:
meth: MethodClassAnalysis
for meth in analysis.get_methods():
if meth.name == name:
return meth
return None
def add_cg_link(cg: nx.DiGraph,
m1: EncodedMethod,
m2: EncodedMethod):
if m1 and m2:
cg.add_edge(m1, m2)
def link_to_exit_methods(cg: nx.DiGraph,
m: EncodedMethod,
exit_methods: List[EncodedMethod]):
for method in exit_methods:
add_cg_link(cg, m, method)
def print_allocation_path(path: List[EncodedMethod]):
print("PATH: ")
for item in path[:-1]:
print(item.name)
print("↓")
print(path[-1])
print()
def print_field_set_path(path: List[EncodedMethod], field: FieldClassAnalysis):
print("FIELD PATH ({}): ".format(field.name))
for item in path[:-1]:
print(item.name)
print("↓")
print(path[-1])
print()
def get_entrypoints(methods: List[MethodClassAnalysis]):
entrypoints: List[MethodClassAnalysis] = []
mca: MethodClassAnalysis
for mca in methods:
if mca.name[:2] == "on":
entrypoints.append(mca)
return entrypoints
def get_opener_paths(cg: nx.DiGraph, main_mcas: List[MethodClassAnalysis], pair: allocator_util.Pair):
entrypoints = get_entrypoints(main_mcas)
opener_paths: List[List[EncodedMethod]] = []
for entrypoint in entrypoints:
for opener in pair.openers:
try:
ssp = nx.shortest_simple_paths(cg, entrypoint.method, opener.method)
opener_paths.extend([x for x in ssp])
except nx.exception.NetworkXNoPath:
pass
return opener_paths
def path_exists(cg: nx.DiGraph,
path: List[EncodedMethod],
pair: allocator_util.Pair,
exitpoints: List[EncodedMethod]):
node: EncodedMethod
# check if a closer is called directly from the same path
for node in path:
for closer in pair.closers:
try:
ssp = nx.shortest_simple_paths(cg, node, closer.method)
closing_paths = [x for x in ssp]
if closing_paths:
return True
except nx.exception.NetworkXNoPath:
pass
# check if a closer is called from an exitpoint
for exitpoint in exitpoints:
for closer in pair.closers:
try:
ssp = nx.shortest_simple_paths(cg, exitpoint, closer.method)
if [x for x in ssp]:
return True
except nx.exception.NetworkXNoPath:
pass
return False
def process_paths(cg: nx.DiGraph,
opener_paths: List[List[EncodedMethod]],
pair: allocator_util.Pair,
exitpoints: List[EncodedMethod]):
open_paths = []
closed_paths = []
for path in opener_paths:
if path_exists(cg, path, pair, exitpoints):
closed_paths.append(path)
else:
open_paths.append(path)
return open_paths, closed_paths
def filter_with_cg(paths: List[List[EncodedMethod]], cg_filter: nx.DiGraph):
return list(filter(lambda x: cg_filter.has_node(x[-2]), paths))
def is_static(field: FieldClassAnalysis):
return STATIC_FLAG & field.get_field().access_flags == STATIC_FLAG
def field_cleared(method: EncodedMethod, field: FieldClassAnalysis) -> bool:
"""Approximate if this field is set to null in the given method"""
instructions: List[Instruction] = [x for x in method.get_instructions()]
# map of register num to its literal value given
register_vals = defaultdict(lambda: -1)
# the register used to set the object value
obj_val = -1
for instruction in instructions:
if instruction.get_op_value() == CONST_4_CODE:
instruction: Instruction11n
register_vals[instruction.A] = instruction.B
elif instruction.get_op_value() == SPUT_OBJECT_CODE:
instruction: Instruction21c
if instruction.BBBB == field.get_field().field_idx:
obj_val = register_vals[instruction.AA]
if obj_val == 0:
return True
return False
def check_writes(field: FieldClassAnalysis) -> EncodedClearsSets:
clears = []
sets = []
method: EncodedMethod
for _, method in field.get_xref_write():
if field_cleared(method, field):
clears.append(method)
else:
sets.append(method)
return clears, sets
def decode_clear_sets(cs: EncodedClearsSets, dx: Analysis) -> ClearsSets:
clears, sets = cs
clears: List[MethodClassAnalysis] = [dx.get_method_analysis(x) for x in clears]
sets: List[MethodClassAnalysis] = [dx.get_method_analysis(x) for x in sets]
return clears, sets
def get_static_fields(ca: ClassAnalysis, dx: Analysis) -> StaticFieldWriteInfo:
field_to_writes: StaticFieldWriteInfo = {}
fields = [x for x in ca.get_fields()]
field: FieldClassAnalysis
for field in filter(is_static, fields):
field_to_writes[field] = decode_clear_sets(check_writes(field), dx)
return field_to_writes