-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensemblePredict.py
319 lines (247 loc) · 9.73 KB
/
ensemblePredict.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
__author__ = "Laurence Elliott - 16600748"
import os, math, string, pefile, time, threading
import tkinter as tk
import numpy as np
from capstone import *
from keras.models import Sequential, Model, Input
from keras import layers, preprocessing
from keras.utils import Sequence
from sklearn.utils import shuffle
from tkinter import messagebox
from tkinter.filedialog import askopenfilenames
from tkinter.ttk import Progressbar
## Defining models (opcode, strings, and ensemble)
# Defining the opcode model
opModel = Sequential()
opModel.add(layers.InputLayer(input_shape=(50,)))
opModel.add(layers.Dense(256, activation='relu'))
opModel.add(layers.BatchNormalization())
opModel.add(layers.Dense(128, activation='relu'))
opModel.add(layers.BatchNormalization())
opModel.add(layers.Dense(64, activation='relu'))
opModel.add(layers.BatchNormalization())
opModel.add(layers.Dense(32, activation='relu'))
opModel.add(layers.BatchNormalization())
opModel.add(layers.Dense(16, activation='relu'))
opModel.add(layers.BatchNormalization())
opModel.add(layers.Dense(3, activation='softmax'))
opModel.load_weights("weights-improvement-574-0.85.hdf5")
opModel.compile(optimizer="rmsprop",
loss='categorical_crossentropy',
metrics=['accuracy'])
class histSequence(Sequence):
def __init__(self, x, y, batch_size):
self.x, self.y = shuffle(x, y)
self.batch_size = batch_size
def __len__(self):
return math.ceil(len(self.x) / self.batch_size)
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) *
self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) *
self.batch_size]
return np.array([
np.load(file_name)
for file_name in batch_x]), np.array(batch_y)
def on_epoch_end(self):
pass
class histSequenceVal(histSequence):
def __init__(self, x, y, batch_size):
self.x, self.y = x, y
self.batch_size = batch_size
# Defining the strings as greyscale images model
model = Sequential()
model.add(layers.InputLayer(input_shape=(100, 100, 1)))
model.add(layers.SpatialDropout2D(rate=0.2))
model.add(layers.Conv2D(32, kernel_size=3, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.SpatialDropout2D(rate=0.1))
model.add(layers.Conv2D(16, kernel_size=3, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.SpatialDropout2D(rate=0.1))
model.add(layers.Flatten())
model.add(layers.Dense(3, activation='softmax'))
class hashCorpusSequence(Sequence):
def __init__(self, x, y, batch_size):
self.x, self.y = shuffle(x, y)
self.batch_size = batch_size
def __len__(self):
return math.ceil(len(self.x) / self.batch_size)
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) *
self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) *
self.batch_size]
return np.array([
np.rint(((np.load(file_name) - np.min(np.load(file_name))) /
(np.max(np.load(file_name)) - np.min(np.load(file_name)))) * 255).astype(int)
for file_name in batch_x]), np.array(batch_y)
def on_epoch_end(self):
pass
class hashCorpusSequenceVal(hashCorpusSequence):
def __init__(self, x, y, batch_size):
self.x, self.y = x, y
self.batch_size = batch_size
model.load_weights("weights-improvement-04-0.72.hdf5")
model.compile(optimizer="adamax",
loss='categorical_crossentropy',
metrics=['accuracy'])
opModel.name = "opcodeModel"
model.name = "stringsAsGreyscaleModel"
def ensemble(models, model_inputs):
outputs = [models[0](model_inputs[0]), models[1](model_inputs[1])]
y = layers.average(outputs)
modelEns = Model(model_inputs, y, name='ensemble')
return modelEns
models = [opModel, model]
model_inputs = [Input(shape=(50,)), Input(shape=(100, 100, 1))]
modelEns = ensemble(models, model_inputs)
modelEns.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
## Pre-processing of PE (EXE, DLL, etc.) file(s)
# https://stackoverflow.com/questions/17195924/python-equivalent-of-unix-strings-utility
# Solution to Python based 'strings' alternative from SO. Decodes bytes of binary file as
# utf-8 strings
def strings(filename, min=4):
with open(filename, errors="ignore", encoding="utf-8") as f:
result = ""
for c in f.read():
if c in string.printable:
result += c
continue
if len(result) >= min:
yield result
result = ""
if len(result) >= min: # catch result at EOF
yield result
# Converting utf-8 string to sequence of words
def wordSequence(pePath):
try:
text = ""
for s in strings(pePath):
text += s + "\n"
sequence = preprocessing.text.text_to_word_sequence(text)[:10000]
return sequence
except Exception as e:
print(e)
# Hashing words of word sequences into sequences of word-specific integers
def hashWordSequences(sequences, maxSeqLen, vocabSize):
hashedSeqs = []
docCount = 0
for sequence in sequences:
try:
text = " ".join(sequence)
hashWordIDs = preprocessing.text.hashing_trick(text, round(vocabSize * 1.5), hash_function='md5')
docLen = len(hashWordIDs)
if docLen < maxSeqLen:
hashWordIDs += [0 for i in range(0, maxSeqLen-docLen)]
hashWordIDs = np.array(hashWordIDs).reshape(100, 100, 1)
hashedSeqs.append(hashWordIDs)
docCount += 1
except Exception as e:
print(e)
return hashedSeqs
# Function takes list of paths to PE files and returns a list
# of lists, with the first index as input for the opcode model,
# and the second index as input for the strings model
def preprocessPEs(pePaths):
mlInputs = []
# Get percentage opcode composition of file assembley code for the top 50 most common opcodes
# in each file
opCodeSet = set()
opCodeDicts = []
opCodeFreqs = {}
count = 1
for sample in pePaths:
try:
pe = pefile.PE(sample, fast_load=True)
entryPoint = pe.OPTIONAL_HEADER.AddressOfEntryPoint
data = pe.get_memory_mapped_image()[entryPoint:]
cs = Cs(CS_ARCH_X86, CS_MODE_32)
opcodes = []
for i in cs.disasm(data, 0x1000):
opcodes.append(i.mnemonic)
opcodeDict = {}
total = len(opcodes)
opCodeSet = set(list(opCodeSet) + opcodes)
for opcode in opCodeSet:
freq = 1
for op in opcodes:
if opcode == op:
freq += 1
try:
opCodeFreqs[opcode] += freq
except:
opCodeFreqs[opcode] = freq
opcodeDict[opcode] = round((freq / total) * 100, 2)
opCodeDicts.append(opcodeDict)
count += 1
except Exception as e:
print(e)
opCodeFreqsSorted = np.genfromtxt("top50opcodes.csv", delimiter=",", dtype="str")[1:, 0]
count = 0
for opDict in opCodeDicts:
opFreqVec = []
for opcode in opCodeFreqsSorted[:50]:
try:
opFreqVec.append(opDict[opcode])
except Exception as e:
if str(type(e)) == "<class 'KeyError'>":
opFreqVec.append(0.0)
mlInputs.append([np.array(opFreqVec)])
count += 1
# Get words from utf-8 strings decoded from raw bytes of files,
# and hash to vectors of integers
sequences = []
count = 0
for sample in pePaths:
sequences.append(wordSequence(sample))
count += 1
with open("finalVocabSize.txt", "r") as f:
maxVocabSize = int(f.readline())
hashSeqs = hashWordSequences(sequences, 10000, maxVocabSize)
count = 0
for hashSeq in hashSeqs:
mlInputs[count].append(np.array(hashSeq))
count += 1
mlInputs = np.array(mlInputs)
return mlInputs
## Function taking paths to PE files as input, and returning ensemble model predictions
# as output
def predictPEs(pePaths):
classNames = ["benign", "malware", "ransomware"]
pePredictions = {}
count = 0
for pePath in pePaths:
x1 = preprocessPEs(pePaths)[count][0].reshape(1, 50)
x2 = preprocessPEs(pePaths)[count][1].reshape(1, 100, 100, 1)
count += 1
pePredictions[pePath] = classNames[np.argmax(modelEns.predict(x=[x1, x2]))]
return pePredictions
if __name__ == "__main__":
tkRoot = tk.Tk()
tkRoot.title("Processing files...")
tkRoot.withdraw()
tkRoot.protocol("WM_DELETE_WINDOW", quit)
w = tkRoot.winfo_screenwidth()
h = tkRoot.winfo_screenheight()
size = tuple(int(pos) for pos in tkRoot.geometry().split('+')[0].split('x'))
x = w / 2 - size[0] / 2
y = h / 2 - size[1] / 2
tkRoot.geometry("300x1+{}+{}".format(round(x) - 150, round(y)))
while True:
try:
pePaths = list(askopenfilenames(filetypes=[("Windows executable files", "*.exe")]))
tkRoot.update()
tkRoot.deiconify()
preds = predictPEs(pePaths)
if len(preds) > 0:
classificationsStr = ""
for key in preds.keys():
# print("'" + key + "'" + " detected as " + preds[key])
classificationsStr += "'" + key + "'" + " detected as " + preds[key] + "\n\n"
tkRoot.withdraw()
messagebox.showinfo("Detections", classificationsStr)
else:
quit()
except Exception as e:
messagebox.showerror("Error", "Error: " + str(e) + "\nPlease try again...")