Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 1c71ce6e-1a3e-4f53-bb5d-77cdd22b8ea3
334 lines
13 KiB
Markdown
334 lines
13 KiB
Markdown
# 🛡️ GUIDA WHITELIST GLOBALE - SISTEMA DDOS PROTECTION
|
|
|
|
## 📋 **PANORAMICA**
|
|
|
|
La **whitelistGlobale** risolve il problema dei **falsi positivi** nel sistema DDoS detection:
|
|
- **Problema**: IP legittimo finisce in `ddos_detect_v03` e viene bloccato
|
|
- **Soluzione**: Aggiungere IP a `whitelistGlobale` → vale per **TUTTI i router**
|
|
- **Automatico**: Sistema rimuove conflitti e sincronizza whitelist
|
|
- **🆕 SICUREZZA**: Anche la whitelist ha timeout di **60 minuti** sui router per maggiore sicurezza
|
|
|
|
## 🔐 **SISTEMA TIMEOUT UNIFICATO**
|
|
|
|
**Tutti gli IP sui router hanno ora timeout di 60 minuti:**
|
|
- **`ddos_detect_v03`**: Timeout 60min (IP malevoli si auto-rimuovono)
|
|
- **`whitelist`**: Timeout 60min (IP fidati si auto-rimuovono, ma vengono ri-sincronizzati)
|
|
- **`ddos2-attackers`**: Nessun timeout (permanenti fino a pulizia database)
|
|
- **`ddos3-attackers`**: Nessun timeout (permanenti fino a pulizia database)
|
|
|
|
### 🔄 **Vantaggi Timeout Whitelist**
|
|
1. **Sicurezza**: IP compromesso in whitelist si rimuove automaticamente dopo 1h
|
|
2. **Ri-sincronizzazione**: Sistema ri-aggiunge IP whitelist ogni 30 minuti se necessario
|
|
3. **Pulizia automatica**: Router non si riempiono di IP whitelist obsoleti
|
|
4. **Audit trail**: Tracciabilità di quando IP vengono ri-applicati
|
|
|
|
## 🚀 **INSTALLAZIONE**
|
|
|
|
### 1. **Creazione Tabella e Stored Procedures**
|
|
```bash
|
|
# Installazione completa
|
|
mysql -h SERVER_IP -u USERNAME -p DATABASE_NAME < create_whitelist_globale.sql
|
|
```
|
|
|
|
### 2. **Aggiornamento Sistema MikroTik**
|
|
```bash
|
|
# Il file mikrotikcontoll.py è già aggiornato con timeout whitelist
|
|
python3 mikrotikcontoll.py # Ora gestisce timeout 60min per TUTTO
|
|
```
|
|
|
|
## 📊 **STRUTTURA TABELLA**
|
|
|
|
```sql
|
|
CREATE TABLE whitelistGlobale (
|
|
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
ip_address VARCHAR(45) UNIQUE NOT NULL,
|
|
comment TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
created_by VARCHAR(100) DEFAULT 'System',
|
|
active TINYINT(1) DEFAULT 1, -- 1=attivo, 0=disabilitato
|
|
reason VARCHAR(255), -- Motivo whitelist
|
|
last_sync TIMESTAMP NULL -- Ultimo sync con router
|
|
);
|
|
```
|
|
|
|
## 🎯 **CASI D'USO PRATICI**
|
|
|
|
### **🚨 Caso 1: Falso Positivo DDoS**
|
|
```
|
|
Situazione: IP 203.45.67.89 è un server partner ma finisce in ddos_detect_v03
|
|
Soluzione: Aggiungerlo alla whitelist globale
|
|
Risultato: IP in whitelist con timeout 60min, ri-sincronizzato ogni 30min
|
|
```
|
|
|
|
```sql
|
|
-- Aggiunta rapida
|
|
CALL add_global_whitelist(
|
|
'203.45.67.89',
|
|
'Server partner per API integrazione',
|
|
'Falso positivo DDoS detection',
|
|
'Admin'
|
|
);
|
|
```
|
|
|
|
### **🏢 Caso 2: IP Aziendali**
|
|
```sql
|
|
-- IP interni che non devono mai essere bloccati
|
|
-- NOTA: Timeout 60min ma ri-sincronizzati automaticamente ogni 30min
|
|
CALL add_global_whitelist('192.168.1.1', 'Gateway aziendale', 'Infrastruttura critica', 'System');
|
|
CALL add_global_whitelist('10.0.0.50', 'Server database principale', 'Server aziendale', 'Admin');
|
|
CALL add_global_whitelist('172.16.0.100', 'Workstation amministratore', 'Accesso privilegiato', 'Admin');
|
|
```
|
|
|
|
### **🌐 Caso 3: IP Fornitori Esterni**
|
|
```sql
|
|
-- Fornitori di servizi che accedono frequentemente
|
|
-- Sistema mantiene IP sempre attivi tramite ri-sincronizzazione
|
|
CALL add_global_whitelist('8.8.8.8', 'DNS Google', 'Servizio essenziale', 'Network Team');
|
|
CALL add_global_whitelist('1.1.1.1', 'DNS Cloudflare', 'Servizio essenziale', 'Network Team');
|
|
```
|
|
|
|
## 🔄 **WORKFLOW AUTOMATICO AGGIORNATO**
|
|
|
|
```mermaid
|
|
graph TD
|
|
A[IP rilevato DDoS] --> B{IP in whitelistGlobale?}
|
|
B -->|SÌ| C[❌ NON bloccare IP]
|
|
B -->|NO| D[✅ Bloccare IP con timeout 60min]
|
|
C --> E[Aggiungere IP a whitelist tutti router con timeout 60min]
|
|
D --> F[Aggiungere IP a ddos_detect_v03 con timeout 60min]
|
|
|
|
G[Admin aggiunge IP a whitelistGlobale] --> H[Sistema rimuove IP da ddos_detect_v03]
|
|
H --> I[Sistema aggiunge IP a whitelist tutti router con timeout 60min]
|
|
|
|
J[Timeout scaduto su router] --> K[Sistema ri-sincronizza whitelist]
|
|
K --> L[IP ri-aggiunto con nuovo timeout 60min]
|
|
```
|
|
|
|
## 📋 **COMANDI GESTIONE**
|
|
|
|
### **✅ Aggiungere IP**
|
|
```sql
|
|
-- Comando completo
|
|
CALL add_global_whitelist(
|
|
'203.45.67.89', -- IP da whitelistare
|
|
'Server partner per e-commerce', -- Descrizione
|
|
'Falso positivo sistema DDoS', -- Motivo
|
|
'Admin' -- Chi lo ha aggiunto
|
|
);
|
|
|
|
-- Comando semplificato
|
|
INSERT INTO whitelistGlobale (ip_address, comment, reason)
|
|
VALUES ('203.45.67.89', 'Server partner', 'Falso positivo');
|
|
```
|
|
|
|
### **⚠️ Disabilitare IP (Sicuro)**
|
|
```sql
|
|
-- Disabilita senza cancellare (raccomandato)
|
|
CALL remove_global_whitelist('203.45.67.89', TRUE);
|
|
|
|
-- Oppure manualmente
|
|
UPDATE whitelistGlobale SET active = 0 WHERE ip_address = '203.45.67.89';
|
|
```
|
|
|
|
### **❌ Rimuovere IP (Permanente)**
|
|
```sql
|
|
-- Cancellazione completa (attenzione!)
|
|
CALL remove_global_whitelist('203.45.67.89', FALSE);
|
|
```
|
|
|
|
### **📊 Monitoring e Statistiche**
|
|
```sql
|
|
-- Statistiche generali
|
|
SELECT * FROM v_whitelist_stats;
|
|
|
|
-- Tutti gli IP attivi
|
|
SELECT ip_address, comment, reason, created_at, last_sync
|
|
FROM whitelistGlobale
|
|
WHERE active = 1
|
|
ORDER BY created_at DESC;
|
|
|
|
-- IP mai sincronizzati
|
|
SELECT ip_address, comment, created_at
|
|
FROM whitelistGlobale
|
|
WHERE active = 1 AND last_sync IS NULL;
|
|
|
|
-- IP con sincronizzazione recente (< 1 ora)
|
|
SELECT ip_address, comment, last_sync,
|
|
TIMESTAMPDIFF(MINUTE, last_sync, NOW()) as minutes_ago
|
|
FROM whitelistGlobale
|
|
WHERE active = 1 AND last_sync > DATE_SUB(NOW(), INTERVAL 1 HOUR)
|
|
ORDER BY last_sync DESC;
|
|
|
|
-- IP aggiunti oggi
|
|
SELECT ip_address, comment, reason, created_by
|
|
FROM whitelistGlobale
|
|
WHERE DATE(created_at) = CURDATE()
|
|
ORDER BY created_at DESC;
|
|
```
|
|
|
|
## 🎛️ **INTEGRAZIONE SISTEMA AUTOMATICO**
|
|
|
|
### **Crontab Aggiornato**
|
|
```bash
|
|
# Sincronizzazione ogni 30 minuti (ora include whitelist con timeout)
|
|
*/30 * * * * cd /path/to/project && python3 mikrotikcontoll.py >> router_sync.log 2>&1
|
|
```
|
|
|
|
### **Output di Sistema Aggiornato**
|
|
```
|
|
🚀 === AVVIO SISTEMA CONTROLLO ROUTER MIKROTIK ===
|
|
🛡️ === FASE 0: CONTROLLO WHITELIST GLOBALE ===
|
|
⚠️ Trovati 2 IP in conflitto da risolvere
|
|
✅ Rimosso 203.45.67.89 da ddos_detect_v03 (motivo: Server partner fidato)
|
|
✅ Rimosso 192.168.100.50 da ddos_detect_v03 (motivo: Falso positivo)
|
|
🎯 Risolti 2 conflitti whitelist vs blacklist
|
|
|
|
🔄 === FASE 3: SINCRONIZZAZIONE ROUTER ===
|
|
🌐 Sincronizzazione whitelist globale per router 192.168.1.1...
|
|
📋 Trovati 15 IP nella whitelist globale
|
|
⏰ Applicando timeout 60min per 203.45.67.89 in whitelist (sicurezza)
|
|
✅ IP 203.45.67.89 aggiunto alla whitelist router 192.168.1.1 con timeout 60min
|
|
🌐 Whitelist globale router 192.168.1.1: 3 IP aggiunti, 3 timeout applicati
|
|
|
|
📊 Recupero IP ddos_detect_v03 (esclusi whitelist globale)...
|
|
📋 Trovati 127 IP ddos_detect_v03 da sincronizzare (dopo filtro whitelist)
|
|
⏰ Applicando timeout 60min per 45.67.89.12 in ddos_detect_v03
|
|
🛡️ IP saltati per whitelist: 5
|
|
|
|
🛡️ Elaborazione 8 IP per whitelist router 192.168.1.1 (CON TIMEOUT)...
|
|
⏰ Applicando timeout 60min per 10.0.0.50 in whitelist (sicurezza)
|
|
✅ IP 10.0.0.50 aggiunto alla whitelist router 192.168.1.1 con timeout 60min
|
|
|
|
📊 === STATISTICHE FINALI ===
|
|
✅ Router sincronizzati con successo: 3
|
|
⏰ IP ddos_detect_v03 con timeout 60min attivi: 145
|
|
🛡️ IP whitelist con timeout 60min applicati: 23
|
|
⏰ NUOVO: Timeout 60min applicato anche alla whitelist per maggiore sicurezza
|
|
```
|
|
|
|
## ⚡ **SCENARI OPERATIVI**
|
|
|
|
### **🚨 Emergenza: IP Partner Bloccato**
|
|
```sql
|
|
-- Soluzione IMMEDIATA (30 secondi)
|
|
CALL add_global_whitelist('203.45.67.89', 'Partner bloccato - URGENTE', 'Falso positivo critico', 'Emergency');
|
|
|
|
-- Il sistema automaticamente:
|
|
-- 1. Rimuove IP da ddos_detect_v03 (database)
|
|
-- 2. Al prossimo sync (max 30min) rimuove dai router
|
|
-- 3. Aggiunge IP a whitelist tutti i router CON TIMEOUT 60min
|
|
-- 4. Ri-sincronizza IP ogni 30min per mantenerlo attivo
|
|
```
|
|
|
|
### **🔐 Scenario Sicurezza: IP Compromesso in Whitelist**
|
|
```
|
|
Situazione: IP in whitelist viene compromesso
|
|
Vantaggio timeout: IP si rimuove automaticamente dopo 60min
|
|
Azione: Disabilitare IP dalla whitelistGlobale per impedire ri-sincronizzazione
|
|
```
|
|
|
|
```sql
|
|
-- Disabilita IP compromesso
|
|
CALL remove_global_whitelist('203.45.67.89', TRUE);
|
|
-- IP si rimuoverà automaticamente dai router al timeout (max 60min)
|
|
-- Non verrà più ri-sincronizzato
|
|
```
|
|
|
|
### **🔍 Investigazione Falsi Positivi**
|
|
```sql
|
|
-- Trova IP che potrebbero essere falsi positivi (alto traffico ma non malevoli)
|
|
SELECT ip_address, COUNT(*) as detections, MIN(retrieved_at) as first_seen, MAX(retrieved_at) as last_seen
|
|
FROM ip_list
|
|
WHERE list_name = 'ddos_detect_v03'
|
|
GROUP BY ip_address
|
|
HAVING detections > 10
|
|
ORDER BY detections DESC;
|
|
|
|
-- Controlla se sono già in whitelist
|
|
SELECT i.ip_address, COUNT(*) as detections, w.ip_address as whitelisted,
|
|
w.last_sync, TIMESTAMPDIFF(MINUTE, w.last_sync, NOW()) as sync_minutes_ago
|
|
FROM ip_list i
|
|
LEFT JOIN whitelistGlobale w ON i.ip_address = w.ip_address AND w.active = 1
|
|
WHERE i.list_name = 'ddos_detect_v03'
|
|
AND w.ip_address IS NULL -- Non ancora in whitelist
|
|
GROUP BY i.ip_address
|
|
HAVING detections > 5
|
|
ORDER BY detections DESC;
|
|
```
|
|
|
|
### **📈 Monitoraggio Efficacia**
|
|
```sql
|
|
-- IP bloccati vs whitelistati oggi con info timeout
|
|
SELECT
|
|
(SELECT COUNT(*) FROM ip_list WHERE list_name = 'ddos_detect_v03' AND DATE(retrieved_at) = CURDATE()) as ip_bloccati,
|
|
(SELECT COUNT(*) FROM whitelistGlobale WHERE active = 1 AND DATE(created_at) = CURDATE()) as ip_whitelistati_oggi,
|
|
(SELECT COUNT(*) FROM whitelistGlobale WHERE active = 1) as whitelist_totale,
|
|
(SELECT COUNT(*) FROM whitelistGlobale WHERE active = 1 AND last_sync > DATE_SUB(NOW(), INTERVAL 2 HOUR)) as sync_recenti;
|
|
|
|
-- Verifica frequenza ri-sincronizzazione
|
|
SELECT ip_address, comment,
|
|
COUNT(*) as sync_count,
|
|
MIN(last_sync) as first_sync,
|
|
MAX(last_sync) as last_sync,
|
|
TIMESTAMPDIFF(HOUR, MIN(last_sync), MAX(last_sync)) as hours_active
|
|
FROM whitelistGlobale
|
|
WHERE active = 1 AND last_sync IS NOT NULL
|
|
GROUP BY ip_address, comment
|
|
ORDER BY sync_count DESC;
|
|
```
|
|
|
|
## 🛡️ **SICUREZZA E BEST PRACTICES**
|
|
|
|
### **✅ Raccomandazioni**
|
|
1. **Sempre usare `reason`**: Documentare perché un IP è whitelistato
|
|
2. **Preferire disabilitazione**: Usare `active = 0` invece di DELETE
|
|
3. **Monitoring regolare**: Controllare `last_sync` per verificare sincronizzazioni
|
|
4. **Backup prima modifiche**: Fare backup prima di modifiche massive
|
|
5. **Log delle operazioni**: Tenere traccia di chi aggiunge/rimuove IP
|
|
6. **🆕 Monitoring timeout**: Verificare che IP critici vengano ri-sincronizzati regolarmente
|
|
|
|
### **⚠️ Attenzioni**
|
|
1. **Non whitelistare IP sospetti**: Verificare sempre prima di aggiungere
|
|
2. **Limitare accessi**: Solo admin autorizzati possono modificare whitelist globale
|
|
3. **Revisione periodica**: Controllare periodicamente se IP whitelistati sono ancora necessari
|
|
4. **Test in staging**: Testare sempre in ambiente di test prima della produzione
|
|
5. **🆕 IP compromessi**: Disabilitare immediatamente IP compromessi dalla whitelist
|
|
6. **🆕 Sincronizzazione critica**: Per IP critici, verificare sincronizzazione ogni 30min
|
|
|
|
### **🕐 Gestione Timeout**
|
|
```sql
|
|
-- Verifica IP che dovrebbero essere sincronizzati ma non lo sono
|
|
SELECT ip_address, comment, last_sync,
|
|
TIMESTAMPDIFF(MINUTE, last_sync, NOW()) as minutes_since_sync
|
|
FROM whitelistGlobale
|
|
WHERE active = 1
|
|
AND (last_sync IS NULL OR last_sync < DATE_SUB(NOW(), INTERVAL 2 HOUR))
|
|
ORDER BY last_sync ASC;
|
|
|
|
-- Forzare ri-sincronizzazione (reset last_sync)
|
|
UPDATE whitelistGlobale SET last_sync = NULL WHERE ip_address = '203.45.67.89';
|
|
```
|
|
|
|
## 🎯 **RISULTATI ATTESI**
|
|
|
|
- **❌ Zero falsi positivi**: IP legittimi non vengono più bloccati
|
|
- **🚀 Gestione centralizzata**: Un solo punto per whitelistare su tutti i router
|
|
- **⚡ Risoluzione automatica**: Conflitti risolti automaticamente dal sistema
|
|
- **📊 Tracciabilità completa**: Log di tutte le operazioni e motivazioni
|
|
- **🔄 Sincronizzazione automatica**: Whitelist aggiornata su tutti i router ogni 30 minuti
|
|
- **🔐 Sicurezza migliorata**: Timeout 60min previene IP compromessi permanenti in whitelist
|
|
- **🛡️ Auto-healing**: Sistema mantiene IP legittimi sempre attivi tramite ri-sincronizzazione
|
|
|
|
### 🎨 **Differenze di Comportamento**
|
|
|
|
| Lista | Timeout Router | Ri-sincronizzazione | Pulizia Database |
|
|
|-------|---------------|-------------------|------------------|
|
|
| **ddos_detect_v03** | ✅ 60min | ❌ No (si rimuove) | ✅ 7 giorni |
|
|
| **whitelist globale** | ✅ 60min | ✅ Ogni 30min | ❌ Permanente |
|
|
| **whitelist router_data** | ✅ 60min | ✅ Ogni 30min | ❌ Gestito manualmente |
|
|
| **ddos2-attackers** | ❌ Permanente | ❌ No | ✅ 15 giorni |
|
|
| **ddos3-attackers** | ❌ Permanente | ❌ No | ✅ 20 giorni |
|
|
|
|
---
|
|
|
|
**🎉 Il sistema è ora completamente automatico, sicuro e gestisce intelligentemente sia la protezione DDoS che la whitelist globale con timeout unificati!** |