Skip to content

Latest commit

Β 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ RT-GIDS: Real-Time GPU-Accelerated Intrusion Detection System

Python Next.js FastAPI XGBoost TypeScript License

A production-ready, real-time network intrusion detection system powered by XGBoost machine learning, featuring a modern Next.js dashboard and comprehensive threat analytics.

Features β€’ Quick Start β€’ Architecture β€’ Documentation β€’ Contributing


πŸ“‹ Table of Contents


🎯 Overview

RT-GIDS (Real-Time GPU-Accelerated Intrusion Detection System) is an advanced network security monitoring platform that combines machine learning-based threat detection with real-time visualization and analytics. The system processes network traffic in real-time, identifies malicious patterns using XGBoost classifiers, and provides comprehensive threat intelligence through an intuitive web dashboard.

Key Highlights

  • Real-Time Detection: Sub-second threat identification with WebSocket streaming
  • Dual ML Models: Full-feature and lightweight realtime models for different use cases
  • Modern Dashboard: Next.js 14 with Three.js 3D visualizations and real-time charts
  • Production Ready: CPU-only mode with in-memory state management (no Docker/GPU required)
  • Comprehensive Analytics: Threat trends, attack frequency, top attackers, and incident overview
  • AI-Powered Narratives: GPT-4o-mini integration for intelligent threat summaries
  • SHAP Explainability: Model interpretability for security analysts

✨ Features

πŸ” Threat Detection

  • Real-time packet analysis with XGBoost-based classification
  • Multi-attack type detection: Port scans, brute force, SQL injection, DDoS, malware C2 traffic
  • Confidence scoring with severity classification (Low/Medium/High/Critical)
  • Automated threat blocking and IP tracking
  • Anomaly detection with statistical analysis

πŸ“Š Analytics & Visualization

  • Live threat radar with 3D globe visualization
  • Attack frequency charts with time-series trends
  • Top attackers geolocation mapping
  • Incident overview with critical event tracking
  • System diagnostics with health monitoring
  • Model insights with SHAP feature importance

🎨 Modern UI/UX

  • Dark/Light theme support
  • Responsive design with mobile compatibility
  • Real-time WebSocket updates
  • Audio notifications for critical threats
  • Smooth animations with Framer Motion
  • Accessibility features (reduced motion, low performance mode)

πŸ”§ Developer Experience

  • Local simulation mode (no external dependencies)
  • Hot reload for both frontend and backend
  • TypeScript for type safety
  • Comprehensive API with OpenAPI documentation
  • Attack seeder for testing and demos

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         RT-GIDS Architecture                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Frontend   │◄───────►│   Backend    │◄───────►│  ML Models   β”‚
β”‚  (Next.js)   β”‚  HTTP   β”‚  (FastAPI)   β”‚  Load   β”‚  (XGBoost)   β”‚
β”‚              β”‚  + WS   β”‚              β”‚         β”‚              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚                         β”‚                         β”‚
      β”‚                         β”‚                         β”‚
      β–Ό                         β–Ό                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Dashboard  β”‚         β”‚  Telemetry   β”‚         β”‚  Threat      β”‚
β”‚  Components  β”‚         β”‚   Service    β”‚         β”‚  Metrics     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚                         β”‚                         β”‚
      β”‚                         β”‚                         β”‚
      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  FakeRedis       β”‚
                    β”‚  (In-Memory)     β”‚
                    β”‚  or Real Redis   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Component Flow

  1. Data Ingestion: Network events posted to /ingest endpoint
  2. ML Inference: XGBoost models classify threats in real-time
  3. Telemetry Processing: Events aggregated and normalized
  4. WebSocket Streaming: Real-time updates pushed to frontend
  5. Dashboard Rendering: React components visualize threats
  6. Analytics: Historical trends and patterns analyzed

πŸ› οΈ Tech Stack

Backend

  • FastAPI 0.109+ - Modern async Python web framework
  • XGBoost 2.1.0 - Gradient boosting for threat classification
  • Pandas 2.1+ - Data manipulation and preprocessing
  • NumPy 1.26+ - Numerical computations
  • SHAP 0.41+ - Model explainability
  • OpenAI 1.12+ - AI-powered threat narratives
  • Uvicorn - ASGI server with WebSocket support
  • Prometheus Client - Metrics collection

Frontend

  • Next.js 14.2 - React framework with App Router
  • TypeScript 5.0 - Type-safe development
  • React 19 - UI library
  • Three.js 0.169 - 3D visualizations (Threat Globe)
  • Recharts 2.15 - Data visualization
  • Framer Motion 11 - Animations
  • Tailwind CSS 3.4 - Utility-first styling
  • Radix UI - Accessible component primitives
  • Axios 1.7 - HTTP client
  • Zustand/SWR - State management and data fetching

