-
Notifications
You must be signed in to change notification settings - Fork 1
/
Android (API Calls)-Based Classification.py
211 lines (129 loc) · 3.7 KB
/
Android (API Calls)-Based Classification.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from androguard.misc import AnalyzeAPK
import pandas as pd
import os, sys
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
from sklearn.decomposition import PCA
from sklearn.model_selection import KFold
from sklearn.metrics import precision_score,recall_score,f1_score, roc_curve, accuracy_score
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.naive_bayes import GaussianNB as GNB
from sklearn.ensemble import VotingClassifier as VEL
from keras.models import Sequential
from keras.layers import Dense
from keras.models import Model
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
import keras
import warnings
import time
warnings.filterwarnings("ignore")
# In[2]:
malwares = [f for f in os.listdir(r'C:/Users/user/Desktop/Materials/Security/Assigment/Malware') if f.endswith('.apk')]
goodwares = [f for f in os.listdir(r'C:/Users/user/Desktop/Materials/Security/Assigment/Goodware') if f.endswith('.apk')]
# In[ ]:
begin = time.time()
api_calls = []
for i in malwares:
v = []
try:
x, xx, xxx = AnalyzeAPK(i)
for method in xxx.get_methods():
for y, yy, yyy in method.get_xref_to():
b = yy.name
v.append(b)
vv = list(dict.fromkeys(v))
api_calls.append(vv)
except:
pass
all_api_calls = []
for sub_a in api_calls:
for item in sub_a:
all_api_calls.append(item)
columns= list(dict.fromkeys(all_api_calls))
rows = []
for u in api_calls:
l= []
for uu in columns:
if uu in u:
l.append(1)
else:
l.append(0)
rows.append(l)
android_m= pd.DataFrame(rows, columns= columns)
android_m["type"]= "malware"
end = time.time()
print(end-begin)
# In[ ]:
android_m
# In[ ]:
begin = time.time()
api_calls_ = []
for i in goodwares:
v = []
try:
x, xx, xxx = AnalyzeAPK(i)
for method in xxx.get_methods():
for y, yy, yyy in method.get_xref_to():
b = yy.name
v.append(b)
vv = list(dict.fromkeys(v))
api_calls_.append(vv)
except:
pass
all_api_calls_ = []
for sub_a in api_calls_:
for item in sub_a:
all_api_calls_.append(item)
columns_= list(dict.fromkeys(all_api_calls_))
rows_ = []
for u in api_calls_:
l= []
for uu in columns_:
if uu in u:
l.append(1)
else:
l.append(0)
rows_.append(l)
android_g= pd.DataFrame(rows_, columns= columns_)
android_g["type"]= "goodware"
end = time.time()
print(end-begin)
# In[ ]:
android_g
# In[ ]:
android_3 = android_g.append(android_m, ignore_index = True)
android_2 = android_3.fillna(0)
target = android_2['type']
target = target.replace('malware', 0)
target = target.replace('goodware', 1)
target = target.astype(float)
features = android_2.drop(columns = ['type'])
features = features.astype(float)
android_1 = features
android_1['type'] = target
android = android_1.sample(frac = 1).reset_index(drop=True)
top_apis = ['startService', 'getDeviceId', 'createFromPdu', 'getClassLoader', 'getClass', 'getMethod',
'getDisplayOriginatingAddress', 'getInputStream', 'getOutputStream', 'killProcess', 'getLine1Number',
'getSimSerialNumber', 'getSubscriberId', 'getLastKnownLocation', 'isProviderEnabled', 'type']
c= android.columns
f = [x for x in c if x not in top_apis]
android = android.drop(columns = f)
android
# In[22]:
android.to_csv("Android API-calls dataset.csv")
# In[23]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]: