rdpahalavan commited on
Commit
0a4a434
1 Parent(s): fad8e2b

Create CIC-IDS2017.py

Browse files
Files changed (1) hide show
  1. CIC-IDS2017.py +131 -0
CIC-IDS2017.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datasets
3
+ import pandas as pd
4
+
5
+
6
+ _CITATION = """\
7
+ @article{maharajan2020attack,
8
+ title={Attack classification and intrusion detection in IoT network using machine learning techniques},
9
+ author={Maharajan, R and Raja, KS},
10
+ journal={Computers \& Electrical Engineering},
11
+ volume={87},
12
+ pages={106783},
13
+ year={2020},
14
+ publisher={Elsevier}
15
+ }"""
16
+
17
+ _DESCRIPTION = """\
18
+ The CIC-IDS2017 dataset is an intrusion detection dataset that consists of network traffic data. \
19
+ It contains different network attacks and normal traffic. This dataset can be used for evaluating \
20
+ intrusion detection systems in IoT networks.
21
+ """
22
+
23
+ _HOMEPAGE = "https://www.unb.ca/cic/datasets/ids-2017.html"
24
+
25
+ _LICENSE = "Unknown"
26
+
27
+ _FOLDERS = {
28
+ "folder_1": "rdpahalavan/CIC-IDS2017/Network-Flows",
29
+ "folder_2": "rdpahalavan/CIC-IDS2017/Payload-Bytes",
30
+ "folder_3": "rdpahalavan/CIC-IDS2017/Packet-Bytes",
31
+ "folder_4": "rdpahalavan/CIC-IDS2017/Packet-Fields",
32
+ }
33
+
34
+
35
+ class CICIDS2017(datasets.GeneratorBasedBuilder):
36
+ VERSION = datasets.Version("1.0.0")
37
+
38
+ BUILDER_CONFIGS = [
39
+ datasets.BuilderConfig(name="folder_1", version=VERSION, description="Folder 1 of CIC-IDS2017 dataset"),
40
+ datasets.BuilderConfig(name="folder_2", version=VERSION, description="Folder 2 of CIC-IDS2017 dataset"),
41
+ datasets.BuilderConfig(name="folder_3", version=VERSION, description="Folder 3 of CIC-IDS2017 dataset"),
42
+ datasets.BuilderConfig(name="folder_4", version=VERSION, description="Folder 4 of CIC-IDS2017 dataset"),
43
+ ]
44
+
45
+ DEFAULT_CONFIG_NAME = "folder_1"
46
+
47
+ def _info(self):
48
+ if self.config.name == "folder_1":
49
+ features = datasets.Features(
50
+ {
51
+ "source_ip": datasets.Value("string"),
52
+ "destination_ip": datasets.Value("string"),
53
+ "timestamp": datasets.Value("string"),
54
+ "protocol": datasets.Value("string"),
55
+ "flow_duration": datasets.Value("float"),
56
+ # Add more features specific to folder_1 configuration
57
+ }
58
+ )
59
+ elif self.config.name == "folder_2":
60
+ features = datasets.Features(
61
+ {
62
+ "source_ip": datasets.Value("string"),
63
+ "destination_ip": datasets.Value("string"),
64
+ "timestamp": datasets.Value("string"),
65
+ "protocol": datasets.Value("string"),
66
+ "flow_duration": datasets.Value("float"),
67
+ # Add more features specific to folder_2 configuration
68
+ }
69
+ )
70
+ elif self.config.name == "folder_3":
71
+ features = datasets.Features(
72
+ {
73
+ "source_ip": datasets.Value("string"),
74
+ "destination_ip": datasets.Value("string"),
75
+ "timestamp": datasets.Value("string"),
76
+ "protocol": datasets.Value("string"),
77
+ "flow_duration": datasets.Value("float"),
78
+ # Add more features specific to folder_3 configuration
79
+ }
80
+ )
81
+ else: # folder_4
82
+ features = datasets.Features(
83
+ {
84
+ "source_ip": datasets.Value("string"),
85
+ "destination_ip": datasets.Value("string"),
86
+ "timestamp": datasets.Value("string"),
87
+ "protocol": datasets.Value("string"),
88
+ "flow_duration": datasets.Value("float"),
89
+ # Add more features specific to folder_4 configuration
90
+ }
91
+ )
92
+
93
+ return datasets.DatasetInfo(
94
+ description=_DESCRIPTION,
95
+ features=features,
96
+ homepage=_HOMEPAGE,
97
+ license=_LICENSE,
98
+ citation=_CITATION,
99
+ )
100
+
101
+ def _split_generators(self, dl_manager):
102
+ folder_path = _FOLDERS[self.config.name]
103
+ data_dir = dl_manager.download(folder_path)
104
+ csv_files = [
105
+ filename for filename in os.listdir(data_dir) if filename.endswith(".csv")
106
+ ]
107
+
108
+ return [
109
+ datasets.SplitGenerator(
110
+ name=datasets.Split.TRAIN,
111
+ gen_kwargs={"data_dir": data_dir, "csv_files": csv_files},
112
+ )
113
+ ]
114
+
115
+ def _generate_examples(self, data_dir, csv_files):
116
+ for csv_file in csv_files:
117
+ file_path = os.path.join(data_dir, csv_file)
118
+ df = pd.read_csv(file_path)
119
+ for idx, row in df.iterrows():
120
+ example = {
121
+ "source_ip": row["source_ip"],
122
+ "destination_ip": row["destination_ip"],
123
+ "timestamp": row["timestamp"],
124
+ "protocol": row["protocol"],
125
+ "flow_duration": row["flow_duration"],
126
+ # Add more feature values according to the dataset columns
127
+ }
128
+ yield idx, example
129
+
130
+
131
+ datasets.load_dataset("rdpahalavan/CIC-IDS2017")