-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfork_droidbox.py
executable file
·158 lines (109 loc) · 4.93 KB
/
fork_droidbox.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
import sys, signal
import subprocess
import time
import os
from os.path import isfile, join, isdir
from os import listdir
from scripts import droidbox
import argparse
from argparse import RawTextHelpFormatter
from tqdm import tqdm
# current_directory = os.path.dirname(os.path.realpath(__file__))
# os.chdir(current_directory)
deviceId = "droidbox-emulator"
strace_analysis = True
can_break = False
def sigint_handler(signal, frame):
global can_break
can_break = True
print 'Interrupted'
subprocess.Popen(["./killAllEmulators.sh"])
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
def main():
parser = argparse.ArgumentParser(
description="Welcome to AndroPyTool\n\n" +
'[!] You must provide the source directory where apks are contained. ',
formatter_class=RawTextHelpFormatter)
parser.add_argument('-s', '--source', help='Source directory for APKs', required=True)
parser.add_argument('-d', '--duration', help='DroidBox analysis duration', required=True)
parser.add_argument('-o', '--output', help='Output directory for results', required=True)
parser.add_argument('--gui', dest='gui', action='store_true')
parser.add_argument('--no-gui', dest='gui', action='store_false')
parser.set_defaults(feature=False)
# parser.add_argument('-g', '--gui', help='GUI Mode: True or False', required=True)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
analyze_with_droidbox(args.source, args.duration, args.output, args.gui)
def analyze_with_droidbox(apks_folders, duration, output_directory, gui):
subprocess.Popen(["adb", "devices"], stdout=subprocess.PIPE)
current_directory = os.path.dirname(os.path.realpath(__file__))
os.chdir(current_directory)
print "Killing current active emulators..."
subprocess.Popen(["./killAllEmulators.sh"])
# output_directory = "logs_outputs_" + filter(None, apks_folders.split("/"))[-1] + "/"
apks_folders += "/"
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# list_folders = [f for f in listdir(apks_folders) if isdir(join(apks_folders, f))]
apk_list = []
for path, subdirs, files in os.walk(apks_folders):
for name in files:
if name.endswith(".apk"):
apk_list.append(os.path.join(path, name))
#print "NUM APKS FOUND: " + str(len(apk_list))
count = 0
for apk_name in tqdm(apk_list):
count += 1
# completed_percentage = "{0:.2f}".format((float(count) / float(len(apk_list)))*100.0)
# print "\n##########################"
# print str(completed_percentage) + "% NEW APK: " + apk_name
# print "##########################"
apk_id = apk_name.split("/")[-1]
log_file_name = "logcat_" + apk_id.replace(".apk", ".txt")
json_file_name = "analysis_" + apk_id.replace(".apk", ".json")
if isfile(join(output_directory, log_file_name)) or isfile(join(output_directory, json_file_name)):
# print "EXISTS: " + output_directory + log_file_name
# print "!! APK already analysed: " + apk_name
continue
file_output_log = open(output_directory + log_file_name, "w")
file_output_json = open(output_directory + json_file_name, "w")
print "\nStarting emulator "
p = None
if not gui:
#print colored('STARTING ' + ' EMULATOR IN NON GUI MODE...', 'green')
print 'STARTING ' + ' EMULATOR IN NON GUI MODE...'
subprocess.Popen(["./startemuNoGUI.sh", deviceId])
print "ADB DEVICE RUNNING "
else:
# print colored('STARTING ' + ' EMULATOR IN GUI MODE...', 'green')
print 'STARTING ' + ' EMULATOR IN GUI MODE...'
subprocess.Popen(["./startemu.sh", deviceId])
print "subprocess called"
p = subprocess.Popen(["adb", "shell", "getprop", "sys.boot_completed"], stdout=subprocess.PIPE)
output = p.stdout.read()
print "Waiting until boot is completed"
while(1):
if output.startswith("1"):
break
else:
print "Boot not completed"
time.sleep(1)
p = subprocess.Popen(["adb", "shell", "getprop", "sys.boot_completed"], stdout=subprocess.PIPE)
output = p.stdout.read()
print "Boot completed !"
print "Calling droidbox..."
output_log, output_json = droidbox.execute_droidbox(["", apk_name, duration], logs_directory=output_directory)
file_output_log.write(output_log)
file_output_log.close()
file_output_json.write(output_json)
file_output_json.close()
subprocess.Popen(["adb", "-s", "emulator-5554", "emu", "kill"])
print "-----------------"
print "-----------------"
print "-----------------"
print "-----------------\n\n"
if __name__ == '__main__':
main()