-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMalwareDetector.py
268 lines (198 loc) · 7.61 KB
/
MalwareDetector.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 12 09:52:18 2018
@author: gowtham
"""
from keras.models import model_from_json
import pandas as pd
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
classifier = model_from_json(loaded_model_json)
classifier.load_weights("model.h5")
#******************************************************************************
import os
import shutil
import sys
app = input("Please Enter the app Name : ")
app_name = ""
for j in app:
if j != " ":
app_name = app_name + j
else:
app_name = app_name + "_"
os.rename(app, app_name)
script = "apktool d -s -o " + app_name[:-4] +" " + app_name
print script
print('extracting and decompiling ' + app_name + " .........")
os.popen(script).readlines()
print(app_name + " is sucsessfully extracted and decompiled :)")
def csv_write(file_, lst, index):
if index < len(lst):
file_.write(str(lst[index]) + "\t")
else:
file_.write("\t")
i = app_name[:-4] + "/AndroidManifest.xml"
j = app_name[:-4] + ".tsv"
try:
a = open(i, 'r')
except:
print("Error in Decompiling")
sys.exit(0)
a.close()
csvf = open(j, "w")
print(app_name[:-4] + ".tsv" + " is created")
actions = []
permissions = []
uses_permissions = []
uses_features = []
categories = []
activities = []
activity_aliases = []
datas = []
meta_datas = []
providers = []
recievers = []
services = []
import xml.etree.ElementTree as ET
tree = ET.parse(i)
root = tree.getroot()
for action in root.iter('action'):#done
#print action.attrib.values()[0]
actions.append(action.attrib.values()[0])
for activity in root.iter('activity'):#done
#print activity.attrib.values()
activities.append(dict(zip([key[44:] for key in activity.attrib.keys()], activity.attrib.values())))
for cate in root.iter('category'):#done
#print cate.attrib.values()[0]
categories.append(cate.attrib.values()[0])
for uses_permission in root.iter('uses-permission'):#done
#print uses_permission.attrib.values()[0]
uses_permissions.append(uses_permission.attrib.values()[0])
for uses_feature in root.iter('uses-feature'):#done
#print uses_feature.attrib.values()
uses_features.append(dict(zip([key[44:] for key in uses_feature.attrib.keys()], uses_feature.attrib.values())))
for permission in root.iter('permission'):#done
#print permission.attrib.values()[1]
permissions.append(dict(zip([key[44:] for key in permission.attrib.keys()], permission.attrib.values())))
for activity_alias in root.iter('activity-alias'):
#print activity_alias.attrib.values()
activity_aliases.append(dict(zip([key[44:] for key in activity_alias.attrib.keys()], activity_alias.attrib.values())))
for data in root.iter('data'):
#print data.attrib
datas.append(dict(zip([key[44:] for key in data.attrib.keys()], data.attrib.values())))
for meta_data in root.iter("meta-data"):
#print meta_data.attrib.values()
meta_datas.append(dict(zip([key[44:] for key in meta_data.attrib.keys()], meta_data.attrib.values())))
for provider in root.iter("provider"):
#print provider.attrib
providers.append(dict(zip([key[44:] for key in provider.attrib.keys()], provider.attrib.values())))
for receiver in root.iter("receiver"):
#print receiver.attrib
recievers.append(dict(zip([key[44:] for key in receiver.attrib.keys()], receiver.attrib.values())))
for service in root.iter("service"):
#print service.attrib
services.append(dict(zip([key[44:] for key in service.attrib.keys()], service.attrib.values())))
csvf.write("Permissions\t Categories\t Activities\t Activity Aliases\t Uses Permissions\t Uses Features\t datas\t Meta Datas\t Providers\t Receivers\t Services\t Actions \n")
maxi = max(len(recievers), len(services), len(activity_aliases), len(meta_datas),len(providers), len(datas),len(permissions), len(actions), len(uses_permissions), len(uses_features),len(categories), len(activities))
for i in range(maxi):
csv_write(csvf, permissions, i,)
csv_write(csvf, categories, i,)
csv_write(csvf, activities, i,)
csv_write(csvf, activity_aliases, i,)
csv_write(csvf, uses_permissions, i,)
csv_write(csvf, uses_features, i,)
csv_write(csvf, datas, i,)
csv_write(csvf, meta_datas, i,)
csv_write(csvf, providers, i,)
csv_write(csvf, recievers, i,)
csv_write(csvf, services, i,)
if i < len(actions):
csvf.write(actions[i] + "\n")
else:
csvf.write("\n")
csvf.close()
shutil.rmtree(app_name[:-4])
#******************************************************************************
data_set = open(app_name[:-4] + "_tmp.tsv", 'w')
data_set.write("App\t Permissions\t Categories\t Activities\t Activity Aliases\t Uses Permissions\t Uses Features\t datas\t Meta Datas\t Providers\t Receivers\t Services\t Actions \n")
data = pd.read_csv(app_name[:-4] + ".tsv", sep = '\t')
data_set.write(str(app_name[:-4]) + "\t ")
for i in range(data.shape[1]):
if i != data.shape[1] - 1:
data_set.write(str(list(data.iloc[:, i].values)) + "\t")
else:
data_set.write(str(list(data.iloc[:, i].values)) + "\n ")
data_set.close()
#******************************************************************************
from EncodeAFeature import encodeFeature
import ast
permissions = pd.read_csv('Permissions.csv', header = None)
permissions_l = list(permissions.iloc[:, 0].values)
uses_features = pd.read_csv('Uses-Features.csv', header = None)
uses_features_l = list(uses_features.iloc[:, 0].values)
actions = pd.read_csv('intents.csv', header = None)
actions_l = list(actions.iloc[:, 0].values)
cwd = os.getcwd()
csvf = open(app_name[:-4] + "_ENC.tsv", "w")
data = pd.read_csv(app_name[:-4] + ".tsv", sep = '\t')
for i in encodeFeature(data, permissions_l, 4, 548):
csvf.write(str(i) + "\t")
FeauX = data.iloc[:, 5].values
b = []
i = 0
while(not pd.isnull(FeauX[i]) and (not data.shape[0] == i+1) ):
dic = ast.literal_eval(FeauX[i])
if 'name' in dic.keys():
if dic['name'] in uses_features_l:
b.append(uses_features_l.index(dic['name']))
else:
b.append(73)
else:
b.append(73)
i = i +1
encoded_lst = []
for i in range(74):
encoded_lst.append(0)
for i in b:
encoded_lst[int(i)] = encoded_lst[int(i)] + 1
for i in encoded_lst:
csvf.write(str(i) + "\t")
FeauX = data.iloc[:, 0].values
b = []
i = 0
while(not pd.isnull(FeauX[i]) and (not data.shape[0] == i+1)):
i = i+1
csvf.write(str(i) + '\t')
for i in encodeFeature(data, actions_l, -1, 291):
csvf.write(str(i) + "\t")
csvf.close()
#******************************************************************************
data_set = open(app_name[:-4] + "_R.tsv", 'w')
data_set.write("App\t ")
for i in range(548):
data_set.write("P" + str(i+1) + "\t")
data_set.write("Other Permissions\t")
for i in range(73):
data_set.write("F" + str(i+1) + "\t")
data_set.write("Other Features\t Number of created permissions \t")
for i in range(291):
data_set.write("I" + str(i+1) + "\t")
data_set.write("Other Intents \n")
data = open(app_name[:-4] + "_ENC.tsv", 'r')
data_set.write(app_name[:-4] + "\t ")
data_set.write(data.read())
data.close()
data_set.close()
#******************************************************************************
app_f = pd.read_csv(app_name[:-4] + "_R.tsv", sep = "\t", index_col = False)
remove = pd.read_csv("remove.csv", header = None)
for i in remove[0]:
del app_f[i]
features = app_f.iloc[:, 1:388].values
prediction = classifier.predict(features)
if prediction == 0:
print(app_name + " contains Malware")
else:
print("No malware is found in " + app_name)