Skip to content

A production-grade cybersecurity system combining Machine Learning-based threat detection with active prevention through automated firewall management.

Notifications You must be signed in to change notification settings

RayenMalouche/NIPDS

Repository files navigation

Network Intrusion Detection & Prevention System (NIDS + NIPS)

License Python React Docker

A production-grade cybersecurity system combining Machine Learning-based threat detection with active prevention through automated firewall management.


🎯 Project Overview

This system implements both passive detection (NIDS) and active prevention (NIPS) to protect networks from cyber threats in real-time.

Key Features

Detection (NIDS)

  • πŸ” Real-time packet capture and analysis using Scapy
  • πŸ€– ML-based detection with 95% accuracy (Random Forest, Isolation Forest)
  • πŸ“Š Rule-based detection for known attack patterns
  • πŸ“ˆ Real-time traffic visualization and analytics

Prevention (NIPS)

  • πŸ›‘οΈ Automatic IP blocking via system firewall (Windows/Linux/macOS)
  • ⚑ Intelligent threat scoring algorithm (cumulative risk assessment)
  • ⏱️ Time-based auto-unblock with configurable durations
  • 🎯 Whitelist/blacklist management for manual control
  • πŸ”„ Rate limiting for suspicious but non-critical threats

Attack Detection

  • Port Scanning
  • SYN Flood / DDoS
  • Brute Force attacks
  • SQL Injection attempts
  • ARP Spoofing
  • DNS Tunneling
  • Suspicious port access
  • Malware traffic patterns

πŸ“Š Performance Metrics

Metric Value
Detection Accuracy 95.2%
False Positive Rate <5%
Processing Speed 10,000+ packets/sec
Detection Latency <100ms
Prevention Latency <50ms
Model Training Time ~3-5 minutes
Supported Threats 8+ types

πŸ› οΈ Technology Stack

Backend

  • Language: Python 3.11
  • Framework: FastAPI + Uvicorn
  • ML/AI: scikit-learn, NumPy, Pandas
  • Network: Scapy, dpkt
  • Database: MongoDB (optional), Redis (optional)
  • Monitoring: Prometheus, Grafana

Frontend

  • Framework: React 18 + TypeScript
  • UI: Tailwind CSS
  • Charts: Recharts
  • Icons: Lucide React

Infrastructure

  • Containerization: Docker, Docker Compose
  • Firewall: netsh (Windows), iptables (Linux), pfctl (macOS)

πŸ“ Project Structure

nids-system/
β”œβ”€β”€ src/                          # Core application code
β”‚   β”œβ”€β”€ main.py                   # FastAPI backend (NIDS + NIPS integrated)
β”‚   β”œβ”€β”€ packet_analyzer.py        # Packet capture & ML detection engine
β”‚   β”œβ”€β”€ nips_engine.py            # Prevention engine (firewall management)
β”‚   β”œβ”€β”€ train_real.py             # Model training with real datasets
β”‚   └── generate_report.py        # Security report generator
β”‚
β”œβ”€β”€ config/                       # Configuration files
β”‚   β”œβ”€β”€ config.yaml               # System configuration
β”‚   β”œβ”€β”€ .env                      # Environment variables
β”‚   β”œβ”€β”€ prometheus.yml            # Prometheus metrics config
β”‚   └── init-mongo.js             # MongoDB initialization
β”‚
β”œβ”€β”€ docker/                       # Docker configuration
β”‚   β”œβ”€β”€ Dockerfile                # Main API container
β”‚   β”œβ”€β”€ Dockerfile.capture        # Packet capture container
β”‚   β”œβ”€β”€ docker-compose.yml        # Multi-container orchestration
β”‚   └── .dockerignore             # Docker ignore rules
β”‚
β”œβ”€β”€ frontend/                     # React dashboard
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.tsx               # Main app with dashboard toggle
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ NIDSDashboard.tsx # Detection-only dashboard
β”‚   β”‚   β”‚   └── NIPSDashboard.tsx # Prevention dashboard
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ package.json
β”‚   └── tailwind.config.js
β”‚
β”œβ”€β”€ grafana/                      # Grafana dashboards
β”‚   β”œβ”€β”€ dashboards/
β”‚   β”‚   └── nids-nips-overview.json
β”‚   └── datasources/
β”‚       └── prometheus.yml
β”‚
β”œβ”€β”€ models/                       # Trained ML models
β”‚   └── *.pkl
β”‚
β”œβ”€β”€ datasets/                     # Training datasets
β”‚   └── NF-UNSW-NB15-v3/
β”‚
β”œβ”€β”€ logs/                         # Application logs
β”œβ”€β”€ alerts/                       # Alert storage
β”œβ”€β”€ reports/                      # Generated reports
β”œβ”€β”€ screenshots/                  # Project screenshots
β”œβ”€β”€ scripts/                      # Utility scripts
β”œβ”€β”€ tests/                        # Test suite
β”‚
β”œβ”€β”€ requirements.txt              # Python dependencies
β”œβ”€β”€ README.md                     # This file
└── .gitignore                    # Git ignore rules

