Skip to content

Repository files navigation

DNS Spoofing Detection - Multi-Model Research Platform

Real-time AI-based DNS threat analysis system supporting multiple detection models with hybrid feature selection (SelectKBest + SHAP) on the BCCC-CIC-Bell-DNS-2024 dataset.

πŸ“‹ Project Overview

This comprehensive research platform implements 6 different detection approaches for DNS spoofing and malicious DNS behavior:

  • Supervised Models: LightGBM (gradient boosting), Random Forest (ensemble trees)
  • Unsupervised Models: One-Class SVM (anomaly detection), Isolation Forest (outlier detection)
  • Deep Learning: BiLSTM + K-Means (sequence analysis with clustering)
  • Ensemble: Voting/stacking ensemble of multiple models

Key Features:

  • Hybrid feature selection combining statistical filtering (SelectKBest) and model-based explainability (SHAP)
  • Real-time inference with <100ms latency constraint
  • Multi-class detection for various DNS attack types (malware, phishing, spam, data exfiltration)
  • Modular architecture for easy model comparison and research

πŸ“Š Dataset

BCCC-CIC-Bell-DNS-2024 (~4.3GB, 26 CSV files)

  • Generated using ALFlowLyzer from CIC-Bell-DNS-2021 and CIC-Bell-DNS-EXF-2021
  • 121 features including network flow metrics, statistical features, and DNS-specific attributes
  • Two categories:
    • BCCC-CIC-Bell-DNS-EXF/: Data exfiltration traffic (18 files: benign, light/heavy exfiltration)
    • BCCC-CIC-Bell-DNS-Mal/: Malicious DNS traffic (7 files: benign, malware, phishing, spam)

Download: Kaggle Dataset

Citation:

Shafi, MohammadMoein, Arash Habibi Lashkari, Hardhik Mohanty. "Unveiling Malicious DNS Behavior Profiling and Generating Benchmark Dataset through Application Layer Traffic Analysis". Computers and Electrical Engineering, 2024.

πŸ—οΈ Project Structure

project_root/
β”œβ”€β”€ BCCC-CIC-Bell-DNS-2024/          # Dataset directory (~4.3GB)
β”‚   β”œβ”€β”€ BCCC-CIC-Bell-DNS-EXF/       # Exfiltration traffic (18 CSV files)
β”‚   └── BCCC-CIC-Bell-DNS-Mal/       # Malicious traffic (7 CSV files)
β”‚
β”œβ”€β”€ configs/                          # Configuration files
β”‚   β”œβ”€β”€ base_config.yaml             # Shared settings (data paths, preprocessing)
β”‚   β”œβ”€β”€ lightgbm_config.yaml         # LightGBM hyperparameters
β”‚   β”œβ”€β”€ random_forest_config.yaml    # Random Forest hyperparameters
β”‚   β”œβ”€β”€ ocsvm_config.yaml            # One-Class SVM hyperparameters
β”‚   β”œβ”€β”€ isolation_forest_config.yaml # Isolation Forest hyperparameters
β”‚   β”œβ”€β”€ bilstm_config.yaml           # BiLSTM + K-Means configuration
β”‚   └── ensemble_config.yaml         # Ensemble model configuration
β”‚
β”œβ”€β”€ scripts/                          # Training scripts
β”‚   β”œβ”€β”€ train_lightgbm.py            # Train LightGBM (supervised)
β”‚   β”œβ”€β”€ train_random_forest.py       # Train Random Forest (supervised)
β”‚   β”œβ”€β”€ train_ocsvm.py               # Train One-Class SVM (unsupervised)
β”‚   β”œβ”€β”€ train_isolation_forest.py    # Train Isolation Forest (unsupervised)
β”‚   β”œβ”€β”€ train_bilstm_kmeans.py       # Train BiLSTM + K-Means (deep learning)
β”‚   └── train_ensemble.py            # Train ensemble model
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ models/                      # Model implementations
β”‚   β”‚   β”œβ”€β”€ __init__.py              # Models package
β”‚   β”‚   β”œβ”€β”€ base_model.py            # Abstract base classes (BaseDetector, etc.)
β”‚   β”‚   β”œβ”€β”€ lightgbm_model.py        # LightGBM detector
β”‚   β”‚   β”œβ”€β”€ random_forest.py         # Random Forest detector
β”‚   β”‚   β”œβ”€β”€ unsupervised_ocsvm.py    # One-Class SVM detector
β”‚   β”‚   β”œβ”€β”€ unsupervised_iforest.py  # Isolation Forest detector
β”‚   β”‚   β”œβ”€β”€ deep_bilstm_kmeans.py    # BiLSTM + K-Means detector
β”‚   β”‚   └── ensemble_model.py        # Ensemble detector
β”‚   β”‚
β”‚   β”œβ”€β”€ trainers/                    # Training orchestration
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ base_trainer.py          # Base trainer class
β”‚   β”‚   β”œβ”€β”€ supervised_trainer.py    # Supervised model trainer
β”‚   β”‚   └── unsupervised_trainer.py  # Unsupervised model trainer
β”‚   β”‚
β”‚   β”œβ”€β”€ evaluation/                  # Evaluation and visualization
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ metrics.py               # Metric computation utilities
β”‚   β”‚   └── visualizations.py        # Plotting utilities
β”‚   β”‚
β”‚   β”œβ”€β”€ preprocessing.py             # Data loading & preprocessing
β”‚   β”œβ”€β”€ feature_selection.py         # Hybrid SelectKBest + SHAP
β”‚   β”œβ”€β”€ real_time_detection.py       # Real-time inference pipeline
β”‚   └── utils.py                     # Helper functions
β”‚
β”œβ”€β”€ notebooks/                        # Jupyter notebooks
β”‚   β”œβ”€β”€ exploratory_analysis.ipynb   # Dataset EDA
β”‚   β”œβ”€β”€ model_comparison.ipynb       # Compare all models
β”‚   └── feature_importance.ipynb     # Feature analysis
β”‚
β”œβ”€β”€ models/                          # Saved model artifacts
β”œβ”€β”€ results/                         # Evaluation results
β”‚   β”œβ”€β”€ plots/                       # Visualizations
β”‚   β”œβ”€β”€ metrics/                     # Performance metrics
β”‚   └── experiments/                 # Experiment tracking
β”‚
β”œβ”€β”€ docs/                            # Documentation
β”‚   β”œβ”€β”€ unsupervised_approach.md     # Unsupervised methods guide
β”‚   β”œβ”€β”€ supervised_vs_unsupervised.md # Model comparison
β”‚   └── architecture.md              # System architecture (NEW)
β”‚
β”œβ”€β”€ tests/                           # Unit tests
β”‚   β”œβ”€β”€ test_models.py
β”‚   β”œβ”€β”€ test_preprocessing.py
β”‚   └── test_feature_selection.py
β”‚
β”œβ”€β”€ logs/                            # Training logs
β”œβ”€β”€ requirements.txt                 # Python dependencies
└── README.md                        # This file

