-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsoFrida.py
375 lines (330 loc) · 15.8 KB
/
soFrida.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
import frida
import argparse
import os,sys
import subprocess
import xml.etree.ElementTree as ET
import json,time
import re
import logging
from threading import Thread
from ppadb.client import Client as AdbClient
from termcolor import colored, cprint
from sflogger import sfLogger, sfFileLogger
from awstester import awsTester
from time import sleep
APKTOOL_PATH = "/usr/local/bin/apktool"
class soFrida:
def __init__(self, socketio):
self.socketio = socketio
self.namespace="/analyze"
self.base_adb_command = ['adb']
self.isStop = False
self.isManual = False
self.apk_path = "./apk/"
self.javaperform = """
setTimeout(function() {
Java.perform(function() {
%s
});
}, 0);"""
# User can add aws service which is supported with awscli
self.aws_servicelist = ['s3', 'lambda', 'kinesis', 'cognito', 'sns', 'dynamodb', 'simpledb']
self.aws_regions = ['us-west-1', 'us-west-2', 'us-east-1', 'us-east-2','ap-east-1', 'ap-south-1', 'ap-northeast-2','ap-southeast-1','ap-southeast-2','ca-central-1','cn-north-1','cn-northwest-1','eu-central-1','eu-west-1','eu-west-2','eu-west-3','eu-north-1','sa-east-1']
def __del__(self):
pass
def set_pkgid(self, pkgid):
self.pkgid = pkgid
self.flogger = sfFileLogger(self.pkgid)
self.flogger.filelogger.info("[+] Vulnerable PKG_ID : " + self.pkgid)
self.flogger.filelogger.info("[!] Logging Start")
def emit(self, data):
print(data)
self.socketio.emit("analyze_status", data, namespace=self.namespace)
def frida_connect(self):
try:
self.device = frida.get_device(self.serial)
return self.device
except Exception as e:
self.device = ""
self.err = str(e)
return self.device
def adb_connect(self):
try:
client = AdbClient(host="127.0.0.1", port=5037)
self.adb_device = client.device(self.serial)
#print(self.adb_device, self.adb_device == None)
if self.adb_device == None:
self.adb_device = "no serial"
self.err = str("Check Your Device!")
else:
return self.adb_device
except Exception as e:
self.adb_device = ""
self.err = str(e)
return self.adb_device
def analyze_apk(self, path):
self.path = path
self.dir = path[:path.rfind(".apk")]
if os.path.exists(self.dir) == False:
cprint("[*] Start Decode by APKTOOL", 'green')
command = [APKTOOL_PATH, "d", "-f", "-o", self.dir, path]
os.system(" ".join(command))
else:
cprint("[*] Aleady Decoded", 'red')
self.mani = self.dir+"/AndroidManifest.xml"
if os.path.exists(self.mani) == False:
cprint("no manifest file", 'red')
sys.exit(1)
tree = ET.parse(self.mani)
root = tree.getroot()
self.package = ""
for mani in root.iter("manifest"):
self.package = mani.attrib["package"]
break
print("[*] PACKAGE NAME : %s" % self.package)
def install_apk(self):
if self.adb_device.is_installed(self.package):
cprint("[*] Aleady Installed Package : %s" % self.package, 'yellow')
else:
cprint("[*] Start Install Package : %s" % self.package, 'yellow')
self.adb_device.install(self.path)
def uninstall_apk(self):
cprint("[*] Start Uninstall Pakcage : %s" % self.package, 'yellow')
self.adb_device.uninstall(self.package)
def get_class_maketrace(self):
print("get_class_maketrace !!!!")
self.acc_key_list = set()
self.sec_key_list = set()
self.session_token = set()
self.awsregion = set()
self.awsservice = set()
self.awsbucket = set()
self.key_found = False
catch_trace_js = open('catch_make_trace.js', 'r').read()
self.trace_flag = False
self.search_flag = True
self.target_cls = ["com.amazonaws.http.HttpRequest", "com.amazonaws.auth.BasicAWSCredentials", "com.amazonaws.auth.BasicSessionCredentials"]
start_function = "\nsearch_loaded_class([%s]);" % (', '.join("'"+x+"'" for x in self.target_cls))
## This is Callback Func.
############################################################
def trace_callback(message, data):
if 'payload' in message:
if message['payload'].find("start_trace:") != -1:
cls = message['payload'].split(":")[1]
cprint("[+] %s Trace Start!!" % (cls), 'yellow')
if cls in self.target_cls:
if cls == "com.amazonaws.http.HttpRequest":
step = "httprequest"
else:
step = "credentials"
self.emit({"step":step, "result":"success", "class":cls})
self.target_cls.remove(cls)
if len(self.target_cls) <2 and "com.amazonaws.http.HttpRequest" not in self.target_cls:
self.trace_flag = True
#elif message['payload'] =="search complete":
# self.search_flag = False
else:
self.findAccessKeyId(message['payload'])
else:
print(message['stack'])
###########################################################
script_list = []
try:
script = self.spwan(catch_trace_js+start_function, trace_callback)
script_list.append(script)
self.emit({"step":"spawn", "result":"success"})
while self.isStop == False:
pass
script_list.reverse()
for script in script_list:
script.unload()
script.off("message", trace_callback)
self.session.detach()
except Exception as e:
self.emit({"step":"spawn", "result":"fail", "msg":str(e)})
return
def spwan(self, runjs, message_callback):
pid = self.device.spawn(self.pkgid)
self.session = self.device.attach(pid)
script = self.session.create_script(runjs)
script.on("message", message_callback)
script.load()
self.device.resume(pid)
return script
def run(self, runjs, message_callback):
self.session = self.device.attach(self.pkgid)
script = self.session.create_script(runjs)
script.on("message", message_callback)
script.load()
return script
def findAccessKeyId (self, text):
# Recognize AWS Key Pairs by RegEx
regex_acc = re.compile(r"(?<![A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])")
regex_sec = re.compile(r"(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])")
regex_svc_A = re.compile(r"(?P<svc>[^/]+)\.(?P<region>[^.]+).amazonaws.com")
regex_svc_B = re.compile(r"(\/\/(?P<svc>[^\/?#]*)).amazonaws.com")
regex_svc_C = re.compile(r"(?P<svc>[^/]+)\-(?P<region>[^.]+).amazonaws.com")
# https://happyrestaurant.s3.amazonaws.com/
regex_s3case = [re.compile(r"https:\/\/(?P<bucket>[^.]+).s3.amazonaws.com"),re.compile(r"https:\/\/(?P<bucket>[^.]+).s3.(?P<region>[^.]+).amazonaws.com"), re.compile(r"https:\/\/(?P<bucket>[^.]+).s3-(?P<region>[^.]+).amazonaws.com"), re.compile(r"https:\/\/s3\-(?P<region>[^.]+).amazonaws.com/(?P<bucket>[^.]+)")]
#if text matched regex_svc_B --> Check reges_s3cases
svc_temp = regex_svc_B.search(text)
if svc_temp != None:
print (text)
if svc_temp.group('svc').find("s3") != -1:
for regex_s3 in regex_s3case :
s3temp = regex_s3.search(text)
if s3temp is not None:
# print (text)
if "s3" not in self.awsservice:
self.emit({"step":"service", "result":"success", "name":"s3"})
if s3temp.group('bucket') not in self.awsbucket:
self.emit({"step":"bucket", "result":"success", "name":s3temp.group('bucket')})
if "region" in s3temp.group():
if s3temp.group('region') not in self.awsregion:
self.emit({"step":"region", "result":"success", "name":s3temp.group('region')})
self.awsservice.add("s3")
self.awsbucket.add(s3temp.group('bucket'))
if s3temp.group('region'):
self.awsregion.add(s3temp.group('region'))
print (self.awsregion)
self.awsregion.add(s3temp.group('region'))
print (self.awsservice)
print (self.awsbucket)
print (self.awsregion)
else:
svc_tempA = regex_svc_A.search(text)
if svc_tempA != None:
print (text)
if svc_tempA.group('region').find("s3") == -1:
if svc_tempA.group('svc') not in self.awsservice:
self.emit({"step":"service", "result":"success", "name":svc_tempA.group('svc')})
if svc_tempA.group('region') not in self.awsregion:
self.emit({"step":"region", "result":"success", "name":svc_tempA.group('region')})
self.awsservice.add(svc_tempA.group('svc'))
self.awsregion.add(svc_tempA.group('region'))
print (self.awsservice)
print (self.awsregion)
sec = regex_sec.search(text)
if sec != None:
#print("[*] SecretKeyId : %s" % str(sec.group()))
if len(self.sec_key_list) == 0:
self.emit({"step":"secretkeyid", "result":"success", "name":str(sec.group())})
self.sec_key_list.add(str(sec.group()))
print (self.sec_key_list)
acc = regex_acc.search(text)
if acc != None:
#print("[*] AccessKeyId : %s" % str(acc.group()))
if len(self.acc_key_list) == 0:
self.emit({"step":"accesskeyid", "result":"success", "name":str(acc.group())})
self.acc_key_list.add(str(acc.group()))
print (self.acc_key_list)
if (text.endswith ("=") or text.endswith ("==")) and ("http" not in text):
if len(self.session_token) == 0:
self.emit({"step":"sessiontoken", "result":"success", "name":text})
self.session_token.add(text)
print (self.session_token)
#if len(self.sec_key_list) > 0 and len(self.acc_key_list) > 0 and len(self.awsservice) > 0 and len(self.awsregion) > 0:
# if "s3" in self.awsservice and len(self.awsbucket) > 0 or "s3" not in self.awsservice:
# self.key_found = True
def print_key(self):
if len(self.acc_key_list) == 0:
cprint("[*] No Access Key", 'red')
else:
cprint("[+] AccessKeyId Found", 'yellow')
for accesskey in list(self.acc_key_list):
cprint("\t[-] %s" % accesskey, 'green')
subprocess.call("aws configure set aws_access_key_id %s"%accesskey, shell=True)
self.flogger.filelogger.info("[+] AccessKeyId : " + accesskey)
if len(self.sec_key_list) == 0:
cprint("[*] No Secret Key", 'red')
else:
cprint("[+] SecretKeyId Found", 'yellow')
for secretkey in list(self.sec_key_list):
cprint("\t[-] %s" % secretkey, 'green')
subprocess.call("aws configure set aws_secret_access_key %s"%secretkey, shell=True)
self.flogger.filelogger.info("[+] SecretAceessKey : " + secretkey)
if len(self.session_token) == 0:
cprint("[*] No SessionToken", 'red')
else:
cprint("[+] SessionToken Found", 'yellow')
for sessiontoken in list(self.session_token):
cprint("\t[-] %s" % sessiontoken, 'green')
subprocess.call("aws configure set aws_session_token %s"%sessiontoken, shell=True)
self.flogger.filelogger.info("[+] SessionToken : " + sessiontoken)
self.flogger.filelogger.info("[+] AWS Service is : " + str(self.awsservice))
self.flogger.filelogger.info("[+] AWS Region is : " + str(self.awsregion))
self.flogger.filelogger.info("[+] AWS Bucket is : " + str(self.awsbucket))
def clear_logcat(self):
adb_command = self.base_adb_command[:]
adb_command.append('logcat')
adb_command.append('-c')
try :
subprocess.Popen(adb_command)
# Wait for cleaning logcat
time.sleep(1)
cprint("[+] Logcat sucessfully cleared", 'green')
except :
cprint ("[!] Error occured wuth cleaning logcat",'red')
def remove_dir(self):
cprint("[*] Remove Apktool Dirctory", 'green')
command = ["rm", "-rf", self.dir]
os.system(" ".join(command))
def clear_recentapp(self, pkgid):
adb_command = self.base_adb_command[:]
adb_command.append('shell')
adb_command.append('am')
adb_command.append('force-stop')
adb_command.append(pkgid)
try :
subprocess.Popen(adb_command)
# Wait for cleaning recent app
time.sleep(1)
print ("app cleaned")
except :
cprint ("[!] Error occured wuth cleaning app",'red')
def soFrida_start(self, serial):
self.serial = serial
self.isStop = False
sleep(1)
while True:
sleep(0.5)
if self.isStop:
break
adb_device = self.adb_connect()
print(adb_device)
if adb_device == "":
self.emit({"step":"adb_connect", "result":"fail", "msg":self.err})
elif adb_device == "no serial":
self.emit({"step":"adb_connect", "result":"no serial", "msg":self.err})
self.isStop = True
else:
self.emit({"step":"adb_connect", "result":"success"})
break
while True:
sleep(0.5)
print("fridacheck", self.isStop)
if self.isStop:
break
device = self.frida_connect()
if device == "":
self.emit({"step":"frida_connect", "result":"fail", "msg":self.err})
else:
self.emit({"step":"frida_connect", "result":"success"})
break
print("isinstalled", self.isStop)
if not self.isStop:
if adb_device.is_installed(self.pkgid):
self.emit({"step":"apk_install", "result":"installed", "package":self.pkgid})
else:
self.emit({"step":"apk_install", "result":"not installed", "package":self.pkgid})
self.emit({"step":"apk_install", "result":"installing", "package":self.pkgid})
try:
adb_device.install(self.apk_path+self.pkgid+".apk")
self.emit({"step":"apk_install", "result":"installed", "package":self.pkgid})
except Exception as e:
self.emit({"step":"apk_install", "result":"fail", "msg":str(e), "package":self.pkgid})
self.emit({"step":"stop"})
self.isStop = True
if not self.isStop:
self.clear_recentapp(self.pkgid)
self.get_class_maketrace()