# πŸ›‘οΈ IDS - Intrusion Detection System Sistema di rilevamento intrusioni moderno per router MikroTik, basato su Machine Learning. ## 🎯 Caratteristiche Principali - **ML Efficiente**: Solo 25 feature mirate (non 150+) per analisi veloce e accurata - **Detection Real-time**: Rilevamento anomalie in <2 secondi - **Multi-Router**: Gestione parallela di 10+ router MikroTik tramite API REST - **Auto-Block**: Blocco automatico IP anomali con timeout configurabile - **Dashboard Web**: Monitoring real-time completo - **PostgreSQL**: Database performante per analisi time-series ## πŸ—οΈ Architettura ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Router MikroTik β”‚ ──(Syslog)──▢ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ (10+ router) β”‚ β”‚ PostgreSQL β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Database β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β” β”‚ Python ML β”‚ β”‚ FastAPI β”‚ β”‚ React β”‚ β”‚ Analyzer β”‚ β”‚ Backend β”‚ β”‚ Dashboard β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ MikroTik Manager β”‚ β”‚ (API REST) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Router 1 β”‚ β”‚ Router 2 β”‚ β”‚ Router N β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## πŸš€ Quick Start ### 1. Setup Backend Python ```bash cd python_ml pip install -r requirements.txt python main.py ``` Il backend FastAPI partirΓ  su `http://0.0.0.0:8000` ### 2. Setup Frontend (giΓ  configurato) Il frontend React Γ¨ giΓ  in esecuzione tramite il workflow "Start application". Accedi alla dashboard web all'URL del tuo Repl. ### 3. Configurazione Router MikroTik Sul router MikroTik, abilita l'API REST: ``` /ip service set api-ssl disabled=no set www-ssl disabled=no ``` Poi aggiungi i router tramite la dashboard web oppure: ```sql INSERT INTO routers (name, ip_address, username, password, api_port, enabled) VALUES ('Router 1', '192.168.1.1', 'admin', 'password', 443, true); ``` ## πŸ“Š Come Funziona ### 1. Raccolta Dati I log arrivano tramite Syslog dai router MikroTik e vengono salvati in PostgreSQL nella tabella `network_logs`. ### 2. Training ML ```bash curl -X POST http://localhost:8000/train \ -H "Content-Type: application/json" \ -d '{ "max_records": 10000, "hours_back": 24, "contamination": 0.01 }' ``` Il sistema estrae **25 feature mirate**: - **Volume**: bytes/sec, packets, connessioni - **Temporali**: burst, intervalli, pattern orari - **Protocolli**: diversitΓ , entropia, TCP/UDP ratio - **Port Scanning**: porte uniche, sequenziali - **Comportamentali**: varianza dimensioni, azioni bloccate ### 3. Detection Real-time ```bash curl -X POST http://localhost:8000/detect \ -H "Content-Type: application/json" \ -d '{ "max_records": 5000, "hours_back": 1, "risk_threshold": 60.0, "auto_block": true }' ``` Il modello Isolation Forest assegna: - **Risk Score** (0-100): livello di pericolositΓ  - **Confidence** (0-100): certezza del rilevamento - **Anomaly Type**: ddos, port_scan, brute_force, botnet, suspicious ### 4. Auto-Block IP con risk_score >= 80 (CRITICO) vengono bloccati automaticamente su tutti i router via API REST con timeout 1h. ## 🎚️ Livelli di Rischio | Score | Livello | Azione | |-------|---------|--------| | 85-100 | πŸ”΄ CRITICO | Blocco immediato | | 70-84 | 🟠 ALTO | Blocco + monitoring | | 60-69 | 🟑 MEDIO | Monitoring | | 40-59 | πŸ”΅ BASSO | Logging | | 0-39 | 🟒 NORMALE | Nessuna azione | ## πŸ“š API Endpoints - `GET /health` - Health check - `POST /train` - Training modello ML - `POST /detect` - Detection anomalie - `POST /block-ip` - Blocco manuale IP - `POST /unblock-ip` - Sblocco IP - `GET /stats` - Statistiche sistema Documentazione completa: `http://localhost:8000/docs` ## πŸ”§ Configurazione Automatica ### Training Automatico (ogni 12h) ```bash 0 */12 * * * curl -X POST http://localhost:8000/train ``` ### Detection Continua (ogni 5 minuti) ```bash */5 * * * * curl -X POST http://localhost:8000/detect \ -H "Content-Type: application/json" \ -d '{"auto_block": true, "risk_threshold": 75}' ``` ## πŸ†š Vantaggi vs Sistema Precedente | Aspetto | Sistema Vecchio | Nuovo IDS | |---------|----------------|-----------| | Feature ML | 150+ | 25 (mirate) | | VelocitΓ  Training | ~5 min | ~10 sec | | VelocitΓ  Detection | Lento | <2 sec | | Comunicazione Router | SSH (lento) | API REST (veloce) | | Falsi Negativi | Alti | Bassi | | Multi-Router | Sequenziale | Parallelo | | Database | MySQL | PostgreSQL | ## πŸ” Troubleshooting ### Troppi Falsi Positivi? Aumenta `risk_threshold` (es. da 60 a 75) ### Non Rileva Attacchi? - Diminuisci `contamination` nel training (es. da 0.01 a 0.02) - Abbassa `risk_threshold` (es. da 75 a 60) ### Connessione Router Fallita? - Verifica API REST abilitata: `/ip service print` - Controlla firewall: porta 443 deve essere aperta - Test: `curl -u admin:password https://ROUTER_IP/rest/system/identity` ## πŸ“ Struttura Progetto ``` . β”œβ”€β”€ python_ml/ # Backend Python ML β”‚ β”œβ”€β”€ ml_analyzer.py # Analisi ML (25 feature) β”‚ β”œβ”€β”€ mikrotik_manager.py # Gestione router API REST β”‚ β”œβ”€β”€ main.py # FastAPI backend β”‚ └── requirements.txt # Dipendenze Python β”œβ”€β”€ client/ # Frontend React β”‚ └── src/ β”‚ └── pages/ # Pagine dashboard β”œβ”€β”€ server/ # Backend Node.js β”‚ β”œβ”€β”€ db.ts # Database PostgreSQL β”‚ β”œβ”€β”€ routes.ts # API routes β”‚ └── storage.ts # Storage interface └── shared/ └── schema.ts # Schema database Drizzle ORM ``` ## πŸ” Sicurezza - Password router NON in chiaro nel codice - Timeout automatico sui blocchi (default 1h) - Whitelist per IP fidati - Logging completo di tutte le azioni - Database PostgreSQL con connessione sicura ## πŸ“ Note Importanti - **Whitelist**: IP in `whitelist` non vengono mai bloccati - **Timeout**: Blocchi hanno timeout (default 1h), poi scadono automaticamente - **Parallelo**: Sistema blocca su tutti i router simultaneamente (veloce) - **Performance**: Analizza 10K log in <2 secondi ## πŸ“– Documentazione - [Python ML Backend](./python_ml/README.md) - Dettagli implementazione ML - [API Docs](http://localhost:8000/docs) - Documentazione FastAPI automatica ## 🀝 Supporto Per problemi o domande: 1. Controlla questa documentazione 2. Verifica i log di debug (`python_ml/main.py`) 3. Testa la connessione database e router 4. Verifica i modelli addestrati (`python_ml/models/`) --- **IDS - Intrusion Detection System v1.0.0** Sistema moderno e performante per proteggere la tua rete MikroTik