Skip to content

Real-time IDS using Raspberry Pi + Machine Learning trained on CICIDS2017 dataset. Live packet capture, flow extraction, REST API alerts, and dashboard.

License

Notifications You must be signed in to change notification settings

Aash55/RaspberryPi-IDS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ Raspberry Pi Real-Time Machine Learning Intrusion Detection System (IDS)

A full end-to-end IDS project that runs on a Raspberry Pi using live packet capture, flow-based feature extraction, a trained machine-learning model, and a Flask web dashboard for real-time alert visualization.

This project is designed for:

βœ”οΈ Cybersecurity research βœ”οΈ College assignments βœ”οΈ Placement portfolios βœ”οΈ Real-world networking demos

🌟 Key Features

πŸ”Ή Real-time packet capture using tcpdump πŸ”Ή Flow extraction using Scapy πŸ”Ή Machine Learning trained on CICIDS2017 dataset πŸ”Ή One-click Flask web dashboard πŸ”Ή Alerts stored in SQLite πŸ”Ή Optional auto-blocking IPs via iptables πŸ”Ή Lightweight, edge-friendly design

πŸ›οΈ System Architecture

Raspberry Pi (Edge Device β†’ AI IDS)

Raw Packets β†’ Flow Features β†’ ML Model β†’ Suspicious? β†’ Send JSON Alert

Laptop / Server

Receive Alerts β†’ SQLite Database β†’ Live Dashboard UI

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Raspberry Pi IDS β”‚ HTTP POST JSON β”‚ Flask Server β”‚ β”‚ ─ tcpdump β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚ /alert API endpoint β”‚ β”‚ ─ scapy flow features β”‚ β”‚ SQLite storage β”‚ β”‚ ─ ML inference β”‚ β”‚ /dashboard live UI β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‚ Project Structure RaspberryPi-IDS/ β”‚ β”œβ”€ src/ β”‚ β”œβ”€ app.py # Flask backend + REST β”‚ β”œβ”€ pi_ids_agent.py # Raspberry Pi IDS agent β”‚ └─ train_model_cicids.py# Model training script β”‚ β”œβ”€ models/ β”‚ └─ ids_rf.joblib # Trained ML model β”‚ β”œβ”€ dataset/ β”‚ └─ sample_flows.csv (Large dataset not included) β”‚ β”œβ”€ scripts/ β”‚ └─ block_ip.sh # Optional firewall blocking β”‚ β”œβ”€ docs/ # Screenshots + diagrams β”‚ β”œβ”€ dashboard.png β”‚ β”œβ”€ architecture.png β”‚ └─ pi_terminal.png β”‚ β”œβ”€ requirements.txt └─ README.md

πŸš€ Installation & Setup 1️⃣ Clone the repository git clone https://github.com/Aash55/RaspberryPi-IDS.git cd RaspberryPi-IDS

2️⃣ Create virtual environment (Laptop / Server) Windows python -m venv venv venv\Scripts\activate

Linux/Mac python3 -m venv venv source venv/bin/activate

3️⃣ Install dependencies pip install -r requirements.txt

🌐 Run the Flask Alert Server (Laptop) python src/app.py

You should see:

Running on http://127.0.0.1:5000

Now open:

πŸ”— Dashboard β†’ http://127.0.0.1:5000/dashboard

πŸ”— Alerts JSON β†’ http://127.0.0.1:5000/alerts

🐍 Run IDS Agent on Raspberry Pi 1️⃣ Copy ML model scp models/ids_rf.joblib pi@:/home/pi/

2️⃣ Copy IDS agent scp src/pi_ids_agent.py pi@:/home/pi/

3️⃣ Run IDS sudo python3 pi_ids_agent.py

It will:

βœ”οΈ Capture packets βœ”οΈ Extract flows βœ”οΈ Run ML inference βœ”οΈ POST alerts

πŸ“¦ Alert JSON Format

Example alert sent from Raspberry Pi:

{ "ts": "2025-12-02 15:03:21", "src": "192.168.46.12", "dst": "8.8.8.8", "sport": 52311, "dport": 443, "proto": 6, "predicted_class": "suspicious", "packet_count": 99, "total_bytes": 12345 }

🧠 Machine Learning Model Dataset

πŸ“‘ CICIDS2017 β€” Cleaned & Preprocessed Version

Contains:

Normal traffic

DoS / DDoS

Port scan

Botnet traffic

Brute force

Web attacks

Selected Features

Light, edge-friendly features used both in training + runtime:

Flow Duration Total Fwd Packets Total Length of Fwd Packets Packet Length Mean Flow Bytes/s Flow Packets/s

These work well for anomaly detection on small hardware.

Algorithm

βœ”οΈ RandomForestClassifier

200 trees

max depth 15

class_weight="balanced"

low inference cost

robust to noise

Training Script

Located at:

src/train_model_cicids.py

Outputs:

models/ids_rf.joblib

πŸ”₯ Optional: Auto IP Blocking

Only in controlled networks ⚠️

sudo iptables -I INPUT -s -j DROP

Script:

scripts/block_ip.sh

It protects from:

βœ”οΈ Port Scans βœ”οΈ DoS / Flooding βœ”οΈ Suspicious high-volume flows

πŸ”’ Cybersecurity Ethics

🚨 Do not use on networks you do not own 🚨 Do not inspect personal user traffic 🚨 Never deploy auto-block on public networks 🚨 Use only for research, education, demo

This project is educational, not a commercial IPS/IDS.

🧭 Why This Is Placement-Ready

βœ” IoT Edge πŸ›°οΈ βœ” Cybersecurity πŸ” βœ” Machine Learning πŸ€– βœ” Networking (TCP/IP) 🌐 βœ” Web development (Flask + JS UI) πŸ–₯️ βœ” Real-time data pipeline ⚑ βœ” SQLite data persistence πŸ—‚οΈ

This shows you can build complete systems, not just simple scripts.

πŸš€ Future Enhancements

πŸ”Ή Train with more classes β†’ multi-class IDS πŸ”Ή Add Suricata/Snort rule engine πŸ”Ή Grafana / Kibana dashboards πŸ”Ή TensorFlow Lite edge inference πŸ”Ή JWT authentication for dashboard πŸ”Ή TimescaleDB for time-series alerts

πŸ“œ License

MIT License β€” Free for research and education.

⭐ Support & Contributions

Enjoy this project? πŸ”Ή Star ⭐ the repository πŸ”Ή Open issues πŸ”Ή Suggest improvements πŸ”Ή Fork and submit PRs

πŸ™ Credits

Developed by ASH55(M.Tech Student) with learning support from Angela Yu, CIC Lab, and helpful tools like Scapy, Flask & RandomForest ML.

πŸ’¬ Contact

If you need help:

Open an issue

DM on GitHub

Ask via discussions

Security + AI + IoT = Your Superpower πŸš€ Build systems, not just code.

⭐ If this helped, please drop a star 🌟

About

Real-time IDS using Raspberry Pi + Machine Learning trained on CICIDS2017 dataset. Live packet capture, flow extraction, REST API alerts, and dashboard.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published