forked from aditya5558/Android-Malware-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_all_features.py
166 lines (96 loc) · 3.71 KB
/
extract_all_features.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
#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( "15IT201_15IT217_M1_common_dict_300.save", "rb" ) )
print "done!"
path_list = ["15IT201_15IT217_M1_testcases/benign","15IT201_15IT217_M1_testcases/malware"]
index = 0
for i in range(2):
count = 0
for path in os.listdir(path_list[i]):
count+=1
if(count==5):
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_feature_vector(os.path.join(path_list[i],path), external_api_dict)
data_point = {}
data_point['x'] = x
data_point['y'] = i
#fp = open(os.path.join('features',str(index) + '.save'), 'wb')
fp = open(os.path.join('15IT201_15IT217_M1_testcases_features',str(path) + '.save'), 'wb')
cPickle.dump(data_point, fp, protocol = cPickle.HIGHEST_PROTOCOL)
fp.close()
except:
print "exception occured"
#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_feature_vector(path, external_api_dict):
feature_vector = np.zeros((max_calls,len(external_api_dict),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():
output = ins.get_output()
match = re.search(r'(L[^;]*;)->[^\(]*\([^\)]*\).*', output)
if match and match.group(1) not in cs:
# 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,external_api_dict[match.group()],seq_no] = 1
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,external_api_dict[match.group()],seq_no] = 1
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)
if __name__ == '__main__':
#x,y = load_data()
extract_all_features()