ML & Data Science

  • XGBoost - Primary ML framework
  • scikit-learn - Model evaluation and preprocessing
  • SHAP - Feature importance and explainability
  • Matplotlib/Seaborn - Visualization

Development Tools

  • ESLint - Code linting
  • Playwright - E2E testing
  • Lighthouse - Performance auditing
  • Stylelint - CSS linting

πŸ“¦ Prerequisites

  • Python 3.10 or higher
  • Node.js 18+ and npm or pnpm
  • Git for cloning the repository
  • Optional: OPENAI_API_KEY for AI narrative summaries

System Requirements

  • OS: Windows 10+, Linux, or macOS
  • RAM: 4GB minimum (8GB recommended)
  • Storage: 2GB free space
  • Network: Local network access for testing

πŸš€ Installation

1. Clone the Repository

git clone https://github.com/yourusername/rt-gids.git
cd rt-gids

2. Backend Setup

# Navigate to backend directory
cd backend

# Create virtual environment
python -m venv .venv

# Activate virtual environment
# Windows:
.venv\Scripts\activate
# Linux/macOS:
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

3. Frontend Setup

# Navigate to frontend directory
cd frontend

# Install dependencies
npm install
# or
pnpm install

4. ML Models (Optional)

If you want to train your own models:

# Navigate to ML training directory
cd RealTime_IDS/scripts

# Preprocess CICIDS2017 dataset
python preprocess_gpu.py

# Train models
python train_gpu.py

Pre-trained models are included in RealTime_IDS/models/.


⚑ Quick Start

Option 1: Unified Launcher (Recommended)

# From project root
python run.py

This starts both backend (port 8000) and frontend (port 3000) automatically.

Option 2: Manual Start

Start Backend

cd backend
.venv\Scripts\activate  # Windows
# or
source .venv/bin/activate  # Linux/macOS

set SIM_MODE=true  # Windows
# or
export SIM_MODE=true  # Linux/macOS

uvicorn backend.app.main:app --reload --port 8000

Start Frontend

cd frontend
npm run dev
# or
pnpm dev

Seed Attack Data (Optional)

# From project root
python attack_seeder.py --count 200

Access the Dashboard


πŸ“ Project Structure

rt-gids/
β”œβ”€β”€ backend/                    # FastAPI backend
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ main.py            # Application entry point
β”‚   β”‚   β”œβ”€β”€ routers/           # API route handlers
β”‚   β”‚   β”‚   β”œβ”€β”€ analytics.py   # Analytics endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ logs.py         # Log streaming
β”‚   β”‚   β”‚   β”œβ”€β”€ metrics.py      # System metrics
β”‚   β”‚   β”‚   β”œβ”€β”€ threats.py      # Threat detection
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”œβ”€β”€ services/          # Business logic
β”‚   β”‚   β”‚   β”œβ”€β”€ ml_service.py   # ML inference
β”‚   β”‚   β”‚   β”œβ”€β”€ telemetry_service.py  # Telemetry
β”‚   β”‚   β”‚   β”œβ”€β”€ model_service.py      # Model management
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”œβ”€β”€ utils/             # Utilities
β”‚   β”‚   β”‚   β”œβ”€β”€ local_runtime.py  # FakeRedis, SIM_MODE
β”‚   β”‚   β”‚   └── model_loader.py   # Model loading
β”‚   β”‚   └── schemas/           # Pydantic models
β”‚   β”œβ”€β”€ requirements.txt       # Python dependencies
β”‚   └── models/                # ML model storage
β”‚
β”œβ”€β”€ frontend/                   # Next.js frontend
β”‚   β”œβ”€β”€ app/                   # Next.js App Router
β”‚   β”‚   β”œβ”€β”€ page.tsx           # Home page
β”‚   β”‚   β”œβ”€β”€ command-center/    # Main dashboard
β”‚   β”‚   β”œβ”€β”€ threat-analytics/  # Threat analytics page
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ components/             # React components
β”‚   β”‚   β”œβ”€β”€ command-center/    # Dashboard components
β”‚   β”‚   β”œβ”€β”€ threat/            # Threat visualization
β”‚   β”‚   β”œβ”€β”€ system/            # System diagnostics
β”‚   β”‚   └── ui/                # UI primitives
β”‚   β”œβ”€β”€ hooks/                 # React hooks
β”‚   β”‚   β”œβ”€β”€ useThreatTelemetry.ts  # Telemetry hook
β”‚   β”‚   β”œβ”€β”€ useThreatAnalytics.ts  # Analytics hook
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ lib/                   # Utilities
β”‚   β”‚   └── api.ts             # API client
β”‚   └── package.json           # Node dependencies
β”‚
β”œβ”€β”€ RealTime_IDS/              # ML training pipeline
β”‚   β”œβ”€β”€ scripts/
β”‚   β”‚   β”œβ”€β”€ preprocess_gpu.py  # Data preprocessing
β”‚   β”‚   └── train_gpu.py       # Model training
β”‚   β”œβ”€β”€ models/                # Trained models
β”‚   β”‚   β”œβ”€β”€ xgb_gpu_ids.json   # Full model
β”‚   β”‚   └── xgb_realtime_ids.json  # Realtime model
β”‚   └── data/                  # Processed datasets
β”‚
β”œβ”€β”€ attack_seeder.py           # Synthetic attack generator
β”œβ”€β”€ run.py                     # Unified launcher
└── README.md                  # This file

