9
9
sys .path .append ('./KitNET' )
10
10
from KitNET .model import test_mut
11
11
12
- np .set_printoptions (precision = 2 )
13
- np .set_printoptions (threshold = 10000000 )
14
- np .set_printoptions (linewidth = 140 )
15
- np .set_printoptions (suppress = True )
16
-
17
-
18
- def logarithmic_compress (X , e = 16. ):
19
- for i in range (X .shape [0 ]):
20
- for j in range (X .shape [1 ]):
21
- if X [i ][j ] <= 1. - e :
22
- X [i ][j ] = - math .log (- X [i ][j ] - 1. , e )
23
- elif 1. - e < X [i ][j ] <= e - 1. :
24
- X [i ][j ] = 1. / (e - 1. ) * X [i ][j ]
25
- else :
26
- X [i ][j ] = math .log (X [i ][j ] + 1. , e )
27
- return X
28
-
29
12
30
13
def Euclidean_Distance (v1 , v2 ):
31
14
dis = np .linalg .norm (v1 - v2 )
32
15
return dis
33
16
34
17
35
18
class Analyzer :
36
-
37
- def __init__ (self , org_rmse_file , org_pcap_file , sta_data_file , model_save_path , limit = None ):
19
+ def __init__ (self ,
20
+ org_rmse_file ,
21
+ org_pcap_file ,
22
+ sta_data_file ,
23
+ model_save_path ,
24
+ limit = None ):
38
25
39
26
self .del_num = 0
40
27
@@ -53,7 +40,6 @@ def __init__(self, org_rmse_file, org_pcap_file, sta_data_file, model_save_path,
53
40
self .grp_size = self .X_list [0 ].mal .shape [0 ]
54
41
55
42
feature_list = []
56
- # print("feature_list",np.array(self.feature_list).shape)
57
43
for i in self .feature_list :
58
44
for j in i :
59
45
feature_list .append (j )
@@ -66,7 +52,7 @@ def __init__(self, org_rmse_file, org_pcap_file, sta_data_file, model_save_path,
66
52
self .feature_list [:, 82 :99 :4 ] = 0.
67
53
68
54
# compiling mutated features
69
- self .rmse_list = test_mut (self .feature_list ,model_save_path )
55
+ self .rmse_list = test_mut (self .feature_list , model_save_path )
70
56
self .rmse_list = np .array (self .rmse_list )
71
57
72
58
# Analyzing partial data
@@ -79,7 +65,8 @@ def __init__(self, org_rmse_file, org_pcap_file, sta_data_file, model_save_path,
79
65
self .glb_dis_list = self .glb_dis_list [:self .len ]
80
66
81
67
if len (self .rmse_list ) < len (self .org_rmse_list ) or limit is not None :
82
- print ("Warning: Statistics are incomplete !" , len (self .rmse_list ), "<" , len (self .org_rmse_list ))
68
+ print ("Warning: Statistics are incomplete !" , len (self .rmse_list ),
69
+ "<" , len (self .org_rmse_list ))
83
70
self .org_pcap = self .org_pcap [:self .len ]
84
71
self .org_rmse_list = self .org_rmse_list [:self .len ]
85
72
@@ -88,7 +75,8 @@ def del_outlier(self, extend=2):
88
75
# print(self.feature_list.shape)
89
76
org_rmse_mean = np .mean (self .org_rmse_list )
90
77
for i in range (self .len ):
91
- if self .rmse_list [i ] > self .org_rmse_list [i ] * extend or self .rmse_list [i ] > org_rmse_mean * extend :
78
+ if self .rmse_list [i ] > self .org_rmse_list [
79
+ i ] * extend or self .rmse_list [i ] > org_rmse_mean * extend :
92
80
self .rmse_list [i ] = 0.
93
81
del_list .append (i )
94
82
for j in range (self .feature_list .shape [1 ]):
@@ -113,7 +101,12 @@ def save_mutated_traffic(self, mut_pcap_path):
113
101
print ("Total #pkts in mutated traffic:" , len (pkt_List ))
114
102
wrpcap (mut_pcap_path , pkt_List )
115
103
116
- def eval (self , AD_threshold , mimic_set_file , test_feat_file , knormer_file , need_mmr = False ):
104
+ def eval (self ,
105
+ AD_threshold ,
106
+ mimic_set_file ,
107
+ test_feat_file ,
108
+ knormer_file ,
109
+ need_mmr = False ):
117
110
118
111
print ("1.Time elapse:" )
119
112
b = self .org_pcap [- 1 ].time - self .org_pcap [0 ].time
@@ -142,7 +135,7 @@ def eval(self, AD_threshold, mimic_set_file, test_feat_file, knormer_file, need_
142
135
a = np .mean (self .rmse_list )
143
136
print (" original:" , b , "mutated:" , a )
144
137
print (" PDR:" , (b - a ) / b )
145
- print ("-" * 64 )
138
+ print ("-" * 64 )
146
139
147
140
print ("# Detected:" )
148
141
b = self .org_rmse_list [self .org_rmse_list > AD_threshold ].shape [0 ]
@@ -166,14 +159,16 @@ def eval(self, AD_threshold, mimic_set_file, test_feat_file, knormer_file, need_
166
159
mut_dis = 0.
167
160
168
161
for i in range (mal_feat .shape [0 ]):
169
- org_dis += max (np .linalg .norm (mal_feat [i ] - mimic_feat , axis = 1 ))
170
- mut_dis += min (np .linalg .norm (mut_feat [i ] - mimic_feat , axis = 1 ))
162
+ org_dis += max (np .linalg .norm (mal_feat [i ] - mimic_feat ,
163
+ axis = 1 ))
164
+ mut_dis += min (np .linalg .norm (mut_feat [i ] - mimic_feat ,
165
+ axis = 1 ))
171
166
MMR = 1. - mut_dis / org_dis
172
167
print ("Feature Changed:" )
173
168
print (" Before:" , org_dis , "After:" , mut_dis )
174
169
print (" MMR:" , MMR )
175
170
176
- def plt_rmse (self ,AD_threshold ): # 对比原来的rmse和现在的rmse
171
+ def plt_rmse (self , AD_threshold ):
177
172
178
173
x = np .arange (0 , self .len , 1 )
179
174
plt .figure ()
@@ -182,11 +177,30 @@ def plt_rmse(self,AD_threshold): # 对比原来的rmse和现在的rmse
182
177
# plt.plot(x,[np.mean(np.log(self.org_rmse_list))]*self.len,c='#8A977B',alpha=0.5)
183
178
# plt.plot(x,[np.mean(np.log(self.rmse_list))]*self.len, c='#FE4365',alpha=0.5)
184
179
185
- plt .scatter (x , self .org_rmse_list , s = 12 , c = '#8A977B' , alpha = 0.5 , label = "Before" )
186
- plt .scatter (x , self .rmse_list , s = 12 , c = '#FE4365' , alpha = 0.5 , label = "After" )
187
- plt .plot (x , [np .mean (self .org_rmse_list )] * self .len , c = '#8A977B' , alpha = 0.3 , linewidth = 4 )
188
- plt .plot (x , [np .mean (self .rmse_list )] * self .len , c = '#FE4365' , alpha = 0.3 , linewidth = 4 )
189
- plt .plot (x , [AD_threshold ] * self .len , c = 'black' , linewidth = 2 ,label = "AD_threshold" )
180
+ plt .scatter (x ,
181
+ self .org_rmse_list ,
182
+ s = 12 ,
183
+ c = '#8A977B' ,
184
+ alpha = 0.5 ,
185
+ label = "Before" )
186
+ plt .scatter (x ,
187
+ self .rmse_list ,
188
+ s = 12 ,
189
+ c = '#FE4365' ,
190
+ alpha = 0.5 ,
191
+ label = "After" )
192
+ plt .plot (x , [np .mean (self .org_rmse_list )] * self .len ,
193
+ c = '#8A977B' ,
194
+ alpha = 0.3 ,
195
+ linewidth = 4 )
196
+ plt .plot (x , [np .mean (self .rmse_list )] * self .len ,
197
+ c = '#FE4365' ,
198
+ alpha = 0.3 ,
199
+ linewidth = 4 )
200
+ plt .plot (x , [AD_threshold ] * self .len ,
201
+ c = 'black' ,
202
+ linewidth = 2 ,
203
+ label = "AD_threshold" )
190
204
plt .title ("RMSE change and mean" )
191
205
plt .xlabel ('pkt no.' )
192
206
plt .ylabel ('RMSE in Kitsune' )
@@ -196,28 +210,49 @@ def plt_rmse(self,AD_threshold): # 对比原来的rmse和现在的rmse
196
210
plt .show ()
197
211
198
212
199
-
200
-
201
213
if __name__ == "__main__" :
202
214
203
215
parse = argparse .ArgumentParser ()
204
- parse .add_argument ('-op' , '--org_pcap_file' , type = str , required = True ,
216
+ parse .add_argument ('-op' ,
217
+ '--org_pcap_file' ,
218
+ type = str ,
219
+ required = True ,
205
220
help = "original malicious (test) traffic (.pcap)" )
206
221
207
- parse .add_argument ('-or' , '--org_rmse_file' , type = str , required = True ,
208
- help = "original RMSE file of test malicious traffic (.pkl)" )
222
+ parse .add_argument (
223
+ '-or' ,
224
+ '--org_rmse_file' ,
225
+ type = str ,
226
+ required = True ,
227
+ help = "original RMSE file of test malicious traffic (.pkl)" )
209
228
210
- parse .add_argument ('-of' , '--org_feat_file' , type = str ,
229
+ parse .add_argument ('-of' ,
230
+ '--org_feat_file' ,
231
+ type = str ,
211
232
help = "original (test) feature (.npy)" )
212
233
213
- parse .add_argument ('-b' , '--mimic_set' , type = str , required = True , help = "benign features to mimic (.npy)" )
214
-
215
- parse .add_argument ('-n' , '--normalizer' , type = str , required = True , help = "compiled feature normalizer (.pkl)" )
216
-
217
- parse .add_argument ('-sf' , '--sta_file' , type = str , default = './example/statistics.pkl' ,
234
+ parse .add_argument ('-b' ,
235
+ '--mimic_set' ,
236
+ type = str ,
237
+ required = True ,
238
+ help = "benign features to mimic (.npy)" )
239
+
240
+ parse .add_argument ('-n' ,
241
+ '--normalizer' ,
242
+ type = str ,
243
+ required = True ,
244
+ help = "compiled feature normalizer (.pkl)" )
245
+
246
+ parse .add_argument ('-sf' ,
247
+ '--sta_file' ,
248
+ type = str ,
249
+ default = './example/statistics.pkl' ,
218
250
help = "statistics to read(.pkl)" )
219
251
220
- parse .add_argument ('-mf' , '--model_file_path' , type = str , default = './example/model.pkl' ,
252
+ parse .add_argument ('-mf' ,
253
+ '--model_file_path' ,
254
+ type = str ,
255
+ default = './example/model.pkl' ,
221
256
help = "model_file after training" )
222
257
223
258
arg = parse .parse_args ()
@@ -237,15 +272,9 @@ def plt_rmse(self,AD_threshold): # 对比原来的rmse和现在的rmse
237
272
AD_threshold = pkl .load (f )
238
273
print ("AD_threshold:" , AD_threshold )
239
274
240
- a .eval (AD_threshold ,arg .mimic_set ,arg .org_feat_file ,arg .normalizer ,need_mmr = True )
275
+ a .eval (AD_threshold ,
276
+ arg .mimic_set ,
277
+ arg .org_feat_file ,
278
+ arg .normalizer ,
279
+ need_mmr = True )
241
280
a .plt_rmse (AD_threshold )
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
0 commit comments