Skip to content

prashantshukla01/Network_Security

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

49 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NetGuard AI Banner

πŸ›‘οΈ NetGuard AI β€” Network Security & Phishing Detection Platform

An end-to-end Machine Learning platform for detecting phishing websites and network intrusions, powered by XGBoost and a modular MLOps pipeline.

Features β€’ Tech Stack β€’ Project Structure β€’ Getting Started β€’ API Reference β€’ Deployment β€’ Contributing β€’ License


✨ Features

Feature Description
πŸ”— Real-Time URL Scanning Paste any URL and get an instant Phishing / Legitimate verdict using a trained ML model + PhishTank API cross-check.
πŸ“„ Batch CSV Analysis Upload a CSV of network traffic features to get bulk predictions with downloadable results.
πŸ€– RAG Chatbot An AI assistant powered by Google Gemini 1.5 Flash that answers questions about the platform, grounded in project context.
πŸ“Š MLflow Experiment Tracking Every training run logs F1, Precision, and Recall metrics to DagsHub/MLflow for experiment comparison.
☁️ AWS S3 Artifact Storage Training artifacts and final models are automatically synced to an S3 bucket after each pipeline run.
πŸ” User Authentication Session-based login/register system backed by MongoDB.
πŸ—οΈ CI/CD Pipeline GitHub Actions workflow for linting, Docker build, ECR push, and self-hosted deployment.
⚑ Optimised Inference Model is loaded once at startup (~27 MB) and reused across all requests; URL scan results are cached in-memory.

πŸš€ Tech Stack

Layer Technologies
ML / Data Science Scikit-Learn, XGBoost, KNN Imputer, Pandas, NumPy
Experiment Tracking MLflow, DagsHub
Backend / API FastAPI, Uvicorn, Mangum (AWS Lambda adapter)
Frontend Jinja2 Templates, Bootstrap 5, Animate.css, Google Fonts (Outfit)
Database MongoDB Atlas (PyMongo)
Cloud & Infra AWS S3, AWS ECR, Docker, GitHub Actions
AI / Chatbot Google Gemini 1.5 Flash (google-generativeai SDK)
Deployment Netlify (serverless), Vercel, Self-hosted Docker

πŸ“ Project Structure

.
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── main.yml                 # CI/CD β€” Build β†’ ECR β†’ Deploy
β”‚
β”œβ”€β”€ data_schema/
β”‚   └── schema.yaml                  # 30-feature schema (int64 columns)
β”‚
β”œβ”€β”€ final_model/
β”‚   β”œβ”€β”€ model.pkl                    # Trained XGBoost/best model (~27 MB)
β”‚   └── preprocessor.pkl             # KNN Imputer pipeline (~2.3 MB)
β”‚
β”œβ”€β”€ Network_Data/
β”‚   └── phisingData.csv              # Raw phishing dataset (11,055 rows)
β”‚
β”œβ”€β”€ networksecurity/                 # Core Python package
β”‚   β”œβ”€β”€ chatbot/
β”‚   β”‚   └── rag_factory.py           # Gemini-powered RAG chatbot
β”‚   β”œβ”€β”€ cloud/
β”‚   β”‚   └── s3_syncer.py             # AWS S3 sync utility
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ data_ingestion.py        # MongoDB β†’ DataFrame β†’ Train/Test
β”‚   β”‚   β”œβ”€β”€ data_validation.py       # Schema validation & KS drift test
β”‚   β”‚   β”œβ”€β”€ data_transformation.py   # KNN Imputation pipeline
β”‚   β”‚   └── model_trainer.py         # Multi-model training + MLflow
β”‚   β”œβ”€β”€ constant/
β”‚   β”‚   └── training_pipeline/
β”‚   β”‚       └── __init__.py          # All pipeline constants
β”‚   β”œβ”€β”€ entity/
β”‚   β”‚   β”œβ”€β”€ config_entity.py         # Config dataclasses for each stage
β”‚   β”‚   └── artifact_entity.py       # Artifact dataclasses for each stage
β”‚   β”œβ”€β”€ exception/
β”‚   β”‚   └── exception.py             # Custom exception with file/line info
β”‚   β”œβ”€β”€ logging/
β”‚   β”‚   └── logger.py                # Timestamped file-based logging
β”‚   β”œβ”€β”€ pipeline/
β”‚   β”‚   └── training_pipeline.py     # Orchestrates the full training run
β”‚   └── utils/
β”‚       β”œβ”€β”€ main_utils/
β”‚       β”‚   └── utils.py             # save/load object, YAML, numpy I/O
β”‚       └── ml_utils/
β”‚           β”œβ”€β”€ feature_extractor.py # URL β†’ 30-feature vector extraction
β”‚           β”œβ”€β”€ metric/              # Classification metric helper
β”‚           └── model/
β”‚               └── estimator.py     # NetworkModel (preprocessor + model)
β”‚
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ css/                         # style.css, premium.css
β”‚   β”œβ”€β”€ js/                          # dashboard.js
β”‚   └── images/                      # hero_banner.png, login_bg.png, etc.
β”‚
β”œβ”€β”€ templates/
β”‚   β”œβ”€β”€ landing.html                 # Public marketing landing page
β”‚   β”œβ”€β”€ login.html                   # Auth page (login + register)
β”‚   β”œβ”€β”€ index.html                   # Main dashboard (URL scan, CSV upload, chatbot)
β”‚   └── table.html                   # Prediction results table
β”‚
β”œβ”€β”€ app.py                           # FastAPI application entry point
β”œβ”€β”€ main.py                          # CLI entry for training pipeline
β”œβ”€β”€ push_data.py                     # Script to seed MongoDB from CSV
β”œβ”€β”€ check_preprocessor.py            # Debug utility for preprocessor inspection
β”œβ”€β”€ setup.py                         # Package metadata & dependency installer
β”œβ”€β”€ requirements.txt                 # Python dependencies
β”œβ”€β”€ Dockerfile                       # Production container (Python 3.10-slim)
β”œβ”€β”€ run.sh                           # Local deployment helper script
β”œβ”€β”€ netlify.toml                     # Netlify serverless configuration
β”œβ”€β”€ vercel.json                      # Vercel deployment configuration
β”œβ”€β”€ AGENTS.md                        # AI agent rules & project conventions
β”œβ”€β”€ CODE_OF_CONDUCT.md               # Contributor Code of Conduct
β”œβ”€β”€ CONTRIBUTING.md                  # Contribution guidelines
└── SECURITY.md                      # Security policy & vulnerability reporting