πŸ–ΌοΈ Screenshots

Real-time NIDS Dashboard showing normal behavior

Real-time NIDS Dashboard showing normal behavior

Forcing a threat

Forcing a threat

Real-time NIDS Dashboard showing threats

Real-time NIDS Dashboard showing threats Real-time NIDS Dashboard showing threats

--- ## πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • Node.js 18+ & npm
  • Docker & Docker Compose (optional)
  • Administrator/root privileges (for packet capture & firewall control)
  • Npcap (Windows) or libpcap (Linux/macOS)

1. Clone Repository

git clone https://github.com/RayenMalouche/NIPDS.git
cd NIPDS

2. Backend Setup

# Create virtual environment
python -m venv venv

# Activate (Windows)
venv\Scripts\activate
# Activate (Linux/macOS)
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp config/.env.example config/.env
# Edit config/.env with your settings

# Train ML models
cd src
python train_real.py

# Start backend (as Administrator/root)
python main.py

3. Frontend Setup

cd frontend

# Install dependencies
npm install

# Start development server
npm start

4. Access Dashboards


🐳 Docker Deployment

Quick Start with Docker

# Build and start all services
docker-compose -f docker/docker-compose.yml up -d

# View logs
docker-compose -f docker/docker-compose.yml logs -f

# Stop services
docker-compose -f docker/docker-compose.yml down

Available Services

Service Port Description
nids-api 8000 Main API server
nids-capture - Packet capture (host network)
mongodb 27017 Alert storage
redis 6379 Cache & session management
prometheus 9090 Metrics collection
grafana 3000 Data visualization

βš™οΈ Configuration

Environment Variables (config/.env)

# Network
CAPTURE_INTERFACE=Wi-Fi
PROMISCUOUS_MODE=true

# API
API_HOST=0.0.0.0
API_PORT=8000

# CORS
CORS_ORIGINS=["http://localhost:3000"]

# NIPS Settings
ENABLE_AUTO_BLOCKING=False  # Set True for active prevention
DEBUG_MODE=True

# Database (optional)
DATABASE_ENABLED=False
MONGO_URL=mongodb://localhost:27017
REDIS_URL=redis://localhost:6379

System Configuration (config/config.yaml)

network:
  interface: "Wi-Fi"
  promiscuous_mode: true
  packet_buffer_size: 10000

detection:
  ml_threshold: 0.7
  ml_based_enabled: true
  rule_based_enabled: true

nips:
  auto_block_enabled: false
  default_block_duration_minutes: 30
  permanent_block_threshold: 5
  threat_score_threshold: 100
  
threat_policies:
  Port Scan:
    action: block
    duration_minutes: 60
    threat_score: 50
  SYN Flood:
    action: block
    duration_minutes: 120
    threat_score: 100
  # ... more policies

πŸ“Š Dataset Information

The system was trained on the NF-UNSW-NB15-v3 dataset from The University of Queensland.

Dataset Details:

  • Source: UQ NIDS Datasets
  • Format: NetFlow v3 features
  • Samples: 175,341 flows
  • Classes: Binary (Benign/Attack)
  • Attack Types: 9 categories (Fuzzers, Analysis, Backdoors, DoS, Exploits, Generic, Reconnaissance, Shellcode, Worms)

Training Results:

  • Accuracy: 95.2%
  • Precision: 0.96 (Normal), 0.95 (Attack)
  • Recall: 0.94 (Normal), 0.96 (Attack)
  • ROC-AUC: 0.98

🎨 Dashboard Features

Dual-View Interface

The frontend provides two dashboards accessible via toggle:

1. NIDS Dashboard (Detection Only)

  • Real-time packet statistics
  • Threat detection alerts (no blocking)
  • Traffic timeline visualization
  • Threat type distribution
  • Alert severity breakdown

2. NIPS Dashboard (Detection + Prevention)

  • All NIDS features +
  • Active firewall blocks counter
  • Blocked IPs management
  • Whitelist/blacklist controls
  • Prevention action indicators
  • Auto-block toggle

Key Metrics Displayed

  • Total packets processed
  • Threats detected
  • Packets prevented (blocked)
  • Active firewall blocks
  • Detection rate
  • Prevention rate
  • System uptime

πŸ”§ API Reference

Core Endpoints

GET  /                           # System status
GET  /stats                      # System statistics
GET  /alerts                     # Recent alerts
GET  /threats/distribution       # Threat breakdown

# NIPS Endpoints
GET  /nips/status                # NIPS engine status
GET  /nips/blocked-ips           # List blocked IPs
POST /nips/whitelist             # Add to whitelist
POST /nips/blacklist             # Add to blacklist
POST /nips/block                 # Manual block
POST /nips/unblock               # Manual unblock
POST /nips/config/auto-block     # Toggle auto-blocking