πŸ€– Detection Models

1. LightGBM (Supervised) ⭐

  • Type: Gradient Boosting Decision Trees
  • Training: Labeled benign + malicious traffic
  • Expected Accuracy: ~96-98%
  • Strengths: Fast training, high accuracy, feature importance
  • Use Case: Production deployment, known attack detection
  • Script: scripts/train_lightgbm.py
  • Config: configs/lightgbm_config.yaml

2. Random Forest (Supervised)

  • Type: Ensemble of decision trees
  • Training: Labeled benign + malicious traffic
  • Expected Accuracy: ~94-96%
  • Strengths: Robust to overfitting, interpretable
  • Use Case: Baseline comparison, feature importance validation
  • Script: scripts/train_random_forest.py
  • Config: configs/random_forest_config.yaml

3. One-Class SVM (Unsupervised) πŸ†•

  • Type: Anomaly detection (one-class classification)
  • Training: Benign traffic only (no attack labels needed)
  • Expected Accuracy: ~85-92%
  • Strengths: Zero-day attack detection, no labeling required
  • Use Case: Novel threat detection, anomaly-based defense
  • Script: scripts/train_ocsvm.py
  • Config: configs/ocsvm_config.yaml

4. Isolation Forest (Unsupervised) πŸ†•

  • Type: Tree-based anomaly detection
  • Training: Benign traffic only
  • Expected Accuracy: ~87-93%
  • Strengths: Fast, scalable, handles high-dimensional data
  • Use Case: Large-scale anomaly detection, real-time systems
  • Script: scripts/train_isolation_forest.py
  • Config: configs/isolation_forest_config.yaml

5. BiLSTM + K-Means (Deep Learning) πŸ†•

  • Type: Sequence modeling with clustering
  • Training: Sequences of DNS flows
  • Expected Performance: Depends on sequence design
  • Strengths: Temporal pattern detection, complex behaviors
  • Use Case: Advanced persistent threats, behavioral analysis
  • Script: scripts/train_bilstm_kmeans.py
  • Config: configs/bilstm_config.yaml

