-
Notifications
You must be signed in to change notification settings - Fork 18
/
mocks.py
398 lines (307 loc) · 13.7 KB
/
mocks.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
391
392
393
394
395
396
397
398
import logging
from utils import *
from base64 import b64decode, urlsafe_b64decode
from helpers import string_hash_code
handler = LogHandler()
log = logging.getLogger(__name__)
log.addHandler(handler)
log.setLevel(logging.INFO)
# stores state data for in between calls
state_data = {}
def dump_string(string: str, vm):
log.info(string, extra={"type": LOG_TYPE_STRING, "fqfn": vm.get_fqfn(vm.call_stack[0])})
def Landroid_util_Base64_decode(params: list, vm, v: list):
# add missing padding because python has very strong opinions about this
if type(v[params[0]]) is list:
v[params[0]] += [61] * (-len(v[params[0]]) % 4)
else:
v[params[0]] += '=' * (-len(v[params[0]]) % 4)
# also sanitize the input by turning everything to bytes
try:
if len(params) == 1 or v[params[1]] == 0:
try:
vm.memory.last_return = list(b64decode(bytes(v[params[0]])))
except:
# maybe switch this around?
vm.memory.last_return = list(b64decode(v[params[0]]))
else:
# check for URL safe base64 decode (that's how people usually use the flag)
vm.memory.last_return = list(urlsafe_b64decode(bytes(v[params[0]])))
except:
# pokemoning the exception for the fallback for weird string formats
vm.memory.last_return = list(b64decode("".join([chr(x) for x in v[params[0]]])))
def Landroid_view_TextUtils_isEmpty(params: list, vm, v: list):
try:
vm.memory.last_return = v[params[0]] is None or len(v[params[0]]) == 0
except Exception:
vm.memory.last_return = 0
def Landroid_text_TextUtils_isEmpty(params: list, vm, v: list):
try:
vm.memory.last_return = v[params[0]] is None or len(v[params[0]]) == 0
except Exception:
vm.memory.last_return = 0
def Ljava_io_ByteArrayOutputStream_0init0(params: list, vm, v: list):
v[params[0]] = []
def Ljava_io_ByteArrayOutputStream_write(params: list, vm, v: list):
v[params[0]].append(v[params[1]])
def Ljava_io_ByteArrayOutputStream_toByteArray(params: list, vm, v: list):
vm.memory.last_return = v[params[0]]
def Ljava_lang_Object_hashCode(params: list, vm, v: list):
vm.memory.last_return = string_hash_code(v[params[0]].decode("utf-8"))
def Ljava_lang_String_hashCode(params: list, vm, v: list):
h = 0
for c in v[params[0]]:
h = int((((31 * h + ord(c)) ^ 0x80000000) & 0xFFFFFFFF) - 0x80000000)
vm.memory.last_return = h
def Ljava_lang_String_0init0(params: list, vm, v: list):
# print(v[params[0]])
# print(v[params[1]])
try:
v[params[0]] = bytearray(v[params[1]]).decode("utf-8")
except ValueError as ve:
# got negative bytes (which somehow work in java land), need to strip the sign
ret = []
for b in v[params[1]]:
if b < 0:
b += 0xFF + 1
ret.append(b)
v[params[0]] = bytearray(ret).decode("utf-8", "ignore")
# print(v[params[0]])
dump_string(f"{v[params[0]]}", vm);
def Ljava_lang_String_charAt(params: list, vm, v: list):
try:
vm.memory.last_return = ord(v[params[0]].decode("utf-8", "surrogatepass")[v[params[1]]])
except AttributeError as ae:
try:
vm.memory.last_return = ord(v[params[0]][v[params[1]]])
except TypeError as te:
vm.memory.last_return = v[params[0]][v[params[1]]]
def Ljava_lang_String_split(params: list, vm, v: list):
vm.memory.last_return = str(v[params[0]]).split(str(v[params[1]]))
def Ljava_lang_String_equals(params: list, vm, v: list):
vm.memory.last_return = v[params[0]] == v[params[1]]
def Ljava_lang_String_length(params: list, vm, v: list):
# TODO: remove catch all
try:
vm.memory.last_return = len(v[params[0]].decode("utf-8"))
except AttributeError as ex:
# TODO: remove catch all
try:
vm.memory.last_return = len(v[params[0]])
except:
vm.memory.last_return = 0
except Exception as ex:
vm.memory.last_return = 0
def Ljava_lang_String_indexOf(params: list, vm, v: list):
# TODO: account for char substring
try:
vm.memory.last_return = v[params[0]].find(chr(v[params[1]]))
except TypeError as te:
# sometimes we get substrings, sometimes we get char codes
vm.memory.last_return = str(v[params[0]]).find(str(v[params[1]]))
except Exception as ex:
# TODO: remove catch all
vm.memory.last_return = 0
def Ljava_lang_String_valueOf(params: list, vm, v: list):
try:
vm.memory.last_return = chr(v[params[0]])
except:
vm.memory.last_return = bytes(bytearray(v[params[0]][v[params[1]]:v[params[1]]+v[params[2]]])).decode('ascii')
dump_string(vm.memory.last_return, vm)
def Ljava_lang_Integer_valueOf(params: list, vm, v: list):
vm.memory.last_return = int(v[params[0]])
def Ljava_lang_Integer_intValue(params: list, vm, v: list):
vm.memory.last_return = int(v[params[0]])
def Ljava_lang_String_toLowerCase(params: list, vm, v: list):
vm.memory.last_return = v[params[0]].lower()
def Ljava_lang_String_getBytes(params: list, vm, v: list):
# TODO: standardize string passing across methods
try:
vm.memory.last_return = list(v[params[0]].encode("utf-8"))
except:
vm.memory.last_return = list(v[params[0]])
def Ljava_lang_String_toCharArray(params: list, vm, v:list):
temp = []
for c in v[params[0]]:
if type(c) is str:
temp.append(ord(c))
else:
temp.append(c)
# print(temp)
# TMP = []
# for i in range(0, len(old), 2):
# TMP.append(old[i] | (old[i+1] << 8))
# print(TMP)
vm.memory.last_return = temp
def Ljava_lang_StringBuilder_0init0(params: list, vm, v: list):
if len(params) > 1:
if isinstance(v[params[1]], list):
v[params[0]] = ''.join(chr(i) for i in v[params[1]])
if isinstance(v[params[1]], str):
v[params[0]] = v[params[1]]
else:
v[params[0]] = ''
dump_string(f"{v[params[0]]}", vm)
def Ljava_lang_StringBuilder_append(params: list, vm, v: list):
# TODO: maybe find a more elegant solution
try:
v[params[0]] = str(v[params[0]]) + chr(v[params[1]])
except TypeError as te:
try:
v[params[0]] = str(v[params[0]]) + v[params[1]].decode("utf-8")
except AttributeError as ae:
v[params[0]] = str(v[params[0]]) + str(v[params[1]])
vm.memory.last_return = v[params[0]]
def Ljava_lang_StringBuilder_length(params: list, vm, v: list):
try:
vm.memory.last_return = len(v[params[0]])
except Exception:
# TODO: remove catch all
vm.memory.last_return = 0
def Ljava_lang_StringBuilder_toString(params: list, vm, v: list):
# print(v[params[0]])
vm.memory.last_return = v[params[0]]
dump_string(f"{v[params[0]]}", vm)
log.info(f"String created: {v[params[0]]}")
def Ljava_lang_StringBuffer_0init0(params: list, vm, v: list):
if isinstance(v[params[1]], list):
v[params[0]] = ''.join(chr(i) for i in v[params[1]])
if isinstance(v[params[1]], str):
v[params[0]] = v[params[1]]
dump_string(f"{v[params[0]]}", vm);
def Ljava_lang_StringBuffer_toString(params: list, vm, v: list):
vm.memory.last_return = v[params[0]]
def Ljava_util_Iterator_hasNext(params: list, vm, v: list):
vm.memory.last_return = False
def Ljava_util_ArrayList_0init0(params: list, vm, v: list):
v[params[0]] = []
def Ljava_util_ArrayList_size(params: list, vm, v: list):
vm.memory.last_return = len(v[params[0]])
def Ljava_util_ArrayList_add(params: list, vm, v: list):
# hack to quickly whip up a list
if not v[params[0]]:
v[params[0]] = []
v[params[0]].append(v[params[1]])
def Ljava_util_ArrayList_get(params: list, vm, v: list):
vm.memory.last_return = v[params[0]][v[params[1]]]
def Ljava_util_List_0init0(params: list, vm, v: list):
v[params[0]] = []
def Ljava_util_List_size(params: list, vm, v: list):
if isinstance(v[params[0]], list):
vm.memory.last_return = len(v[params[0]])
else:
vm.memory.last_return = 0
def Ljavax_crypto_spec_SecretKeySpec_0init0(params: list, vm, v: list):
state_data['aes_key'] = v[params[1]]
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
def Ljavax_crypto_Cipher_doFinal(params: list, vm, v:list):
# TODO: CLEANUP + CBC
key = bytearray(state_data['aes_key'])
cipher_text = bytearray(v[params[1]])
iv = b'\00'*16
# most common two modes are CBC and ECB try one and then the other
# won't bother with IVs for now
try:
cipher = AES.new(key, AES.MODE_ECB)
original_data = unpad(cipher.decrypt(cipher_text), AES.block_size)
# TODO: POKEMON!
except:
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
original_data = unpad(cipher.decrypt(cipher_text), AES.block_size)
dump_string(f"String decrypted: {original_data.decode('utf-8')} with key {key.decode('utf-8')}", vm)
vm.memory.last_return = original_data
def Ljava_lang_System_arraycopy(params: list, vm, v:list):
v[params[2]][v[params[3]]:v[params[1]]+v[params[4]]] = v[params[0]][v[params[1]]:v[params[1]]+v[params[4]]]
def try_to_mock_method(method_idx: int, params: list, vm, v) -> bool:
class_name: str = vm.dex.method_ids[method_idx].class_name
method_name: str = vm.dex.method_ids[method_idx].method_name
log.debug("Translating method: %s->%s with %s" % (
class_name, method_name, [str(v[param])[0:8] for param in params]))
fqcn = class_name.replace('/', '_').replace(';', '') + '_' + method_name.replace('<', '0').replace('>', '0')
fp = globals().get(fqcn, None)
if fp:
try:
# print(fp)
# print("mock before", params, v, params[1], len(v))
fp(params, vm, v)
except Exception as ex:
log.error("Could not execute mock for %s->%s(%s): %s" % (class_name, method_name, [str(v[param])[0:8] for param in params], ex))
return False
elif class_name == "Landroid/view/Display;":
vm.memory.last_return = 0
return False
else:
if any([x in method_name for x in ["Int", "Long", "Float"]]) and "get" in method_name:
vm.memory.last_return = 0
return False
if "String" in method_name and "get" in method_name and len(method_name) > 9:
vm.memory.last_return = "None"
return False
if "Array" in method_name and "get" in method_name:
vm.memory.last_return = []
# print("????????????????????")
return True
def Ljava_lang_Class_forName(params: list, vm, v:list):
vm.memory.last_return = v[params[0]]
def Ljava_lang_Class_getMethod(params: list, vm, v:list):
vm.memory.last_return = [v[params[0]], v[params[1]]]
def Ljava_lang_StackTraceElement_getClassName(params: list, vm, v:list):
vm.memory.last_return = v[params[0]]["class_name"]
def Ljava_lang_Thread_currentThread(params: list, vm, v:list):
vm.memory.last_return = "CURRENT_THREAD"
def Ljava_lang_Thread_getStackTrace(params: list, vm, v:list):
if v[params[0]] == "CURRENT_THREAD":
st = [{"class_name": "java.lang.Thread", "method_name": "getStackTrace"}]
for m_id in vm.call_stack[::-1]:
st.append({"class_name": vm.dex.method_ids[m_id].class_name[1:-1].replace("/","."), "method_name": vm.dex.method_ids[m_id].method_name})
vm.memory.last_return = st
def Ljava_lang_reflect_Method_invoke(params: list, vm, v:list):
try:
t_class_name = v[params[0]][0].replace('.', '/')
except:
t_class_name = "None"
log.error("Error solving reflection! Register state: " + str(v))
t_method_name = v[params[0]][1]
full_name = f"L{t_class_name};->{t_method_name}"
for index, method in enumerate(vm.dex.method_ids):
if f"{method.class_name}->{method.method_name}" not in full_name:
continue
class_name: str = vm.dex.method_ids[index].class_name
method_name: str = vm.dex.method_ids[index].method_name
log.debug("Invoke Translating method: %s->%s with %s" % (
class_name, method_name, [str(v[param]) for param in params]))
fqcn = class_name.replace('/', '_').replace(';', '') + '_' + method_name.replace('<', '0').replace('>', '0')
fp = globals().get(fqcn, None)
if fp:
try:
fp(params[1:], vm, v)
except Exception as ex:
log.error("Could not execute mock for %s->%s(%s): %s" % (class_name, method_name, [str(v[param])[0:8] for param in params], ex))
return False
elif class_name == "Landroid/view/Display;":
vm.memory.last_return = 0
return False
else:
if any([x in method_name for x in ["Int", "Long", "Float"]]) and "get" in method_name:
vm.memory.last_return = 0
return False
if "String" in method_name and "get" in method_name and len(method_name) > 9:
vm.memory.last_return = "None"
return False
if "Array" in method_name and "get" in method_name:
vm.memory.last_return = []
class_name = v[params[0]][0]
method_name = v[params[0]][1]
try:
fqcn = "L"+class_name.replace('.', '_').replace(';', '') + '_' + method_name.replace('<', '0').replace('>', '0')
except:
fqcn = "None"
log.error("Error solving reflection! Register state:" + str(v))
fp = globals().get(fqcn, None)
if fp:
fp(params[1:], vm, v)
else:
log.debug(f"Reflected method not found in mocks {full_name}")
def Ljava_lang_StackTraceElement_getMethodName(params: list, vm, v:list):
vm.memory.last_return = v[params[0]]["method_name"]