A real-time network traffic analysis tool that captures packets, visualizes network activity, and detects security threats using machine learning.
This project combines traditional packet capture with machine learning to provide:
- Live network traffic monitoring
- Protocol analysis and visualization
- Automated threat detection (DDoS, Brute Force, Port Scans, etc.)
- Interactive web dashboard
- Real-time packet capture using Scapy
- Protocol parsing: TCP, UDP, HTTP, DNS
- Bandwidth monitoring and throughput calculation
- Packet size distribution analysis
- Machine Learning Model: Random Forest classifier trained on CICIDS2017 dataset
- Rule-Based Detection: Real-time pattern matching for common attacks
- Supported Threats:
- DDoS (Distributed Denial of Service)
- DoS (Denial of Service)
- Port Scanning
- Brute Force (SSH, FTP, Telnet, RDP)
- Web Attacks
- Botnet Activity
- Real-time visualization with Chart.js
- Protocol distribution charts
- Bandwidth graphs
- Threat alerts and anomaly logs
- Export functionality (CSV, JSON, PCAP)
- Python 3.9+
- Root/Administrator privileges (required for packet capture)
- ~500MB disk space for ML model and dataset
git clone <repository-url>
cd "CN Traffic Analyzer"python3 -m venv venv
source venv/bin/activate # Linux/Mac
# or
venv\Scripts\activate # Windowspip install -r requirements.txt# Quick training (5-10 minutes)
python -m ml.training_pipeline --quick
# Full training (30+ minutes, better accuracy)
python -m ml.training_pipeline# Linux/Mac (requires sudo for packet capture)
sudo venv/bin/python app.py
# Windows (run as Administrator)
python app.pyOpen your browser and navigate to:
http://localhost:8080
- Select a network interface (or leave blank for auto-detect)
- Optionally enter a BPF filter (e.g.,
tcp port 80) - Click "Start Capture"
- Monitor real-time traffic and threat detections
Run the attack simulator to test detection capabilities:
# Target your server IP
python generate_attacks.py <server-ip>
# Examples:
python generate_attacks.py 192.168.1.100
python generate_attacks.py 127.0.0.1 http # HTTP flood only
python generate_attacks.py 127.0.0.1 ssh # SSH brute force onlyCN Traffic Analyzer/
├── app.py # Flask web server
├── packet_capture.py # Scapy-based packet capture
├── packet_analyzer.py # Protocol parsing
├── anomaly_detector.py # Rule-based detection
├── generate_attacks.py # Attack traffic generator for testing
│
├── ml/ # Machine Learning Module
│ ├── training_pipeline.py # Model training script
│ ├── predict.py # Prediction and rule-based detection
│ ├── preprocessing.py # Data preprocessing
│ ├── flask_integration.py # Flask API integration
│ ├── model.pkl # Trained model (generated)
│ └── preprocessor.pkl # Data preprocessor (generated)
│
├── MachineLearningCVE/ # CICIDS2017 Dataset (CSV files)
│
├── static/
│ ├── css/style.css # Dashboard styles
│ └── js/dashboard.js # Frontend JavaScript
│
├── templates/
│ └── index.html # Dashboard template
│
└── requirements.txt # Python dependencies
| Endpoint | Method | Description |
|---|---|---|
/ |
GET | Web dashboard |
/api/capture/start |
POST | Start packet capture |
/api/capture/stop |
POST | Stop packet capture |
/api/stats |
GET | Get traffic statistics |
/api/packets/recent |
GET | Get recent packets |
/api/anomalies |
GET | Get detected threats |
/api/ml/status |
GET | ML model status |
/api/stream |
GET | SSE stream for real-time updates |
/api/export/csv |
GET | Export as CSV |
/api/export/json |
GET | Export as JSON |
/api/export/pcap |
GET | Export as PCAP |
- Scapy sniffs packets on the selected network interface
- Each packet is parsed to extract protocol, IPs, ports, flags
- Flow statistics are computed (packet rate, byte rate, duration)
- Rule-Based Layer: Immediate detection based on port and traffic patterns
- ML Layer: Random Forest model classifies traffic based on 37 features
- Detected threats are logged and displayed on the dashboard
- Algorithm: Random Forest Classifier
- Training Data: CICIDS2017 dataset (~2.8 million samples)
- Features: 37 network flow features
- Classes: Normal, DDoS, DoS, PortScan, BruteForce, WebAttack, Botnet
- Accuracy: ~99.7% on test set
Packet capture requires root privileges:
sudo venv/bin/python app.pyUse the virtual environment's Python:
sudo venv/bin/python app.py
# or
sudo -E python app.py- Ensure correct network interface is selected
- Check if traffic exists:
tcpdump -i <interface> - Verify no firewall is blocking
Train the model first:
python -m ml.training_pipeline --quick- Backend: Python, Flask, Scapy
- ML: scikit-learn, pandas, numpy
- Frontend: HTML, CSS, JavaScript, Chart.js, Bootstrap
- Data: CICIDS2017 Intrusion Detection Dataset
- Abhay Pratap Rana
- Saloni Sharma
- Vaishnav Verma
BTech Computer Science and AI, Rishihood University
This project is for educational purposes.