πŸ—οΈ Architecture

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚              Training Pipeline               β”‚
                    β”‚                                              β”‚
  MongoDB Atlas ──▢ β”‚ Ingestion β†’ Validation β†’ Transformation     β”‚
  (or local CSV)    β”‚     β”‚           β”‚              β”‚             β”‚
                    β”‚     β–Ό           β–Ό              β–Ό             β”‚
                    β”‚  Train/Test   Drift        KNN Imputer       β”‚
                    β”‚   Split       Report       Pipeline          β”‚
                    β”‚                                              β”‚
                    β”‚              Model Training                  β”‚
                    β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
                    β”‚  β”‚ Random Forest β”‚ XGBoost β”‚ AdaBoost β”‚      β”‚
                    β”‚  β”‚ Decision Tree β”‚ GBM    β”‚ Logistic  β”‚      β”‚
                    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
                    β”‚         β”‚                                    β”‚
                    β”‚         β–Ό                                    β”‚
                    β”‚    Best Model ──▢ MLflow ──▢ DagsHub         β”‚
                    β”‚         β”‚                                    β”‚
                    β”‚         β–Ό                                    β”‚
                    β”‚   final_model/ ──▢ AWS S3                    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚              Inference (FastAPI)              β”‚
                    β”‚                                              β”‚
                    β”‚  /predict_url ──▢ Feature Extractor          β”‚
                    β”‚       β”‚              β”‚                       β”‚
                    β”‚       β”‚         PhishTank API                β”‚
                    β”‚       β”‚              β”‚                       β”‚
                    β”‚       β–Ό              β–Ό                       β”‚
                    β”‚   NetworkModel.predict()                     β”‚
                    β”‚       β”‚                                      β”‚
                    β”‚       β–Ό                                      β”‚
                    β”‚  { prediction: "Phishing" | "Legitimate" }   β”‚
                    β”‚                                              β”‚
                    β”‚  /predict ──▢ CSV β†’ NumPy β†’ predict()        β”‚
                    β”‚  /api/chat ──▢ Gemini RAG chatbot             β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Getting Started

Prerequisites

  • Python 3.10+
  • MongoDB Atlas account (or local MongoDB instance)
  • AWS CLI configured (for S3 artifact storage)
  • Google API Key (for the RAG chatbot β€” Gemini 1.5 Flash)

1. Clone the Repository

git clone https://github.com/prashantshukla01/Network_Security.git
cd Network_Security

2. Create & Activate Virtual Environment

# Using conda
conda create -n networkml python=3.10 -y
conda activate networkml

# Or using venv
python3 -m venv venv
source venv/bin/activate   # macOS/Linux
venv\Scripts\activate      # Windows

3. Install Dependencies

pip install -r requirements.txt
pip install python-multipart   # Required for file uploads

4. Configure Environment Variables

Create a .env file in the project root:

MONGO_DB_URL=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/?appName=<appName>
GOOGLE_API_KEY=<your-google-gemini-api-key>
AWS_ACCESS_KEY_ID=<your-aws-access-key>
AWS_SECRET_ACCESS_KEY=<your-aws-secret-key>
SECRET_KEY=<random-session-secret-key>

⚠️ Important: Never commit your .env file. It is already listed in .gitignore.

