Skip to content

Repository files navigation

πŸ›‘οΈ Network Security β€” Phishing URL Detection System

Python FastAPI MongoDB MLflow Docker scikit-learn CI/CD License

An end-to-end Machine Learning system for detecting phishing URLs β€” built with a production-grade MLOps pipeline.


πŸ“Œ Overview

Network Security is a full-stack MLOps project that detects phishing websites using machine learning. It goes beyond a simple notebook β€” implementing a complete, production-ready pipeline covering data ingestion from MongoDB, data validation with drift detection, feature transformation, model training with hyperparameter tuning, experiment tracking via MLflow & DagsHub, and deployment as a REST API using FastAPI inside a Docker container with CI/CD via GitHub Actions.

πŸ” The model analyzes network-level features of URLs to classify them as phishing or legitimate β€” a real-world cybersecurity problem with direct industry relevance.


✨ Key Highlights

Feature Description
πŸ—„οΈ Data Source Phishing dataset stored and fetched from MongoDB Atlas
βœ… Data Validation Schema checks + KS-test based data drift detection
βš™οΈ Feature Engineering KNN Imputer for missing values, preprocessing pipeline serialized as .pkl
πŸ€– Model Selection Automated comparison of 5 classifiers with GridSearchCV
πŸ“Š Experiment Tracking All runs tracked via MLflow integrated with DagsHub
πŸš€ REST API Full prediction & training endpoints via FastAPI
🐳 Containerized Fully Dockerized application
πŸ”„ CI/CD Automated pipeline via GitHub Actions

πŸ—οΈ System Architecture

Raw Data (CSV)
      β”‚
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  MongoDB Atlas  β”‚  ← Data Storage
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Data Ingestion β”‚  ← Pulls data, splits into Train/Test
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Data Validation  β”‚  ← Schema validation + Drift Detection (KS-Test)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Data Transformationβ”‚  ← KNN Imputer, Preprocessing β†’ saved as .pkl
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Model Trainer  β”‚  ← Trains 5 models, selects best, logs to MLflow
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  FastAPI Server  β”‚  ← /train and /predict endpoints
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Docker + CI/CD  β”‚  ← Containerized & deployed via GitHub Actions
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🧠 ML Pipeline β€” Deep Dive

1. πŸ“₯ Data Ingestion

  • Connects to MongoDB Atlas and exports the NetworkData collection as a Pandas DataFrame
  • Handles missing values (na β†’ NaN) and drops internal MongoDB _id field
  • Splits data into train (80%) / test (20%) and saves to the artifact store

2. βœ… Data Validation

  • Validates that train and test datasets conform to the expected schema (column count, column names)
  • Detects data drift using the Kolmogorov-Smirnov (KS) test on each feature column
  • Generates a drift report (report.yaml) flagging drifted columns with p-values

3. πŸ”§ Data Transformation

  • Applies KNN Imputer (k=3, weights=uniform) to handle missing values
  • Serializes the complete preprocessing pipeline as preprocessing.pkl for inference consistency

4. πŸ€– Model Training & Selection

Trains and evaluates 5 classification models using cross-validated GridSearchCV:

Model Hyperparameters Tuned
Random Forest n_estimators
Decision Tree criterion
Gradient Boosting learning_rate, subsample, n_estimators
Logistic Regression β€”
AdaBoost learning_rate, n_estimators
  • Selects the best model by test score
  • Evaluates with F1-Score, Precision, and Recall
  • Logs all metrics and models to MLflow / DagsHub

5. πŸ“Š Experiment Tracking (MLflow + DagsHub)

  • Every training run is logged with metrics: f1_score, precision, recall
  • Models are registered in the MLflow Model Registry
  • Experiments are tracked remotely on DagsHub

πŸš€ API Endpoints

Once the server is running, navigate to http://localhost:8000/docs for the interactive Swagger UI.

Method Endpoint Description
GET / Redirects to Swagger UI
GET /train Triggers the full training pipeline
POST /predict Upload a CSV file and get phishing predictions

Prediction Response

The /predict endpoint accepts a CSV file, runs inference using the saved model + preprocessor, and returns an HTML table with a predicted_column appended (1 = Phishing, -1 = Legitimate).


πŸ—‚οΈ Project Structure

