-
Notifications
You must be signed in to change notification settings - Fork 25
/
amdh.py
executable file
·424 lines (322 loc) · 17.4 KB
/
amdh.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env -S python
# -*- coding: utf-8 -*-
import json
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from core.settings import Settings
from core.snapshot import Snapshot
from core.app import App
from utils.main_settings import *
main_settings = MainSettings()
def process_settings(adb_instance, device_id=""):
if main_settings.harden:
settings_check = Settings(SETTINGS_FILE, adb_instance, True, out=main_settings.out[device_id])
else:
settings_check = Settings(SETTINGS_FILE, adb_instance, out=main_settings.out[device_id])
if main_settings.scan_settings:
with open(f"{main_settings.output_dir}/{device_id}_report_settings.json", 'w') as fp:
json.dump(settings_check.check(), fp, indent=4)
main_settings.out["std"].print_info("Report generated: %s_report_settings.json" % device_id)
def process_applications(adb_instance, device_id=""):
packages = []
report_apps = {}
if main_settings.app_type:
packages = adb_instance.list_installed_packages(main_settings.app_type.value)
try:
for package in packages:
if not main_settings.list_apps:
main_settings.out[device_id].print_info(package)
report_apps[package] = dict()
app = App(adb_instance, package, main_settings.scan_applications, main_settings.dump_apks,
main_settings.apks_dump_folder + "/" + device_id)
perms, dangerous_perms, is_device_admin, known_malware = app.check_app()
if known_malware:
main_settings.out[device_id].print_error(f"{package} is known as malware")
report_apps[package]["malware"] = True
if main_settings.scan_applications:
if len(dangerous_perms) and dangerous_perms.items():
main_settings.out[device_id].print_warning_header(
"Package {} has some dangerous permissions: ".format(package))
for perm, desc in dangerous_perms.items():
main_settings.out[device_id].print_warning("\t " + perm + ": ")
main_settings.out[device_id].print_warning("\t\t" + desc)
report_apps[package]["permissions"] = dict()
report_apps[package]["permissions"] = {"all_permissions": list(perms.keys()),
"dangerous_perms": dangerous_perms}
report_apps[package]["is_device_admin"] = is_device_admin
else:
main_settings.out[device_id].print_info("Package {} has no dangerous permissions".format(package))
if is_device_admin:
message = f"/!\ \t {package} is device admin \t /!\ "
padding = len(message)
main_settings.out[device_id].print_warning("-" * padding)
main_settings.out[device_id].print_warning(message)
main_settings.out[device_id].print_warning("-" * padding)
report_apps[package] = {"is_device_admin": is_device_admin}
if main_settings.rm_admin_recv:
removed, dpm = app.remove_device_admin_for_app()
if removed:
main_settings.out[device_id][package] = {"device_admin_revoked": True}
main_settings.out[device_id].print_info(
"Device admin receivers for {} removed\n".format(app.package_name))
else:
main_settings.out[device_id].print_error(
"An error occured while removing the device admin " + dpm + " .")
# Revoke all Dangerous permissions
if main_settings.revoke and app.dangerous_perms:
succeeded = app.revoke_dangerous_perms()
if succeeded:
report_apps[package]["revoked_dangerous_pemissions"] = "succeeded"
main_settings.out[device_id].print_info("Dangerous permissions revoked\n")
else:
main_settings.out[device_id].print_error(f"An error occured while revoking"
"permission {perm} to package {app.package_name}")
elif main_settings.revoke and not app.dangerous_perms:
main_settings.out[device_id].print_info("No dangerous permissions granted for this package\n")
main_settings.out[device_id].print_info(
"----------------------------MALWARE SCAN--------------------------------")
if app.score > 0:
main_settings.out[device_id].print_high_warning(
f'The application uses some permissions used also by malware. Percentage : {app.score}%')
if app.malware_combination > 0:
main_settings.out[device_id].print_high_warning(f'{str(app.malware_combination)} permissions '
f'combinations used also by malware')
if app.malware_only_perms > 0:
main_settings.out[device_id].print_high_warning(f'{str(app.malware_only_perms)} permissions '
f' used by malware only.')
main_settings.out[device_id].print_info(
"-----------------------------------------------------------------------------\n")
except Exception as e:
print(e)
if main_settings.scan_applications:
with open(f"{main_settings.output_dir}/{device_id}_report_apps.json", 'w') as fp:
json.dump(report_apps, fp, indent=4)
main_settings.out["std"].print_info("Report generated: %s_report_apps.json" % device_id)
return report_apps
def process_snapshot(adb_instance, device_id):
main_settings.lock.acquire()
input("Unlock the device %s and press ENTER key to continue" % device_id)
# TODO: Check if device is unlocked
# set stay_awake to 1
adb_instance.content_insert_settings("global", "stay_on_while_plugged_in", "1", "i")
main_settings.out["std"].print_info(f"Starting snapshot on {device_id}")
if not os.path.isdir(main_settings.snapshot_dir):
os.makedirs(main_settings.snapshot_dir)
snapshot_path = main_settings.snapshot_dir + "/" + device_id + "_" + datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
if not os.path.isdir(snapshot_path):
os.makedirs(snapshot_path)
if main_settings.app_type:
snapshot = Snapshot(adb_instance, main_settings.app_type.value, out_dir=snapshot_path)
else:
snapshot = Snapshot(adb_instance, out_dir=snapshot_path)
report = snapshot.get_report()
with open(snapshot_path + "/snapshot.json", 'w') as fp:
json.dump(report, fp, indent=4)
adb_instance.content_insert_settings("global", "stay_on_while_plugged_in", "0", "i")
main_settings.out["std"].print_info(f"Snapshot {device_id} finished")
main_settings.lock.release()
def process_snapshot_cmp(adb_instance):
cmp_report = Snapshot(adb_instance, snapshot_file=main_settings.snapshot_report,
backup=main_settings.backup).snapshot_compare()
main_settings.out["std"].print_info("Installed Apps after snapshot was taken")
main_settings.out["std"].print(json.dumps(cmp_report["apps"]["new_installed_apps"], indent=4))
main_settings.out["std"].print_info("Apps exists in snapshot")
print(json.dumps(cmp_report["apps"]["apps_exist_in_snap"], indent=4))
main_settings.out["std"].print_info("Uninstalled after snapshot was taken")
print(json.dumps(cmp_report["apps"]["uninstalled_apps"], indent=4))
main_settings.out["std"].print_info("Changed settings after snapshot was taken")
print(json.dumps(cmp_report["settings"], indent=4))
def process_snapshot_restore(adb_instance):
input("Unlock your phone and press ENTER key to continue")
adb_instance.content_insert_settings("global", "stay_on_while_plugged_in", "1", "i")
main_settings.out["std"].print_info("Starting restore")
restore_report = Snapshot(adb_instance, snapshot_file=main_settings.snap_to_restore,
backup=False).snapshot_restore()
adb_instance.content_insert_settings("global", "stay_on_while_plugged_in", "0", "i")
main_settings.out["std"].print_info("Restore finished")
main_settings.out["std"].print_info("Restore report")
print(json.dumps(restore_report, indent=4))
def interactive_list_processes(adb_instance, device_id):
main_settings.lock.acquire()
process_choice_list = []
current_processes = adb_instance.list_backgroud_apps().split("\n")
print("Current running user processes on the device %s" % device_id)
for i in range(0, len(current_processes) - 1):
print(" {}- {}".format(i + 1, current_processes[i]))
print("")
choice = input("Select id(s) of process(es) to kill (separated by comma ','): ")
chosen_processes = choice.replace(" ", "").split(",")
for c in chosen_processes:
if c.isdigit() and (0 < int(c) < len(current_processes) + 1):
process_choice_list = process_choice_list + [c]
main_settings.lock.release()
else:
print("[X] ERROR: process does not exist")
print("Exiting device %s" % device_id)
main_settings.lock.release()
return
for proc in process_choice_list:
adb_instance.force_stop_app(current_processes[int(proc) - 1])
def interactive_list_apps(adb_instance, device_id):
main_settings.lock.acquire()
if main_settings.app_type:
packages = adb_instance.list_installed_packages(main_settings.app_type.value)
if main_settings.app_type.value == 'e':
print("Uninstalling or disabling system Apps can break your system")
print("List of installed packages on device %s: " % device_id)
nbr_listed_apps = 0
apps_choice_list = []
for package in packages:
if nbr_listed_apps < LIST_APPS_MAX_PRINT and \
packages.index(package) < (len(packages) - 1):
print("\t[" + str(packages.index(package) + 1) + "] " + package)
nbr_listed_apps = nbr_listed_apps + 1
else:
if packages.index(package) == (len(packages) - 1):
print("\t[" + str(packages.index(package) + 1) + "] " + package)
while True:
choice = input("Select application(s) (separated by comma ','), 'c' to continue"
" listing apps and 'A' for actions menu: ")
if choice == 'c':
nbr_listed_apps = 1
break
if choice == 'A':
break
else:
chosen_apps = choice.replace(" ", "").split(",")
for c in chosen_apps:
if c.isdigit() and (0 < int(c) < len(packages) + 1):
apps_choice_list = apps_choice_list + [c]
else:
print(f"option {c} does not exist")
if choice == 'A':
break
if not len(apps_choice_list):
print("No application selected")
main_settings.lock.release()
return
while True:
print("choose an action")
print("\td: disable selected apps")
print("\tu: uninstall selected apps")
print("\ts: static analysis")
print("\te: exit")
print("")
action = input("Action: ")
action = action.replace(" ", "")
if action == 'd' or action == 'u' or action == 'e':
break
elif action == 's':
break
else:
print("ERROR: Invalid action")
continue
for id_app in apps_choice_list:
if action == 'd':
try:
adb_instance.disable_app(packages[int(id_app) - 1])
main_settings.out["std"].print_success(packages[int(id_app) - 1] + " disabled")
except Exception as e:
main_settings.out["std"].print_error("An Error occurred while disabling " + packages[int(id_app) - 1])
elif action == 'u':
try:
adb_instance.uninstall_app(packages[int(id_app) - 1])
main_settings.out["std"].print_success(packages[int(id_app) - 1] + " uninstalled")
except Exception as e:
main_settings.out["std"].print_error(
"An Error occurred while uninstalling " + packages[int(id_app) - 1])
elif action == 's':
# TO-DO: Do analysis in other thread and log in file/DB
app = App(adb_instance, packages[int(id_app) - 1], dump_apk=True, out_dir=main_settings.apks_dump_folder)
main_settings.out["std"].print_info(f"Package {packages[int(id_app) - 1]}")
package_info = app.static_analysis()
print(package_info)
main_settings.out["std"].print_info("\tMalware identification")
for key, value in package_info["detected_malware"].items():
if value > 0:
main_settings.out["std"].print_error("\t\t " + key + ": " + str(value) + " positives tests")
else:
main_settings.out["std"].print_info("\t\t " + key + ": " + str(value) + " positive test")
if package_info and package_info["packed_file"] and \
package_info["packed_file"][packages[int(id_app) - 1]].keys():
main_settings.out["std"].print_info("\tPacked files")
main_settings.out["std"].print_error(
f"The package {packages[int(id_app) - 1]} has another Application (APK) inside")
for file in package_info["packed_file"][packages[int(id_app) - 1]]:
for perm in package_info["packed_file"][packages[int(id_app) - 1]][file]:
main_settings.out["std"].print_error("\tDangerous Permission: " + perm)
elif action == 'e':
break
main_settings.lock.release()
def process(device_id):
global main_settings
result = {device_id: {}}
adb_instance = ADB(main_settings.adb_path, device_id)
main_settings.lock.acquire()
main_settings.out[device_id] = Out(filename=f"{main_settings.output_dir}/{device_id}.log")
main_settings.lock.release()
if adb_instance.check_pending_update():
main_settings.out["std"].print_warning("%s: The system has a pending updates!" % device_id)
main_settings.out[device_id].print_warning("%s: The system has a pending updates!" % device_id)
if main_settings.scan_applications or main_settings.dump_apks or main_settings.list_apps:
result[device_id]['apps'] = process_applications(adb_instance, device_id)
if main_settings.scan_settings:
process_settings(adb_instance, device_id)
if main_settings.list_apps:
interactive_list_apps(adb_instance, device_id)
if main_settings.list_processes:
interactive_list_processes(adb_instance, device_id)
if main_settings.snapshot:
process_snapshot(adb_instance, device_id)
if main_settings.restore_snap:
main_settings.set_output_std()
process_snapshot_restore(adb_instance)
if main_settings.cmp_snap:
main_settings.set_output_std()
process_snapshot_cmp(adb_instance)
return result
def check_device_up(devices):
global main_settings
connected_devices = ADB(main_settings.adb_path).list_devices()
for device in devices:
if device not in connected_devices.keys():
main_settings.out["std"].print_error(f"{device} not found")
sys.exit(1)
device_status = connected_devices[device]
if "offline" in device_status or "unauthorized" in device_status \
or "no permissions" in device_status:
main_settings.out["std"].print_error(
f"The device {device} cannot be used. Reason: {connected_devices[device]}")
sys.exit(1)
def initializer(args):
global main_settings
main_settings.arguments = args
main_settings.init_vars()
def amdh():
global main_settings
main_settings.args_parse()
main_settings.init_vars()
connected_devices = ADB(ADB_BINARY).list_devices()
if len(main_settings.devices) == 0 or not main_settings.devices:
if len(connected_devices) == 0:
main_settings.out["std"].print_error("No device found")
sys.exit(1)
elif len(connected_devices) > 1:
main_settings.out["std"].print_error("Please use -d to specify the devices to use")
main_settings.out["std"].print_info("Current connected devices")
for device in connected_devices:
print(device)
sys.exit(1)
else:
main_settings.devices.append(list(connected_devices.keys())[0])
check_device_up(main_settings.devices)
main_settings.out["std"].print_info("Start ...")
workers(main_settings)
main_settings.set_output_std()
main_settings.out["std"].print_info("Finished")
def workers(settings):
with ThreadPoolExecutor(max_workers=len(settings.devices)) as executor:
results = {device: executor.submit(process, device) for device in settings.devices}
return as_completed(results)
if __name__ == "__main__":
amdh()