A production-grade hybrid Intrusion Detection System combining machine learning and stateful threat analytics for real-time network security monitoring.
- 🔴 Live Network Monitoring - Real-time packet capture and analysis with
pysharkandtshark - 📊 Offline File Analysis - Process
.pcap,.pcapng, and.csvnetwork traffic files - 🤖 ML-Based Classification - RandomForest model achieving 94% accuracy on CICIDS2017 dataset
- 🚨 Advanced Threat Detection - Stateful, threshold-based detection for:
- Port scans (SYN/FIN/NULL/XMAS patterns)
- SYN/UDP floods
- Brute-force attacks on service ports
- Data exfiltration anomalies
- DNS tunneling and tunnel frequency anomalies
- HTTP attack signatures (SQLi/XSS/directory traversal)
- Connection-rate and protocol anomalies
- 🌐 Interactive Web Dashboard - Real-time alerts and monitoring via Flask + Socket.IO
- 🤖 AI-Powered Explanations - Google Gemini integration for plain-language threat analysis
- 🔒 Security Hardened - Secure defaults for session cookies, CORS, and file uploads
- Python 3.11+ (tested with 3.13)
- Wireshark and
tsharkcommand-line tool - Windows users: Run terminal with administrator privileges for packet capture
# 1. Clone the repository
git clone https://github.com/adityashm/network-ids.git
cd network-ids
# 2. Create and activate virtual environment
python -m venv .venv
# On Windows:
.\.venv\Scripts\Activate.ps1
# On Linux/Mac:
source .venv/bin/activate
# 3. Install dependencies
pip install -r requirements.txt
# 4. Configure environment
cp .env.example .env
# Edit .env with your settings (SECRET_KEY, NETWORK_INTERFACE, GEMINI_API_KEY, etc.)
# 5. Run the application
python "ids project/src/app.py"
# 6. Open browser and navigate to:
# http://127.0.0.1:5000curl http://127.0.0.1:5000/health- File:
ids project/src/app.py - Responsibilities:
- Flask + Socket.IO server
- Health endpoint (
/health) - File analysis endpoint (
/analyze) - Live capture start/stop events
- Alert standardization/sanitization
- ML model loading and inference orchestration
- Optional Gemini explanation endpoint over Socket.IO
- File:
ids project/src/enhanced_detection.py - Implements stateful, threshold-based detection for:
- Port scans (SYN/FIN/NULL/XMAS patterns)
- SYN/UDP floods
- Brute-force behavior on common service ports
- Data exfiltration anomalies
- DNS tunneling frequency anomalies
- HTTP attack signatures (SQLi/XSS/traversal indicators)
- Connection-rate and protocol anomalies
- File:
ids project/src/extract_flows.py - Converts packets into flow-level features aligned with CIC-IDS2017-style columns (
FEATURE_COLUMNS).
- File:
ids project/src/src_train.py - Preprocessing:
- Column cleanup
- NaN/infinity normalization
- Label normalization (
BENIGN -> 0, non-benign ->1)
- Model:
RandomForestClassifier(class_weight='balanced')
- Output model:
ids project/models/ids_model.joblib
- File:
ids project/src/combine_csv.py - Combines multiple CIC CSV files into:
ids project/src/data/processed/MachineLearningCVE.csv
- File:
ids project/src/templates/index.html - Provides:
- Live monitoring controls
- Status and alert feed
- Offline upload analysis panel
- Modal for AI explanations
IDS/
README.md
requirements.txt
app.spec
.env.example
ids project/
models/
ids_model.joblib
data/
uploads/
src/
app.py # Canonical server entrypoint
appc.py # Legacy compatibility launcher
enhanced_detection.py
extract_flows.py
src_train.py
detect_live.py
combine_csv.py
templates/
index.html
data/
raw/MachineLearningCSV/
processed/MachineLearningCVE.csv
- Python 3.11+ (tested in this workspace with 3.13)
- Wireshark
tsharkinstalled and accessible - Windows users should run terminal/editor with privileges required for packet capture
- Create and activate virtual environment:
python -m venv .venv
.\.venv\Scripts\Activate.ps1- Install dependencies:
pip install -r requirements.txt- Configure environment variables:
Copy-Item .env.example .envThen edit .env and set at least:
SECRET_KEY(strong random value)NETWORK_INTERFACE(for exampleWi-Fion Windows)TSHARK_PATHif auto-detection failsGEMINI_API_KEYif AI explanations are needed
From workspace root:
python "ids project/src/app.py"Default URL:
http://127.0.0.1:5000
Health check:
http://127.0.0.1:5000/health
- Combine raw CSV files (optional if already combined):
python "ids project/src/combine_csv.py"- Train model:
python "ids project/src/src_train.py"- Run standalone live detector (optional):
python "ids project/src/detect_live.py"- Use web app upload flow (
/analyze) for offline file analysis.
- Do not commit
.envwith real secrets. - The app now rejects placeholder
SECRET_KEYvalues and generates an in-memory fallback key with a warning. - File uploads are extension allow-listed via
ALLOWED_UPLOAD_EXTENSIONS. - Session cookies are configured with secure defaults (
HttpOnly,SameSite=Lax,Securewhen not in debug). - CORS origins are configurable via
CORS_ALLOWED_ORIGINSand default to localhost only.
- PyInstaller spec:
app.spec - Includes model and templates in the bundle.
Model file not found:- Train first with
python "ids project/src/src_train.py"
- Train first with
tshark not found:- Install Wireshark and set
TSHARK_PATHin.env
- Install Wireshark and set
- Live capture fails:
- Verify
NETWORK_INTERFACEand terminal permissions
- Verify
- No Gemini explanations:
- Set
GEMINI_API_KEYin.env
- Set