-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstart.py
239 lines (191 loc) · 6.21 KB
/
start.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
import os
import sys
import atexit
import time
import click
import json
from pymongo import MongoClient
from multiprocessing import Pool, Process, Event
from tqdm import tqdm
from model.android_sample_model import AndroidSampleModel
from generator.method_generator import MethodCombGenerator
from generator.api_generator import ApiGenerator
from quark.Objects.quark import Quark
from db.database import DataBase
from utils.tools import distribute, api_filter
from itertools import repeat
db = DataBase()
@click.command()
@click.option(
"-a",
"--apk",
help="APK file",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
required=True,
)
@click.option(
"-e",
"--export",
help="Export all rules of apk to JSON file",
type=click.Path(exists=True, file_okay=False, dir_okay=True)
)
@click.option(
"-m",
"--multiprocess",
default=1,
help="Analyze APK with multiple processing, defaut will be single process",
type=click.INT,
show_default=True
)
@click.option(
"-d",
"--debug",
is_flag=True,
help="Debug mode, it will delete apk analying progress after finish",
)
@click.option(
"-s",
"--stage",
default=1,
type=click.INT,
show_default=True,
help="The stage of rule generate",
)
def main(apk, multiprocess, debug, export, stage):
"""Quark rule generate project"""
apk = AndroidSampleModel(apk)
# Export all rules into JSON files
if export:
result = db.find_rules_by_sample(apk.id)
if result["status"] is not 1:
if not click.confirm(f'The apk generate progress is not complete, Do you sure want to continue?'):
return
rules_num = len(result)
tqdm.write(f"Rules number: {rules_num}")
if click.confirm(f'This command will create {rules_num} file into {export}, Do you sure want to continue?'):
count = 1
tqdm.write(f"Start export rule into {export}")
for obj in tqdm(result):
rules = rule_obj_generate(obj, count)
f_name = f"{str(count)}.json"
path = os.path.join(export, f_name)
with open(path, "w") as f:
json.dump(rules, f, indent=4)
count += 1
return
# Apis generate
primary, secondary, p_count = api_filter(apk, 0.2)
if stage == 1:
first_apis = primary
second_apis = primary
elif stage == 2:
first_apis = primary
second_apis = secondary
elif stage == 3:
first_apis = secondary
second_apis = primary
elif stage == 4:
first_apis = secondary
second_apis = secondary
api_generator = ApiGenerator(first_apis)
apis = list(api_generator.initialize())
tqdm.write(f"Analyzing apk with {multiprocess} process")
if multiprocess == 1:
result = db.search_sample_data(apk.id)
new_apis = []
if result is not None:
done_apis = result["progress"]
new_apis = []
for single_api in apis:
if not single_api.id in done_apis:
new_apis.append(single_api)
else:
new_apis = apis
tqdm.write(f"APIs usage number: {len(apis)}")
tqdm.write(f"Analysis api done: {len(done_apis)}")
tqdm.write(f"The rest of APIs number: {len(new_apis)}")
generator = MethodCombGenerator(apk)
generator.first_stage_rule_generate(new_apis, primary)
else:
generate_multiprocess(apk, apis, second_apis, multiprocess)
db.set_status(apk.id, 1)
if debug:
tqdm.write("Start with debug mode, will delete data after process.")
result = db.search_sample_data(apk.id)
filename = result["filename"]
progress = result["progress"]
api_num = result["api_num"]
tqdm.write(f"Filename: {filename}")
tqdm.write(f"Progress number: {len(progress)}")
tqdm.write(f"APIS number: {api_num}")
db.delete_sample_data(apk.id)
def rule_obj_generate(rule, f_name):
api1 = rule["api1"]
api2 = rule["api2"]
cls1 = api1["class_name"]
cls2 = api2["class_name"]
md1 = api1["method_name"]
md2 = api2["method_name"]
des1 = api1["descriptor"]
des2 = api2["descriptor"]
description = f"{f_name}. {cls1}{md1}->{cls2}{md2}"
rule_obj = {
"crime": description,
"permission": [],
"api": [
{
"class": cls1,
"method": md1,
"descriptor": des1
},
{
"class": cls2,
"method": md2,
"descriptor": des2
}
],
"score": 1,
}
return rule_obj
def generate_multiprocess(apk, apis, second_apis, multiprocess):
result = db.search_sample_data(apk.id)
new_apis = []
if result is not None:
done_apis = result["progress"]
new_apis = []
for single_api in apis:
if not single_api.id in done_apis:
new_apis.append(single_api)
else:
new_apis = apis
# tqdm.write(f"APIs usage number: {len(apis)}")
# tqdm.write(f"Analysis api done: {len(done_apis)}")
# tqdm.write(f"The rest of APIs number: {len(new_apis)}")
api_pools = distribute(new_apis, multiprocess)
event = Event()
jobs = list()
for i in range(multiprocess):
p = Process(target=generate, args=(api_pools[i], second_apis, i+1, apk, event))
jobs.append(p)
p.start()
if len(new_apis) > multiprocess*2:
while True:
if event.is_set():
print("Exiting all child processess..")
for i in jobs:
#Terminate each process
i.terminate()
print("break")
#Terminating main process
time.sleep(1)
break
time.sleep(2)
generate_multiprocess(apk, new_apis, second_apis, multiprocess)
for j in jobs:
j.join()
def generate(f_pool, s_pool, pbar, apk, event):
generator = MethodCombGenerator(apk, pbar)
generator.first_stage_rule_generate(f_pool, s_pool)
event.set()
if __name__ == "__main__":
main()