-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_builder_v2.py
283 lines (217 loc) · 9.1 KB
/
dataset_builder_v2.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
##
# Dataset builder with multiprocessing
##
import os
import sqlite3
import pickle
import shutil
from collections import OrderedDict
from math import ceil
from multiprocessing import Process, Queue
import mmh3
import numpy as np
from common import get_files_paths, split_list, update_progress
# Permissions paths
normal_perms = 'data/permissions/normal.txt'
signature_perms = 'data/permissions/signature.txt'
dangerous_perms = 'data/permissions/dangerous.txt'
# Permission levels (tmp values)
PERM_PROTECTION_LEVEL = {
'NO_LEVEL': 1,
'UNKNOWN': 2,
'NORMAL': 3,
'SIGNATURE': 4,
'DANGEROUS': 5
}
# Available api mapping levels
AXPLORER_APIS = [16, 17, 18, 19, 21, 22, 23, 25, 25]
PSCOUT_APIS = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
def get_perm_level_dict():
ret = {}
with open(normal_perms) as f:
for perm in f.read().strip().split('\n'):
ret[perm] = PERM_PROTECTION_LEVEL['NORMAL']
with open(signature_perms) as f:
for perm in f.read().strip().split('\n'):
ret[perm] = PERM_PROTECTION_LEVEL['SIGNATURE']
with open(dangerous_perms) as f:
for perm in f.read().strip().split('\n'):
ret[perm] = PERM_PROTECTION_LEVEL['DANGEROUS']
return ret
def load_mapping_data():
ret = {}
with sqlite3.connect('mappings/mapping.db') as conn:
cursor = conn.cursor()
table_names = []
cursor.execute('SELECT sql FROM sqlite_master WHERE sql IS NOT NULL')
table_names = [sql[0].split()[2] for sql in cursor.fetchall()]
for table_name in table_names:
key = '_'.join(table_name.split('_')[1:])
if key not in ret:
ret[key] = {}
cursor.execute('SELECT * FROM ' + table_name)
for row in cursor.fetchall():
if row[0] in ret[key]:
if not row[1] in ret[key][row[0]]:
ret[key][row[0]].append(row[1])
else:
ret[key][row[0]] = [row[1]]
return ret
class DatasetBuilderProcess(Process):
def __init__(self, process_id, file_list, mapping_dict, perm_level_dict, image_size, args):
super().__init__(target=self)
self.process_id = process_id
self.mapping_dict = mapping_dict
self.perm_level_dict = perm_level_dict
self.image_size = image_size
self.file_list = file_list
self.total_files = len(file_list)
self.queue = args[0]
def is_valid_api_level(self, api_level):
if api_level in AXPLORER_APIS or api_level in PSCOUT_APIS:
return True
else:
return False
def get_permission_level_by_api(self, api, api_level):
if api.startswith('Landroid'):
max_perm_level = PERM_PROTECTION_LEVEL['NO_LEVEL']
else:
max_perm_level = PERM_PROTECTION_LEVEL['UNKNOWN']
if not self.is_valid_api_level(api_level):
return max_perm_level
if api in self.mapping_dict['API_' + str(api_level)]:
perms = self.mapping_dict['API_' + str(api_level)][api]
for perm in perms:
if perm in self.perm_level_dict:
level = self.perm_level_dict[perm]
max_perm_level = max(max_perm_level, level)
return max_perm_level
def rgba2rgb(self, rgba_color):
alpha = rgba_color[-1]
rgb_bg = [0, 0, 0] # rgb background
rgb_color = [
ceil((1 - alpha) * rgb_bg[0] + alpha * rgba_color[0]), # 0 - r
ceil((1 - alpha) * rgb_bg[1] + alpha * rgba_color[1]), # 1 - g
ceil((1 - alpha) * rgb_bg[2] + alpha * rgba_color[2]) # 2 - b
]
return rgb_color
def protection_level_to_alpha(self, level):
return level / PERM_PROTECTION_LEVEL['DANGEROUS']
def to_3dim_24bit(self, x):
x &= 0xffffff
ret = []
for i in range(3):
_8bit = (x >> 8 * (2 - i)) & 0xff
ret.append(_8bit)
return ret
def unique(self, input_list, keep_order=False):
if keep_order:
return list(OrderedDict.fromkeys(input_list))
else:
return np.unique(input_list)
def encode_api(self, api):
return mmh3.hash(api) & 0xffffff
def run(self):
dataset = {}
dataset['labels'] = []
dataset['filenames'] = []
# create tmp dir for process and cd into it
if os.path.exists('tmp_dataset_builder'):
tmp_path = os.path.join('tmp_dataset_builder', str(self.process_id))
os.mkdir(tmp_path)
os.chdir(tmp_path)
else:
return
files_done = 0
for file in self.file_list:
try:
# TMP!! Ignore files with size more than 7 mb
if os.path.getsize(file) > (7 << 20): # to Mbs
continue
#
# Processing single file
with open(file) as f:
apis = f.read().strip().split('\n')
# (image_size * rgb channels)
data_row = np.zeros(self.image_size * 3, dtype=np.uint8)
cnt = 0
# For every api
for api in apis:
v3_api = self.to_3dim_24bit(self.encode_api(api)) # encode api -> 3 dim vector
protection_level = self.get_permission_level_by_api(api, 16) # get protection level of permission needed by api
alpha_level = self.protection_level_to_alpha(protection_level) # map protection level to interval (0, 1)
rgb_api = self.rgba2rgb(v3_api + [alpha_level]) # rgba (with api as rgb and protection level as apha) -> rgb
# data_row: (r channel, g channel, b channel)
data_row[cnt] = rgb_api[0]
data_row[cnt + self.image_size] = rgb_api[1]
data_row[cnt + self.image_size * 2] = rgb_api[2]
cnt += 1
# stack new row with dataset
if 'data' not in dataset:
dataset['data'] = np.array([data_row])
else:
dataset['data'] = np.vstack((dataset['data'], data_row))
# set label for current file
if 'malware' in file:
dataset['labels'].append(1)
else:
dataset['labels'].append(0)
# save filename
dataset['filenames'].append(os.path.basename(file).rstrip('.txt'))
files_done += 1
# save intermediate result
if files_done % 1000 == 0:
with open('chunk_%d.pkl' % (files_done // 1000), 'wb') as pkl_file:
pickle.dump(dataset, pkl_file)
dataset.clear()
dataset['labels'] = []
dataset['filenames'] = []
except Exception as e:
print('\n%s\n%s\n' % (file, e))
if files_done % 100 == 0:
print('Process %d progress: %d out of %d' % (self.process_id, files_done, self.total_files))
with open('chunk_%d.pkl' % ((files_done // 1000) + 1), 'wb') as pkl_file:
pickle.dump(dataset, pkl_file)
# self.queue.put(dataset)
# self.queue.put('done')
print('----------------> Process %d is done!' % self.process_id)
def main():
mapping_dict = load_mapping_data()
perm_level_dict = get_perm_level_dict()
api_seq_files = get_files_paths('api_sequences/')
# total_files = len(api_seq_files)
image_size = 384 * 384 # Let it be :D
# Give apks chunk for every process
process_count = 6
chunks = split_list(api_seq_files, process_count)
# Multiprocessing stuff
queue = Queue()
processes = [
DatasetBuilderProcess(i, chunks[i], mapping_dict, perm_level_dict,
image_size, (queue,)) for i in range(process_count)]
print('Building dataset...')
if not os.path.exists('tmp_dataset_builder'):
os.mkdir('tmp_dataset_builder')
for process in processes:
process.start()
# Collect dataset chunks from every process
# i_chunk = 0
# for _ in range(process_count):
# dataset_chunk = queue.get()
# dataset_chunk['labels'] = np.array(dataset_chunk['labels'])
# dataset_chunk['filenames'] = np.array(dataset_chunk['filenames'])
# with open('dataset_chunk_%d.bin' % i_chunk, 'wb') as pickle_file:
# pickle.dump(dataset_chunk, pickle_file)
# i_chunk += 1
for process in processes:
process.join()
# dataset['labels'] = np.array(dataset['labels'])
# dataset['filenames'] = np.array(dataset['filenames'])
# Serialize dataset dictionary object
# with open('dataset.bin', 'wb') as pickle_file:
# pickle.dump(dataset, pickle_file)
# print('Dataset dictionary object is dumped to dataset.bin.')
# shutil.rmtree('tmp_dataset_builder')
print('Done.')
if __name__ == '__main__':
main()