πŸ€– Machine Learning Models

Model Architecture

RT-GIDS uses XGBoost Gradient Boosting classifiers for threat detection:

1. Full IDS Model (xgb_gpu_ids.json)

  • Purpose: Comprehensive threat detection with all features
  • Features: All CICIDS2017 dataset features
  • Accuracy: ~99.92%
  • Training Time: ~25 seconds
  • Use Case: Deep analysis, high accuracy requirements

2. Realtime Model (xgb_realtime_ids.json)

  • Purpose: Fast real-time packet classification
  • Features: 5 lightweight features
    • packet_length
    • protocol
    • src_port
    • dst_port
    • ip_version
  • Accuracy: ~87.76%
  • Training Time: ~6 seconds
  • Use Case: Live network monitoring, low latency

Training Pipeline

Step 1: Data Preprocessing

cd RealTime_IDS/scripts
python preprocess_gpu.py

Process:

  1. Load CICIDS2017 CSV files
  2. Clean and encode categorical features
  3. Handle missing values and outliers
  4. Create binary labels (Benign=0, Attack=1)
  5. Output: features.csv and labels.csv

Step 2: Model Training

python train_gpu.py

Process:

  1. Load preprocessed data
  2. Train/test split (80/20, stratified)
  3. Train full model with all features
  4. Extract realtime features (5 features)
  5. Train lightweight realtime model
  6. Evaluate and save models
  7. Generate metrics and visualizations

Output Files:

  • xgb_gpu_ids.json - Full model
  • xgb_realtime_ids.json - Realtime model
  • metrics.json - Training metrics
  • confusion_matrix_full.png - Confusion matrix
  • feature_importance_full.csv - Feature rankings

Model Hyperparameters

Full Model:

n_estimators=200
max_depth=8
learning_rate=0.1
subsample=0.8
colsample_bytree=0.8
tree_method="hist"  # CPU mode

Realtime Model:

n_estimators=150
max_depth=6
learning_rate=0.1
tree_method="hist"

Model Deployment

Models are automatically loaded by:

  • ModelService: Loads xgb_realtime_ids.json for real-time inference
  • MLService: Loads realtime model (preferred) or full model (fallback)

Models support:

  • Batch inference for performance
  • SHAP explainability for feature importance
  • Confidence scoring for threat severity

πŸ“‘ API Documentation

Base URL

http://localhost:8000

Key Endpoints

Threat Detection

  • POST /ingest - Ingest network events
  • GET /api/threats/stats - Get threat statistics
  • GET /api/threats/recent?n=100 - Get recent threats
  • WebSocket /ws/logs?token=api::dev-token-abc - Real-time log stream

Analytics

  • GET /api/analytics/incidents-overview - Incident summary
  • GET /api/analytics/top-attackers?limit=5 - Top attacker IPs
  • GET /api/analytics/trends - Attack trends (15 minutes)
  • GET /api/analytics/attack-frequency?window=5m - Attack frequency

Metrics

  • GET /api/metrics/system - System metrics
  • GET /api/metrics/threat - Threat metrics
  • GET /api/metrics/anomalies - Anomaly detection

Model Insights

  • GET /api/model/info - Model information
  • POST /api/model/reload - Reload ML model
  • GET /api/shap/explain - SHAP feature importance

Health & Diagnostics

  • GET /api/health - Health check
  • GET /api/health/scan - System diagnostics
  • WebSocket /ws/telemetry - Real-time telemetry stream

Interactive API Docs

Visit http://localhost:8000/docs for Swagger UI with:

  • Endpoint documentation
  • Request/response schemas
  • Try-it-out functionality

🎨 Frontend Dashboard

Pages

1. Command Center (/command-center)

  • Incident Overview: Total attacks, notifications, critical events
  • Top Attackers: IP addresses with attack counts and geolocation
  • Attack Frequency Chart: Time-series visualization
  • AI Core Status: Model information and reload capability
  • System Summary: Real-time metrics bar

2. Threat Analytics (/threat-analytics)

  • Threat Radar: 3D radar visualization with attack points
  • Threat Globe: Interactive 3D globe with attacker locations
  • Threat Table: Detailed event log with filtering
  • Timeline: Attack timeline visualization

