Skip to content

Srejoye/KryptorisAI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ KryptorisAI

A real-time AI-powered intrusion detection and automated response system β€” combining XGBoost, LSTM and statistical anomaly detection to classify live HTTP traffic and neutralize threats instantly.

πŸ”— Built with Python Β· Flask Β· PyTorch Β· XGBoost Β· Vanilla JS


πŸ“Œ Overview

KryptorisAI is a real-time machine learning cybersecurity intelligence system that monitors incoming HTTP traffic, classifies each window of requests as Normal, Suspicious or Attack, and autonomously responds β€” rate-limiting flagged IPs, queueing medium-risk cases for admin review and hard-blocking confirmed threats. Every detection cycle runs in under a second.

Unlike rule-based firewalls, KryptorisAI learns from traffic patterns. Unlike batch ML pipelines, it acts immediately. The system trains its own models on startup, persists them to disk and begins live inference within seconds β€” all from a single Python process with no external infrastructure.


πŸ”’ Why a Simulation Environment?

Deploying an autonomous intrusion detection and response system on real-world infrastructure is not as simple as pointing it at a live server. It requires:

  • Backend-level access to web infrastructure and server logs
  • Visibility into live request streams, which are typically private and legally protected
  • Administrative authorization to apply automated mitigations such as rate-limiting or IP blocking
  • Security compliance, since an active mitigation system acting on production traffic carries operational risk if misconfigured

KryptorisAI is an architectural proof-of-concept. It demonstrates that the full detection and response pipeline β€” hybrid ML inference, adaptive fusion, SHAP explainability, tiered mitigation and human-in-the-loop review β€” can be built, evaluated and operated as a coherent system. To do this safely and reproducibly without requiring privileged access to real production systems, it includes a dedicated traffic simulation and attack orchestration environment that reproduces realistic HTTP behaviors: DDoS bursts, brute-force login attempts, traffic spikes and normal baseline traffic.


πŸ“Š Evaluation Metrics

Both models are trained and evaluated on a synthetic dataset of 6,000 labelled traffic windows, split 80/20 across Normal, Suspicious and Attack classes. The dataset is generated programmatically by evaluation/metrics.py to reproduce realistic statistical distributions for each traffic class.

Model Accuracy Precision Recall F1 ROC-AUC
XGBoost 0.92 0.95 0.94 0.93 0.94
LSTM 0.95 0.93 0.95 0.92 0.93

The LSTM demonstrates stronger temporal pattern recognition, while XGBoost provides high-confidence point-in-time classification β€” validating the hybrid fusion approach.


✨ Features

Feature Description
Hybrid ML Pipeline XGBoost (point-in-time) + LSTM (sequential memory) + Statistical detector running in parallel per window
Adaptive Fusion Engine Weighted score fusion with dynamic weight-flipping when models strongly disagree
Tiered Auto-Response Four escalation tiers: Monitor β†’ Rate Limit β†’ Admin Approval β†’ Auto-Block
Live SHAP Explainability Top contributing features computed and visualized on every single inference window
Human-in-the-Loop Medium-risk IPs queued for admin approve/reject via REST API before any block is applied
Dual-mode Ingestion Accepts real traffic via API; falls back to synthetic traffic generation when buffer is empty
TTL-based Blocklist Blocked IPs auto-expire after a configurable TTL (default: 30 minutes)
Control Panel Trigger attack scenarios (DDoS, Brute-force, Spike) live from the dashboard
Live Dashboard Real-time charts, SHAP bars, alert feed, blocked IPs β€” all polling at 1s intervals
Model Persistence Trained models saved to disk; reloaded instantly on subsequent server starts
Input Validation API enforces required fields per log entry; rejects malformed or unauthorized payloads

πŸš€ How to Run

No frontend build tools or package managers required. Pure Python backend + static frontend.

# Clone the repository
git clone https://github.com/Srejoye/KryptorisAI
cd KryptorisAI

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp backend/.env.example backend/.env   # then set your INGEST_API_KEY

# Start the server
python backend/main.py

Open http://localhost:5000 in your browser. On first launch, models train automatically (~10–20 seconds). On subsequent starts, saved models are loaded instantly from disk.


πŸ–₯️ Usage

Step 1 β€” Watch the Dashboard Initialize

On startup, a background thread begins generating synthetic traffic and running inference immediately. The dashboard at http://localhost:5000 goes live before any real data arrives β€” charts update, risk scores pulse, the status badge activates.

Step 2 β€” Launch the Control Panel