Example Usage

# Get system stats
curl http://localhost:8000/stats

# Block an IP manually
curl -X POST http://localhost:8000/nips/block \
  -H "Content-Type: application/json" \
  -d '{"ip": "192.168.1.100", "action": "block"}'

# Add to whitelist
curl -X POST http://localhost:8000/nips/whitelist \
  -H "Content-Type: application/json" \
  -d '{"ip": "192.168.1.50", "action": "whitelist"}'

πŸ§ͺ Testing

Run Tests

cd src
pytest tests/ -v

# With coverage
pytest tests/ --cov=. --cov-report=html

Performance Benchmarks

python tests/benchmark_detection.py

Generate Test Report

python generate_report.py

πŸ“ˆ Monitoring with Grafana

Access Grafana

  1. Start services: docker-compose up -d
  2. Open: http://localhost:3000
  3. Login: admin / admin (change on first login)

Pre-configured Dashboards

  • NIDS/NIPS Overview: Real-time metrics, alerts, blocks
  • Network Traffic: Packet rates, protocols, bandwidth
  • Threat Analysis: Attack types, sources, trends
  • System Health: CPU, memory, latency

πŸ”’ Security Considerations

Whitelisting (CRITICAL)

Always whitelist trusted IPs before enabling auto-blocking:

# In src/main.py
nips.add_to_whitelist('192.168.1.14')    # Your PC
nips.add_to_whitelist('192.168.1.1')     # Your router
nips.add_to_whitelist('192.168.1.0/24')  # Your subnet
nips.add_to_whitelist('8.8.8.8')         # DNS servers

Safe Mode Testing

  1. Set ENABLE_AUTO_BLOCKING = False initially
  2. Test detection for 30+ minutes
  3. Verify no false positives
  4. Enable blocking only after thorough testing

Emergency Procedures

If system blocks critical IPs:

# Windows (as Admin)
netsh advfirewall firewall delete rule name="NIPS_Block_*"

# Linux
sudo iptables -F

# Or via API
curl -X POST http://localhost:8000/nips/config/auto-block?enabled=false

πŸ› Troubleshooting

Common Issues

1. "Permission denied" errors

Solution: Run as Administrator (Windows) or with sudo (Linux)

2. NIPS blocking internet

Solution:

# Check whitelist
curl http://localhost:8000/nips/blocked-ips

# Ensure your network is whitelisted:
# - Your PC IP
# - Router/gateway
# - DNS servers (8.8.8.8, 1.1.1.1)

3. Packet capture not working

Solution:

  • Windows: Install Npcap (check "WinPcap compatibility mode")
  • Linux: sudo apt-get install libpcap-dev
  • Verify interface name: ipconfig (Windows) or ifconfig (Linux)

4. Model not found

Solution:

cd src
python train_real.py  # Train new model

πŸ“š Documentation


πŸŽ“ Educational Use

This project demonstrates:

  • Machine Learning: Ensemble methods, anomaly detection, feature engineering
  • Network Security: Packet analysis, intrusion detection, threat prevention
  • Backend Development: REST APIs, async programming, background tasks
  • DevOps: Docker, containerization, monitoring, CI/CD
  • System Programming: Firewall integration, OS-level operations
  • Data Engineering: Real-time processing, time-series data

πŸ“ Citation

If you use this project in your research or publication, please cite:

@software{nids_nips_2024,
  author = {Mohamed Rayen Malouche},
  title = {Network Intrusion Detection and Prevention System},
  year = {2024},
  url = {https://github.com/yourusername/nids-system}
}

Dataset Citation:

@article{moustafa2015unsw,
  title={UNSW-NB15: a comprehensive data set for network intrusion detection systems},
  author={Moustafa, Nour and Slay, Jill},
  journal={2015 military communications and information systems conference (MilCIS)},
  pages={1--6},
  year={2015}
}

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see LICENSE file for details.


πŸ‘€ Author

Mohamed Rayen Malouche


πŸ™ Acknowledgments

  • UNSW-NB15 Dataset providers
  • Scapy development team
  • FastAPI and React communities
  • scikit-learn contributors

πŸ“Š Project Status

  • βœ… Detection (NIDS): Production-ready
  • βœ… Prevention (NIPS): Production-ready
  • βœ… ML Training: Completed (95%+ accuracy)
  • βœ… Docker Deployment: Tested
  • βœ… Documentation: Comprehensive
  • πŸ”„ Cloud Deployment: In progress
  • πŸ”„ Mobile App: Planned

⚠️ Disclaimer: This system is designed for educational and legitimate security monitoring purposes. Always ensure proper authorization before monitoring network traffic. Use responsibly and in compliance with local laws and regulations.

About

A production-grade cybersecurity system combining Machine Learning-based threat detection with active prevention through automated firewall management.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published