-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathextract_all_features_compressed.py
194 lines (114 loc) · 4.13 KB
/
extract_all_features_compressed.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
import sys
sys.path.append('/home/vikram_mm/.local/lib/python3.5/site-packages/')
from androguard.core.bytecodes import apk, dvm
from androguard.core.analysis import analysis
import numpy as np
import re
import cPickle
import os
max_h = 50
max_calls = 50
def extract_all_features():
print "loading dict..."
external_api_dict = cPickle.load( open( "common_dict_300.save", "rb" ) )
print "done!"
#X = []if __name__ if __name__ == '__main__':== '__main__':
#Y = []
#path_list = ["Dataset/benign","Dataset/all_drebin"]
path_list = ["Dataset/all_drebin"]
index = 0
for i in range(2):
count = 0
for path in os.listdir(path_list[i])[::-1]:
count+=1
if(count==34):
break
index+=1
print count,os.path.join(path_list[i],path)
try:
#X.append(get_feature_vector(os.path.join(path_list[i],path), external_api_dict))
#Y.append(i)
x = get_compressed_feature_vector(os.path.join(path_list[i],path), external_api_dict)
#print x.shape
#print x
#exit(0)
data_point = {}
data_point['x'] = x
data_point['y'] = 1
#fp = open(os.path.join('features',str(index) + '.save'), 'wb')
#fp = open(os.path.join('all_compressed_features',str(path) + '.save'), 'wb')
fp = open(os.path.join('acf2',str(path) + '.save'), 'wb')
cPickle.dump(data_point, fp, protocol = cPickle.HIGHEST_PROTOCOL)
fp.close()
except Exception as e:
print "exception occured"
print e
#count=count-1
#print X
#print Y
#print np.asarray(X)
#print np.asarray(Y)
#return np.asarray(X),np.asarray(Y)
return
def get_compressed_feature_vector(path, external_api_dict):
feature_vector = np.zeros((max_calls,max_h),dtype=int)
call_no = 0
seq_no = 0
if path.endswith('.apk'):
app = apk.APK(path)
app_dex = dvm.DalvikVMFormat(app.get_dex())
else:
app_dex = dvm.DalvikVMFormat(open(path, "rb").read())
app_x = analysis.newVMAnalysis(app_dex)
cs = [cc.get_name() for cc in app_dex.get_classes()]
# print len(app_dex.get_methods())
for method in app_dex.get_methods():
g = app_x.get_method(method)
if method.get_code() == None:
continue
# print "***********"
# print "method beeing investigated - ", g
for i in g.get_basic_blocks().get():
# print "i.childs : " ,i.childs
if(i.childs!=[] and seq_no<max_h):
call_no = 0
for ins in i.get_instructions():
# This is a string that contains methods, variables, or
# anything else.
output = ins.get_output()
match = re.search(r'(L[^;]*;)->[^\(]*\([^\)]*\).*', output)
if match and match.group(1) not in cs and call_no<max_calls:
# print "instruction : ", ins.get_basic_blocks()
# print "output : ", output
# print "external api detected: ", match.group()
# if(i.childs!=[]):
# print "-------->",i.childs[0][2].childs
# break
feature_vector[call_no,seq_no] = external_api_dict[match.group()]
call_no+=1
rand_child_selected = np.random.randint(len(i.childs))
# print rand_child_selected
traverse_graph(i.childs[rand_child_selected][2],feature_vector,cs,call_no,seq_no,external_api_dict)
seq_no+=1
return feature_vector
def traverse_graph(node,feature_vector,cs,call_no,seq_no,external_api_dict):
for ins in node.get_instructions():
output = ins.get_output()
match = re.search(r'(L[^;]*;)->[^\(]*\([^\)]*\).*', output)
if match and match.group(1) not in cs and call_no<max_calls:
feature_vector[call_no,seq_no] = external_api_dict[match.group()]
call_no+=1
if(call_no<max_calls and node.childs!=[]):
rand_child_selected = np.random.randint(len(node.childs))
traverse_graph(node.childs[rand_child_selected][2],feature_vector,cs,call_no,seq_no,external_api_dict)
def main():
"""
For test
"""
if __name__ == '__main__':
#x,y = load_data()
extract_all_features()
'''print x.shape
print y.shape
np.save('x200.npy', x)
np.save('y200.npy', y)'''