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
|
from attack.adversarialDataset import AdversarialC2Data
from torch.utils.data import DataLoader
from TargetModel.TargetLR import TargetLR
from TargetModel.TargetMLP import TargetMLP
from TargetModel.TargetSVM import TargetSVM
from TargetModel.TargetDT import TargetDT
from TargetModel.TargetIF import TargetIF
from sklearn.metrics import precision_score, recall_score, f1_score
from sklearn.metrics import confusion_matrix
def attack(arch, name, theta, gamma, modify_times, target_class, model, filter_name):
advmodel_filename = "/home/sunhanwu/work2021/TrafficAdversarial/experiment/src/modelFile/target_cicids_{}_{}.pkt".format(arch, name)
advdata_filename = "/home/sunhanwu/work2021/TrafficAdversarial/experiment/src/adversarialData/advdata_cicids_{}_{}_{}_{}_{}_{}.npy".format("dnn",
name, theta, gamma, modify_times, filter_name)
adv_data = AdversarialC2Data(advdata_filename, target_class=target_class, keep_target=True, norm=False)
model.load(advmodel_filename)
dataloader = DataLoader(adv_data, batch_size=128, shuffle=True)
y_pred, y_true = model.eval(dataloader)
precision = precision_score(y_true=y_true, y_pred=y_pred)
recall = recall_score(y_true=y_true, y_pred=y_pred)
f1 = f1_score(y_true=y_true, y_pred=y_pred)
con_matrix = confusion_matrix(y_true, y_pred)
print("{} pre: {:.2%}, recall: {:.2%}, f1: {:.2%}".format(name, precision, recall, f1))
print(con_matrix)
def attackMTA(arch, name, theta, gamma, modify_times, target_class, model, feature_type):
advmodel_filename = "/home/sunhanwu/work2021/TrafficAdversarial/experiment/src/modelFile/target_mta_{}_{}_{}.pkt".format(feature_type, arch, name)
print("target model: {}".format(advmodel_filename))
advdata_filename = "/home/sunhanwu/work2021/TrafficAdversarial/experiment/src/adversarialData/advdata_mta_{}_{}_{}_{}_{}_{}.npy".format(
feature_type, "dnn", name, theta, gamma, modify_times)
adv_data = AdversarialC2Data(advdata_filename, target_class=target_class, keep_target=True, norm=False)
model.load(advmodel_filename)
dataloader = DataLoader(adv_data, batch_size=128, shuffle=True)
y_pred, y_true = model.eval(dataloader)
precision = precision_score(y_true=y_true, y_pred=y_pred)
recall = recall_score(y_true=y_true, y_pred=y_pred)
f1 = f1_score(y_true=y_true, y_pred=y_pred)
con_matrix = confusion_matrix(y_true, y_pred)
print("{} pre: {:.2%}, recall: {:.2%}, f1: {:.2%}".format(name, precision, recall, f1))
print(con_matrix)
def attackMTACICFLowMeter(arch, name, theta, gamma, modify_times, target_class, model, proxy_arch):
advmodel_filename = "/home/sunhanwu/work2021/TrafficAdversarial/experiment/src/modelFile/target_mta_cicflowmeter_{}_{}.pkt".format(arch, name)
print("target model: {}".format(advmodel_filename))
advdata_filename = "/home/sunhanwu/work2021/TrafficAdversarial/experiment/src/adversarialData/advdata_mta_cicflowmeter_{}_{}_{}_{}_{}.npy".format(
proxy_arch, name, theta, gamma, modify_times)
adv_data = AdversarialC2Data(advdata_filename, target_class=target_class, keep_target=True, norm=False)
model.load(advmodel_filename)
dataloader = DataLoader(adv_data, batch_size=128, shuffle=True)
y_pred, y_true = model.eval(dataloader)
precision = precision_score(y_true=y_true, y_pred=y_pred)
recall = recall_score(y_true=y_true, y_pred=y_pred)
f1 = f1_score(y_true=y_true, y_pred=y_pred)
con_matrix = confusion_matrix(y_true, y_pred)
print("{} pre: {:.2%}, recall: {:.2%}, f1: {:.2%}".format(name, precision, recall, f1))
print(con_matrix)
def attackCICIDSModels():
theta = 1
gamma = 0.1
modify_times = 20
target_class = 0
param_lr = {
'C': 0.3
}
param_svm = {
'kernel': 'rbf',
}
param_if= {
'outliers_fraction1': 0.2,
"n_estimators": 200
}
param_mlp = {
'activate': 'relu',
'hidden_size': (50, 25, 13),
'learning_rate_init': 0.001,
'max_iter': 200,
'momentum': 0.9,
'solver': 'adam',
'alpha': 0.01,
'batch_size': 128
}
malwares = [
"Botnet",
"Fuzzing",
"PortScan",
"BruteForce",
"DDoS"
]
name = "DDoS"
arch = "if"
filer_name = "both"
lr = TargetLR(param_lr)
dt = TargetDT()
svm = TargetSVM(param_svm)
IF = TargetIF(param_if)
mlp = TargetMLP(param_mlp)
attack(arch,name,theta,gamma,modify_times,target_class,IF, filter_name=filer_name)
def attackMTAModels():
theta = 10.0
gamma = 0.9
modify_times = 20
target_class = 0
param_lr = {
'c': 0.3
}
param_svm = {
'kernel': 'rbf',
}
param_if= {
'outliers_fraction1': 0.2,
"n_estimators": 200
}
param_mlp = {
'activate': 'relu',
'hidden_size': (50, 25, 13),
'learning_rate_init': 0.001,
'max_iter': 200,
'momentum': 0.9,
'solver': 'adam',
'alpha': 0.01,
'batch_size': 128
}
malwares = [
"Botnet",
"Fuzzing",
"PortScan",
"BruteForce",
"DDoS"
]
Botnets = [
"Tofsee",
"Dridex",
"Quakbot",
"TrickBot",
"Gozi"
]
lr = TargetLR(param_lr)
dt = TargetDT()
svm = TargetSVM(param_svm)
IF = TargetIF(param_if)
mlp = TargetMLP(param_mlp)
model = [lr, dt, svm, IF, mlp]
model_s = ['lr', 'dt', 'svm', 'if', 'mlp']
proxy_arch = "dnn"
for i in range(5):
for botname in Botnets:
print("{}:{}".format(model_s[i], botname))
attackMTACICFLowMeter(model_s[i],botname,theta,gamma,modify_times,target_class,model[i], proxy_arch)
if __name__ == '__main__':
attackMTAModels()
|