forked from abhi-r3v0/Adhrit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·408 lines (349 loc) · 12.5 KB
/
Copy pathapp.py
File metadata and controls
executable file
·408 lines (349 loc) · 12.5 KB
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
399
400
401
402
403
404
405
406
407
408
from sqlite3.dbapi2 import Cursor
import threading
from adhrit.adhrit import main, welcome
from flask import Flask, json, jsonify, render_template, request
from threading import Thread
from http import HTTPStatus
import multiprocessing
import os, configparser, time
from adhrit.recons.dbaccess import create_status_table, dbconnection, select_query
from adhrit.recons.apk_extract import extraction
from adhrit.recons.reset import reset_db
from adhrit.recons.smarser.parser import parser
from adhrit.recons.secrets import secret_scanner
from subprocess import call
from adhrit.recons.clean import cleaner
import sqlite3, hashlib
from shutil import rmtree
from sqlite3 import Error
from colorama import Fore
ALLOWED_EXTENSIONS = {'apk'}
app = Flask(__name__)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_hash():
sha256_hash = hashlib.sha256()
with open('app.apk',"rb") as f:
for byte_block in iter(lambda: f.read(4096),b""):
sha256_hash.update(byte_block)
return str(sha256_hash.hexdigest())
def data_from_db(query):
rows = select_query(query)
rowarray_list = []
rowarray_list = []
for row in rows:
d = dict(zip(row.keys(), row))
rowarray_list.append(d)
json_data = rowarray_list
if len(json_data) != 0:
data = json_data[0]
return data
else:
return 0
def null_elimination(data):
if data != 0:
null_key_list = []
for key, value in data.items():
if 'Hash' in key:
continue
val_list = eval(value)
if not val_list:
null_key_list.append(key)
# removing all unused components
for key in null_key_list:
del data[key]
return data
def query_on_StatusDB(hash_of_apk):
dbname = "adhrit.db"
conn = dbconnection(dbname)
create_status_table(conn)
Cursor = conn.cursor()
query = "SELECT Hash, Manifest, Bytecode, Secrets from `StatusDB` WHERE `Hash` = '%s'" % str(hash_of_apk)
Cursor.execute(query)
data=Cursor.fetchone()
if data is None:
query = f"INSERT INTO StatusDB(Hash, Manifest, Bytecode, Secrets) values('{hash_of_apk}', 'incomplete', 'incomplete', 'incomplete')"
Cursor.execute(query)
conn.commit()
query = "SELECT Hash, Manifest, Bytecode, Secrets from `StatusDB` WHERE `Hash` = '%s'" % str(hash_of_apk)
Cursor.execute(query)
data=Cursor.fetchone()
result ={'hash_of_apk': data[0], 'manifest':data[1], 'bytecode':data[2], 'secrets' : data[3]}
return result
else:
result ={'hash_of_apk': data[0], 'manifest':data[1], 'bytecode':data[2], 'secrets' : data[3]}
return result
def del_row(hash_of_apk):
dbname = "adhrit.db"
conn = dbconnection(dbname)
query1 = f"DELETE FROM DataDB WHERE Hash = '{hash_of_apk}'"
query2 = f"DELETE FROM BytecodeDB WHERE Hash = '{hash_of_apk}'"
query3 = f"DELETE FROM SecretsDB WHERE Hash = '{hash_of_apk}'"
try:
Cursor = conn.cursor()
Cursor.execute(query1)
Cursor.execute(query2)
Cursor.execute(query3)
conn.commit()
except Error as e:
print(e)
def get_description(key):
with open('description.json') as f:
desc = json.load(f)
return desc[key]
def get_link(key):
with open('links.json') as f:
link = json.load(f)
return link[key]
def getreport(hash_key, scan_type):
response = {}
if scan_type == "manifest":
query_manifest = "SELECT * FROM `DataDB` WHERE `Hash` = '%s'" % str(hash_key)
manifest_data = data_from_db(query_manifest)
manifest_newdata = null_elimination(manifest_data)
#Sorting manifest data for response
for key, value in manifest_newdata.items():
if 'Hash' in key:
continue
val_list = eval(value)
if key == 'ApplicationInfo':
response.__setitem__("Application Information", val_list)
if key == 'Activity':
val_list.insert(0,get_description(key))
response.__setitem__("List of all Activities", val_list)
if key == 'ExportedActivity':
val_list.insert(0,get_description(key))
response.__setitem__("List of Exported Activities", val_list)
if key == 'BroadcastReceiver':
val_list.insert(0,get_description(key))
response.__setitem__(key, val_list)
if key == 'ExportedReceiver':
val_list.insert(0,get_description(key))
response.__setitem__("List of Broadcast Receivers", val_list)
if key == 'Permission':
val_list.insert(0,get_description(key))
response.__setitem__("List of all Permissions", val_list)
if key == 'CriticalPerm':
val_list.insert(0,get_description(key))
response.__setitem__("Critical Permissions", val_list)
if key == 'CustomPerm':
val_list.insert(0,get_description(key))
response.__setitem__("Custom Permissions", val_list)
if key == 'Deeplinks':
val_list.insert(0,get_description(key))
response.__setitem__("Deeplinks", val_list)
if key == 'Service':
val_list.insert(0,get_description(key))
response.__setitem__("Services", val_list)
if key == 'ExportedService':
val_list.insert(0,get_description(key))
response.__setitem__("Exported Services", val_list)
if key == 'Taskaffinity':
val_list.insert(0,get_description(key))
response.__setitem__("Task Affinity", val_list)
if key == 'ImplicitIntent':
tmp_imp_intents = []
for key in val_list.keys():
tmp_imp_intents.extend(val_list[key])
key = 'ImplicitIntent'
tmp_imp_intents.insert(0,get_description(key))
response.__setitem__("Implicit Intents", tmp_imp_intents)
if key == 'Provider':
tmp_providers = []
provider_obj_list = []
for key_parent,value_parent in val_list.items():
for key, value in value_parent.items():
if key == 'name':
temp = key + ' : ' + value
provider_obj_list.insert(0, temp)
else:
temp = key + ' : ' + value
provider_obj_list.append(temp)
provider_obj_list.append("")
tmp_providers.extend(provider_obj_list)
provider_obj_list = []
key = 'Provider'
tmp_providers.insert(0,get_description(key))
response.__setitem__(key, tmp_providers)
elif scan_type == "bytecode":
query_bytecode = "SELECT * FROM `BytecodeDB` WHERE `Hash` = '%s'" % str(hash_key)
bytecode_data = data_from_db(query_bytecode)
bytecode_newdata = null_elimination(bytecode_data)
#Sorting bytecode data for response
for key, value in bytecode_newdata.items():
if 'Hash' in key:
continue
val_list = eval(value)
if key == 'Unsafe_Intent_Urls':
val_list.insert(0,get_description(key))
val_list.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list)
if key == 'No_Tls_Validity_Checks':
val_list.insert(0,get_description(key))
val_list.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list)
if key == 'Sticky_Broadcasts':
val_list.insert(0,get_description(key))
val_list.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list)
if key == 'Dynamic_or_exported_Broadcast_Receivers':
val_list.insert(0,get_description(key))
val_list.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list)
if key == 'Javascript_Enabled':
val_list.insert(0,get_description(key))
val_list.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list)
if key == 'Weak_Dynamic_Invocation_Checks_On_Content_Providers':
val_list.insert(0,get_description(key))
val_list.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list)
if key == 'SharedPrefs_usage':
val_list.insert(0,get_description(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list)
if key == 'SQLite_DB_usage':
val_list.insert(0,get_description(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list)
elif scan_type == "vulns":
query_vulns = "SELECT * FROM `BytecodeDB` WHERE `Hash` = '%s'" % str(hash_key)
vuln_data = data_from_db(query_vulns)
vuln_newdata = null_elimination(vuln_data)
#Sorting Vulnerability data for response
for key, value in vuln_newdata.items():
if 'Hash' in key:
continue
val_list_vulns = eval(value)
if key == 'Overwritable_Cookie':
val_list_vulns.insert(0,get_description(key))
val_list_vulns.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list_vulns)
if key == 'File_Access_Via_Urls':
val_list_vulns.insert(0,get_description(key))
val_list_vulns.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list_vulns)
if key == 'Content_Access_Via_Urls':
val_list_vulns.insert(0,get_description(key))
val_list_vulns.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list_vulns)
if key == 'Ecb_Instances':
val_list_vulns.insert(0,get_description(key))
val_list_vulns.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list_vulns)
if key == 'Unencrypted_Socket_Communications':
val_list_vulns.insert(0,get_description(key))
val_list_vulns.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list_vulns)
if key == 'execSQL_used':
val_list_vulns.insert(0,get_description(key))
val_list_vulns.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list_vulns)
if key == 'Insecure_Socket_Factory':
val_list_vulns.insert(0,get_description(key))
val_list_vulns.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list_vulns)
if key == 'Empty_Pending_Intents':
val_list_vulns.insert(0,get_description(key))
val_list_vulns.append(get_link(key))
key = key.replace('_',' ')
response.__setitem__(key, val_list_vulns)
elif scan_type == "secrets":
empty_response = ['Not found']
query_secrets = "SELECT * FROM `SecretsDB` WHERE `Hash` = '%s'" % str(hash_key)
secrets_data = data_from_db(query_secrets)
secrets_newdata = null_elimination(secrets_data)
if secrets_newdata != 0:
for key, value in secrets_newdata.items():
if 'Hash' in key:
continue
val_list = eval(value)
# print(type(val_list))
if key == 'Urls':
val_list.insert(0,get_description(key))
response.__setitem__('URLs', val_list)
if key == 'Strings':
val_list.insert(0,get_description(key))
response.__setitem__(key, val_list)
if key == 'Api_keys':
val_list.insert(0,get_description(key))
response.__setitem__('API Keys', val_list)
else:
response.__setitem__('URLs', empty_response)
response.__setitem__('Strings from Native libs', empty_response)
response.__setitem__('API Keys', empty_response)
return response
@app.route('/scan', methods=['POST'])
def scan():
if request.method == 'POST':
if 'file' not in request.files:
return HTTPStatus.NO_CONTENT
uploaded_files = request.files["file"]
if uploaded_files and allowed_file(uploaded_files.filename):
uploaded_files.save(uploaded_files.filename)
# Renaming to app.apk
source = uploaded_files.filename
dest = 'app.apk'
os.rename(source, dest)
global hash_of_apk
hash_of_apk = get_hash()
status_hash_of_apk = query_on_StatusDB(hash_of_apk) # Creates a row if Hash is not found else return the values
welcome()
if 'incomplete' in status_hash_of_apk.values():
del_row(hash_of_apk)
pwd = os.getcwd()
path = str(pwd) + '/'+hash_of_apk
rmtree(path, ignore_errors = True)
extraction('app.apk',hash_of_apk)
p1 = multiprocessing.Process(target=parser, args=[hash_of_apk])
p1.start()
secret_scanner(hash_of_apk)
main(hash_of_apk)
p1.join()
while(True):
time.sleep(2)
status = query_on_StatusDB(hash_of_apk)
if 'incomplete' not in status.values():
cleaner(hash_of_apk)
break
else:
print(Fore.GREEN + "[INFO] Scanning was already done for this apk, fetching data from db!\n" + Fore.BLUE)
response = jsonify(status_code=HTTPStatus.OK, hash_key=hash_of_apk)
os.system('rm app.apk')
return response ,{'Access-Control-Allow-Origin': '*'}
return jsonify(status_msg="apk not sent properly")
@app.route("/report/<hash_key>/<scan_type>")
def report(hash_key, scan_type):
response = getreport(hash_key, scan_type)
return response,{'Access-Control-Allow-Origin': '*'}
@app.route("/testbed")
def test():
pass
@app.route('/')
def func():
return "Adhrit up and running *-*"
@app.route("/reset")
def reset():
reset_db()
return jsonify(sts_msg = "Resetted db and Scan Id!")
if __name__ == '__main__':
global hash_of_apk
# from waitress import serve
# serve(app, host = '127.0.0.1', port=5000)
app.run(debug=False, use_reloader=True,threaded=False , processes=4)
#threaded=True,
# curl -X POST -F file=@app.apk http://localhost:5000/scan