The Control Panel is a separate app that simulates real HTTP traffic and notifies KryptorisAI simultaneously. It exists precisely to exercise the full detection pipeline without requiring access to a real backend β€” sending actual HTTP requests to a local target server while signalling KryptorisAI to switch detection mode accordingly.

python backend/tools/control_panel.py

Open http://localhost:8080, select a scenario card and click β–Ά Start Simulation. The panel spawns worker threads sending real HTTP requests to your target (http://localhost:8000 by default) while calling /api/trigger-attack on KryptorisAI to switch detection mode.

⚠️ INGEST_API_KEY must be set in .env for the control panel to authenticate with KryptorisAI.

ℹ️ Port 8000 is the optional attack target. If nothing is running there, requests fail silently and KryptorisAI continues on synthetic data β€” the dashboard remains fully functional. To point the control panel at your own backend, set TARGET_URL in .env to your server's address.

Scenario Threads Behaviour
🟒 Normal 3 Randomized GET requests across endpoints, 0.5–2s apart
πŸ”΄ DDoS 20 Rapid-fire GETs with spoofed X-Forwarded-For IPs, 10ms apart
🟠 Brute Force 20 Repeated POST to /login with common passwords, 100ms apart
🟣 Spike 5 Quiet for 2–4s, then 80 requests fired in rapid succession

To trigger a scenario without the control panel:

curl -X POST http://localhost:5000/api/trigger-attack \
  -H "x-api-key: your-key" \
  -H "Content-Type: application/json" \
  -d '{"type": "ddos"}'

Step 3 β€” (Optional) Ingest Real Traffic

If you have access to a real backend and want to connect KryptorisAI to live infrastructure, you can push log entries directly to the ingest endpoint. This replaces synthetic generation with actual traffic data and is the intended path for production integration β€” subject to appropriate authorization on the target system.

curl -X POST http://localhost:5000/api/ingest \
  -H "x-api-key: your-key" \
  -H "Content-Type: application/json" \
  -d '[{"ip":"1.2.3.4","method":"POST","endpoint":"/api/login","status_code":401}]'

Required fields: ip, method, endpoint, status_code. Optional: bytes, response_ms, timestamp.

Step 4 β€” Observe Detection and Response

  • Risk Score and badge transition: βœ… Normal β†’ ⚠️ Suspicious β†’ 🚨 Attack
  • SHAP panel shows which features drove each prediction
  • Alert feed logs severity, reasons and top offending IPs
  • Blocked IPs table updates as the response engine acts

Step 5 β€” Manage Blocked IPs

curl -X POST http://localhost:5000/api/approve-block/1.2.3.4  # Approve pending block
curl -X POST http://localhost:5000/api/reject-block/1.2.3.4   # Reject pending block
curl -X POST http://localhost:5000/api/manual-block/9.9.9.9   # Force-block any IP
curl -X POST http://localhost:5000/api/unblock/1.2.3.4        # Remove all restrictions

πŸ—‚οΈ Project Structure

KryptorisAI/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ main.py                  # Flask app, API routes, background threads, state management
β”‚   β”œβ”€β”€ config.py                # All thresholds, window settings, feature names (env-configurable)
β”‚   β”œβ”€β”€ utils.py                 # Synthetic traffic generator, Dashboard collector
β”‚   β”œβ”€β”€ inference/
β”‚   β”‚   └── engine.py            # CyberDetectionPipeline β€” orchestrates all components
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ xgb_model.py         # XGBoost classifier with StandardScaler and SHAP explainer
β”‚   β”‚   β”œβ”€β”€ lstm_model.py        # 2-layer LSTM network (PyTorch) for sequential classification
β”‚   β”‚   β”œβ”€β”€ stats_model.py       # Statistical anomaly detector (Z-score, EMA, spike, fail rate)
β”‚   β”‚   β”œβ”€β”€ fusion.py            # Adaptive weighted fusion engine
β”‚   β”‚   └── saved/               # Persisted model weights (xgb.pkl, lstm.pt)
β”‚   β”œβ”€β”€ features/
β”‚   β”‚   └── extractor.py         # 12-feature extractor with EMA, rolling stats, real elapsed-time RPS
β”‚   β”œβ”€β”€ alert/
β”‚   β”‚   └── alert_system.py      # Rule + ML hybrid alerting with per-IP cooldown
β”‚   β”œβ”€β”€ mitigation/
β”‚   β”‚   └── response.py          # Tiered response engine with human-in-the-loop support
β”‚   β”œβ”€β”€ evaluation/
β”‚   β”‚   └── metrics.py           # Synthetic data generation and model training/evaluation
β”‚   └── tools/
β”‚       └── control_panel.py     # Dashboard control panel logic
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ index.html               # Dashboard shell β€” layout, live stat panels, charts, alert feed
β”‚   β”œβ”€β”€ script.js                # Polling logic, Chart.js wrappers, SHAP renderer, DOM updates
β”‚   └── style.css                # Design system β€” dark theme, animations, responsive layout
└── requirements.txt

