๐ก๏ธ Nginx WAF AI - Production Ready โ
A production-ready AI-powered Web Application Firewall (WAF) system using machine learning for real-time threat detection. All monitoring and visualization is handled through Grafana dashboards.
๐๏ธ Architecture โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Nginx Node 1 โ โ Nginx Node 2 โ โ Traffic Gen โ
โ Port 8081 โ โ Port 8082 โ โ (Attacks) โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโ โ
โ Log Server 1 โ โ โ Log Server 2 โ โ
โ Port 8080 โ โ โ Port 8083 โ โ
โโโโโโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โโโโโโโโโโโโโผโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโ โ
โ WAF AI API โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Port 8000 โ
โโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Redis โ โ Prometheus โ โ Loki โ
โ Port 6379 โ โ Port 9090 โ โ Port 3100 โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ โ
โโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโ
โ Grafana โ
โ Port 3080 โ
โโโโโโโโโโโโโโโโโโโ๐ Quick Start โ
Prerequisites โ
- Docker and Docker Compose
- 8GB+ RAM recommended
- Ports 3080, 8000, 8081, 8082, 9090, 6379 available
1. Clone and Start โ
bash
git clone <repository-url>
cd nginx-waf-ai
docker-compose up -d2. Access Grafana Dashboard โ
- URL: http://localhost:3080
- Username: admin
- Password: waf-admin
3. Initialize the System โ
bash
# Create admin user
curl -X POST http://localhost:8000/auth/users \
-H "Content-Type: application/json" \
-d '{
"username": "admin",
"email": "admin@example.com",
"password": "admin123",
"role": "admin"
}'
# Login to get token
curl -X POST http://localhost:8000/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "admin",
"password": "admin123"
}'
# Add nginx nodes (use your token)
curl -X POST http://localhost:8000/api/nodes/add \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"node_id": "nginx-node-1",
"hostname": "nginx-node-1",
"ssh_host": "nginx-node-1",
"ssh_port": 22,
"ssh_username": "root",
"nginx_config_path": "/etc/nginx/conf.d"
}'
# Start traffic collection
curl -X POST http://localhost:8000/api/traffic/start-collection \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '["http://log-server-1:8080", "http://log-server-2:8080"]'
# Train ML model (generates synthetic data if no real traffic)
curl -X POST http://localhost:8000/api/training/start \
-H "Authorization: Bearer YOUR_TOKEN"
# Start real-time processing
curl -X POST http://localhost:8000/api/processing/start \
-H "Authorization: Bearer YOUR_TOKEN"๐ Monitoring & Dashboards โ
Grafana Dashboards โ
The system includes pre-configured Grafana dashboards:
Unified WAF Monitoring - Main dashboard with:
- Threat detection metrics
- Traffic analysis
- System health
- Rule deployment status
Infrastructure Status - System components:
- Container health
- Resource usage
- Network performance
Prometheus Metrics โ
Available at http://localhost:9090/graph
Key metrics:
waf_threats_detected_total- Threats by typewaf_requests_total- Total requests by node/statuswaf_traffic_volume_total- Traffic volumewaf_rules_active- Active WAF ruleswaf_nodes_registered- Registered nginx nodes
Log Aggregation โ
Loki collects logs from all nginx nodes at http://localhost:3100
๐ฏ Core Features โ
Real-Time Threat Detection โ
- SQL Injection detection
- XSS Attack identification
- Directory Traversal prevention
- Brute Force protection
- Anomaly Detection using ML
Machine Learning Engine โ
- Supervised Learning with threat classification
- Unsupervised Learning for anomaly detection
- Real-time Processing of HTTP requests
- Adaptive Learning from traffic patterns
- Synthetic Training Data generation
WAF Rule Management โ
- Dynamic Rule Generation from ML insights
- Automatic Deployment to nginx nodes
- Rule Optimization and conflict resolution
- Live Configuration Updates
Security & Authentication โ
- JWT-based Authentication
- Role-based Access Control (Admin/Operator/Viewer)
- Rate Limiting protection
- Security Headers middleware
- IP Blocking and whitelisting
๐ง Configuration โ
Environment Variables โ
Key configuration in docker-compose.yml:
yaml
waf-api:
environment:
- REDIS_URL=redis://redis:6379
- LOG_LEVEL=INFO
- NGINX_NODES=http://log-server-1:8080,http://log-server-2:8080
- WAF_CORS_ORIGINS=http://localhost:3080,http://127.0.0.1:3080Traffic Generator Settings โ
Control attack simulation:
yaml
traffic-generator:
environment:
- ATTACK_PROBABILITY=0.1 # 10% malicious traffic
- REQUEST_RATE=5 # requests per second๐ ๏ธ API Endpoints โ
Authentication โ
POST /auth/login- User loginPOST /auth/users- Create user (admin)GET /auth/users- List users (admin)
System Management โ
GET /health- System health checkGET /metrics- Prometheus metricsGET /api/stats- System statisticsGET /api/debug/status- Debug information (admin)
ML Training & Processing โ
POST /api/training/start- Train ML models (operator)POST /api/processing/start- Start real-time processing (operator)GET /api/threats- Get detected threats (viewer)
Traffic & Rules โ
POST /api/traffic/start-collection- Start traffic collection (operator)GET /api/traffic/stats- Traffic statistics (viewer)GET /api/rules- Get active WAF rules (viewer)POST /api/rules/deploy- Deploy rules to nodes (operator)
Node Management โ
POST /api/nodes/add- Add nginx node (admin)GET /api/nodes- List nodes (viewer)
๐งช Testing Attack Scenarios โ
The traffic generator simulates realistic attacks:
bash
# Test SQL injection detection
curl "http://localhost:8081/api/users?id=1' OR 1=1--"
# Test XSS detection
curl "http://localhost:8081/search?q=<script>alert('xss')</script>"
# Test directory traversal
curl "http://localhost:8081/api/file?path=../../../etc/passwd"
# Normal traffic
curl "http://localhost:8081/"
curl "http://localhost:8081/api/products"๐ Production Deployment โ
Security Hardening โ
- Change default passwords in docker-compose.yml
- Use HTTPS with proper certificates
- Configure firewall rules
- Set up log rotation
- Enable backup strategies
Scaling Considerations โ
- Horizontal scaling: Add more nginx nodes
- Database scaling: Use external Redis cluster
- Load balancing: Add load balancer for WAF API
- Monitoring scaling: Use Prometheus federation
Performance Tuning โ
- Adjust traffic collection frequency
- Optimize ML model parameters
- Configure proper resource limits
- Use SSD storage for logs
๐ Troubleshooting โ
Common Issues โ
ML Training Fails
bash# Check if traffic collection is active curl -H "Authorization: Bearer YOUR_TOKEN" http://localhost:8000/api/traffic/stats # If no traffic, training will use synthetic dataNo Threats Detected
- Ensure traffic generator is running
- Check ML model is trained
- Verify real-time processing is active
Grafana Dashboard Empty
- Wait 30-60 seconds for metrics to populate
- Check Prometheus targets: http://localhost:9090/targets
- Verify WAF API is exposing metrics: http://localhost:8000/metrics
Authentication Issues
- Create user first with
/auth/users - Use JWT token in Authorization header
- Check token hasn't expired
- Create user first with
๐ Development โ
Adding New Threat Detection โ
- Update
src/training_data_generator.pywith new patterns - Modify
src/traffic_collector.pypattern detection - Add new threat types to ML engine
- Update Grafana dashboards for new metrics
Custom Rules โ
- Extend
src/waf_rule_generator.py - Add rule templates in nginx configuration
- Update deployment logic in
src/nginx_manager.py
๐ค Contributing โ
- Fork the repository
- Create feature branch:
git checkout -b feature/new-feature - Commit changes:
git commit -am 'Add new feature' - Push to branch:
git push origin feature/new-feature - Submit Pull Request
๐ License โ
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments โ
- Built with FastAPI, scikit-learn, and modern security practices
- Inspired by modern WAF solutions and ML-driven security
- Grafana dashboards for comprehensive monitoring
- Docker containerization for easy deployment