6. Ensemble Model (Meta-Learner) πŸ†•

  • Type: Voting/stacking ensemble
  • Training: Combines multiple base models
  • Expected Accuracy: Best overall (98%+)
  • Strengths: Leverages strengths of all models
  • Use Case: Critical deployments requiring highest accuracy
  • Script: scripts/train_ensemble.py
  • Config: configs/ensemble_config.yaml

πŸš€ Getting Started

Prerequisites

  • Python 3.8+
  • 8GB+ RAM recommended (dataset is ~4.3GB)
  • GPU optional (for BiLSTM model)
  • Windows/Linux/MacOS

Installation

  1. Navigate to project directory:

    cd "c:\Users\Ishank\Desktop\cns project 2024 dataset"
  2. Install dependencies:

    pip install -r requirements.txt
  3. Verify dataset location:

    dir BCCC-CIC-Bell-DNS-2024  # Windows
    ls BCCC-CIC-Bell-DNS-2024   # Linux/Mac

Quick Start - Training Models

1. LightGBM (Supervised - Recommended)

# Quick test with 10% sample
python scripts/train_lightgbm.py --sample 0.1 --experiment-name lightgbm_test

# Full training
python scripts/train_lightgbm.py --experiment-name lightgbm_full

# Custom hyperparameters
python scripts/train_lightgbm.py --config configs/lightgbm_config.yaml

2. One-Class SVM (Unsupervised)

# Quick test
python scripts/train_ocsvm.py --sample 0.1 --experiment-name ocsvm_test

# Full training
python scripts/train_ocsvm.py --experiment-name ocsvm_full

# Adjust sensitivity (nu parameter)
python scripts/train_ocsvm.py --nu 0.01 --experiment-name ocsvm_strict

3. Random Forest (Supervised)

# Quick test
python scripts/train_random_forest.py --sample 0.1 --experiment-name rf_test

# Full training
python scripts/train_random_forest.py --experiment-name rf_full

4. Isolation Forest (Unsupervised)

# Quick test
python scripts/train_isolation_forest.py --sample 0.1 --experiment-name iforest_test

# Full training
python scripts/train_isolation_forest.py --experiment-name iforest_full

5. Ensemble Model

# Train ensemble (trains base models automatically)
python scripts/train_ensemble.py --experiment-name ensemble_full

# Use pre-trained models
python scripts/train_ensemble.py --use-pretrained --experiment-name ensemble_pretrained

Model Comparison

# Compare all models on same test set
python scripts/compare_models.py \
    --models lightgbm random_forest ocsvm iforest \
    --sample 0.1 \
    --output results/comparison_report.html

πŸ”§ Configuration System

The project uses a hierarchical configuration system:

Base Configuration (configs/base_config.yaml)

Shared settings across all models:

data:
  dataset_path: "BCCC-CIC-Bell-DNS-2024"
  test_size: 0.2
  random_state: 42
  
preprocessing:
  handle_missing: "median"
  scale_features: true
  
output:
  models_dir: "models"
  results_dir: "results"

Model-Specific Configurations

Each model has its own config file that extends the base config:

  • lightgbm_config.yaml: LightGBM hyperparameters
  • ocsvm_config.yaml: One-Class SVM settings
  • random_forest_config.yaml: Random Forest parameters
  • etc.

Edit configs to customize training parameters!

πŸ“ˆ Training Pipeline

All models follow a consistent pipeline:

  1. Configuration Loading: Load base + model-specific config
  2. Data Loading: Memory-efficient CSV loading (pandas/Dask)
  3. Preprocessing: Missing values, encoding, normalization
  4. Train/Test Split: Stratified split (preserves class distribution)
  5. Feature Selection (optional): SelectKBest + SHAP
  6. Model Training: Train model with specified hyperparameters
  7. Evaluation: Comprehensive metrics (accuracy, precision, recall, F1, ROC-AUC)
  8. Visualization: Confusion matrix, ROC curves, feature importance
  9. Model Saving: Persist model + metadata

🎯 Expected Performance

Model Type Accuracy Training Time Inference Speed Use Case
LightGBM Supervised 96-98% ~10 min <50ms Production
Random Forest Supervised 94-96% ~15 min <100ms Baseline
One-Class SVM Unsupervised 85-92% ~20 min <100ms Zero-day
Isolation Forest Unsupervised 87-93% ~5 min <30ms Real-time
BiLSTM + K-Means Deep Learning TBD ~60 min <200ms Research
Ensemble Meta 98%+ ~30 min <150ms Critical

Times on 4.3GB dataset with i7 CPU, 16GB RAM

