-
Notifications
You must be signed in to change notification settings - Fork 38
/
lucid_dataset_parser.py
700 lines (572 loc) · 30.7 KB
/
lucid_dataset_parser.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
# Copyright (c) 2022 @ FBK - Fondazione Bruno Kessler
# Author: Roberto Doriguzzi-Corin
# Project: LUCID: A Practical, Lightweight Deep Learning Solution for DDoS Attack Detection
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import time
import pyshark
import socket
import pickle
import random
import hashlib
import argparse
import ipaddress
from sklearn.feature_extraction.text import CountVectorizer
from multiprocessing import Process, Manager, Value, Queue
from util_functions import *
# Sample commands
# split a pcap file into smaller chunks to leverage multi-core CPUs: tcpdump -r dataset.pcap -w dataset-chunk -C 1000
# dataset parsing (first step): python3 lucid_dataset_parser.py --dataset_type SYN2020 --dataset_folder ./sample-dataset/ --packets_per_flow 10 --dataset_id SYN2020 --traffic_type all --time_window 10
# dataset parsing (second step): python3 lucid_dataset_parser.py --preprocess_folder ./sample-dataset/
IDS2018_DDOS_FLOWS = {'attackers': ['18.218.115.60', '18.219.9.1','18.219.32.43','18.218.55.126','52.14.136.135','18.219.5.43','18.216.200.189','18.218.229.235','18.218.11.51','18.216.24.42'],
'victims': ['18.218.83.150','172.31.69.28']}
IDS2017_DDOS_FLOWS = {'attackers': ['172.16.0.1'],
'victims': ['192.168.10.50']}
CUSTOM_DDOS_SYN = {'attackers': ['11.0.0.' + str(x) for x in range(1,255)],
'victims': ['10.42.0.2']}
DOS2019_FLOWS = {'attackers': ['172.16.0.5'], 'victims': ['192.168.50.1', '192.168.50.4']}
DDOS_ATTACK_SPECS = {
'DOS2017' : IDS2017_DDOS_FLOWS,
'DOS2018' : IDS2018_DDOS_FLOWS,
'SYN2020' : CUSTOM_DDOS_SYN,
'DOS2019': DOS2019_FLOWS
}
vector_proto = CountVectorizer()
vector_proto.fit_transform(protocols).todense()
random.seed(SEED)
np.random.seed(SEED)
class packet_features:
def __init__(self):
self.id_fwd = (0,0,0,0,0) # 5-tuple src_ip_addr, src_port,,dst_ip_addr,dst_port,protocol
self.id_bwd = (0,0,0,0,0) # 5-tuple src_ip_addr, src_port,,dst_ip_addr,dst_port,protocol
self.features_list = []
def __str__(self):
return "{} -> {}".format(self.id_fwd, self.features_list)
def get_ddos_flows(attackers,victims):
DDOS_FLOWS = {}
if '/' in attackers: # subnet
DDOS_FLOWS['attackers'] = [str(ip) for ip in list(ipaddress.IPv4Network(attackers).hosts())]
else: # single address
DDOS_FLOWS['attackers'] = [str(ipaddress.IPv4Address(attackers))]
if '/' in victims: # subnet
DDOS_FLOWS['victims'] = [str(ip) for ip in list(ipaddress.IPv4Network(victims).hosts())]
else: # single address
DDOS_FLOWS['victims'] = [str(ipaddress.IPv4Address(victims))]
return DDOS_FLOWS
# function that build the labels based on the dataset type
def parse_labels(dataset_type=None, attackers=None,victims=None, label=1):
output_dict = {}
if attackers is not None and victims is not None:
DDOS_FLOWS = get_ddos_flows(attackers, victims)
elif dataset_type is not None and dataset_type in DDOS_ATTACK_SPECS:
DDOS_FLOWS = DDOS_ATTACK_SPECS[dataset_type]
else:
return None
for attacker in DDOS_FLOWS['attackers']:
for victim in DDOS_FLOWS['victims']:
ip_src = str(attacker)
ip_dst = str(victim)
key_fwd = (ip_src, ip_dst)
key_bwd = (ip_dst, ip_src)
if key_fwd not in output_dict:
output_dict[key_fwd] = label
if key_bwd not in output_dict:
output_dict[key_bwd] = label
return output_dict
def parse_packet(pkt):
pf = packet_features()
tmp_id = [0,0,0,0,0]
try:
pf.features_list.append(float(pkt.sniff_timestamp)) # timestampchild.find('Tag').text
pf.features_list.append(int(pkt.ip.len)) # packet length
pf.features_list.append(int(hashlib.sha256(str(pkt.highest_layer).encode('utf-8')).hexdigest(),
16) % 10 ** 8) # highest layer in the packet
pf.features_list.append(int(int(pkt.ip.flags, 16))) # IP flags
tmp_id[0] = str(pkt.ip.src) # int(ipaddress.IPv4Address(pkt.ip.src))
tmp_id[2] = str(pkt.ip.dst) # int(ipaddress.IPv4Address(pkt.ip.dst))
protocols = vector_proto.transform([pkt.frame_info.protocols]).toarray().tolist()[0]
protocols = [1 if i >= 1 else 0 for i in
protocols] # we do not want the protocols counted more than once (sometimes they are listed twice in pkt.frame_info.protocols)
protocols_value = int(np.dot(np.array(protocols), powers_of_two))
pf.features_list.append(protocols_value)
protocol = int(pkt.ip.proto)
tmp_id[4] = protocol
if pkt.transport_layer != None:
if protocol == socket.IPPROTO_TCP:
tmp_id[1] = int(pkt.tcp.srcport)
tmp_id[3] = int(pkt.tcp.dstport)
pf.features_list.append(int(pkt.tcp.len)) # TCP length
pf.features_list.append(int(pkt.tcp.ack)) # TCP ack
pf.features_list.append(int(pkt.tcp.flags, 16)) # TCP flags
pf.features_list.append(int(pkt.tcp.window_size_value)) # TCP window size
pf.features_list = pf.features_list + [0, 0] # UDP + ICMP positions
elif protocol == socket.IPPROTO_UDP:
pf.features_list = pf.features_list + [0, 0, 0, 0] # TCP positions
tmp_id[1] = int(pkt.udp.srcport)
pf.features_list.append(int(pkt.udp.length)) # UDP length
tmp_id[3] = int(pkt.udp.dstport)
pf.features_list = pf.features_list + [0] # ICMP position
elif protocol == socket.IPPROTO_ICMP:
pf.features_list = pf.features_list + [0, 0, 0, 0, 0] # TCP and UDP positions
pf.features_list.append(int(pkt.icmp.type)) # ICMP type
else:
pf.features_list = pf.features_list + [0, 0, 0, 0, 0, 0] # padding for layer3-only packets
tmp_id[4] = 0
pf.id_fwd = (tmp_id[0], tmp_id[1], tmp_id[2], tmp_id[3], tmp_id[4])
pf.id_bwd = (tmp_id[2], tmp_id[3], tmp_id[0], tmp_id[1], tmp_id[4])
return pf
except AttributeError as e:
# ignore packets that aren't TCP/UDP or IPv4
return None
# Offline preprocessing of pcap files for model training, validation and testing
def process_pcap(pcap_file,dataset_type,in_labels,max_flow_len,labelled_flows,max_flows=0, traffic_type='all',time_window=TIME_WINDOW):
start_time = time.time()
temp_dict = OrderedDict()
start_time_window = -1
pcap_name = pcap_file.split("/")[-1]
print("Processing file: ", pcap_name)
cap = pyshark.FileCapture(pcap_file)
for i, pkt in enumerate(cap):
if i % 1000 == 0:
print(pcap_name + " packet #", i)
# start_time_window is used to group packets/flows captured in a time-window
if start_time_window == -1 or float(pkt.sniff_timestamp) > start_time_window + time_window:
start_time_window = float(pkt.sniff_timestamp)
pf = parse_packet(pkt)
store_packet(pf, temp_dict, start_time_window, max_flow_len)
if max_flows > 0 and len(temp_dict) >= max_flows:
break
apply_labels(temp_dict, labelled_flows, in_labels, traffic_type)
print('Completed file {} in {} seconds.'.format(pcap_name, time.time() - start_time))
# Transforms live traffic into input samples for inference
def process_live_traffic(cap, dataset_type, in_labels, max_flow_len, traffic_type='all',time_window=TIME_WINDOW):
start_time = time.time()
temp_dict = OrderedDict()
labelled_flows = []
start_time_window = start_time
time_window = start_time_window + time_window
if isinstance(cap, pyshark.LiveCapture) == True:
for pkt in cap.sniff_continuously():
if time.time() >= time_window:
break
pf = parse_packet(pkt)
temp_dict = store_packet(pf, temp_dict, start_time_window, max_flow_len)
elif isinstance(cap, pyshark.FileCapture) == True:
while time.time() < time_window:
try:
pkt = cap.next()
pf = parse_packet(pkt)
temp_dict = store_packet(pf,temp_dict,start_time_window,max_flow_len)
except:
break
apply_labels(temp_dict,labelled_flows, in_labels,traffic_type)
return labelled_flows
def store_packet(pf,temp_dict,start_time_window, max_flow_len):
if pf is not None:
if pf.id_fwd in temp_dict and start_time_window in temp_dict[pf.id_fwd] and \
temp_dict[pf.id_fwd][start_time_window].shape[0] < max_flow_len:
temp_dict[pf.id_fwd][start_time_window] = np.vstack(
[temp_dict[pf.id_fwd][start_time_window], pf.features_list])
elif pf.id_bwd in temp_dict and start_time_window in temp_dict[pf.id_bwd] and \
temp_dict[pf.id_bwd][start_time_window].shape[0] < max_flow_len:
temp_dict[pf.id_bwd][start_time_window] = np.vstack(
[temp_dict[pf.id_bwd][start_time_window], pf.features_list])
else:
if pf.id_fwd not in temp_dict and pf.id_bwd not in temp_dict:
temp_dict[pf.id_fwd] = {start_time_window: np.array([pf.features_list]), 'label': 0}
elif pf.id_fwd in temp_dict and start_time_window not in temp_dict[pf.id_fwd]:
temp_dict[pf.id_fwd][start_time_window] = np.array([pf.features_list])
elif pf.id_bwd in temp_dict and start_time_window not in temp_dict[pf.id_bwd]:
temp_dict[pf.id_bwd][start_time_window] = np.array([pf.features_list])
return temp_dict
def apply_labels(flows, labelled_flows, labels, traffic_type):
for five_tuple, flow in flows.items():
if labels is not None:
short_key = (five_tuple[0], five_tuple[2]) # for IDS2017/IDS2018 dataset the labels have shorter keys
flow['label'] = labels.get(short_key, 0)
for flow_key, packet_list in flow.items():
# relative time wrt the time of the first packet in the flow
if flow_key != 'label':
amin = np.amin(packet_list,axis=0)[0]
packet_list[:, 0] = packet_list[:, 0] - amin
if traffic_type == 'ddos' and flow['label'] == 0: # we only want malicious flows from this dataset
continue
elif traffic_type == 'benign' and flow['label'] > 0: # we only want benign flows from this dataset
continue
else:
labelled_flows.append((five_tuple,flow))
# returns the total number of flows
def count_flows(preprocessed_flows):
ddos_flows = 0
total_flows = len(preprocessed_flows)
ddos_fragments = 0
total_fragments = 0
for flow in preprocessed_flows:
flow_fragments = len(flow[1]) - 1
total_fragments += flow_fragments
if flow[1]['label'] > 0:
ddos_flows += 1
ddos_fragments += flow_fragments # the label does not count
return (total_flows, ddos_flows, total_flows - ddos_flows), (total_fragments, ddos_fragments, total_fragments-ddos_fragments)
# balance the dataset based on the number of benign and malicious fragments of flows
def balance_dataset(flows,total_fragments=float('inf')):
new_flow_list = []
_,(_, ddos_fragments, benign_fragments) = count_flows(flows)
if ddos_fragments == 0 or benign_fragments == 0:
min_fragments = total_fragments
else:
min_fragments = min(total_fragments/2,ddos_fragments,benign_fragments)
random.shuffle(flows)
new_benign_fragments = 0
new_ddos_fragments = 0
for flow in flows:
if flow[1]['label'] == 0 and (new_benign_fragments < min_fragments ):
new_benign_fragments += len(flow[1]) - 1
new_flow_list.append(flow)
elif flow[1]['label'] > 0 and (new_ddos_fragments < min_fragments):
new_ddos_fragments += len(flow[1]) - 1
new_flow_list.append(flow)
return new_flow_list, new_benign_fragments, new_ddos_fragments
# convert the dataset from dictionaries with 5-tuples keys into a list of flow fragments and another list of labels
def dataset_to_list_of_fragments(dataset):
keys = []
X = []
y = []
for flow in dataset:
tuple = flow[0]
flow_data = flow[1]
label = flow_data['label']
for key, fragment in flow_data.items():
if key != 'label':
X.append(fragment)
y.append(label)
keys.append(tuple)
return X,y,keys
def train_test_split(flow_list,train_size=TRAIN_SIZE, shuffle=True):
test_list = []
_,(total_examples,_,_) = count_flows(flow_list)
test_examples = total_examples - total_examples*train_size
if shuffle == True:
random.shuffle(flow_list)
current_test_examples = 0
while current_test_examples < test_examples:
flow = flow_list.pop(0)
test_list.append(flow)
current_test_examples += len(flow[1])-1
return flow_list,test_list
def main(argv):
command_options = " ".join(str(x) for x in argv[1:])
help_string = 'Usage[0]: python3 lucid_dataset_parser.py --dataset_type <dataset_name> --dataset_folder <folder path> --dataset_id <dataset identifier> --packets_per_flow <n> --time_window <t>\n' \
'Usage[1]: python3 lucid_dataset_parser.py --preprocess_folder <folder path>'
manager = Manager()
parser = argparse.ArgumentParser(
description='Dataset parser',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-d', '--dataset_folder', nargs='+', type=str,
help='Folder with the dataset')
parser.add_argument('-o', '--output_folder', nargs='+', type=str,
help='Output folder')
parser.add_argument('-f', '--traffic_type', default='all', nargs='+', type=str,
help='Type of flow to process (all, benign, ddos)')
parser.add_argument('-p', '--preprocess_folder', nargs='+', type=str,
help='Folder with preprocessed data')
parser.add_argument('--preprocess_file', nargs='+', type=str,
help='File with preprocessed data')
parser.add_argument('-b', '--balance_folder', nargs='+', type=str,
help='Folder where balancing datasets')
parser.add_argument('-n', '--packets_per_flow', nargs='+', type=str,
help='Packet per flow sample')
parser.add_argument('-s', '--samples', default=float('inf'), type=int,
help='Number of training samples in the reduced output')
parser.add_argument('-i', '--dataset_id', nargs='+', type=str,
help='String to append to the names of output files')
parser.add_argument('-m', '--max_flows', default=0, type=int,
help='Max number of flows to extract from the pcap files')
parser.add_argument('-l', '--label', default=1, type=int,
help='Label assigned to the DDoS class')
parser.add_argument('-t', '--dataset_type', nargs='+', type=str,
help='Type of the dataset. Available options are: DOS2017, DOS2018, DOS2019, SYN2020')
parser.add_argument('-w', '--time_window', nargs='+', type=str,
help='Length of the time window')
parser.add_argument('--no_split', help='Do not split the dataset', action='store_true')
args = parser.parse_args()
if args.packets_per_flow is not None:
max_flow_len = int(args.packets_per_flow[0])
else:
max_flow_len = MAX_FLOW_LEN
if args.time_window is not None:
time_window = float(args.time_window[0])
else:
time_window = TIME_WINDOW
if args.dataset_id is not None:
dataset_id = str(args.dataset_id[0])
else:
dataset_id = ''
if args.traffic_type is not None:
traffic_type = str(args.traffic_type[0])
else:
traffic_type = 'all'
if args.dataset_folder is not None and args.dataset_type is not None:
process_list = []
flows_list = []
if args.output_folder is not None and os.path.isdir(args.output_folder[0]) is True:
output_folder = args.output_folder[0]
else:
output_folder = args.dataset_folder[0]
filelist = glob.glob(args.dataset_folder[0]+ '/*.pcap')
in_labels = parse_labels(args.dataset_type[0],args.dataset_folder[0],label=args.label)
start_time = time.time()
for file in filelist:
try:
flows = manager.list()
p = Process(target=process_pcap,args=(file,args.dataset_type[0],in_labels,max_flow_len,flows,args.max_flows, traffic_type,time_window))
process_list.append(p)
flows_list.append(flows)
except FileNotFoundError as e:
continue
for p in process_list:
p.start()
for p in process_list:
p.join()
np.seterr(divide='ignore', invalid='ignore')
try:
preprocessed_flows = list(flows_list[0])
except:
print ("ERROR: No traffic flows. \nPlease check that the dataset folder name (" + args.dataset_folder[0] + ") is correct and \nthe folder contains the traffic traces in pcap format (the pcap extension is mandatory)")
exit(1)
#concatenation of the features
for results in flows_list[1:]:
preprocessed_flows = preprocessed_flows + list(results)
process_time = time.time()-start_time
if dataset_id == '':
dataset_id = str(args.dataset_type[0])
filename = str(int(time_window)) + 't-' + str(max_flow_len) + 'n-' + dataset_id + '-preprocess'
output_file = output_folder + '/' + filename
output_file = output_file.replace("//", "/") # remove double slashes when needed
with open(output_file + '.data', 'wb') as filehandle:
# store the data as binary data stream
pickle.dump(preprocessed_flows, filehandle)
(total_flows, ddos_flows, benign_flows), (total_fragments, ddos_fragments, benign_fragments) = count_flows(preprocessed_flows)
log_string = time.strftime("%Y-%m-%d %H:%M:%S") + " | dataset_type:" + args.dataset_type[0] + \
" | flows (tot,ben,ddos):(" + str(total_flows) + "," + str(benign_flows) + "," + str(ddos_flows) + \
") | fragments (tot,ben,ddos):(" + str(total_fragments) + "," + str(benign_fragments) + "," + str(ddos_fragments) + \
") | options:" + command_options + " | process_time:" + str(process_time) + " |\n"
print (log_string)
# saving log file
with open(output_folder + '/history.log', "a") as myfile:
myfile.write(log_string)
if args.preprocess_folder is not None or args.preprocess_file is not None:
if args.preprocess_folder is not None:
output_folder = args.output_folder[0] if args.output_folder is not None else args.preprocess_folder[0]
filelist = glob.glob(args.preprocess_folder[0] + '/*.data')
else:
output_folder = args.output_folder[0] if args.output_folder is not None else os.path.dirname(os.path.realpath(args.preprocess_file[0]))
filelist = args.preprocess_file
# obtain time_window and flow_len from filename and ensure that all files have the same values
time_window = None
max_flow_len = None
dataset_id = None
for file in filelist:
filename = file.split('/')[-1].strip()
current_time_window = int(filename.split('-')[0].strip().replace('t',''))
current_max_flow_len = int(filename.split('-')[1].strip().replace('n',''))
current_dataset_id = str(filename.split('-')[2].strip())
if time_window != None and current_time_window != time_window:
print ("Incosistent time windows!!")
exit()
else:
time_window = current_time_window
if max_flow_len != None and current_max_flow_len != max_flow_len:
print ("Incosistent flow lengths!!")
exit()
else:
max_flow_len = current_max_flow_len
if dataset_id != None and current_dataset_id != dataset_id:
dataset_id = "IDS201X"
else:
dataset_id = current_dataset_id
preprocessed_flows = []
for file in filelist:
with open(file, 'rb') as filehandle:
# read the data as binary data stream
preprocessed_flows = preprocessed_flows + pickle.load(filehandle)
# balance samples and redux the number of samples when requested
preprocessed_flows, benign_fragments, ddos_fragments = balance_dataset(preprocessed_flows,args.samples)
if len(preprocessed_flows) == 0:
print("Empty dataset!")
exit()
preprocessed_train, preprocessed_test = train_test_split(preprocessed_flows,train_size=TRAIN_SIZE, shuffle=True)
preprocessed_train, preprocessed_val = train_test_split(preprocessed_train, train_size=TRAIN_SIZE, shuffle=True)
X_train, y_train, _ = dataset_to_list_of_fragments(preprocessed_train)
X_val, y_val, _ = dataset_to_list_of_fragments(preprocessed_val)
X_test, y_test, _ = dataset_to_list_of_fragments(preprocessed_test)
# normalization and padding
X_full = X_train + X_val + X_test
y_full = y_train + y_val + y_test
mins,maxs = static_min_max(time_window=time_window)
total_examples = len(y_full)
total_ddos_examples = np.count_nonzero(y_full)
total_benign_examples = total_examples - total_ddos_examples
output_file = output_folder + '/' + str(time_window) + 't-' + str(max_flow_len) + 'n-' + dataset_id + '-dataset'
if args.no_split == True: # don't split the dataset
norm_X_full = normalize_and_padding(X_full, mins, maxs, max_flow_len)
#norm_X_full = padding(X_full,max_flow_len) # only padding
norm_X_full_np = np.array(norm_X_full)
y_full_np = np.array(y_full)
hf = h5py.File(output_file + '-full.hdf5', 'w')
hf.create_dataset('set_x', data=norm_X_full_np)
hf.create_dataset('set_y', data=y_full_np)
hf.close()
[full_packets] = count_packets_in_dataset([norm_X_full_np])
log_string = time.strftime("%Y-%m-%d %H:%M:%S") + " | Total examples (tot,ben,ddos):(" + str(total_examples) + "," + str(total_benign_examples) + "," + str(total_ddos_examples) + \
") | Total packets:(" + str(full_packets) + \
") | options:" + command_options + " |\n"
else:
norm_X_train = normalize_and_padding(X_train,mins,maxs,max_flow_len)
norm_X_val = normalize_and_padding(X_val, mins, maxs, max_flow_len)
norm_X_test = normalize_and_padding(X_test, mins, maxs, max_flow_len)
norm_X_train_np = np.array(norm_X_train)
y_train_np = np.array(y_train)
norm_X_val_np = np.array(norm_X_val)
y_val_np = np.array(y_val)
norm_X_test_np = np.array(norm_X_test)
y_test_np = np.array(y_test)
hf = h5py.File(output_file + '-train.hdf5', 'w')
hf.create_dataset('set_x', data=norm_X_train_np)
hf.create_dataset('set_y', data=y_train_np)
hf.close()
hf = h5py.File(output_file + '-val.hdf5', 'w')
hf.create_dataset('set_x', data=norm_X_val_np)
hf.create_dataset('set_y', data=y_val_np)
hf.close()
hf = h5py.File(output_file + '-test.hdf5', 'w')
hf.create_dataset('set_x', data=norm_X_test_np)
hf.create_dataset('set_y', data=y_test_np)
hf.close()
[train_packets, val_packets, test_packets] = count_packets_in_dataset([norm_X_train_np, norm_X_val_np, norm_X_test_np])
log_string = time.strftime("%Y-%m-%d %H:%M:%S") + " | examples (tot,ben,ddos):(" + str(total_examples) + "," + str(total_benign_examples) + "," + str(total_ddos_examples) + \
") | Train/Val/Test sizes: (" + str(norm_X_train_np.shape[0]) + "," + str(norm_X_val_np.shape[0]) + "," + str(norm_X_test_np.shape[0]) + \
") | Packets (train,val,test):(" + str(train_packets) + "," + str(val_packets) + "," + str(test_packets) + \
") | options:" + command_options + " |\n"
print(log_string)
# saving log file
with open(output_folder + '/history.log', "a") as myfile:
myfile.write(log_string)
if args.balance_folder is not None and args.output_folder is not None:
output_folder = args.output_folder[0] if args.output_folder is not None else args.balance_folder[0]
datasets = []
for folder in args.balance_folder:
datasets += glob.glob(folder + '/*.hdf5')
train_filelist = {}
val_filelist = {}
test_filelist = {}
min_samples_train = float('inf')
min_samples_val = float('inf')
min_samples_test = float('inf')
output_filename_prefix = None
for file in datasets:
filename = file.split('/')[-1].strip()
dataset = h5py.File(file, "r")
X = np.array(dataset["set_x"][:]) # features
Y = np.array(dataset["set_y"][:]) # labels
if 'train' in filename:
key = filename.split('dataset')[0].strip() + 'dataset-balanced-train.hdf5'
if output_filename_prefix ==None:
output_filename_prefix = filename.split('IDS')[0].strip()
else:
if filename.split('IDS')[0].strip() != output_filename_prefix:
print ("Inconsistent datasets!")
exit()
train_filelist[key] = (X,Y)
if X.shape[0] < min_samples_train:
min_samples_train = X.shape[0]
elif 'val' in filename:
key = filename.split('dataset')[0].strip() + 'dataset-balanced-val.hdf5'
if output_filename_prefix ==None:
output_filename_prefix = filename.split('IDS')[0].strip()
else:
if filename.split('IDS')[0].strip() != output_filename_prefix:
print ("Inconsistent datasets!")
exit()
val_filelist[key] = (X,Y)
if X.shape[0] < min_samples_val:
min_samples_val = X.shape[0]
elif 'test' in filename:
key = filename.split('dataset')[0].strip() + 'dataset-balanced-test.hdf5'
if output_filename_prefix ==None:
output_filename_prefix = filename.split('IDS')[0].strip()
else:
if filename.split('IDS')[0].strip() != output_filename_prefix:
print ("Inconsistent datasets!")
exit()
test_filelist[key] = (X, Y)
if X.shape[0] < min_samples_test:
min_samples_test = X.shape[0]
final_X = {'train':None,'val':None,'test':None}
final_y = {'train':None,'val':None,'test':None}
for key,value in train_filelist.items():
X_short = value[0][:min_samples_train,...]
y_short = value[1][:min_samples_train,...]
if final_X['train'] is None:
final_X['train'] = X_short
final_y['train'] = y_short
else:
final_X['train'] = np.vstack((final_X['train'],X_short))
final_y['train'] = np.hstack((final_y['train'],y_short))
for key,value in val_filelist.items():
X_short = value[0][:min_samples_val,...]
y_short = value[1][:min_samples_val,...]
if final_X['val'] is None:
final_X['val'] = X_short
final_y['val'] = y_short
else:
final_X['val'] = np.vstack((final_X['val'],X_short))
final_y['val'] = np.hstack((final_y['val'],y_short))
for key,value in test_filelist.items():
X_short = value[0][:min_samples_test,...]
y_short = value[1][:min_samples_test,...]
if final_X['test'] is None:
final_X['test'] = X_short
final_y['test'] = y_short
else:
final_X['test'] = np.vstack((final_X['test'],X_short))
final_y['test'] = np.hstack((final_y['test'],y_short))
for key,value in final_X.items():
filename = output_filename_prefix + 'IDS201X-dataset-balanced-' + key + '.hdf5'
hf = h5py.File(output_folder + '/' + filename, 'w')
hf.create_dataset('set_x', data=value)
hf.create_dataset('set_y', data=final_y[key])
hf.close()
total_flows = final_y['train'].shape[0]+final_y['val'].shape[0]+final_y['test'].shape[0]
ddos_flows = np.count_nonzero(final_y['train'])+np.count_nonzero(final_y['val'])+np.count_nonzero(final_y['test'])
benign_flows = total_flows-ddos_flows
[train_packets, val_packets, test_packets] = count_packets_in_dataset([final_X['train'], final_X['val'], final_X['test']])
log_string = time.strftime("%Y-%m-%d %H:%M:%S") + " | total_flows (tot,ben,ddos):(" + str(total_flows) + "," + str(benign_flows) + "," + str(ddos_flows) + \
") | Packets (train,val,test):(" + str(train_packets) + "," + str(val_packets) + "," + str(test_packets) + \
") | Train/Val/Test sizes: (" + str(final_y['train'].shape[0]) + "," + str(final_y['val'].shape[0]) + "," + str(final_y['test'].shape[0]) + \
") | options:" + command_options + " |\n"
print(log_string)
# saving log file
with open(output_folder + '/history.log', "a") as myfile:
myfile.write(log_string)
if args.dataset_folder is None and args.preprocess_folder is None and args.preprocess_file is None and args.balance_folder is None:
print (help_string)
if args.dataset_type is None and args.dataset_folder is not None:
print("Please specify the dataset type (DOS2017, DOS2018, DOS2020)!")
print(help_string)
if args.output_folder is None and args.balance_folder is not None:
print("Please specify the output folder!")
print(help_string)
if __name__ == "__main__":
main(sys.argv)