yashika0998 commited on
Commit
de14cb0
1 Parent(s): de50948

Upload 2 files

Browse files

Pre-processed dataset IOT-23

Files changed (2) hide show
  1. IoT23DatasetPreprocessingNotebook.ipynb +296 -0
  2. README.md +134 -0
IoT23DatasetPreprocessingNotebook.ipynb ADDED
@@ -0,0 +1,296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "metadata": {
7
+ "id": "YbyU8YKP5KOh"
8
+ },
9
+ "outputs": [],
10
+ "source": [
11
+ "# Capture to supress the download ouput\n",
12
+ "%%capture\n",
13
+ "!pip install datasets evaluate transformers;\n",
14
+ "!pip install huggingface_hub;\n",
15
+ "!pip install pandas;"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": 18,
21
+ "metadata": {
22
+ "colab": {
23
+ "base_uri": "https://localhost:8080/"
24
+ },
25
+ "id": "hzlMD2hyVrtD",
26
+ "outputId": "53ad9ba2-a64b-4bd8-eeca-4449035b0595"
27
+ },
28
+ "outputs": [
29
+ {
30
+ "output_type": "stream",
31
+ "name": "stdout",
32
+ "text": [
33
+ "Mounted at /content/drive\n"
34
+ ]
35
+ }
36
+ ],
37
+ "source": [
38
+ "# Load dataset with google drive\n",
39
+ "# We downloaded the dataset from kaggle and uploaded it to google drive, then used google colab to load\n",
40
+ "# It is possible to download it directly using the kaggle api\n",
41
+ "\n",
42
+ "# Link to dataset: https://www.kaggle.com/datasets/engraqeel/iot23preprocesseddata?resource=download\n",
43
+ "# Link to kaggle api docs: https://www.kaggle.com/docs/api#interacting-with-datasets\n",
44
+ "\n",
45
+ "from google.colab import drive\n",
46
+ "drive.mount('/content/drive')\n",
47
+ "reduced_iot_path = \"/content/drive/MyDrive/PATH/TO/FILE/iot23_combined_new.csv\""
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": 19,
53
+ "metadata": {
54
+ "id": "9fLFeygkITnn"
55
+ },
56
+ "outputs": [],
57
+ "source": [
58
+ "import pandas as pd\n",
59
+ "\n",
60
+ "# Define Features\n",
61
+ "# ts\tuid\tid.orig_h\tid.orig_p\tid.resp_h\tid.resp_p\tproto\tservice\tduration\torig_bytes\tresp_bytes\tconn_state\tlocal_orig\tlocal_resp\tmissed_bytes\thistory\torig_pkts\torig_ip_bytes\tresp_pkts\tresp_ip_bytes\tlabel\n",
62
+ "# https://docs.zeek.org/en/master/scripts/base/protocols/conn/main.zeek.html#type-Conn::Info\n",
63
+ "\n",
64
+ "pandas_features = {\n",
65
+ " 'id.orig_p': int,\n",
66
+ " 'id.resp_p': int,\n",
67
+ " 'proto': str,\n",
68
+ " 'service': str,\n",
69
+ " 'duration': float,\n",
70
+ " 'orig_bytes': pd.Int64Dtype(),\n",
71
+ " 'resp_bytes': pd.Int64Dtype(),\n",
72
+ " 'conn_state': str,\n",
73
+ " 'missed_bytes': pd.Int64Dtype(),\n",
74
+ " 'history': str,\n",
75
+ " 'orig_pkts': pd.Int64Dtype(),\n",
76
+ " 'orig_ip_bytes': pd.Int64Dtype(),\n",
77
+ " 'resp_pkts': pd.Int64Dtype(),\n",
78
+ " 'resp_ip_bytes': pd.Int64Dtype(),\n",
79
+ " 'label': str\n",
80
+ "}\n",
81
+ "\n",
82
+ "all_column_names = ['ts', 'uid', 'id.orig_h', 'id.orig_p', 'id.resp_h', 'id.resp_p', 'proto', 'service', 'duration', 'orig_bytes', 'resp_bytes', 'conn_state', 'local_orig', 'local_resp', 'missed_bytes', 'history', 'orig_pkts', 'orig_ip_bytes', 'resp_pkts', 'resp_ip_bytes', 'label'];\n",
83
+ "important_column_names = ['id.resp_p', 'proto', 'conn_state', 'orig_pkts', 'orig_ip_bytes', 'resp_ip_bytes', 'label'];\n",
84
+ "exclude_column_names = ['ts','uid','id.orig_h', 'id.resp_h', 'local_orig', 'local_resp']\n",
85
+ "\n",
86
+ "column_names = [column for column in all_column_names if column not in exclude_column_names]"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": 20,
92
+ "metadata": {
93
+ "id": "Zll2DOeT9yv2"
94
+ },
95
+ "outputs": [],
96
+ "source": [
97
+ "# Load dataset with Pandas\n",
98
+ "from datasets import Dataset\n",
99
+ "import pandas as pd\n",
100
+ "reduced_iot_dataset_pandas = pd.read_csv(reduced_iot_path, usecols=column_names, na_values=['-'], dtype=pandas_features)"
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": 21,
106
+ "metadata": {
107
+ "id": "SUb01Eg7I7wS"
108
+ },
109
+ "outputs": [],
110
+ "source": [
111
+ "# Remove Duplicates\n",
112
+ "reduced_iot_dataset_pandas = reduced_iot_dataset_pandas.drop_duplicates()"
113
+ ]
114
+ },
115
+ {
116
+ "cell_type": "code",
117
+ "source": [
118
+ "# Make label Benign / Malicious\n",
119
+ "reduced_iot_dataset_pandas['label'] = reduced_iot_dataset_pandas['label'].apply(lambda x: \"Benign\" if x == \"Benign\" else \"Malicious\")"
120
+ ],
121
+ "metadata": {
122
+ "id": "M06Rb8fj2fzk"
123
+ },
124
+ "execution_count": null,
125
+ "outputs": []
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": null,
130
+ "metadata": {
131
+ "colab": {
132
+ "base_uri": "https://localhost:8080/"
133
+ },
134
+ "id": "xamjYrWbgxkf",
135
+ "outputId": "1ddb22b6-98ab-44b4-83e1-b995e8c1ea4a"
136
+ },
137
+ "outputs": [
138
+ {
139
+ "output_type": "execute_result",
140
+ "data": {
141
+ "text/plain": [
142
+ "orig_bytes\n",
143
+ "0 564771\n",
144
+ "<NA> 241092\n",
145
+ "48 2121\n",
146
+ "29 1463\n",
147
+ "45 1348\n",
148
+ " ... \n",
149
+ "1088 1\n",
150
+ "1093 1\n",
151
+ "1094 1\n",
152
+ "1104 1\n",
153
+ "770 1\n",
154
+ "Length: 431, dtype: int64"
155
+ ]
156
+ },
157
+ "metadata": {},
158
+ "execution_count": 8
159
+ }
160
+ ],
161
+ "source": [
162
+ "# Test distribution of data\n",
163
+ "reduced_iot_dataset_pandas.value_counts('orig_bytes', dropna=False)"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": null,
169
+ "metadata": {
170
+ "id": "Js3KG0xNvwpf"
171
+ },
172
+ "outputs": [],
173
+ "source": [
174
+ "# Final step: convert to hugging face dataset\n",
175
+ "reduced_iot_dataset = Dataset.from_pandas(reduced_iot_dataset_pandas).remove_columns(\"__index_level_0__\")"
176
+ ]
177
+ },
178
+ {
179
+ "cell_type": "code",
180
+ "source": [
181
+ "# Test distribution of data again\n",
182
+ "reduced_iot_dataset.to_pandas().value_counts('orig_bytes', dropna=False)"
183
+ ],
184
+ "metadata": {
185
+ "colab": {
186
+ "base_uri": "https://localhost:8080/"
187
+ },
188
+ "id": "pOxVs7H-0-3k",
189
+ "outputId": "dcd084dd-3369-4d8e-91ca-c9fbfc1636f0"
190
+ },
191
+ "execution_count": null,
192
+ "outputs": [
193
+ {
194
+ "output_type": "execute_result",
195
+ "data": {
196
+ "text/plain": [
197
+ "orig_bytes\n",
198
+ "0.0 564771\n",
199
+ "NaN 241092\n",
200
+ "48.0 2121\n",
201
+ "29.0 1463\n",
202
+ "45.0 1348\n",
203
+ " ... \n",
204
+ "1088.0 1\n",
205
+ "1093.0 1\n",
206
+ "1094.0 1\n",
207
+ "1104.0 1\n",
208
+ "770.0 1\n",
209
+ "Length: 431, dtype: int64"
210
+ ]
211
+ },
212
+ "metadata": {},
213
+ "execution_count": 12
214
+ }
215
+ ]
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "source": [
220
+ "# Authenticate hugging face\n",
221
+ "!huggingface-cli login"
222
+ ],
223
+ "metadata": {
224
+ "colab": {
225
+ "base_uri": "https://localhost:8080/"
226
+ },
227
+ "id": "pq6DF3Z8wdmF",
228
+ "outputId": "aaf5e476-96ce-4edc-d65c-858a7e4e52ec"
229
+ },
230
+ "execution_count": null,
231
+ "outputs": [
232
+ {
233
+ "output_type": "stream",
234
+ "name": "stdout",
235
+ "text": [
236
+ "\n",
237
+ " _| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|\n",
238
+ " _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|\n",
239
+ " _|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|\n",
240
+ " _| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|\n",
241
+ " _| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|\n",
242
+ " \n",
243
+ " A token is already saved on your machine. Run `huggingface-cli whoami` to get more information or `huggingface-cli logout` if you want to log out.\n",
244
+ " Setting a new token will erase the existing one.\n",
245
+ " To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .\n",
246
+ "Token: \n",
247
+ "Add token as git credential? (Y/n) Y\n",
248
+ "Token is valid (permission: write).\n",
249
+ "Your token has been saved in your configured git credential helpers (store).\n",
250
+ "Your token has been saved to /root/.cache/huggingface/token\n",
251
+ "Login successful\n"
252
+ ]
253
+ }
254
+ ]
255
+ },
256
+ {
257
+ "cell_type": "code",
258
+ "source": [
259
+ "# Push to the hugging face hub\n",
260
+ "reduced_iot_dataset.push_to_hub(\"19kmunz/iot-23-preprocessed-allcolumns\")"
261
+ ],
262
+ "metadata": {
263
+ "id": "Nz5RwnjxwnwY"
264
+ },
265
+ "execution_count": null,
266
+ "outputs": []
267
+ },
268
+ {
269
+ "cell_type": "code",
270
+ "source": [
271
+ "# Test loading the data set\n",
272
+ "from datasets import load_dataset\n",
273
+ "pulledDataSet= load_dataset(\"19kmunz/iot-23-preprocessed\", download_mode=\"force_redownload\")"
274
+ ],
275
+ "metadata": {
276
+ "id": "6rMEA58Pzlyx"
277
+ },
278
+ "execution_count": null,
279
+ "outputs": []
280
+ }
281
+ ],
282
+ "metadata": {
283
+ "colab": {
284
+ "provenance": []
285
+ },
286
+ "kernelspec": {
287
+ "display_name": "Python 3",
288
+ "name": "python3"
289
+ },
290
+ "language_info": {
291
+ "name": "python"
292
+ }
293
+ },
294
+ "nbformat": 4,
295
+ "nbformat_minor": 0
296
+ }
README.md ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ dataset_info:
3
+ features:
4
+ - name: id.orig_p
5
+ dtype: int64
6
+ - name: id.resp_p
7
+ dtype: int64
8
+ - name: proto
9
+ dtype: string
10
+ - name: service
11
+ dtype: string
12
+ - name: duration
13
+ dtype: float64
14
+ - name: orig_bytes
15
+ dtype: int64
16
+ - name: resp_bytes
17
+ dtype: int64
18
+ - name: conn_state
19
+ dtype: string
20
+ - name: missed_bytes
21
+ dtype: int64
22
+ - name: history
23
+ dtype: string
24
+ - name: orig_pkts
25
+ dtype: int64
26
+ - name: orig_ip_bytes
27
+ dtype: int64
28
+ - name: resp_pkts
29
+ dtype: int64
30
+ - name: resp_ip_bytes
31
+ dtype: int64
32
+ - name: label
33
+ dtype: string
34
+ splits:
35
+ - name: train
36
+ num_bytes: 93994789
37
+ num_examples: 819024
38
+ download_size: 11805369
39
+ dataset_size: 93994789
40
+ configs:
41
+ - config_name: default
42
+ data_files:
43
+ - split: train
44
+ path: data/train-*
45
+ task_categories:
46
+ - question-answering
47
+ - tabular-classification
48
+ language:
49
+ - en
50
+ tags:
51
+ - code
52
+ pretty_name: d
53
+ ---
54
+ # Aposemat IoT-23 - a Labeled Dataset with Malcious and Benign Iot Network Traffic
55
+ **Homepage:** [https://www.stratosphereips.org/datasets-iot23](https://www.stratosphereips.org/datasets-iot23)
56
+
57
+ This dataset contains a subset of the data from 20 captures of Malcious network traffic and 3 captures from live Benign Traffic on Internet of Things (IoT) devices. Created by Sebastian Garcia, Agustin Parmisano, & Maria Jose Erquiaga at the Avast AIC laboratory with the funding of Avast Software, this dataset is one of the best in the field for Intrusion Detection Systems (IDS) for IoT Devices [(Comparative Analysis of IoT Botnet Datasets)](https://doi.org/10.53070/bbd.1173687).
58
+
59
+ The selection of the subset was determined by [Aqeel Ahmed on Kaggle](https://www.kaggle.com/datasets/engraqeel/iot23preprocesseddata) and contains 6 million samples. The Kaggle upload, nor this one, have employed data balancing. The Kaggle card does not contain methodology to understand what criteria was used to select these samples. If you want ensure best practice, use this dataset to mock-up processing the data into a model before using the full dataset with data balancing. This will require processing the 8GB of conn.log.labelled files.
60
+
61
+ This dataset only notes if the data is Malcious or Benign. The original dataset labels the type of malcious traffic aswell. This means this processing of the dataset is only suited for binary classification.
62
+
63
+ # Feature information:
64
+
65
+ All features originate from the [Zeek](https://docs.zeek.org/en/master/scripts/base/protocols/conn/main.zeek.html#type-Conn::Info) processing performed by the dataset creators. [See notes here for caviats for each column](https://docs.zeek.org/en/master/scripts/base/protocols/conn/main.zeek.html#type-Conn::Info).
66
+ <details>
67
+ <summary>Expand for feature names, descriptions, and datatypes</summary>
68
+
69
+ Name: id.orig_p
70
+ Description: The originator’s port number.
71
+ Data type: int64 - uint64 in original
72
+
73
+ Name: id.resp_p
74
+ Description: The responder’s port number.
75
+ Data type: int64 - uint64 in original
76
+
77
+ Name: proto
78
+ Description: The transport layer protocol of the connection.
79
+ Data type: string - enum(unknown_transport, tcp, udp, icmp). Only TCP and UDP in subset
80
+
81
+ Name: service
82
+ Description: An identification of an application protocol being sent over the connection.
83
+ Data type: optional string
84
+
85
+ Name: duration
86
+ Description: How long the connection lasted.
87
+ Data type: optional float64 - time interval
88
+
89
+ Name: orig_bytes
90
+ Description: The number of payload bytes the originator sent.
91
+ Data type: optional int64 - uint64 in original
92
+
93
+ Name: resp_bytes
94
+ Description:The number of payload bytes the responder sent.
95
+ Data type: optional int64 - uint64 in original
96
+
97
+ Name: conn_state
98
+ Description: Value indicating connection state. (S0, S1, SF, REJ, S2, S3, RSTO, RSTR, RSTOS0, RSTRH, SH, SHR, OTH)
99
+ Data type: optional string
100
+
101
+ Name: missed_bytes
102
+ Description: Indicates the number of bytes missed in content gaps, which is representative of packet loss.
103
+ Data type: optional int64 - uint64 in original. default = 0
104
+
105
+ Name: history
106
+ Description: Records the state history of connections as a string of letters.
107
+ Data type: optional string
108
+
109
+ Name: orig_pkts
110
+ Description: Number of packets that the originator sent.
111
+ Data type: optional int64 - uint64 in original
112
+
113
+ Name: orig_ip_bytes
114
+ Description: Number of IP level bytes that the originator sent.
115
+ Data type: optional int64 - uint64 in original
116
+
117
+ Name: resp_pkts
118
+ Description: Number of packets that the responder sent.
119
+ Data type: optional int64 - uint64 in original
120
+
121
+ Name: resp_ip_bytes
122
+ Description: Number of IP level bytes that the responder sent.
123
+ Data type: optional int64 - uint64 in original
124
+
125
+ Name: label
126
+ Description: Specifies if data point is benign or some form of malicious. See the dataset creators paper for descriptions of attack types
127
+ Data type: string - enum(Malicious, Benign)
128
+
129
+ NOTE: ts, uid, id.orig_h, id.resp_h have been removed as they are dataset specific. Models should not be trained with specific timestamps or IP addresses (id.orig_h) using this dataset, as that can lead to over fitting to dataset specific times and addresses.
130
+ Further local_orig, local_resp have been removed as they are null in all rows, so they are useless for training.
131
+ </details>
132
+
133
+ ## Citation
134
+ If you are using this dataset for your research, please reference it as “Sebastian Garcia, Agustin Parmisano, & Maria Jose Erquiaga. (2020). IoT-23: A labeled dataset with malicious and benign IoT network traffic (Version 1.0.0) [Data set]. Zenodo. http://doi.org/10.5281/zenodo.4743746”