File size: 4,174 Bytes
b05ab6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import datasets
import pandas as pd


_CITATION = """\
@article{maharajan2020attack,
  title={Attack classification and intrusion detection in IoT network using machine learning techniques},
  author={Maharajan, R and Raja, KS},
  journal={Computers \& Electrical Engineering},
  volume={87},
  pages={106783},
  year={2020},
  publisher={Elsevier}
}"""

_DESCRIPTION = """\
The CIC-IDS2017 dataset is an intrusion detection dataset that consists of network traffic data. \
It contains different network attacks and normal traffic. This dataset can be used for evaluating \
intrusion detection systems in IoT networks.
"""

_HOMEPAGE = "https://www.unb.ca/cic/datasets/ids-2017.html"

_LICENSE = "Unknown"

_FOLDERS = {
    "Network-Flows": "Network-Flows",
    "Packet-Fields": "Packet-Fields",
    "Packet-Bytes": "Packet-Bytes",
    "Payload-Bytes": "Payload-Bytes",
}


class CICIDS2017(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="Network-Flows", version=VERSION, description="Folder 1 of CIC-IDS2017 dataset"),
        datasets.BuilderConfig(name="Packet-Fields", version=VERSION, description="Folder 2 of CIC-IDS2017 dataset"),
        datasets.BuilderConfig(name="Packet-Bytes", version=VERSION, description="Folder 3 of CIC-IDS2017 dataset"),
        datasets.BuilderConfig(name="Payload-Bytes", version=VERSION, description="Folder 4 of CIC-IDS2017 dataset"),
    ]

    DEFAULT_CONFIG_NAME = "Network-Flows"

    def _info(self):
        if self.config.name == "Network-Flows":
            features = datasets.Features(
                {
                    "flow_id": datasets.Value("int64"),
                    "source_ip": datasets.Value("string"),
                    # Add more features specific to folder_1 configuration
                }
            )
        elif self.config.name == "Packet-Fields":
            features = datasets.Features(
                {
                    "flow_id": datasets.Value("int64"),
                    "packet_id": datasets.Value("int64"),
                    # Add more features specific to folder_2 configuration
                }
            )
        elif self.config.name == "Packet-Bytes":
            features = datasets.Features(
                {
                    "flow_id": datasets.Value("int64"),
                    "packet_id": datasets.Value("int64"),
                    # Add more features specific to folder_3 configuration
                }
            )
        else:  # folder_4
            features = datasets.Features(
                {
                    "flow_id": datasets.Value("int64"),
                    "packet_id": datasets.Value("int64"),
                    # Add more features specific to folder_4 configuration
                }
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        folder_path = _FOLDERS[self.config.name]
        data_dir = dl_manager.download(folder_path)
        csv_files = [
            filename for filename in os.listdir(data_dir) if filename.endswith(".csv")
        ]

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"data_dir": data_dir, "csv_files": csv_files},
            )
        ]

    def _generate_examples(self, data_dir, csv_files):
        for csv_file in csv_files:
            file_path = os.path.join(data_dir, csv_file)
            df = pd.read_csv(file_path)
            for idx, row in df.iterrows():
                example = {
                    "source_ip": row["source_ip"],
                    "destination_ip": row["destination_ip"],
                    "timestamp": row["timestamp"],
                    "protocol": row["protocol"],
                    "flow_duration": row["flow_duration"],
                    # Add more feature values according to the dataset columns
                }
                yield idx, example