NetworkSecurity/
β”œβ”€β”€ networksecurity/
β”‚   β”œβ”€β”€ components/               # Core ML pipeline stages
β”‚   β”‚   β”œβ”€β”€ data_ingestion.py
β”‚   β”‚   β”œβ”€β”€ data_validation.py
β”‚   β”‚   β”œβ”€β”€ data_transformation.py
β”‚   β”‚   └── model_trainer.py
β”‚   β”œβ”€β”€ pipeline/
β”‚   β”‚   └── training_pipeline.py  # Orchestrates full pipeline
β”‚   β”œβ”€β”€ entity/
β”‚   β”‚   β”œβ”€β”€ config_entity.py      # Configuration dataclasses
β”‚   β”‚   └── artifact_entity.py    # Artifact dataclasses
β”‚   β”œβ”€β”€ constants/
β”‚   β”‚   └── training_pipeline/    # All constants & hyperparams
β”‚   β”œβ”€β”€ utils/                    # Utility functions
β”‚   └── exception/ & logging/     # Custom exception & logger
β”œβ”€β”€ templates/
β”‚   └── table.html                # Prediction results UI
β”œβ”€β”€ app.py                        # FastAPI application
β”œβ”€β”€ main.py                       # Run pipeline manually
β”œβ”€β”€ push_data.py                  # Load CSV data into MongoDB
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ setup.py
└── .github/workflows/main.yml    # CI/CD pipeline

βš™οΈ Setup & Installation

Prerequisites

  • Python 3.10+
  • MongoDB Atlas account
  • DagsHub account (for MLflow tracking)
  • Docker (optional, for containerized run)

1. Clone the Repository

git clone https://github.com/RudrakshJani/NetworkSecurity.git
cd NetworkSecurity

2. Create a Virtual Environment

python -m venv venv
source venv/bin/activate        # Linux/macOS
venv\Scripts\activate           # Windows

3. Install Dependencies

pip install -r requirements.txt

4. Configure Environment Variables

Create a .env file in the project root:

MONGODB_URL_KEY=your_mongodb_atlas_connection_string
MLFLOW_TRACKING_URI=your_dagshub_mlflow_uri
MLFLOW_TRACKING_USERNAME=your_dagshub_username
MLFLOW_TRACKING_PASSWORD=your_dagshub_token

5. Push Data to MongoDB (First-time setup)

python push_data.py

6. Run the Application

python app.py

Visit http://localhost:8000/docs to access the API.


🐳 Docker

Build the Image

docker build -t networksecurity:latest .

Run the Container

docker run -p 8000:8000 --env-file .env networksecurity:latest

πŸ”„ CI/CD Pipeline

This project uses GitHub Actions for continuous integration. On every push to main:

  1. Checkout β€” Pulls the latest code
  2. Lint β€” Code quality check
  3. Unit Tests β€” Automated test execution

The workflow is defined in .github/workflows/main.yml.


πŸ› οΈ Tech Stack

Category Technology
Language Python 3.10+
ML Framework Scikit-learn
API Framework FastAPI + Uvicorn
Database MongoDB Atlas
Experiment Tracking MLflow + DagsHub
Containerization Docker
CI/CD GitHub Actions
Data Processing Pandas, NumPy
Config Management PyYAML, python-dotenv

πŸ“ˆ Why This Project Stands Out

  • βœ… Not just a notebook β€” fully modular, production-grade codebase
  • βœ… MLOps best practices β€” artifact management, experiment tracking, model registry
  • βœ… Real-world problem β€” phishing detection is a top cybersecurity challenge
  • βœ… Scalable design β€” pipeline components are loosely coupled and independently testable
  • βœ… End-to-end β€” from raw data in MongoDB to a live REST API in Docker

πŸ‘¨β€πŸ’» Author

Rudraksh Jani πŸ“§ rudrakshjani05@gmail.com


πŸ“„ License

This project is licensed under the MIT License β€” see the LICENSE file for details.


If you found this project useful or interesting, please consider giving it a ⭐ on GitHub!

About

An end-to-end MLOps pipeline for detecting phishing URLs. Built with scikit-learn, this production-grade system features MongoDB data ingestion, drift detection, MLflow experiment tracking, and a FastAPI REST API deployed via Docker and GitHub Actions.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages