Skip to content
Open
33 changes: 33 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
"customizations": {
"codespaces": {
"openFiles": [
"README.md",
"Clean_files.py"
]
},
"vscode": {
"settings": {},
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
]
}
},
"updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y <packages.txt; [ -f requirements.txt ] && pip3 install --user -r requirements.txt; pip3 install --user streamlit; echo '✅ Packages installed and Requirements met'",
"postAttachCommand": {
"server": "streamlit run Clean_files.py --server.enableCORS false --server.enableXsrfProtection false"
},
"portsAttributes": {
"8501": {
"label": "Application",
"onAutoForward": "openPreview"
}
},
"forwardPorts": [
8501
]
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore the dataset directory
/dataset/
# Ignore the cleaned dataset directory
/cleaned/
72 changes: 72 additions & 0 deletions Clean_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
import pandas as pd
import numpy as np


def clean_csv(file_name, output_dir='cleaned'):
print(f"Cleaning {file_name}...")

try:
df = pd.read_csv(file_name)
except Exception as e:
print(f"Error loading file: {e}")
return

# Drop unneeded columns (keep 'Label' or 'Attempted Category' for categories)
columns_to_drop = [
'Src IP dec', 'Dast IP dec', 'Timestamp',
'Fwd Bytes/Bulk Avg', 'Fwd Packet/Bulk Avg', 'Fwd Bulk Rate Avg',
'Bwd Bytes/Bulk Avg', 'Bwd Packet/Bulk Avg', 'Bwd Bulk Rate Avg',
'Fwd PSH Flags', 'Bwd PSH Flags', 'Fwd URG Flags', 'Bwd URG Flags',
'Fwd RST Flags', 'Bwd RST Flags', 'FWD Init Win Bytes', 'Bwd Init Win Bytes',
'Fwd Act Data Pkts', 'Fwd Seg Size Min',
'ICMP Code', 'ICMP Type'
# Drop Attempted Category only if you don't want it
# 'Attempted Category'
]

df.drop(columns=columns_to_drop, inplace=True, errors='ignore')

# Replace infinite values with NaN
df.replace([np.inf, -np.inf], np.nan, inplace=True)

# Drop columns with >50% missing data
df.dropna(thresh=len(df)*0.5, axis=1, inplace=True)

# Fill remaining NaNs with median
df.fillna(df.median(numeric_only=True), inplace=True)

# Drop duplicates
df.drop_duplicates(inplace=True)

# Map labels to categories
# Keep 'BENIGN' as 'Benign', others keep original attack names for multi-class classification
df['Label'] = df['Label'].apply(lambda x: 'Benign' if x == 'BENIGN' else x)

# check unique attack categories present
print("Unique Labels after mapping:", df['Label'].unique())

# Save cleaned data
base_file_name = os.path.basename(file_name)
output_filename = base_file_name.replace('.csv', '_cleaned.csv')
output_filepath = os.path.join(output_dir, output_filename)
try:
df.to_csv(output_filepath, index=False)
print(f"Data cleaned and saved to '{output_filepath}'")

except Exception as e:
print(f"Error saving file: {e}")

def main():
# Make directory for cleaned files if it doesn't exist
output_dir = input("Enter directory to save cleaned files (default 'cleaned'): ") or 'cleaned'
os.makedirs(output_dir, exist_ok=True)
dataset_dir = input("Enter directory containing the CSV files (default './dataset'): ") or './dataset'

# Loop through each file in the dataset directory
for file_name in os.listdir(dataset_dir):
if file_name.endswith('.csv'):
clean_csv(os.path.join(dataset_dir, file_name), output_dir=output_dir)

if __name__ == "__main__":
main()
77 changes: 26 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,42 @@
# (UPDATE IN README.md) Project Title
# 🛡️ Cyber Attack Detection Model