πŸ” Real-Time Detection

Use any trained model for real-time detection:

from src.models import load_model
from src.real_time_detection import RealTimeDNSDetector

# Load trained model
model = load_model('models/lightgbm_best.pkl')

# Initialize detector
detector = RealTimeDNSDetector(model=model)

# Predict single DNS flow
flow_data = {...}  # Dictionary with 121 DNS features
result = detector.predict_single(flow_data)

print(f"Prediction: {result['label']}")        # 'Benign' or 'Malicious'
print(f"Confidence: {result['confidence']:.2%}")  # 95.4%
print(f"Latency: {result['latency_ms']:.1f}ms")  # 23.4ms

πŸ“Š Model Architecture

Abstract Base Classes

All models inherit from BaseDetector:

from src.models import BaseDetector, SupervisedDetector, UnsupervisedDetector

class MyCustomDetector(SupervisedDetector):
    def build_model(self):
        # Build your model
        pass
    
    def train(self, X, y):
        # Training logic
        pass
    
    def predict(self, X):
        # Prediction logic
        pass

Benefits:

  • Consistent API across all models
  • Easy to add new models
  • Built-in save/load functionality
  • Standardized evaluation

πŸ“ Command Line Interface

Common Arguments

All training scripts support:

--config CONFIG          # Path to config file
--sample FLOAT           # Sample fraction (0.0-1.0)
--experiment-name NAME   # Experiment identifier
--no-cache               # Disable caching
--clear-cache            # Clear cache before run
--verbose                # Detailed logging

Model-Specific Arguments

LightGBM:

--skip-feature-selection  # Use all features
--k-best INT             # SelectKBest k value
--shap-top-n INT         # SHAP top features

One-Class SVM:

--nu FLOAT               # Outlier fraction (0.01-0.5)
--kernel STR             # 'rbf', 'poly', 'sigmoid'
--gamma STR              # 'scale', 'auto', or float

Ensemble:

--method STR             # 'voting', 'stacking', 'weighted'
--use-pretrained         # Load pre-trained base models

πŸ› Troubleshooting

Memory Issues

  • Use --sample 0.1 for testing
  • Enable Dask in base_config.yaml: use_dask: true
  • Reduce feature count: lower k_best value

Import Errors

# Verify all dependencies installed
pip install -r requirements.txt

# Check Python version
python --version  # Should be 3.8+

Slow Training

  • Start with small sample: --sample 0.1
  • Reduce n_estimators in config
  • Use cached data (don't use --no-cache)

Model Comparison

  • Ensure all models trained on same data split
  • Use fixed random_state in base_config.yaml
  • Compare metrics from same experiment run

πŸ“š Documentation

  • docs/unsupervised_approach.md: Comprehensive guide to unsupervised methods
  • docs/supervised_vs_unsupervised.md: Comparison of approaches
  • docs/architecture.md: System design and architecture (TODO)
  • .github/copilot-instructions.md: Development guidelines

πŸ§ͺ Testing

Run unit tests:

# All tests
pytest tests/

# Specific module
pytest tests/test_models.py

# With coverage
pytest --cov=src tests/

🀝 Contributing

This is a research project for DNS spoofing detection. Contributions welcome!

To add a new model:

  1. Create model class inheriting from BaseDetector or SupervisedDetector/UnsupervisedDetector
  2. Implement required abstract methods: build_model(), train(), predict(), evaluate()
  3. Create config file in configs/
  4. Create training script in scripts/
  5. Update src/models/__init__.py

πŸ“– Research Context

This project supports research in:

  • Malicious DNS behavior profiling
  • Zero-day attack detection (unsupervised methods)
  • Feature engineering for DNS traffic
  • Model comparison for cybersecurity
  • Real-time threat detection systems

βœ… Key Features

βœ… 6 detection models (supervised, unsupervised, deep learning, ensemble)
βœ… Modular architecture (easy to extend and compare)
βœ… Consistent API (BaseDetector abstract class)
βœ… Hybrid feature selection (SelectKBest + SHAP)
βœ… Memory-efficient (handles 4.3GB dataset)
βœ… Real-time inference (<100ms latency)
βœ… Comprehensive evaluation (10+ metrics, visualizations)
βœ… Experiment tracking (reproducible results)
βœ… Production-ready (save/load, logging, error handling)


Status: βœ… Structure implemented | ⚠️ Some models pending | πŸš€ Ready for research

For questions or issues, consult the documentation in docs/ or review configuration files in configs/.

About

Real-time AI-based DNS threat analysis system

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages