forked from yanminglai/Malware-GAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMalGAN_v1.py
84 lines (65 loc) · 3.12 KB
/
MalGAN_v1.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
from __future__ import print_function, division
from keras.layers import Input, Dense, Activation
from keras.layers.merge import Maximum, Concatenate
from keras.models import Model
from keras.optimizers import Adam
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
class MalGAN():
def __init__(self):
self.apifeature_dims = 160
self.z_dims = 10
self.hide_layers = 256
self.generator_layers = [self.apifeature_dims+self.z_dims, self.hide_layers, self.apifeature_dims]
self.substitute_detector_layers = [self.apifeature_dims, self.hide_layers, 1]
self.blackbox = 'rf'
optimizer = Adam(lr=0.001)
# Build and Train blackbox_detector
self.blackbox_detector = self.build_blackbox_detector()
# Build and compile the substitute_detector
self.substitute_detector = self.build_substitute_detector()
self.substitute_detector.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
# Build the generator
self.generator = self.build_generator()
# The generator takes malware and noise as input and generates adversarial malware examples
example = Input(shape=(self.apifeature_dims,))
noise = Input(shape=(self.apifeature_dims,))
input = [example, noise]
malware_examples = self.generator(input)
# For the combined model we will only train the generator
self.substitute_detector.trainable = False
# The discriminator takes generated images as input and determines validity
validity = self.substitute_detector(malware_examples)
# The combined model (stacked generator and substitute_detector)
# Trains the generator to fool the discriminator
self.combined = Model(input, validity)
self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
def build_blackbox_detector(self):
if self.blackbox is 'rf':
black_detector = RandomForestClassifier(n_estimators=50, max_depth=4)
return black_detector
def build_generator(self):
example = Input(shape=(self.apifeature_dims,))
noise = Input(shape=(self.apifeature_dims,))
x = Concatenate([example, noise], axis=1)
for dim in self.generator_layers[1:]:
x = Dense(dim)(x)
x = Activation(activation='Sigmoid')(x)
x = Maximum([example, x])
generator = Model([example, noise], x, name='generator')
generator.summary()
return generator
def build_substitute_detector(self):
input = Input(shape=(self.substitute_detector_layers[0],))
x = input
for dim in self.substitute_detector_layers[1:]:
x = Dense(dim)(x)
x = Activation(activation='Sigmoid')(x)
substitute_detector = Model(input, x, name='generator')
substitute_detector.summary()
return substitute_detector
def load_data(self, filename):
(xmal, ymal), (xben, yben) = np.load(filename)
return (xmal, ymal), (xben, yben)