🎨 Tech Stack

Technology Role
Python + Flask Backend API server, background inference threads, state management
XGBoost Point-in-time binary classification on the 12-feature vector
PyTorch (LSTM) Sequential classification over a rolling 10-window feature buffer
SHAP Per-inference explainability via TreeExplainer on every XGBoost prediction
scikit-learn StandardScaler for feature normalization, evaluation metrics
NumPy Feature computation, rolling statistics, sequence buffering
HTML5 + CSS3 Dashboard layout, dark-mode design system, keyframe animations
Vanilla JS + Chart.js Real-time polling, live chart updates, SHAP bar rendering

πŸ” Detection Pipeline

Incoming Logs (real or synthetic)
        β”‚
        β–Ό
  Feature Extractor  β†’  12 signals: RPS, spike, fail rate, IP concentration,
        β”‚                 auth failures, EMA, rolling mean/std, endpoint ratio...
        β”‚
   β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
   β–Ό         β–Ό
XGBoost    LSTM          +   Statistical Anomaly Detector (Z-score, EMA dev, spike)
(current   (last 10
 window)    windows)
   β”‚         β”‚                      β”‚
   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜                      β”‚
        β–Ό                           β–Ό
   Fusion Engine  ←─────── anomaly_score boost (+0.15 if score > 0.7)
  (adaptive weights: 60/40 default β†’ 30/70 on model disagreement)
        β”‚
        β–Ό
   Risk Score + Classification (Normal / Suspicious / Attack)
        β”‚
   β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β–Ό                             β–Ό
Alert System              Response Engine
(rule + ML hybrid,       (Monitor / Rate-Limit /
 per-IP cooldown)         Pending Approval / Auto-Block)
   β”‚                             β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β–Ό
     Flask API + Live Dashboard

πŸ“Š Response Tiers

Risk Score Action Details
β‰₯ 0.95 Auto-Block Top offending IPs added to blocklist instantly; expires after TTL
0.85 – 0.95 Pending Admin Top 2 IPs queued for human approve/reject before any block
0.60 – 0.85 Rate Limit Top 3 IPs throttled at 100 req/min
< 0.60 Monitor IPs logged and tracked; no restriction applied

All thresholds are configurable via environment variables: RISK_AUTOBLOCK, RISK_APPROVAL, RISK_RATELIMIT.


🌐 API Reference

Endpoint Method Description
/api/state GET Current window: risk score, class, features, SHAP values, alert
/api/data GET Time-series history β€” last 60 windows of RPS and risk
/api/alerts GET Last 20 alerts with severity, reasons, top IPs, feature snapshot
/api/blocked-ips GET All blocked and rate-limited IPs with metadata
/api/blocklist GET TTL-managed blocklist (stale entries auto-cleaned)
/api/pending-approvals GET IPs awaiting admin decision
/api/approve-block/<ip> POST Admin approves β†’ IP moved to blocked
/api/reject-block/<ip> POST Admin rejects β†’ IP returned to monitoring
/api/unblock/<ip> POST Remove IP from all restriction lists
/api/manual-block/<ip> POST Force-block any IP immediately
/api/ingest POST Submit real log entries (API key required)
/api/trigger-attack POST Switch active traffic scenario (API key required)
/api/status GET Server readiness and ingest buffer depth

πŸ“š Concepts Covered

  • Ensemble ML Inference β€” combining gradient-boosted trees and recurrent networks for complementary signal coverage
  • Adaptive Model Fusion β€” dynamic weight assignment based on inter-model disagreement
  • Statistical Anomaly Detection β€” Z-score, EMA deviation, spike detection and error rate thresholding in parallel with ML
  • SHAP Explainability β€” model-agnostic feature attribution surfaced live per prediction, not just offline
  • Human-in-the-Loop Security β€” automated escalation with mandatory admin override at medium-risk tier
  • Window-accurate RPS β€” elapsed time computed from real log timestamps rather than fixed window constants
  • Sequence Modeling for Security β€” LSTM over rolling feature buffers to detect gradual attack escalation

⭐ Support

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

About

Enterprise-style Intrusion Detection & Response System (IDRS) featuring ML-driven threat detection, attack simulation and automated response workflows.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors