yashika0998 commited on
Commit
b59a76f
·
1 Parent(s): 576360a

Upload 2 files

Browse files
IoT23DatasetPreprocessingNotebook-2.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 = all_columns"
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-2.md ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ dataset_info:
3
+ features:
4
+ - name: ts
5
+ dtype: float64
6
+ - name: uid
7
+ dtype: string
8
+ - name: id.orig_h
9
+ dtype: string
10
+ - name: id.orig_p
11
+ dtype: int64
12
+ - name: id.resp_h
13
+ dtype: string
14
+ - name: id.resp_p
15
+ dtype: int64
16
+ - name: proto
17
+ dtype: string
18
+ - name: service
19
+ dtype: string
20
+ - name: duration
21
+ dtype: float64
22
+ - name: orig_bytes
23
+ dtype: int64
24
+ - name: resp_bytes
25
+ dtype: int64
26
+ - name: conn_state
27
+ dtype: string
28
+ - name: local_orig
29
+ dtype: float64
30
+ - name: local_resp
31
+ dtype: float64
32
+ - name: missed_bytes
33
+ dtype: int64
34
+ - name: history
35
+ dtype: string
36
+ - name: orig_pkts
37
+ dtype: int64
38
+ - name: orig_ip_bytes
39
+ dtype: int64
40
+ - name: resp_pkts
41
+ dtype: int64
42
+ - name: resp_ip_bytes
43
+ dtype: int64
44
+ - name: label
45
+ dtype: string
46
+ splits:
47
+ - name: train
48
+ num_bytes: 1232978140
49
+ num_examples: 6046623
50
+ download_size: 274218995
51
+ dataset_size: 1232978140
52
+ configs:
53
+ - config_name: default
54
+ data_files:
55
+ - split: train
56
+ path: data/train-*
57
+ task_categories:
58
+ - tabular-classification
59
+ - table-question-answering
60
+ language:
61
+ - en
62
+ tags:
63
+ - code
64
+ ---
65
+ # Aposemat IoT-23 - a Labeled Dataset with Malcious and Benign Iot Network Traffic
66
+ **Homepage:** [https://www.stratosphereips.org/datasets-iot23](https://www.stratosphereips.org/datasets-iot23)
67
+
68
+ 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).
69
+
70
+ 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.
71
+
72
+ # Feature information:
73
+
74
+ 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).
75
+ <details>
76
+ <summary>Expand for feature names, descriptions, and datatypes</summary>
77
+
78
+ Name: ts
79
+ Desription: This is the time of the first packet.
80
+ Data Type: float64 - Timestamp
81
+
82
+ Name: uid
83
+ Description: A Zeek-defined unique identifier of the connection.
84
+ Data type: string
85
+
86
+ Name: id.orig_h
87
+ Description: The originator’s IP address.
88
+ Data type: string - for the form 255.255.255.255 for IPv4 or [aaaa:bbbb:cccc:dddd:eeee:ffff:1111:2222] for IPv6
89
+
90
+ Name: id.orig_p
91
+ Description: The originator’s port number.
92
+ Data type: int64 - uint64 in original
93
+
94
+ Name: id.resp_h
95
+ Description: The responder’s IP address.
96
+ Data type: string - for the form 255.255.255.255 for IPv4 or [aaaa:bbbb:cccc:dddd:eeee:ffff:1111:2222] for IPv6
97
+
98
+ Name: id.resp_p
99
+ Description: The responder’s port number.
100
+ Data type: int64 - uint64 in original
101
+
102
+ Name: proto
103
+ Description: The transport layer protocol of the connection.
104
+ Data type: string - enum(unknown_transport, tcp, udp, icmp). Only TCP and UDP in subset
105
+
106
+ Name: service
107
+ Description: An identification of an application protocol being sent over the connection.
108
+ Data type: optional string
109
+
110
+ Name: duration
111
+ Description: How long the connection lasted.
112
+ Data type: optional float64 - time interval
113
+
114
+ Name: orig_bytes
115
+ Description: The number of payload bytes the originator sent.
116
+ Data type: optional int64 - uint64 in original
117
+
118
+ Name: resp_bytes
119
+ Description:The number of payload bytes the responder sent.
120
+ Data type: optional int64 - uint64 in original
121
+
122
+ Name: conn_state
123
+ Description: Value indicating connection state. (S0, S1, SF, REJ, S2, S3, RSTO, RSTR, RSTOS0, RSTRH, SH, SHR, OTH)
124
+ Data type: optional string
125
+
126
+ Name: local_orig
127
+ Description: If the connection is originated locally, this value will be T. If it was originated remotely it will be F.
128
+ Data type: optional float64 - bool in original but null for all columns
129
+
130
+ Name: local_resp
131
+ Description: If the connection is responded to locally, this value will be T. If it was responded to remotely it will be F.
132
+ Data type: optional float64 - bool in original but null for all columns
133
+
134
+ Name: missed_bytes
135
+ Description: Indicates the number of bytes missed in content gaps, which is representative of packet loss.
136
+ Data type: optional int64 - uint64 in original. default = 0
137
+
138
+ Name: history
139
+ Description: Records the state history of connections as a string of letters.
140
+ Data type: optional string
141
+
142
+ Name: orig_pkts
143
+ Description: Number of packets that the originator sent.
144
+ Data type: optional int64 - uint64 in original
145
+
146
+ Name: orig_ip_bytes
147
+ Description: Number of IP level bytes that the originator sent.
148
+ Data type: optional int64 - uint64 in original
149
+
150
+ Name: resp_pkts
151
+ Description: Number of packets that the responder sent.
152
+ Data type: optional int64 - uint64 in original
153
+
154
+ Name: resp_ip_bytes
155
+ Description: Number of IP level bytes that the responder sent.
156
+ Data type: optional int64 - uint64 in original
157
+
158
+ Name: label
159
+ Description: Specifies if data point is benign or some form of malicious. See the dataset creators paper for descriptions of attack types
160
+ Data type: string - enum('PartOfAHorizontalPortScan', 'Okiru', 'DDoS', 'C&C-HeartBeat',
161
+ 'Benign', 'C&C-Torii', 'C&C', 'C&C-FileDownload', 'Okiru-Attack',
162
+ 'Attack', 'FileDownload', 'C&C-HeartBeat-FileDownload',
163
+ 'C&C-Mirai')
164
+
165
+ NOTE: ts, uid, id.orig_h, id.resp_h SHOULD BE removed as they are dataset specific. Models should not be trained with specific timestamps or IP addresses (id.orig_h), as that can lead to over fitting to dataset specific times and addresses.
166
+ Further local_orig, local_resp SHOULD BE removed as they are null in all rows, so they are useless for training.
167
+ </details>
168
+
169
+ ## Citation
170
+ 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”
171
+ [More Information needed](https://github.com/huggingface/datasets/blob/main/CONTRIBUTING.md#how-to-contribute-to-the-dataset-cards)