5. Seed the Database (Optional)

If starting fresh, push the phishing dataset to MongoDB:

python push_data.py

6. Run the Application

python app.py
# Server starts at http://0.0.0.0:8000

Or use the helper script:

chmod +x run.sh
./run.sh

πŸ§ͺ Training Pipeline

Run the full ML training pipeline (Ingestion β†’ Validation β†’ Transformation β†’ Training β†’ S3 Sync):

python main.py

Or trigger via the API:

GET http://127.0.0.1:8000/train

Models Evaluated

Model Hyperparameter Grid
Random Forest n_estimators: [8, 16, 32, 128, 256]
Decision Tree criterion: [gini, entropy, log_loss]
Gradient Boosting learning_rate, subsample, n_estimators
XGBoost learning_rate, n_estimators, max_depth
AdaBoost learning_rate, n_estimators
Logistic Regression Default

The best model is automatically selected based on test score and pushed to final_model/.

MLflow Tracking

Metrics are logged to DagsHub:

# Set tracking (already configured in model_trainer.py via dagshub.init)
export MLFLOW_TRACKING_URI=https://dagshub.com/prashantshukla01/Network_Security.mlflow
export MLFLOW_TRACKING_USERNAME=<your-username>
export MLFLOW_TRACKING_PASSWORD=<your-token>

# View locally
mlflow ui

πŸ“‘ API Reference

Method Endpoint Description
GET / Landing page
GET /login Login / Register page
POST /login Submit login credentials
POST /register Register a new user
GET /logout Clear session and redirect
GET /dashboard Main dashboard (auth required)
GET /train Trigger full training pipeline
POST /predict Upload CSV for batch prediction
POST /predict_url Scan a single URL (JSON body: {"url": "..."})
GET /api/history Get last 10 prediction history records
POST /api/chat Chat with the RAG assistant (JSON body: {"message": "..."})

Example: Scan a URL

curl -X POST http://127.0.0.1:8000/predict_url \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com"}'

Response:

{
  "url": "https://example.com",
  "prediction": "Legitimate",
  "raw_result": 1
}

🐳 Deployment

Docker

# Build
docker build -t netguard-ai .

# Run
docker run -d -p 8000:8000 \
  -e MONGO_DB_URL="<your-mongo-url>" \
  -e GOOGLE_API_KEY="<your-key>" \
  --name netguard \
  netguard-ai

AWS ECR + Self-Hosted (via CI/CD)

The GitHub Actions workflow (.github/workflows/main.yml) handles:

  1. Continuous Integration β€” Lint + unit test stubs
  2. Continuous Delivery β€” Docker build β†’ push to AWS ECR
  3. Continuous Deployment β€” Pull from ECR β†’ run on self-hosted runner (port 8080:8000)

Required GitHub Secrets:

Secret Description
AWS_ACCESS_KEY_ID AWS IAM access key
AWS_SECRET_ACCESS_KEY AWS IAM secret key
AWS_REGION e.g. ap-south-1
AWS_ECR_LOGIN_URI ECR registry URI
ECR_REPOSITORY_NAME ECR repository name
MONGO_DB_URL MongoDB Atlas connection string

Netlify (Serverless)

Configured via netlify.toml β€” uses Mangum adapter to wrap FastAPI as a serverless function.

Vercel

Configured via vercel.json β€” routes all traffic to app.py.


πŸ”’ Dataset Features

The model uses 30 features extracted from URLs and web page attributes:

Click to expand full feature list
# Feature Type
1 having_IP_Address int64
2 URL_Length int64
3 Shortining_Service int64
4 having_At_Symbol int64
5 double_slash_redirecting int64
6 Prefix_Suffix int64
7 having_Sub_Domain int64
8 SSLfinal_State int64
9 Domain_registeration_length int64
10 Favicon int64
11 port int64
12 HTTPS_token int64
13 Request_URL int64
14 URL_of_Anchor int64
15 Links_in_tags int64
16 SFH int64
17 Submitting_to_email int64
18 Abnormal_URL int64
19 Redirect int64
20 on_mouseover int64
21 RightClick int64
22 popUpWidnow int64
23 Iframe int64
24 age_of_domain int64
25 DNSRecord int64
26 web_traffic int64
27 Page_Rank int64
28 Google_Index int64
29 Links_pointing_to_page int64
30 Statistical_report int64

Values: 1 (Legitimate), 0 (Suspicious), -1 (Phishing)


🀝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

πŸ“œ Code of Conduct

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.

πŸ” Security

For reporting vulnerabilities, see SECURITY.md.


πŸ‘€ Author

Prashant Shukla


πŸ“„ License

This project is open source and available under the MIT License.


Made with ❀️ for a safer internet.

About

This project aims to detect malicious network activity using Machine Learning-based Intrusion Detection. It focuses on analyzing network traffic data to classify whether behavior is normal or attack-related, helping organizations strengthen their cybersecurity posture.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors