π‘οΈ Your Bank Just Called β But Was It Really Them? Detecting Phishing Attacks with Machine Learning
An end-to-end MLOps pipeline that detects phishing websites before they steal your data.
Getting Started Β· Architecture Β· Results Β· Deployment
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.
| 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 |
ββββββββββββββββββββββββββββββββββββββββββββ
β 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) β
ββββββββββββββββ
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
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 |
| 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.
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).
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 |
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
- Python 3.12+
- MongoDB Atlas account (or local MongoDB)
- AWS account (for S3 and ECR)
- Docker (optional, for containerized deployment)
# 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.txtCreate 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/python push_data.pypython main.pypython app.pyThe API will be live at http://localhost:8000.
| 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 |
All training experiments are tracked on DagsHub MLflow, including:
- Model hyperparameters
- F1-score, Precision, Recall (train + test)
- Serialized model artifacts
- Run comparisons across experiments
Build and run locally:
docker build -t network-security .
docker run -d -p 8000:8000 --name networksecurity network-security| 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 |
Ansh Bire
- π§ bireansh1@gmail.com
- π GitHub
- π DagsHub - Experiment Tracking
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