Skip to content

Repository files navigation

πŸ›‘οΈ Your Bank Just Called β€” But Was It Really Them? Detecting Phishing Attacks with Machine Learning

Python FastAPI MongoDB MLflow Docker AWS scikit-learn Status

An end-to-end MLOps pipeline that detects phishing websites before they steal your data.

Getting Started Β· Architecture Β· Results Β· Deployment


πŸ“Œ The Problem

Every 11 seconds, a new phishing attack is launched. In 2024 alone, phishing was responsible for over $10 billion in losses worldwide, and 36% of all data breaches involved phishing.

Traditional rule-based filters catch known threats β€” but attackers are evolving faster than the rules. They use URL shorteners, IP-based addresses, suspicious iframes, fake SSL certificates, and domain age manipulation to bypass conventional detection.

This project solves that. By analyzing 30 real-time website features β€” from URL structure and domain metadata to traffic patterns and page behavior β€” this system learns the fingerprint of a phishing site and flags it before a user ever clicks.

Whether it's a fake banking portal, a spoofed login page, or a malicious redirect, the model catches it with 97.4% F1-score on unseen data.


🎯 What This Project Does

Capability Description
πŸ” Phishing Detection Classifies websites as legitimate or phishing based on 30 extracted features
πŸ“Š Automated ML Pipeline End-to-end pipeline: Ingestion β†’ Validation β†’ Transformation β†’ Training
πŸ§ͺ Experiment Tracking All model runs tracked via MLflow + DagsHub
πŸ”„ Data Drift Detection Automatically detects distribution shifts using the KS-2 sample test
☁️ Cloud-Native Model artifacts synced to AWS S3; deployed on AWS EC2 via Docker
🌐 REST API FastAPI-based prediction endpoint β€” upload a CSV and get results instantly
πŸ” CI/CD GitHub Actions pipeline: lint β†’ build β†’ push to ECR β†’ deploy to EC2

πŸ—οΈ System Architecture

                          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                          β”‚            GitHub Actions CI/CD          β”‚
                          β”‚  (Lint β†’ Build β†’ Push ECR β†’ Deploy EC2)  β”‚
                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                         β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   MongoDB    │───▢│   Data     │───▢│    Data      │───▢│    Data      β”‚
β”‚  (Raw Data)  β”‚    β”‚ Ingestion  β”‚    β”‚  Validation  β”‚    β”‚Transformationβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                                                β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  FastAPI   │◀───│  AWS S3 +    │◀───│    Model     β”‚
                    β”‚  Predict   β”‚    β”‚  ECR + EC2   β”‚    β”‚   Trainer    β”‚
                    β”‚  Endpoint  β”‚    β”‚  (Deploy)    β”‚    β”‚  (Best Model)β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚                                      β”‚
                          β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
                          └──────────▢│   MLflow +   β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”‚   DagsHub    β”‚
                                      β”‚  (Tracking)  β”‚
                                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

NetworkSecurity/
β”‚
β”œβ”€β”€ networksequrity/                # Core ML package
β”‚   β”œβ”€β”€ components/                 # Pipeline components
β”‚   β”‚   β”œβ”€β”€ data_ingestion.py       #   β†’ Fetch data from MongoDB, train-test split
β”‚   β”‚   β”œβ”€β”€ data_validation.py      #   β†’ Schema checks, column validation, drift detection
β”‚   β”‚   β”œβ”€β”€ data_transformation.py  #   β†’ KNN Imputer for missing values
β”‚   β”‚   └── model_trainer.py        #   β†’ Train 5 classifiers, hyperparameter tuning, MLflow
β”‚   β”œβ”€β”€ pipeline/
β”‚   β”‚   └── training_pipeline.py    # Orchestrates the full training workflow
β”‚   β”œβ”€β”€ cloud/
β”‚   β”‚   └── s3_syncer.py            # Sync artifacts & models to AWS S3
β”‚   β”œβ”€β”€ entity/                     # Config & artifact dataclasses
β”‚   β”œβ”€β”€ constant/                   # Constants (paths, params, thresholds)
β”‚   β”œβ”€β”€ exception/                  # Custom exception handling
β”‚   β”œβ”€β”€ logging/                    # Structured logging
β”‚   └── utils/                      # ML utilities, metrics, model wrapper
β”‚
β”œβ”€β”€ app.py                          # FastAPI application (train & predict endpoints)
β”œβ”€β”€ main.py                         # Standalone training script
β”œβ”€β”€ push_data.py                    # Push CSV data to MongoDB
β”œβ”€β”€ DockerFile                      # Container definition (Python 3.12-slim)
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ setup.py                        # Package configuration
β”œβ”€β”€ data_schema/schema.yaml         # Column schema (30 features + target)
β”œβ”€β”€ Network_data/phisingData.csv    # Raw phishing dataset
β”œβ”€β”€ templates/table.html            # Jinja2 template for prediction results
β”‚
β”œβ”€β”€ .github/workflows/main.yml      # CI/CD: GitHub Actions β†’ ECR β†’ EC2
└── logs/                           # Training run logs with metrics