3. System Diagnostics (/system-diagnostics)

  • System Health: CPU, memory, disk usage
  • Network Stats: Packet counts, throughput
  • Model Status: ML model information
  • Service Status: Backend service health

4. Model Insights (/model-insights)

  • Feature Importance: SHAP-based feature rankings
  • Model Metrics: Accuracy, precision, recall
  • Prediction Examples: Sample predictions with explanations

Key Features

  • Real-time Updates: WebSocket streaming for live data
  • Responsive Design: Mobile and desktop support
  • Dark/Light Theme: System preference detection
  • Audio Notifications: Sound alerts for critical threats
  • Performance Controls: Reduced motion, low performance mode
  • Accessibility: WCAG-compliant components

βš™οΈ Configuration

Environment Variables

Backend

# Simulation mode (default: true)
SIM_MODE=true

# Model directory
RTGIDS_MODELS_DIR=D:/CN/RealTime_IDS/models

# OpenAI API (optional)
OPENAI_API_KEY=sk-...
OPENAI_MODEL=gpt-4o-mini

# Batch processing
RTGIDS_BATCH_MAX=32
RTGIDS_BATCH_TIMEOUT=0.05

Frontend

# Backend URL (optional, defaults to http://127.0.0.1:8000)
NEXT_PUBLIC_BACKEND_URL=http://127.0.0.1:8000

SIM_MODE Explained

When SIM_MODE=true (default):

  • Uses FakeRedis (in-memory state)
  • No external Redis required
  • CPU-only ML inference
  • Tokenless WebSocket connections
  • Perfect for local development

When SIM_MODE=false:

  • Requires real Redis instance
  • GPU inference if available
  • Token-based WebSocket auth
  • Production-ready mode

πŸ’‘ Usage Examples

Ingest Network Event

curl -X POST http://localhost:8000/ingest \
  -H "Content-Type: application/json" \
  -d '{
    "timestamp": "2024-01-15T10:30:00Z",
    "src_ip": "192.168.1.100",
    "dst_ip": "192.168.1.5",
    "dst_port": 80,
    "proto": "TCP",
    "bytes_in": 1024,
    "bytes_out": 2048,
    "is_attack": true,
    "label": "Port Scan"
  }'

Get Threat Statistics

curl http://localhost:8000/api/threats/stats

Get Top Attackers

curl http://localhost:8000/api/analytics/top-attackers?limit=10

WebSocket Connection (JavaScript)

const ws = new WebSocket('ws://127.0.0.1:8000/ws/logs?token=api::dev-token-abc');

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log('Threat detected:', data);
};

Python Client Example

import requests

# Ingest event
response = requests.post(
    'http://localhost:8000/ingest',
    json={
        'src_ip': '10.0.0.1',
        'dst_ip': '192.168.1.1',
        'dst_port': 443,
        'proto': 'TCP',
        'is_attack': True
    }
)

# Get analytics
stats = requests.get('http://localhost:8000/api/analytics/incidents-overview').json()
print(f"Total attacks: {stats['total_attacks']}")

πŸ”§ Development

Running in Development Mode

# Backend with hot reload
cd backend
uvicorn backend.app.main:app --reload --port 8000

# Frontend with hot reload
cd frontend
npm run dev

Running Tests

# Backend tests
cd backend
pytest

# Frontend tests
cd frontend
npm run test

# E2E tests
npm run verify:tier0

Code Quality

# Lint frontend
cd frontend
npm run lint

# Format Python (using black)
cd backend
black app/

# Type check
cd frontend
npm run type-check

Building for Production

# Build frontend
cd frontend
npm run build

# Start production server
npm run start

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes and add tests
  4. Commit with clear messages: git commit -m 'Add amazing feature'
  5. Push to your branch: git push origin feature/amazing-feature
  6. Open a Pull Request

Contribution Guidelines

  • Follow existing code style and conventions
  • Add tests for new features
  • Update documentation as needed
  • Ensure all tests pass
  • Write clear commit messages

Areas for Contribution

  • πŸ› Bug fixes
  • ✨ New features
  • πŸ“š Documentation improvements
  • 🎨 UI/UX enhancements
  • πŸš€ Performance optimizations
  • πŸ§ͺ Test coverage
  • 🌐 Internationalization

πŸ“„ License

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


πŸ™ Acknowledgments

  • CICIDS2017 Dataset - For training data
  • XGBoost - Machine learning framework
  • FastAPI - Modern Python web framework
  • Next.js - React framework
  • Three.js - 3D graphics library
  • SHAP - Model explainability

πŸ“ž Support


Made with ❀️ by the RT-GIDS Team

⭐ Star this repo if you find it helpful!

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages