/
feature_Extraction.py
198 lines (127 loc) · 5.34 KB
/
feature_Extraction.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
'''
The given Code extracts all features of .apk file'''
from androguard.core.bytecodes.dvm import DalvikVMFormat
from androguard.core.analysis.analysis import Analysis
from androguard.decompiler.decompiler import DecompilerDAD
from androguard.core.bytecodes.apk import APK
from androguard.core.analysis import analysis
from androguard.core.bytecodes import dvm
from constants import SPECIAL_STRINGS, DB_REGEX, API_CALLS, PERMISSIONS,INTENTS
import math
import hashlib
def extract_features(file_path):
result = {}
try:
a = APK(file_path)
d = DalvikVMFormat(a.get_dex())
dx = Analysis(d)
vm = dvm.DalvikVMFormat(a.get_dex())
#vmx = analysis.uVMAnalysis(vm)
d.set_vmanalysis(dx)
d.set_decompiler(DecompilerDAD(d, dx))
except:
return None
result['android_version_code'] = a.get_androidversion_code()
result['android_version_name'] = a.get_androidversion_name()
result['max_sdk'] = a.get_max_sdk_version()
result['min_sdk'] = a.get_min_sdk_version()
#result['libraries'] = a.get_libraries()
result['filename'] = a.get_filename()
result['target_sdk'] = a.get_target_sdk_version()
result['md5'] = hashlib.md5(a.get_raw()).hexdigest()
result['sha256'] = hashlib.sha256(a.get_raw()).hexdigest()
result['permissions'] = a.get_permissions()
result['activities'] = a.get_activities()
result['providers'] = a.get_providers()
result['services'] = a.get_services()
result['strings'] = d.get_strings()
result['class_names'] = [c.get_name() for c in d.get_classes()]
result['method_names'] = [m.get_name() for m in d.get_methods()]
result['field_names'] = [f.get_name() for f in d.get_fields()]
#result['is_native_code'] = 1 if analysis.is_native_code(dx) else 0
result['is_obfuscation'] = 1 if analysis.is_ascii_obfuscation(d) else 0
#result['is_crypto_code'] = 1 if analysis.is_crypto_code(dx) else 0
'''result['is_dyn_code'] = 1 if analysis.is_dyn_code(dx) else 0
result['is_reflection_code'] = 1 if analysis.is_reflection_code(vmx) else 0'''
result['is_database'] = 1 if d.get_regex_strings(DB_REGEX) else 0
arr=[]
s=a.get_elements("action","name")
for i in s:
arr.append(i)
result['intents']=arr
s_list = []
s_list.extend(result['class_names'])
s_list.extend(result['method_names'])
s_list.extend(result['field_names'])
result['entropy_rate'] = entropy_rate(s_list)
result['feature_vectors'] = {}
# Search for the presence of api calls in a given apk
result['feature_vectors']['api_calls'] = []
for call in API_CALLS:
status = 1 if dx.get_method(call) else 0
result['feature_vectors']['api_calls'].append(status)
# Search for the presence of permissions in a given apk
result['feature_vectors']['permissions'] = []
for permission in PERMISSIONS:
status = 1 if permission in result['permissions'] else 0
result['feature_vectors']['permissions'].append(status)
#Search for the presence of intents in a given apk
result['feature_vectors']['intents']=[]
n=len(INTENTS)
m=len(result['intents'])
for i in range(n):
stri=INTENTS[i]
flg=False
for j in range(m):
if stri in result['intents'][j]:
flg=True
break
if flg:
status=1
else:
status=0
result['feature_vectors']['intents'].append(status)
#Check for special strings in code
result['feature_vectors']['special_strings'] = []
for word in SPECIAL_STRINGS:
status = 1 if d.get_regex_strings(word) else 0
result['feature_vectors']['special_strings'].append(status)
return result
#fuction to calculate entropy rate
def entropy_rate(data):
for s in data:
prob = [float(s.count(c)) / len(s) for c in dict.fromkeys(list(s))]
entropy = - sum([p * math.log(p) / math.log(2.0) for p in prob])
p = 1.0 / len(data)
idealize = -1.0 * len(data) * p * math.log(p) / math.log(2.0)
return round((abs(idealize) - entropy) / idealize, 2)
#fuction to create vector
def create_vector_single(apk):
feature_vector = []
feature_vector.extend(apk['feature_vectors']['permissions'])
feature_vector.extend(apk['feature_vectors']['api_calls'])
feature_vector.extend(apk['feature_vectors']['intents'])
feature_vector.extend(apk['feature_vectors']['special_strings'])
entropy_rate = int(apk['entropy_rate'])
db = int(apk['is_database'])
feature_vector.append(entropy_rate)
feature_vector.append(db)
return feature_vector
#
def create_vector_multiple(data):
feature_vector = []
target_vector = []
for apk in data:
apk_vector = []
apk_vector.extend(apk['feature_vectors']['permissions'])
apk_vector.extend(apk['feature_vectors']['api_calls'])
apk_vector.extend(apk['feature_vectors']['intents'])
apk_vector.extend(apk['feature_vectors']['special_strings'])
entropy_rate = int(apk['entropy_rate'])
db = int(apk['is_database'])
apk_vector.append(entropy_rate)
apk_vector.append(db)
target_type = 1 if apk['data_type'] == 'malware' else 0
feature_vector.append(apk_vector)
target_vector.append(target_type)
return feature_vector, target_vector