-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathtypeAD.py
716 lines (546 loc) · 25.2 KB
/
typeAD.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
'''
Type anomaly detection file
'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers.core import Dense
from keras import optimizers
import keras.backend as K
import json
from sklearn.utils import shuffle
import os
import sys
import time
'''
Data class processing
'''
class data_cls:
def __init__(self,train_test,**kwargs):
col_names = ["duration","protocol_type","service","flag","src_bytes",
"dst_bytes","land","wrong_fragment","urgent","hot","num_failed_logins",
"logged_in","num_compromised","root_shell","su_attempted","num_root",
"num_file_creations","num_shells","num_access_files","num_outbound_cmds",
"is_host_login","is_guest_login","count","srv_count","serror_rate",
"srv_serror_rate","rerror_rate","srv_rerror_rate","same_srv_rate",
"diff_srv_rate","srv_diff_host_rate","dst_host_count","dst_host_srv_count",
"dst_host_same_srv_rate","dst_host_diff_srv_rate","dst_host_same_src_port_rate",
"dst_host_srv_diff_host_rate","dst_host_serror_rate","dst_host_srv_serror_rate",
"dst_host_rerror_rate","dst_host_srv_rerror_rate","labels","dificulty"]
self.index = 0
# Data formated path and test path.
self.loaded = False
self.train_test = train_test
self.train_path = kwargs.get('train_path', '../../datasets/NSL/KDDTrain+.txt')
self.test_path = kwargs.get('test_path','../../datasets/NSL/KDDTest+.txt')
self.formated_train_path = kwargs.get('formated_train_path',
"../../datasets/formated/formated_train_type.data")
self.formated_test_path = kwargs.get('formated_test_path',
"../../datasets/formated/formated_test_type.data")
self.attack_types = ['normal','DoS','Probe','R2L','U2R']
self.attack_map = { 'normal': 'normal',
'back': 'DoS',
'land': 'DoS',
'neptune': 'DoS',
'pod': 'DoS',
'smurf': 'DoS',
'teardrop': 'DoS',
'mailbomb': 'DoS',
'apache2': 'DoS',
'processtable': 'DoS',
'udpstorm': 'DoS',
'ipsweep': 'Probe',
'nmap': 'Probe',
'portsweep': 'Probe',
'satan': 'Probe',
'mscan': 'Probe',
'saint': 'Probe',
'ftp_write': 'R2L',
'guess_passwd': 'R2L',
'imap': 'R2L',
'multihop': 'R2L',
'phf': 'R2L',
'spy': 'R2L',
'warezclient': 'R2L',
'warezmaster': 'R2L',
'sendmail': 'R2L',
'named': 'R2L',
'snmpgetattack': 'R2L',
'snmpguess': 'R2L',
'xlock': 'R2L',
'xsnoop': 'R2L',
'worm': 'R2L',
'buffer_overflow': 'U2R',
'loadmodule': 'U2R',
'perl': 'U2R',
'rootkit': 'U2R',
'httptunnel': 'U2R',
'ps': 'U2R',
'sqlattack': 'U2R',
'xterm': 'U2R'
}
formated = False
# Test formated data exists
if os.path.exists(self.formated_train_path) and os.path.exists(self.formated_test_path):
formated = True
# If it does not exist, it's needed to format the data
if not formated:
''' Formating the dataset for ready-2-use data'''
self.df = pd.read_csv(self.train_path,sep=',',names=col_names,index_col=False)
if 'dificulty' in self.df.columns:
self.df.drop('dificulty', axis=1, inplace=True) #in case of difficulty
data2 = pd.read_csv(self.test_path,sep=',',names=col_names,index_col=False)
if 'dificulty' in data2:
del(data2['dificulty'])
train_indx = self.df.shape[0]
frames = [self.df,data2]
self.df = pd.concat(frames)
# Dataframe processing
self.df = pd.concat([self.df.drop('protocol_type', axis=1), pd.get_dummies(self.df['protocol_type'])], axis=1)
self.df = pd.concat([self.df.drop('service', axis=1), pd.get_dummies(self.df['service'])], axis=1)
self.df = pd.concat([self.df.drop('flag', axis=1), pd.get_dummies(self.df['flag'])], axis=1)
# 1 if ``su root'' command attempted; 0 otherwise
self.df['su_attempted'] = self.df['su_attempted'].replace(2.0, 0.0)
# One-hot-Encoding for reaction.
all_labels = self.df['labels'] # Get all labels in df
mapped_labels = np.vectorize(self.attack_map.get)(all_labels) # Map attacks
self.df = self.df.reset_index(drop=True)
self.df = pd.concat([self.df.drop('labels', axis=1),pd.get_dummies(mapped_labels)], axis=1)
# Normalization of the df
#self.df = (self.df-self.df.mean())/(self.df.max()-self.df.min())
for indx,dtype in self.df.dtypes.iteritems():
if dtype == 'float64' or dtype == 'int64':
if self.df[indx].max() == 0 and self.df[indx].min()== 0:
self.df[indx] = 0
else:
self.df[indx] = (self.df[indx]-self.df[indx].min())/(self.df[indx].max()-self.df[indx].min())
# Save data
test_df = self.df.iloc[train_indx:self.df.shape[0]]
test_df = shuffle(test_df,random_state=np.random.randint(0,100))
self.df = self.df[:train_indx]
self.df = shuffle(self.df,random_state=np.random.randint(0,100))
test_df.to_csv(self.formated_test_path,sep=',',index=False)
self.df.to_csv(self.formated_train_path,sep=',',index=False)
''' Get n-row batch from the dataset
Return: df = n-rows
labels = correct labels for detection
Sequential for largest datasets
'''
def get_sequential_batch(self, batch_size=100):
if self.loaded is False:
self.df = pd.read_csv(self.formated_path,sep=',', nrows = batch_size)
self.loaded = True
else:
self.df = pd.read_csv(self.formated_path,sep=',', nrows = batch_size,
skiprows = self.index)
self.index += batch_size
labels = self.df[self.attack_types]
for att in self.attack_types:
del(self.df[att])
return self.df,labels
''' Get n-rows from loaded data
The dataset must be loaded in RAM
'''
def get_batch(self, batch_size=100):
if self.loaded is False:
self._load_df()
indexes = list(range(self.index,self.index+batch_size))
if max(indexes)>self.data_shape[0]-1:
dif = max(indexes)-self.data_shape[0]
indexes[len(indexes)-dif-1:len(indexes)] = list(range(dif+1))
self.index=batch_size-dif
batch = self.df.iloc[indexes]
else:
batch = self.df.iloc[indexes]
self.index += batch_size
labels = batch[self.attack_types]
for att in self.attack_types:
del(batch[att])
return batch,labels
def get_full(self):
if self.loaded is False:
self._load_df()
batch = self.df
labels = batch[self.attack_types]
for att in self.attack_types:
del(batch[att])
return batch,labels
def get_shape(self):
if self.loaded is False:
self._load_df()
self.data_shape = self.df.shape
# stata + labels
return self.data_shape
def _load_df(self):
if self.train_test == 'train':
self.df = pd.read_csv(self.formated_train_path,sep=',') # Read again the csv
else:
self.df = pd.read_csv(self.formated_test_path,sep=',')
self.index=0
# Shuffle again:
self.df = shuffle(self.df,random_state=np.random.randint(0,100))
self.loaded = True
def huber_loss(y_true, y_pred, clip_value=1):
assert clip_value > 0.
x = y_true - y_pred
if np.isinf(clip_value):
# Spacial case for infinity since Tensorflow does have problems
# if we compare `K.abs(x) < np.inf`.
return .5 * K.square(x)
condition = K.abs(x) < clip_value
squared_loss = .5 * K.square(x)
linear_loss = clip_value * (K.abs(x) - .5 * clip_value)
if K.backend() == 'tensorflow':
import tensorflow as tf
if hasattr(tf, 'select'):
return tf.select(condition, squared_loss, linear_loss)
else:
return tf.where(condition, squared_loss, linear_loss)
elif K.backend() == 'theano':
from theano import tensor as T
return T.switch(condition, squared_loss, linear_loss)
else:
raise RuntimeError('Unknown backend "{}".'.format(K.backend()))
import keras.losses
keras.losses.huber_loss = huber_loss
class QNetwork():
"""
Q-Network Estimator
Represents the global model for the table
"""
def __init__(self,obs_size,num_actions,hidden_size = 100,
hidden_layers = 1,learning_rate=.02):
"""
Initialize the network with the provided shape
"""
# Network arquitecture
self.model = Sequential()
# Add imput layer
self.model.add(Dense(hidden_size, input_shape=(obs_size,),
activation='relu'))
# Add hidden layers
for layers in range(hidden_layers):
self.model.add(Dense(hidden_size, activation='relu'))
# Add output layer
self.model.add(Dense(num_actions))
optimizer = optimizers.SGD(learning_rate)
# optimizer = optimizers.Adam(0.00025)
# optimizer = optimizers.AdaGrad(learning_rate)
# optimizer = optimizers.RMSpropGraves(learning_rate, 0.95, self.momentum, 1e-2)
# Compilation of the model with optimizer and loss
self.model.compile(loss=huber_loss,optimizer=optimizer)
def predict(self,state,batch_size=1):
"""
Predicts action values.
"""
return self.model.predict(state,batch_size=batch_size)
def update(self, states, q):
"""
Updates the estimator with the targets.
Args:
states: Target states
q: Estimated values
Returns:
The calculated loss on the batch.
"""
loss = self.model.train_on_batch(states, q)
return loss
def copy_model(model):
"""Returns a copy of a keras model."""
model.save('tmp_model')
return keras.models.load_model('tmp_model')
#Policy interface
class Policy:
def __init__(self, num_actions, estimator):
self.num_actions = num_actions
self.estimator = estimator
class Epsilon_greedy(Policy):
def __init__(self,estimator ,num_actions,epsilon,decay_rate, epoch_length):
Policy.__init__(self, num_actions, estimator)
self.name = "Epsilon Greedy"
if (epsilon is None or epsilon < 0 or epsilon > 1):
print("EpsilonGreedy: Invalid value of epsilon", flush = True)
sys.exit(0)
self.epsilon = epsilon
self.step_counter = 0
self.epoch_length = epoch_length
self.decay_rate = decay_rate
# # if epsilon set to 1, it will be decayed over time
# if self.epsilon == 1:
# self.epsilon_decay = True
# else:
# self.epsilon_decay = False
# Always decay
self.epsilon_decay = True
def get_actions(self,states):
# get next action
if np.random.rand() <= self.epsilon:
actions = np.random.randint(0, self.num_actions,states.shape[0])
else:
self.Q = self.estimator.predict(states,states.shape[0])
# TODO: fix performance in this loop
actions = []
for row in range(self.Q.shape[0]):
best_actions = np.argwhere(self.Q[row] == np.amax(self.Q[row]))
actions.append(best_actions[np.random.choice(len(best_actions))].item())
self.step_counter += 1
# decay epsilon after each epoch
if self.epsilon_decay:
if self.step_counter % self.epoch_length == 0:
self.epsilon = max(.01, self.epsilon * self.decay_rate**self.step_counter)
return actions
'''
Reinforcement learning Agent definition
'''
class Agent(object):
def __init__(self, actions,obs_size, policy="EpsilonGreedy", **kwargs):
self.actions = actions
self.num_actions = len(actions)
self.obs_size = obs_size
self.epsilon = kwargs.get('epsilon', 1)
self.gamma = kwargs.get('gamma', .001)
self.minibatch_size = kwargs.get('minibatch_size', 2)
self.epoch_length = kwargs.get('epoch_length', 100)
self.decay_rate = kwargs.get('decay_rate',0.99)
self.ExpRep = kwargs.get('ExpRep',True)
if self.ExpRep:
self.memory = ReplayMemory(self.obs_size, kwargs.get('mem_size', 10))
self.ddqn_time = 100
self.ddqn_update = self.ddqn_time
self.model_network = QNetwork(self.obs_size, self.num_actions,
kwargs.get('hidden_size', 100),
kwargs.get('hidden_layers',1),
kwargs.get('learning_rate',.1))
self.target_model_network = QNetwork(self.obs_size, self.num_actions,
kwargs.get('hidden_size', 100),
kwargs.get('hidden_layers',1),
kwargs.get('learning_rate',.1))
self.target_model_network.model = QNetwork.copy_model(self.model_network.model)
if policy == "EpsilonGreedy":
self.policy = Epsilon_greedy(self.model_network,len(actions),
self.epsilon,self.decay_rate,
self.epoch_length)
def act(self,states):
# Get actions under the policy
actions = self.policy.get_actions(states)
return actions
def learn(self, states, actions,next_states, rewards, done):
if self.ExpRep:
self.memory.observe(states, actions, rewards, done)
else:
self.states = states
self.actions = actions
self.next_states = next_states
self.rewards = rewards
self.done = done
def update_model(self):
if self.ExpRep:
(states, actions, rewards, next_states, done) = self.memory.sample_minibatch(self.minibatch_size)
else:
states = self.states
rewards = self.rewards
next_states = self.next_states
actions = self.actions
done = self.done
next_actions = []
# Compute Q targets
Q_prime = self.model_network.predict(next_states,self.minibatch_size)
# TODO: fix performance in this loop
for row in range(Q_prime.shape[0]):
best_next_actions = np.argwhere(Q_prime[row] == np.amax(Q_prime[row]))
next_actions.append(best_next_actions[np.random.choice(len(best_next_actions))].item())
sx = np.arange(len(next_actions))
# Compute Q(s,a)
Q = self.target_model_network.predict(states,self.minibatch_size)
# Q-learning update
# target = reward + gamma * max_a'{Q(next_state,next_action))}
targets = rewards.reshape(Q[sx,actions].shape) + \
self.gamma * Q_prime[sx,next_actions] * \
(1-done.reshape(Q[sx,actions].shape))
Q[sx,actions] = targets
loss = self.model_network.model.train_on_batch(states,Q)#inputs,targets
# timer to ddqn update
self.ddqn_update -= 1
if self.ddqn_update == 0:
self.ddqn_update = self.ddqn_time
# self.target_model_network.model = QNetwork.copy_model(self.model_network.model)
self.target_model_network.model.set_weights(self.model_network.model.get_weights())
return loss
class ReplayMemory(object):
"""Implements basic replay memory"""
def __init__(self, observation_size, max_size):
self.observation_size = observation_size
self.num_observed = 0
self.max_size = max_size
self.samples = {
'obs' : np.zeros(self.max_size * 1 * self.observation_size,
dtype=np.float32).reshape(self.max_size, self.observation_size),
'action' : np.zeros(self.max_size * 1, dtype=np.int16).reshape(self.max_size, 1),
'reward' : np.zeros(self.max_size * 1).reshape(self.max_size, 1),
'terminal' : np.zeros(self.max_size * 1, dtype=np.int16).reshape(self.max_size, 1),
}
def observe(self, state, action, reward, done):
index = self.num_observed % self.max_size
self.samples['obs'][index, :] = state
self.samples['action'][index, :] = action
self.samples['reward'][index, :] = reward
self.samples['terminal'][index, :] = done
self.num_observed += 1
def sample_minibatch(self, minibatch_size):
max_index = min(self.num_observed, self.max_size) - 1
sampled_indices = np.random.randint(max_index, size=minibatch_size)
s = np.asarray(self.samples['obs'][sampled_indices, :], dtype=np.float32)
s_next = np.asarray(self.samples['obs'][sampled_indices+1, :], dtype=np.float32)
a = self.samples['action'][sampled_indices].reshape(minibatch_size)
r = self.samples['reward'][sampled_indices].reshape((minibatch_size, 1))
done = self.samples['terminal'][sampled_indices].reshape((minibatch_size, 1))
return (s, a, r, s_next, done)
'''
Reinforcement learning Enviroment Definition
'''
class RLenv(data_cls):
def __init__(self,train_test,**kwargs):
data_cls.__init__(self,train_test,**kwargs)
self.data_shape = data_cls.get_shape(self)
self.batch_size = kwargs.get('batch_size',1) # experience replay -> batch = 1
self.iterations_episode = kwargs.get('iterations_episode',10)
if self.batch_size=='full':
self.batch_size = int(self.data_shape[0]/iterations_episode)
def _update_state(self):
self.states,self.labels = data_cls.get_batch(self,self.batch_size)
# Update statistics
self.true_labels += np.sum(self.labels).values
'''
Returns:
+ Observation of the enviroment
'''
def reset(self):
# Statistics
self.true_labels = np.zeros(len(env.attack_types),dtype=int)
self.estimated_labels = np.zeros(len(env.attack_types),dtype=int)
self.state_numb = 0
#self.states,self.labels = data_cls.get_sequential_batch(self,self.batch_size)
self.states,self.labels = data_cls.get_batch(self,self.batch_size)
# Update statistics
self.true_labels += np.sum(self.labels).values
self.total_reward = 0
self.steps_in_episode = 0
return self.states.values
'''
Returns:
State: Next state for the game
Reward: Actual reward
done: If the game ends (no end in this case)
'''
def act(self,actions):
# Clear previous rewards
self.reward = np.zeros(len(actions))
# Actualize new rewards == get_reward
self.reward = (actions == self.labels.values.argmax(axis=1)).astype(np.int32)
labels,counts = np.unique(actions,return_counts=True)
self.estimated_labels[labels] += counts
# Get new state and new true values
self._update_state()
# Done allways false in this continuous task
self.done = False
return self.states, self.reward, self.done
if __name__ == "__main__":
kdd_20_path = '../../datasets/NSL/KDDTrain+_20Percent.txt'
kdd_train = '../../datasets/NSL/KDDTrain+.txt'
kdd_test = '../../datasets/NSL/KDDTest+.txt'
formated_train_path = "../../datasets/formated/formated_train_type.data"
formated_test_path = "../../datasets/formated/formated_test_type.data"
# Valid actions = '0' supose no attack, '1' supose attack
epsilon = 1 # exploration
# Train batch
batch_size = 1
# batch of memory ExpRep
minibatch_size = 100
ExpRep = True
iterations_episode = 100
#3max_memory = 100
decay_rate = 0.99
gamma = 0.001
hidden_size = 100
hidden_layers = 3
# Initialization of the enviroment
env = RLenv('train',train_path=kdd_train,test_path=kdd_test,
formated_train_path = formated_train_path,
formated_test_path = formated_test_path,batch_size=batch_size,
iterations_episode=iterations_episode)
# num_episodes = int(env.data_shape[0]/(iterations_episode)/10)
num_episodes = 200
valid_actions = list(range(len(env.attack_types))) # only detect type of attack
num_actions = len(valid_actions)
# Initialization of the Agent
obs_size = env.data_shape[1]-len(env.attack_types)
agent = Agent(valid_actions,obs_size,"EpsilonGreedy",
epoch_length = iterations_episode,
epsilon = epsilon,
decay_rate = decay_rate,
gamma = gamma,
hidden_size=hidden_size,
hidden_layers=hidden_layers,
minibatch_size=minibatch_size,
mem_size = 10000,ExpRep=ExpRep)
# Statistics
reward_chain = []
loss_chain = []
# Main loop
for epoch in range(num_episodes):
start_time = time.time()
loss = 0.
total_reward_by_episode = 0
# Reset enviromet, actualize the data batch
states = env.reset()
done = False
# Iteration in one episode
for i_iteration in range(iterations_episode):
# Get actions for actual states following the policy
actions = agent.act(states)
#Enviroment actuation for this actions
next_states, reward, done = env.act(actions)
# If the epoch*batch_size*iterations_episode is largest than the df
agent.learn(states,actions,next_states,reward,done)
# Train network, update loss after at least minibatch_learns
if ExpRep and epoch*iterations_episode + i_iteration >= minibatch_size:
loss += agent.update_model()
elif not ExpRep:
loss += agent.update_model()
update_end_time = time.time()
# Update the state
states = next_states
# Update statistics
total_reward_by_episode += np.sum(reward,dtype=np.int32)
# Update user view
reward_chain.append(total_reward_by_episode)
loss_chain.append(loss)
# Correcting next states labels
env.true_labels -= np.sum(env.labels).values
end_time = time.time()
print("\r|Epoch {:03d}/{:03d} | Loss {:4.4f} |"
"Tot reward in ep {:03d}| time: {:2.2f}|"
.format(epoch, num_episodes
,loss, total_reward_by_episode,(end_time-start_time)))
print("\r|Estimated: {}|Labels: {}".format(env.estimated_labels,env.true_labels))
# Save trained model weights and architecture, used in test
agent.model_network.model.save_weights("models/type_model.h5", overwrite=True)
with open("models/type_model.json", "w") as outfile:
json.dump(agent.model_network.model.to_json(), outfile)
# Plot training results
plt.figure(1)
plt.subplot(211)
plt.plot(np.arange(len(reward_chain)),reward_chain)
plt.title('Total reward by episode')
plt.xlabel('n Episode')
plt.ylabel('Total reward')
plt.subplot(212)
plt.plot(np.arange(len(loss_chain)),loss_chain)
plt.title('Loss by episode')
plt.xlabel('n Episode')
plt.ylabel('loss')
plt.tight_layout()
#plt.show()
plt.savefig('results/train_type_improved.eps', format='eps', dpi=1000)