# ๐Ÿ“‹ Risposta alle Tue Domande ## 1๏ธโƒฃ Sistema di Esportazione (come guardianshift) Ho analizzato il progetto precedente e creato la stessa struttura di deployment: ### โœ… Script Creati (in `/deployment/`) 1. **`setup_almalinux.sh`** - Installazione iniziale su AlmaLinux 9 - Installa Python 3.11, Node.js 20, PostgreSQL - Configura database e utente `ids` - Prepara ambiente di produzione 2. **`setup_crontab.sh`** - Configurazione automazione - Training ML ogni 12 ore (00:00 e 12:00) - Detection automatica ogni 5 minuti - Monitoring processi (riavvio automatico se down) - Backup database giornaliero - Restart settimanale completo 3. **`update_from_git.sh`** - Aggiornamenti da git.alfacom.it - Pull automatico da git - Backup e restore configurazione locale (.env) - Aggiornamento dipendenze - Sync database schema - Restart automatico servizi 4. **Script di supporto**: - `check_backend.sh` - Monitora backend Python - `check_frontend.sh` - Monitora frontend Node - `restart_all.sh` - Restart completo sistema - `backup_db.sh` - Backup PostgreSQL ### ๐Ÿ”„ Workflow Aggiornamento ```bash # Sul server AlmaLinux cd /opt/ids sudo -u ids ./deployment/update_from_git.sh ``` **Cosa fa lo script:** 1. Salva `.env` (password NON vanno su git!) 2. `git pull origin main` da git.alfacom.it 3. Ripristina `.env` locale 4. Aggiorna dipendenze Node.js e Python 5. Sync schema database 6. Restart automatico tutti i servizi ## 2๏ธโƒฃ Deployment su AlmaLinux 9 con Git ### ๐Ÿ“ฆ Installazione Iniziale ```bash # 1. Clone da git.alfacom.it cd /opt/ids git clone https://git.alfacom.it/your-repo/ids.git . # 2. Esegui setup ./deployment/setup_almalinux.sh # 3. Configura environment (IMPORTANTE!) cp .env.example .env nano .env # Inserisci password sicure (vedi sotto) # 4. Installa dipendenze npm install cd python_ml && pip3.11 install -r requirements.txt # 5. Setup database npm run db:push # 6. Configura syslog (per ricevere log router) ./deployment/setup_syslog_server.sh # 7. Avvia sistema ./deployment/setup_crontab.sh ``` ### ๐Ÿ” Configurazione Sicura (.env) **File `.env` NON va mai committato su git!** ```bash # Genera password sicure PGPASSWORD=$(openssl rand -base64 32) SESSION_SECRET=$(openssl rand -base64 32) # Modifica .env nano .env ``` File `.env`: ```bash PGHOST=localhost PGPORT=5432 PGDATABASE=ids_database PGUSER=ids_user PGPASSWORD= # openssl rand -base64 32 SESSION_SECRET= # openssl rand -base64 32 VITE_PYTHON_API_URL=http://localhost:8000 NODE_ENV=production ``` ### ๐Ÿ”„ Aggiornamenti Futuri ```bash cd /opt/ids sudo -u ids ./deployment/update_from_git.sh ``` **Git mantiene solo codice, mai password!** ### ๐Ÿ“ Struttura su Server ``` /opt/ids/ # Directory principale โ”œโ”€โ”€ .env # Configurazione locale (NON su git!) โ”œโ”€โ”€ deployment/ # Script deployment โ”‚ โ”œโ”€โ”€ setup_almalinux.sh โ”‚ โ”œโ”€โ”€ setup_crontab.sh โ”‚ โ”œโ”€โ”€ update_from_git.sh โ”‚ โ””โ”€โ”€ ... โ”œโ”€โ”€ python_ml/ # Backend Python โ”‚ โ”œโ”€โ”€ main.py # FastAPI โ”‚ โ”œโ”€โ”€ ml_analyzer.py # ML core โ”‚ โ”œโ”€โ”€ mikrotik_manager.py โ”‚ โ””โ”€โ”€ syslog_parser.py # Parser log router โ”œโ”€โ”€ client/ # Frontend React โ”œโ”€โ”€ server/ # Backend Node.js โ””โ”€โ”€ backups/ # Backup database (auto) /var/log/ids/ # Log sistema โ”œโ”€โ”€ backend.log โ”œโ”€โ”€ frontend.log โ”œโ”€โ”€ training.log โ”œโ”€โ”€ detect.log โ””โ”€โ”€ syslog_parser.log /var/log/mikrotik/ # Log router in arrivo โ””โ”€โ”€ raw.log ``` ## 3๏ธโƒฃ Raccolta Dati dai Router MikroTik **Questo รจ il punto CRITICO!** Ecco come funziona: ### ๐Ÿ”„ Flusso Completo ``` Router MikroTik (10+ router) โ”‚ โ”‚ Syslog UDP:514 โ”‚ (log firewall, connessioni) โ–ผ Server AlmaLinux - RSyslog โ”‚ Riceve log โ”‚ /var/log/mikrotik/raw.log โ–ผ syslog_parser.py (Python) โ”‚ Legge file log in tempo reale โ”‚ Parsa righe (IP, porte, protocollo, ecc) โ–ผ PostgreSQL Database โ”‚ Tabella: network_logs โ”‚ (timestamp, source_ip, dest_ip, protocol, ecc) โ–ผ ML Analyzer (Python) โ”‚ Training & Detection โ–ผ Backend FastAPI โ”‚ API per frontend โ–ผ Dashboard React ``` ### ๐Ÿ“ก Configurazione Router MikroTik **Su OGNI router MikroTik**, esegui: ```mikrotik # 1. Configura destinazione syslog /system logging action add name=ids-server target=remote remote=192.168.1.100 remote-port=514 # IMPORTANTE: Sostituisci 192.168.1.100 con IP del tuo server AlmaLinux! # 2. Abilita logging firewall /system logging add action=ids-server topics=firewall,info add action=ids-server topics=account,info # 3. Aggiungi regole firewall per loggare connessioni /ip firewall filter add chain=forward action=accept log=yes log-prefix="ACCEPT: " comment="Log accepted" add chain=forward action=drop log=yes log-prefix="DROP: " comment="Log dropped" ``` ### ๐Ÿ–ฅ๏ธ Configurazione Server AlmaLinux ```bash # 1. Installa e configura rsyslog ./deployment/setup_syslog_server.sh # 2. Apri firewall per porta 514/UDP firewall-cmd --permanent --add-port=514/udp firewall-cmd --reload # 3. Avvia syslog parser cd /opt/ids/python_ml nohup python3.11 syslog_parser.py > /var/log/ids/syslog_parser.log 2>&1 & ``` ### ๐Ÿ” Esempio Log Router โ†’ Database **Log inviato dal router:** ``` Jan 15 10:30:45 router1 firewall,info: DROP: src-address=203.0.113.45:54321->192.168.1.10:80, proto TCP, len 60 ``` **Parsato da `syslog_parser.py`:** ```python { "timestamp": "2025-01-15 10:30:45", "router_name": "router1", "source_ip": "203.0.113.45", "source_port": 54321, "destination_ip": "192.168.1.10", "destination_port": 80, "protocol": "tcp", "packet_length": 60, "action": "drop" } ``` **Salvato in database PostgreSQL:** ```sql INSERT INTO network_logs (timestamp, router_name, source_ip, source_port, destination_ip, destination_port, protocol, packet_length, action, raw_message) VALUES (...); ``` ### โœ… Verifica Sistema Funzionante ```bash # 1. Verifica rsyslog in ascolto netstat -ulnp | grep 514 # 2. Verifica log in arrivo dai router tail -f /var/log/mikrotik/raw.log # 3. Verifica parser funzionante tail -f /var/log/ids/syslog_parser.log # 4. Verifica database popolato psql -U ids_user -d ids_database -c "SELECT COUNT(*) FROM network_logs;" # 5. Verifica log recenti psql -U ids_user -d ids_database -c "SELECT * FROM network_logs ORDER BY timestamp DESC LIMIT 10;" ``` ### ๐Ÿ“Š Performance - **10+ router** โ†’ ~100-1000 log/minuto per router - **Totale** โ†’ ~1000-10000 log/minuto - **Parser** โ†’ Processa in tempo reale (<1ms per log) - **Database** โ†’ Commit batch ogni 100 log ### โš™๏ธ Automazione **Syslog parser giร  incluso in crontab:** ```bash # Monitoring ogni 5 minuti (riavvio automatico se down) */5 * * * * /opt/ids/deployment/check_syslog_parser.sh ``` ## ๐Ÿ“š Documentazione Completa 1. **`deployment/README_DEPLOYMENT.md`** - Guida completa deployment 2. **`deployment/CONFIGURAZIONE_ROUTER_MIKROTIK.md`** - Setup router 3. **`README.md`** - Documentazione generale sistema 4. **`python_ml/README.md`** - Dettagli backend Python ML ## ๐ŸŽฏ Checklist Rapida ### Installazione - [ ] Server AlmaLinux 9 pronto - [ ] Clone da git.alfacom.it in `/opt/ids` - [ ] Eseguito `setup_almalinux.sh` - [ ] Configurato `.env` con password sicure - [ ] Installate dipendenze (npm install, pip install) - [ ] Database schema sincronizzato (npm run db:push) ### Raccolta Log Router - [ ] Eseguito `setup_syslog_server.sh` - [ ] Firewall aperto porta 514/UDP - [ ] Router configurati per inviare log (vedi CONFIGURAZIONE_ROUTER_MIKROTIK.md) - [ ] Log visibili in `/var/log/mikrotik/raw.log` - [ ] `syslog_parser.py` in esecuzione - [ ] Database `network_logs` si popola ### Automazione - [ ] Eseguito `setup_crontab.sh` - [ ] Backend Python avviato (porta 8000) - [ ] Frontend Node avviato (porta 5000) - [ ] Training automatico ogni 12h - [ ] Detection automatica ogni 5min - [ ] Backup automatico funzionante ### Primo Training - [ ] Atteso 24h per raccolta log - [ ] Almeno 10.000 log nel database - [ ] Eseguito primo training manuale - [ ] Modello ML salvato - [ ] Detection funzionante ## ๐Ÿš€ Comandi Rapidi ```bash # Aggiornamento da git cd /opt/ids && sudo -u ids ./deployment/update_from_git.sh # Restart completo /opt/ids/deployment/restart_all.sh # Verifica log tail -f /var/log/mikrotik/raw.log tail -f /var/log/ids/backend.log # Verifica database psql -U ids_user -d ids_database -c "SELECT COUNT(*) FROM network_logs;" # Training manuale curl -X POST http://localhost:8000/train -H "Content-Type: application/json" -d '{"max_records": 10000}' # Detection manuale curl -X POST http://localhost:8000/detect -H "Content-Type: application/json" -d '{"auto_block": true}' ``` --- **Sistema completo per deployment su AlmaLinux 9 con raccolta automatica log da router MikroTik! ๐Ÿ›ก๏ธ**