|
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"), |
|
|
|
} |
|
) |
|
elif self.config.name == "Packet-Fields": |
|
features = datasets.Features( |
|
{ |
|
"flow_id": datasets.Value("int64"), |
|
"packet_id": datasets.Value("int64"), |
|
|
|
} |
|
) |
|
elif self.config.name == "Packet-Bytes": |
|
features = datasets.Features( |
|
{ |
|
"flow_id": datasets.Value("int64"), |
|
"packet_id": datasets.Value("int64"), |
|
|
|
} |
|
) |
|
else: |
|
features = datasets.Features( |
|
{ |
|
"flow_id": datasets.Value("int64"), |
|
"packet_id": datasets.Value("int64"), |
|
|
|
} |
|
) |
|
|
|
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"], |
|
|
|
} |
|
yield idx, example |