-
Notifications
You must be signed in to change notification settings - Fork 7
/
train_MalGAN.py
312 lines (266 loc) · 11.8 KB
/
train_MalGAN.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
# generator : 输入层维数:128(特征维数)+20(噪声维数) 隐层数:256 输出层:128
# subsititude detector: 128 - 256 - 1
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.neural_network import MLPClassifier
from sklearn import linear_model, svm, tree
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
import matplotlib.pyplot as plt
import numpy as np
from VOTEClassifier import VOTEClassifier
class MalGAN:
def __init__(
self,
blackbox="RF",
same_train_data=1,
filename="mydata.npz",
apifeature_dims=160,
):
self.apifeature_dims = apifeature_dims
self.z_dims = 20 # The larger the noise length is, the more API is added
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 = blackbox # RF LR DT SVM MLP VOTE
self.same_train_data = same_train_data # MalGAN and the black-boxdetector are trained on same or different training sets
optimizer = Adam(lr=0.001)
self.filename = filename
# 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.z_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":
blackbox_detector = RandomForestClassifier(
n_estimators=100, max_depth=10, random_state=1
)
elif self.blackbox is "SVM":
blackbox_detector = svm.SVC()
elif self.blackbox is "LR":
blackbox_detector = linear_model.LogisticRegression()
elif self.blackbox is "DT":
blackbox_detector = tree.DecisionTreeClassifier()
elif self.blackbox is "MLP":
blackbox_detector = MLPClassifier(
hidden_layer_sizes=(50,),
max_iter=10,
alpha=1e-4,
solver="sgd",
verbose=0,
tol=1e-4,
random_state=1,
learning_rate_init=0.1,
)
elif self.blackbox is "VOTE":
blackbox_detector = VOTEClassifier()
elif self.blackbox is "XGB":
blackbox_detector = XGBClassifier(max_depth=5, n_estimators=90)
return blackbox_detector
def build_generator(self):
example = Input(shape=(self.apifeature_dims,))
noise = Input(shape=(self.z_dims,))
x = Concatenate(axis=1)([example, noise])
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="substitute_detector")
substitute_detector.summary()
return substitute_detector
def load_data(self):
data = np.load(self.filename)
xmal, ymal, xben, yben = data["xmal"], data["ymal"], data["xben"], data["yben"]
return (xmal, ymal), (xben, yben)
def train(self, epochs=500, batch_size=32, is_first=1):
# Load and Split the dataset
(xmal, ymal), (xben, yben) = self.load_data()
xtrain_mal, xtest_mal, ytrain_mal, ytest_mal = train_test_split(
xmal, ymal, test_size=0.20
)
xtrain_ben, xtest_ben, ytrain_ben, ytest_ben = train_test_split(
xben, yben, test_size=0.20
)
if self.same_train_data: # use the same train_data
bl_xtrain_mal, bl_ytrain_mal, bl_xtrain_ben, bl_ytrain_ben = (
xtrain_mal,
ytrain_mal,
xtrain_ben,
ytrain_ben,
)
else: # use the different train_data
xtrain_mal, bl_xtrain_mal, ytrain_mal, bl_ytrain_mal = train_test_split(
xtrain_mal, ytrain_mal, test_size=0.50
)
xtrain_ben, bl_xtrain_ben, ytrain_ben, bl_ytrain_ben = train_test_split(
xtrain_ben, ytrain_ben, test_size=0.50
)
# if is_first is Ture, Train the blackbox_detctor
if is_first:
self.blackbox_detector.fit(
np.concatenate([xmal, xben]), np.concatenate([ymal, yben])
)
ytrain_ben_blackbox = self.blackbox_detector.predict(bl_xtrain_ben)
Original_Train_TPR = self.blackbox_detector.score(bl_xtrain_mal, bl_ytrain_mal)
Original_Test_TPR = self.blackbox_detector.score(xtest_mal, ytest_mal)
Train_TPR, Test_TPR = [Original_Train_TPR], [Original_Test_TPR]
best_TPR = 1.0
for epoch in range(epochs):
for step in range(xtrain_mal.shape[0] // batch_size):
# --------------------------
# Train substitute_detector
# --------------------------
# Select a random batch of malware examples
idx = np.random.randint(0, xtrain_mal.shape[0], batch_size)
xmal_batch = xtrain_mal[idx]
noise = np.random.uniform(0, 1, (batch_size, self.z_dims))
idx = np.random.randint(0, xmal_batch.shape[0], batch_size)
xben_batch = xtrain_ben[idx]
yben_batch = ytrain_ben_blackbox[idx]
# Generate a batch of new malware examples
gen_examples = self.generator.predict([xmal_batch, noise])
ymal_batch = self.blackbox_detector.predict(
np.ones(gen_examples.shape) * (gen_examples > 0.5)
)
# Train the substitute_detector
d_loss_fake = self.substitute_detector.train_on_batch(
gen_examples, ymal_batch
)
d_loss_real = self.substitute_detector.train_on_batch(
xben_batch, yben_batch
)
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
# ---------------------
# Train Generator
# ---------------------
idx = np.random.randint(0, xtrain_mal.shape[0], batch_size)
xmal_batch = xtrain_mal[idx]
noise = np.random.uniform(0, 1, (batch_size, self.z_dims))
# Train the generator
g_loss = self.combined.train_on_batch(
[xmal_batch, noise], np.zeros((batch_size, 1))
)
# Compute Train TPR
noise = np.random.uniform(0, 1, (xtrain_mal.shape[0], self.z_dims))
gen_examples = self.generator.predict([xtrain_mal, noise])
TPR = self.blackbox_detector.score(
np.ones(gen_examples.shape) * (gen_examples > 0.5), ytrain_mal
)
Train_TPR.append(TPR)
# Compute Test TPR
noise = np.random.uniform(0, 1, (xtest_mal.shape[0], self.z_dims))
gen_examples = self.generator.predict([xtest_mal, noise])
TPR = self.blackbox_detector.score(
np.ones(gen_examples.shape) * (gen_examples > 0.5), ytest_mal
)
Test_TPR.append(TPR)
# Save best model
if TPR < best_TPR:
self.combined.save_weights("saves/malgan.h5")
best_TPR = TPR
# Plot the progress
if is_first:
print(
"%d [D loss: %f, acc.: %.2f%%] [G loss: %f]"
% (epoch, d_loss[0], 100 * d_loss[1], g_loss)
)
flag = ["DiffTrainData", "SameTrainData"]
print("\n\n---{0} {1}".format(self.blackbox, flag[self.same_train_data]))
print(
"\nOriginal_Train_TPR: {0}, Adver_Train_TPR: {1}".format(
Original_Train_TPR, Train_TPR[-1]
)
)
print(
"\nOriginal_Test_TPR: {0}, Adver_Test_TPR: {1}".format(
Original_Test_TPR, Test_TPR[-1]
)
)
# Plot TPR
plt.figure()
plt.plot(
range(len(Train_TPR)), Train_TPR, c="r", label="Training Set", linewidth=2
)
plt.plot(
range(len(Test_TPR)),
Test_TPR,
c="g",
linestyle="--",
label="Validation Set",
linewidth=2,
)
plt.xlabel("Epoch")
plt.ylabel("TPR")
plt.legend()
plt.savefig(
"saves/Epoch_TPR({0}, {1}).png".format(
self.blackbox, flag[self.same_train_data]
)
)
plt.show()
def retrain_blackbox_detector(self):
(xmal, ymal), (xben, yben) = self.load_data()
xtrain_mal, xtest_mal, ytrain_mal, ytest_mal = train_test_split(
xmal, ymal, test_size=0.20
)
xtrain_ben, xtest_ben, ytrain_ben, ytest_ben = train_test_split(
xben, yben, test_size=0.20
)
# Generate Train Adversarial Examples
noise = np.random.uniform(0, 1, (xtrain_mal.shape[0], self.z_dims))
gen_examples = self.generator.predict([xtrain_mal, noise])
gen_examples = np.ones(gen_examples.shape) * (gen_examples > 0.5)
self.blackbox_detector.fit(
np.concatenate([xtrain_mal, xtrain_ben, gen_examples]),
np.concatenate([ytrain_mal, ytrain_ben, ytrain_mal]),
)
# Compute Train TPR
train_TPR = self.blackbox_detector.score(gen_examples, ytrain_mal)
# Compute Test TPR
noise = np.random.uniform(0, 1, (xtest_mal.shape[0], self.z_dims))
gen_examples = self.generator.predict([xtest_mal, noise])
gen_examples = np.ones(gen_examples.shape) * (gen_examples > 0.5)
test_TPR = self.blackbox_detector.score(gen_examples, ytest_mal)
print(
"\n---TPR after the black-box detector is retrained(Before Retraining MalGAN)."
)
print("\nTrain_TPR: {0}, Test_TPR: {1}".format(train_TPR, test_TPR))
if __name__ == "__main__":
malgan = MalGAN(blackbox="DT", filename="datanew60.npz", apifeature_dims=60)
malgan.train(epochs=100, batch_size=32)
malgan.retrain_blackbox_detector()
malgan.train(epochs=100, batch_size=32, is_first=False)