|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 3, |
| 6 | + "id": "61949a39", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stdout", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "Saved Bot_vs_BENIGN.csv\n", |
| 14 | + "Saved DDoS_vs_BENIGN.csv\n", |
| 15 | + "Saved DoS GoldenEye_vs_BENIGN.csv\n", |
| 16 | + "Saved DoS Hulk_vs_BENIGN.csv\n", |
| 17 | + "Saved DoS Slowhttptest_vs_BENIGN.csv\n", |
| 18 | + "Saved DoS slowloris_vs_BENIGN.csv\n", |
| 19 | + "Saved FTP-Patator_vs_BENIGN.csv\n", |
| 20 | + "Saved Heartbleed_vs_BENIGN.csv\n", |
| 21 | + "Saved Infiltration_vs_BENIGN.csv\n", |
| 22 | + "Saved PortScan_vs_BENIGN.csv\n", |
| 23 | + "Saved SSH-Patator_vs_BENIGN.csv\n", |
| 24 | + "Saved Web Attack Brute Force_vs_BENIGN.csv\n", |
| 25 | + "Saved Web Attack Sql Injection_vs_BENIGN.csv\n", |
| 26 | + "Saved Web Attack XSS_vs_BENIGN.csv\n", |
| 27 | + "Execution time: 736.30 seconds\n" |
| 28 | + ] |
| 29 | + } |
| 30 | + ], |
| 31 | + "source": [ |
| 32 | + "# Importing the libraries\n", |
| 33 | + "import pandas as pd\n", |
| 34 | + "import numpy as np\n", |
| 35 | + "import time\n", |
| 36 | + "from sklearn.preprocessing import StandardScaler, LabelEncoder\n", |
| 37 | + "from sklearn.utils import resample\n", |
| 38 | + "from sklearn import preprocessing\n", |
| 39 | + "from warnings import simplefilter\n", |
| 40 | + "from imblearn.under_sampling import RandomUnderSampler\n", |
| 41 | + "\n", |
| 42 | + "# Suppress FutureWarning messages\n", |
| 43 | + "simplefilter(action='ignore', category=FutureWarning)\n", |
| 44 | + "\n", |
| 45 | + "# Record start time\n", |
| 46 | + "start_time = time.time()\n", |
| 47 | + "\n", |
| 48 | + "# Load the main dataset (all_data.csv)\n", |
| 49 | + "main_dataset = pd.read_csv(\"combined_data.csv\")\n", |
| 50 | + "\n", |
| 51 | + "# List of attack types and BENIGN\n", |
| 52 | + "attack_types = [\"Bot\", \"DDoS\", \"DoS GoldenEye\", \"DoS Hulk\", \"DoS Slowhttptest\", \"DoS slowloris\", \"FTP-Patator\",\n", |
| 53 | + " \"Heartbleed\", \"Infiltration\", \"PortScan\", \"SSH-Patator\", \"Web Attack Brute Force\",\n", |
| 54 | + " \"Web Attack Sql Injection\", \"Web Attack XSS\"]\n", |
| 55 | + "benign_type = \"BENIGN\"\n", |
| 56 | + "\n", |
| 57 | + "# Loop through each attack type\n", |
| 58 | + "for attack_type in attack_types:\n", |
| 59 | + " # Create a DataFrame for the current attack type\n", |
| 60 | + " attack_data = main_dataset[main_dataset[\" Label\"] == attack_type]\n", |
| 61 | + " \n", |
| 62 | + " # Create a DataFrame for BENIGN data\n", |
| 63 | + " benign_data = main_dataset[main_dataset[\" Label\"] == benign_type]\n", |
| 64 | + " \n", |
| 65 | + " # Concatenate the attack and benign data\n", |
| 66 | + " combined_data = pd.concat([attack_data, benign_data], axis=0)\n", |
| 67 | + " \n", |
| 68 | + " # Shuffle the combined data\n", |
| 69 | + " combined_data = combined_data.sample(frac=1, random_state=42)\n", |
| 70 | + " \n", |
| 71 | + " # Save the combined data to a CSV file\n", |
| 72 | + " output_filename = f\"{attack_type}_vs_{benign_type}.csv\"\n", |
| 73 | + " combined_data.to_csv(output_filename, index=False)\n", |
| 74 | + " print(f\"Saved {output_filename}\")\n", |
| 75 | + "\n", |
| 76 | + "# Calculate and print the execution time\n", |
| 77 | + "end_time = time.time()\n", |
| 78 | + "execution_time = end_time - start_time\n", |
| 79 | + "print(f\"Execution time: {execution_time:.2f} seconds\")\n" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": 2, |
| 85 | + "id": "a456a074", |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [ |
| 88 | + { |
| 89 | + "name": "stdout", |
| 90 | + "output_type": "stream", |
| 91 | + "text": [ |
| 92 | + "File: Bot_vs_BENIGN.csv\n", |
| 93 | + "Number of Benign instances: 1741839\n", |
| 94 | + "Number of Attack instances: 1956\n", |
| 95 | + "Shape of the dataset: (1743795, 85)\n", |
| 96 | + "-----------------------------\n", |
| 97 | + "File: DDoS_vs_BENIGN.csv\n", |
| 98 | + "Number of Benign instances: 1741839\n", |
| 99 | + "Number of Attack instances: 128025\n", |
| 100 | + "Shape of the dataset: (1869864, 85)\n", |
| 101 | + "-----------------------------\n", |
| 102 | + "File: DoS GoldenEye_vs_BENIGN.csv\n", |
| 103 | + "Number of Benign instances: 1741839\n", |
| 104 | + "Number of Attack instances: 10293\n", |
| 105 | + "Shape of the dataset: (1752132, 85)\n", |
| 106 | + "-----------------------------\n", |
| 107 | + "File: DoS Hulk_vs_BENIGN.csv\n", |
| 108 | + "Number of Benign instances: 1741839\n", |
| 109 | + "Number of Attack instances: 230124\n", |
| 110 | + "Shape of the dataset: (1971963, 85)\n", |
| 111 | + "-----------------------------\n", |
| 112 | + "File: DoS Slowhttptest_vs_BENIGN.csv\n", |
| 113 | + "Number of Benign instances: 1741839\n", |
| 114 | + "Number of Attack instances: 5499\n", |
| 115 | + "Shape of the dataset: (1747338, 85)\n", |
| 116 | + "-----------------------------\n", |
| 117 | + "File: DoS slowloris_vs_BENIGN.csv\n", |
| 118 | + "Number of Benign instances: 1741839\n", |
| 119 | + "Number of Attack instances: 5796\n", |
| 120 | + "Shape of the dataset: (1747635, 85)\n", |
| 121 | + "-----------------------------\n", |
| 122 | + "File: FTP-Patator_vs_BENIGN.csv\n", |
| 123 | + "Number of Benign instances: 1741839\n", |
| 124 | + "Number of Attack instances: 7935\n", |
| 125 | + "Shape of the dataset: (1749774, 85)\n", |
| 126 | + "-----------------------------\n", |
| 127 | + "File: Heartbleed_vs_BENIGN.csv\n", |
| 128 | + "Number of Benign instances: 1741839\n", |
| 129 | + "Number of Attack instances: 11\n", |
| 130 | + "Shape of the dataset: (1741850, 85)\n", |
| 131 | + "-----------------------------\n", |
| 132 | + "File: Infiltration_vs_BENIGN.csv\n", |
| 133 | + "Number of Benign instances: 1741839\n", |
| 134 | + "Number of Attack instances: 36\n", |
| 135 | + "Shape of the dataset: (1741875, 85)\n", |
| 136 | + "-----------------------------\n", |
| 137 | + "File: PortScan_vs_BENIGN.csv\n", |
| 138 | + "Number of Benign instances: 1741839\n", |
| 139 | + "Number of Attack instances: 158804\n", |
| 140 | + "Shape of the dataset: (1900643, 85)\n", |
| 141 | + "-----------------------------\n", |
| 142 | + "File: SSH-Patator_vs_BENIGN.csv\n", |
| 143 | + "Number of Benign instances: 1741839\n", |
| 144 | + "Number of Attack instances: 5897\n", |
| 145 | + "Shape of the dataset: (1747736, 85)\n", |
| 146 | + "-----------------------------\n", |
| 147 | + "File: Web Attack Brute Force_vs_BENIGN.csv\n", |
| 148 | + "Number of Benign instances: 1741839\n", |
| 149 | + "Number of Attack instances: 1507\n", |
| 150 | + "Shape of the dataset: (1743346, 85)\n", |
| 151 | + "-----------------------------\n", |
| 152 | + "File: Web Attack Sql Injection_vs_BENIGN.csv\n", |
| 153 | + "Number of Benign instances: 1741839\n", |
| 154 | + "Number of Attack instances: 21\n", |
| 155 | + "Shape of the dataset: (1741860, 85)\n", |
| 156 | + "-----------------------------\n", |
| 157 | + "File: Web Attack XSS_vs_BENIGN.csv\n", |
| 158 | + "Number of Benign instances: 1741839\n", |
| 159 | + "Number of Attack instances: 652\n", |
| 160 | + "Shape of the dataset: (1742491, 85)\n", |
| 161 | + "-----------------------------\n" |
| 162 | + ] |
| 163 | + } |
| 164 | + ], |
| 165 | + "source": [ |
| 166 | + "import pandas as pd\n", |
| 167 | + "import glob\n", |
| 168 | + "\n", |
| 169 | + "# List of file names\n", |
| 170 | + "file_names = [\n", |
| 171 | + " 'Bot_vs_BENIGN.csv', 'DDoS_vs_BENIGN.csv', 'DoS GoldenEye_vs_BENIGN.csv',\n", |
| 172 | + " 'DoS Hulk_vs_BENIGN.csv', 'DoS Slowhttptest_vs_BENIGN.csv',\n", |
| 173 | + " 'DoS slowloris_vs_BENIGN.csv', 'FTP-Patator_vs_BENIGN.csv',\n", |
| 174 | + " 'Heartbleed_vs_BENIGN.csv', 'Infiltration_vs_BENIGN.csv',\n", |
| 175 | + " 'PortScan_vs_BENIGN.csv', 'SSH-Patator_vs_BENIGN.csv',\n", |
| 176 | + " 'Web Attack Brute Force_vs_BENIGN.csv',\n", |
| 177 | + " 'Web Attack Sql Injection_vs_BENIGN.csv', 'Web Attack XSS_vs_BENIGN.csv'\n", |
| 178 | + "]\n", |
| 179 | + "\n", |
| 180 | + "# Loop through each file\n", |
| 181 | + "for file_name in file_names:\n", |
| 182 | + " # Read the file using pandas\n", |
| 183 | + " data = pd.read_csv(file_name)\n", |
| 184 | + " \n", |
| 185 | + " # Count the number of benign and attack instances\n", |
| 186 | + " num_benign = (data[' Label'] == 'BENIGN').sum()\n", |
| 187 | + " num_attack = (data[' Label'] != 'BENIGN').sum()\n", |
| 188 | + " \n", |
| 189 | + " # Print information\n", |
| 190 | + " print(f\"File: {file_name}\")\n", |
| 191 | + " print(f\"Number of Benign instances: {num_benign}\")\n", |
| 192 | + " print(f\"Number of Attack instances: {num_attack}\")\n", |
| 193 | + " print(\"Shape of the dataset:\", data.shape)\n", |
| 194 | + " print(\"-----------------------------\")\n" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": null, |
| 200 | + "id": "b48fcc8f", |
| 201 | + "metadata": {}, |
| 202 | + "outputs": [], |
| 203 | + "source": [] |
| 204 | + } |
| 205 | + ], |
| 206 | + "metadata": { |
| 207 | + "kernelspec": { |
| 208 | + "display_name": "Python 3 (ipykernel)", |
| 209 | + "language": "python", |
| 210 | + "name": "python3" |
| 211 | + }, |
| 212 | + "language_info": { |
| 213 | + "codemirror_mode": { |
| 214 | + "name": "ipython", |
| 215 | + "version": 3 |
| 216 | + }, |
| 217 | + "file_extension": ".py", |
| 218 | + "mimetype": "text/x-python", |
| 219 | + "name": "python", |
| 220 | + "nbconvert_exporter": "python", |
| 221 | + "pygments_lexer": "ipython3", |
| 222 | + "version": "3.11.4" |
| 223 | + } |
| 224 | + }, |
| 225 | + "nbformat": 4, |
| 226 | + "nbformat_minor": 5 |
| 227 | +} |
0 commit comments