File size: 9,915 Bytes
1c48ab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
736e94b
1c48ab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "YbyU8YKP5KOh"
      },
      "outputs": [],
      "source": [
        "# Capture to supress the download ouput\n",
        "%%capture\n",
        "!pip install datasets evaluate transformers;\n",
        "!pip install huggingface_hub;\n",
        "!pip install pandas;"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hzlMD2hyVrtD",
        "outputId": "53ad9ba2-a64b-4bd8-eeca-4449035b0595"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ],
      "source": [
        "# Load dataset with google drive\n",
        "# We downloaded the dataset from kaggle and uploaded it to google drive, then used google colab to load\n",
        "# It is possible to download it directly using the kaggle api\n",
        "\n",
        "# Link to dataset: https://www.kaggle.com/datasets/engraqeel/iot23preprocesseddata?resource=download\n",
        "# Link to kaggle api docs: https://www.kaggle.com/docs/api#interacting-with-datasets\n",
        "\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive')\n",
        "reduced_iot_path =  \"/content/drive/MyDrive/PATH/TO/FILE/iot23_combined_new.csv\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "id": "9fLFeygkITnn"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "\n",
        "# Define Features\n",
        "# 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",
        "# https://docs.zeek.org/en/master/scripts/base/protocols/conn/main.zeek.html#type-Conn::Info\n",
        "\n",
        "pandas_features = {\n",
        "    'id.orig_p': int,\n",
        "    'id.resp_p': int,\n",
        "    'proto': str,\n",
        "    'service': str,\n",
        "    'duration': float,\n",
        "    'orig_bytes': pd.Int64Dtype(),\n",
        "    'resp_bytes': pd.Int64Dtype(),\n",
        "    'conn_state': str,\n",
        "    'missed_bytes': pd.Int64Dtype(),\n",
        "    'history': str,\n",
        "    'orig_pkts': pd.Int64Dtype(),\n",
        "    'orig_ip_bytes': pd.Int64Dtype(),\n",
        "    'resp_pkts': pd.Int64Dtype(),\n",
        "    'resp_ip_bytes': pd.Int64Dtype(),\n",
        "    'label': str\n",
        "}\n",
        "\n",
        "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",
        "important_column_names = ['id.resp_p', 'proto', 'conn_state', 'orig_pkts', 'orig_ip_bytes', 'resp_ip_bytes', 'label'];\n",
        "exclude_column_names = ['ts','uid','id.orig_h', 'id.resp_h', 'local_orig', 'local_resp']\n",
        "\n",
        "column_names = [column for column in all_column_names if column not in exclude_column_names]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "id": "Zll2DOeT9yv2"
      },
      "outputs": [],
      "source": [
        "# Load dataset with Pandas\n",
        "from datasets import Dataset\n",
        "import pandas as pd\n",
        "reduced_iot_dataset_pandas = pd.read_csv(reduced_iot_path, usecols=column_names, na_values=['-'], dtype=pandas_features)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {
        "id": "SUb01Eg7I7wS"
      },
      "outputs": [],
      "source": [
        "# Remove Duplicates\n",
        "reduced_iot_dataset_pandas = reduced_iot_dataset_pandas.drop_duplicates()"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Make label Benign / Malicious\n",
        "reduced_iot_dataset_pandas['label'] = reduced_iot_dataset_pandas['label'].apply(lambda x: \"Benign\" if x == \"Benign\" else \"Malicious\")"
      ],
      "metadata": {
        "id": "M06Rb8fj2fzk"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xamjYrWbgxkf",
        "outputId": "1ddb22b6-98ab-44b4-83e1-b995e8c1ea4a"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "orig_bytes\n",
              "0       564771\n",
              "<NA>    241092\n",
              "48        2121\n",
              "29        1463\n",
              "45        1348\n",
              "         ...  \n",
              "1088         1\n",
              "1093         1\n",
              "1094         1\n",
              "1104         1\n",
              "770          1\n",
              "Length: 431, dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ],
      "source": [
        "# Test distribution of data\n",
        "reduced_iot_dataset_pandas.value_counts('orig_bytes', dropna=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Js3KG0xNvwpf"
      },
      "outputs": [],
      "source": [
        "# Final step: convert to hugging face dataset\n",
        "reduced_iot_dataset = Dataset.from_pandas(reduced_iot_dataset_pandas).remove_columns(\"__index_level_0__\")"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Test distribution of data again\n",
        "reduced_iot_dataset.to_pandas().value_counts('orig_bytes', dropna=False)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pOxVs7H-0-3k",
        "outputId": "dcd084dd-3369-4d8e-91ca-c9fbfc1636f0"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "orig_bytes\n",
              "0.0       564771\n",
              "NaN       241092\n",
              "48.0        2121\n",
              "29.0        1463\n",
              "45.0        1348\n",
              "           ...  \n",
              "1088.0         1\n",
              "1093.0         1\n",
              "1094.0         1\n",
              "1104.0         1\n",
              "770.0          1\n",
              "Length: 431, dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Authenticate hugging face\n",
        "!huggingface-cli login"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pq6DF3Z8wdmF",
        "outputId": "aaf5e476-96ce-4edc-d65c-858a7e4e52ec"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "    _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|\n",
            "    _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|\n",
            "    _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|\n",
            "    _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|\n",
            "    _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|\n",
            "    \n",
            "    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",
            "    Setting a new token will erase the existing one.\n",
            "    To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .\n",
            "Token: \n",
            "Add token as git credential? (Y/n) Y\n",
            "Token is valid (permission: write).\n",
            "Your token has been saved in your configured git credential helpers (store).\n",
            "Your token has been saved to /root/.cache/huggingface/token\n",
            "Login successful\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Push to the hugging face hub\n",
        "reduced_iot_dataset.push_to_hub(\"19kmunz/iot-23-preprocessed-allcolumns\")"
      ],
      "metadata": {
        "id": "Nz5RwnjxwnwY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test loading the data set\n",
        "from datasets import load_dataset\n",
        "pulledDataSet= load_dataset(\"19kmunz/iot-23-preprocessed\", download_mode=\"force_redownload\")"
      ],
      "metadata": {
        "id": "6rMEA58Pzlyx"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}