πŸ“Š Model Performance

The pipeline evaluates 5 classifiers with hyperparameter tuning via GridSearchCV and selects the best performer:

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

πŸ† Best Model Results (from training logs)

Training Set Test Set
Run F1-Score Precision Recall F1-Score Precision Recall
Run 1 0.9912 0.9888 0.9936 0.9766 0.9702 0.9832
Run 2 0.9920 0.9895 0.9945 0.9710 0.9557 0.9869
Run 3 0.9915 0.9896 0.9934 0.9738 0.9674 0.9803

Key Takeaway: The best model consistently achieves ~99.1% F1-score on training data and ~97.4% F1-score on the test set β€” with recall above 98%, meaning almost no phishing site goes undetected.


πŸ”¬ Feature Engineering

The model uses 30 handcrafted features extracted from website characteristics:

Category Features
URL Analysis having_IP_Address, URL_Length, Shortining_Service, having_At_Symbol, double_slash_redirecting, Prefix_Suffix, having_Sub_Domain
Security Signals SSLfinal_State, HTTPS_token, Domain_registeration_length
Page Behavior Redirect, on_mouseover, RightClick, popUpWidnow, Iframe
External References Request_URL, URL_of_Anchor, Links_in_tags, SFH, Submitting_to_email, Abnormal_URL, Favicon
Domain Intelligence age_of_domain, DNSRecord, web_traffic, Page_Rank, Google_Index, Links_pointing_to_page, Statistical_report, port

Missing values are handled using KNN Imputer (k=3, uniform weights).


πŸš€ Deployment

βœ… Current Status: DEPLOYED & LIVE

The application is fully deployed and accessible using the following infrastructure:

Component Technology Status
Containerization Docker (Python 3.12-slim) βœ… Running
Container Registry AWS ECR (Elastic Container Registry) βœ… Active
Compute AWS EC2 (Self-hosted runner) βœ… Live
Storage AWS S3 (Model artifacts & training data) βœ… Synced
CI/CD GitHub Actions (3-stage pipeline) βœ… Automated
Experiment Tracking MLflow via DagsHub βœ… Tracking
Database MongoDB Atlas βœ… Connected

CI/CD Pipeline Flow

Push to main β†’ GitHub Actions triggers:

  1️⃣  Continuous Integration
      └── Lint code β†’ Run unit tests

  2️⃣  Continuous Delivery
      └── Configure AWS β†’ Login to ECR β†’ Build Docker image β†’ Push to ECR

  3️⃣  Continuous Deployment (Self-hosted EC2 runner)
      └── Pull image from ECR β†’ Stop old container β†’ Run new container on port 8000

πŸ› οΈ Getting Started

Prerequisites

  • Python 3.12+
  • MongoDB Atlas account (or local MongoDB)
  • AWS account (for S3 and ECR)
  • Docker (optional, for containerized deployment)

Installation

# Clone the repository
git clone https://github.com/HowWilbert/Network-Security.git
cd Network-Security

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Configuration

Create a .env file in the root directory:

MONGO_DB_URL=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/
MONGODB_URL_KEY=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/

Push Data to MongoDB

python push_data.py

Run Training Pipeline

python main.py

Launch the API Server

python app.py

The API will be live at http://localhost:8000.

API Endpoints

Method Endpoint Description
GET / Redirects to interactive API docs
GET /train Triggers the full training pipeline
POST /predict Upload a CSV file β†’ Get phishing predictions

πŸ§ͺ Experiment Tracking

All training experiments are tracked on DagsHub MLflow, including:

  • Model hyperparameters
  • F1-score, Precision, Recall (train + test)
  • Serialized model artifacts
  • Run comparisons across experiments

🐳 Docker

Build and run locally:

docker build -t network-security .
docker run -d -p 8000:8000 --name networksecurity network-security

πŸ“¦ Tech Stack

Layer Technologies
ML Framework scikit-learn (Random Forest, Gradient Boosting, AdaBoost, Decision Tree, Logistic Regression)
Data Processing pandas, NumPy, KNN Imputer
Experiment Tracking MLflow, DagsHub
API FastAPI, Uvicorn
Database MongoDB Atlas, PyMongo
Cloud AWS S3, ECR, EC2
CI/CD GitHub Actions
Containerization Docker
Visualization Matplotlib, Seaborn

πŸ‘€ Author

Ansh Bire


πŸ“œ License

This project is open-source and available for educational and research purposes.


If you found this project useful, give it a ⭐ on GitHub!

Built with πŸ’» and β˜• by Ansh Bire

About

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages