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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
}
|