(UPDATE IN README.md)
Briefly describe the purpose/result(s) of your project, the skills you applied, and the Apply AI program.
This project is a machine learning-powered **web application** that detects cyber attacks based on real-world network traffic. It leverages the **CICIDS dataset** to train a predictive model and deploys the model using **Streamlit** for interactive use.

*EXAMPLE:*
*Uncovered and meticulously analyzed three distinct biases present in ChatGPT, employing advanced Python techniques and data analysis methodologies, all within AI4ALL's cutting-edge AI4ALL Ignite accelerator.*
The app enables users to interact with a trained classifier to determine whether a given network input is normal or an attack.

---

## Problem Statement <!--- do not change this line -->
## 📚 Dataset: CICIDS 2017

(UPDATE IN README.md)
Describe the motivation for this project, why it is relevant, and what its impacts are.
The [CICIDS 2017 dataset](https://www.unb.ca/cic/datasets/ids-2017.html) was created by the Canadian Institute for Cybersecurity and contains realistic network traffic including both benign and malicious behaviors.

*EXAMPLE:*
*Given the substantial daily output of responses, the identification and mitigation of ChatGPT's biases become critical, safeguarding both the multitude of users and the far-reaching consequences they may influence.*
It includes attacks such as:
- DoS
- Brute Force
- Botnet
- PortScan
- DDoS
- Web Attack
- Infiltration

## Key Results <!--- do not change this line -->
---

(UPDATE IN README.md)
Enumerate the main results of this project in a list and describe them.
## 🚀 Live Streamlit App

*EXAMPLE:*
1. *Recorded over 1,000 unique prompts and their responses generated by ChatGPT*
2. *Identified three biases in ChatGPT's responses*
- *When prompted about this world event*
- *When prompted about this field of science*
- *When prompted about this political party*
🔗 Access the app here: **[CLICK HERE](https://eauxzl4c8ru37cmlv6ygry.streamlit.app/)**

Deployed via **Streamlit Community Cloud** using the `Isha` branch, which contains the trained model and deployment script.

## Methodologies <!--- do not change this line -->
---

(UPDATE IN README.md)
## 🧠 Machine Learning Model

*EXAMPLE:*
*To accomplish this, we utilized the OpenAI API to interact with ChatGPT, and we designed a custom Python script to generate diverse prompts and collect corresponding responses. The data was then processed and analyzed using pandas, enabling us to detect patterns and biases in the AI model's outputs.*
*Engineered a Python script to generate over 1,000 prompts and elicit their responses from ChatGPT, utilizing pandas to collect the data. When prompted for solutions to this specific relevant crisis, nearly 80% of ChatGPT's responses promoted a certain worldview.*
- **Training Dataset**: CICIDS 2017 using Kaggle
- **Model File**: `new_model.joblib`
- **Branch Location**: [`Isha` branch](https://github.com/AI4ALL-Cyber-Project/GitHub_Pages_Template/tree/Isha)
- **Script**: `app.py` — loads the model and creates a web interface using Streamlit
- `...`: Other branches include data exploration and preprocessing.
- **Features**: Model predicts based on selected input features extracted from the dataset


## Data Sources <!--- do not change this line -->

(UPDATE IN README.md)
Include any relevant data sources that were used in your project.

*EXAMPLE:*
*Kaggle Datasets: [Link to Kaggle Dataset](https://www.kaggle.com/datasets)*

## Technologies Used <!--- do not change this line -->

(UPDATE IN README.md)
List the technologies, libraries, and frameworks used in your project.

*EXAMPLE:*
- *Python*
- *pandas*
- *OpenAI API*


## Authors <!--- do not change this line -->

(UPDATE IN README.md)
List the names and contact information (e.g., email, GitHub profiles) of the authors or contributors.

*EXAMPLE:*
*This project was completed in collaboration with:*
- *John Doe ([john.doe@example.com](mailto:john.doe@example.com))*
- *Jane Smith ([jane.smith@example.com](mailto:jane.smith@example.com))*
Loading