This commit introduces detailed documentation for deploying the Intrusion Detection System (IDS) on AlmaLinux 9, including setup scripts, MikroTik router configuration, and update procedures via git. It also includes the syslog parser script for processing router logs and saving them to PostgreSQL. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: b2b01a4a-55da-4f33-9143-6bf0399e0a03 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/c9ITWqD
510 lines
13 KiB
Markdown
510 lines
13 KiB
Markdown
# 🚀 Guida Deployment IDS su AlmaLinux 9
|
|
|
|
Guida completa per il deployment del sistema IDS su server AlmaLinux 9 con aggiornamenti tramite git.alfacom.it
|
|
|
|
## 📋 Prerequisiti
|
|
|
|
- Server AlmaLinux 9 con accesso root
|
|
- Accesso a git.alfacom.it
|
|
- Almeno 4GB RAM, 20GB disco
|
|
- Connessione di rete ai router MikroTik
|
|
|
|
## 🔧 Installazione Iniziale
|
|
|
|
### Step 1: Preparazione Server
|
|
|
|
Connettiti al server AlmaLinux come root:
|
|
|
|
```bash
|
|
ssh root@<IP_SERVER>
|
|
```
|
|
|
|
### Step 2: Clone Repository
|
|
|
|
```bash
|
|
cd /tmp
|
|
git clone https://git.alfacom.it/your-repo/ids.git
|
|
cd ids
|
|
```
|
|
|
|
### Step 3: Esegui Installazione Base
|
|
|
|
```bash
|
|
chmod +x deployment/setup_almalinux.sh
|
|
./deployment/setup_almalinux.sh
|
|
```
|
|
|
|
Questo script:
|
|
- ✅ Aggiorna il sistema
|
|
- ✅ Installa Python 3.11, Node.js 20, PostgreSQL
|
|
- ✅ Configura database PostgreSQL
|
|
- ✅ Crea utente `ids`
|
|
- ✅ Crea directory `/opt/ids`
|
|
|
|
### Step 4: Clone Repository in Destinazione Finale
|
|
|
|
```bash
|
|
cd /opt/ids
|
|
sudo -u ids git clone https://git.alfacom.it/your-repo/ids.git .
|
|
```
|
|
|
|
### Step 5: Configura Environment Variables
|
|
|
|
**IMPORTANTE**: NON usare mai password in chiaro nel codice!
|
|
|
|
```bash
|
|
sudo -u ids cp .env.example .env
|
|
sudo -u ids nano .env
|
|
```
|
|
|
|
Modifica il file `.env`:
|
|
|
|
```bash
|
|
# Database PostgreSQL
|
|
PGHOST=localhost
|
|
PGPORT=5432
|
|
PGDATABASE=ids_database
|
|
PGUSER=ids_user
|
|
PGPASSWORD=<GENERA_PASSWORD_SICURA_QUI> # Usa: openssl rand -base64 32
|
|
|
|
# Session Secret
|
|
SESSION_SECRET=<GENERA_SECRET_SICURA_QUI> # Usa: openssl rand -base64 32
|
|
|
|
# Python Backend URL
|
|
VITE_PYTHON_API_URL=http://localhost:8000
|
|
|
|
# Environment
|
|
NODE_ENV=production
|
|
```
|
|
|
|
**Genera password sicure**:
|
|
```bash
|
|
# Password database
|
|
openssl rand -base64 32
|
|
|
|
# Session secret
|
|
openssl rand -base64 32
|
|
```
|
|
|
|
### Step 6: Aggiorna Password Database
|
|
|
|
```bash
|
|
# Modifica password utente PostgreSQL
|
|
sudo -u postgres psql -c "ALTER USER ids_user WITH PASSWORD '<PASSWORD_DA_.ENV>';"
|
|
```
|
|
|
|
### Step 7: Installa Dipendenze
|
|
|
|
```bash
|
|
# Dipendenze Node.js
|
|
cd /opt/ids
|
|
sudo -u ids npm install
|
|
|
|
# Dipendenze Python
|
|
cd /opt/ids/python_ml
|
|
sudo -u ids pip3.11 install -r requirements.txt
|
|
|
|
# Sync schema database
|
|
cd /opt/ids
|
|
sudo -u ids npm run db:push
|
|
```
|
|
|
|
### Step 8: Configura Syslog Server
|
|
|
|
Questo è **CRITICO** per ricevere log dai router:
|
|
|
|
```bash
|
|
cd /opt/ids/deployment
|
|
chmod +x setup_syslog_server.sh
|
|
./setup_syslog_server.sh
|
|
```
|
|
|
|
Verifica che rsyslog sia in ascolto:
|
|
```bash
|
|
netstat -ulnp | grep 514
|
|
```
|
|
|
|
### Step 9: Configura Router MikroTik
|
|
|
|
Segui la guida: `deployment/CONFIGURAZIONE_ROUTER_MIKROTIK.md`
|
|
|
|
Configurazione minima su ogni router:
|
|
```mikrotik
|
|
/system logging action
|
|
add name=ids-server target=remote remote=<IP_SERVER_ALMALINUX> remote-port=514
|
|
|
|
/system logging
|
|
add action=ids-server topics=firewall,info
|
|
```
|
|
|
|
### Step 10: Avvia Syslog Parser
|
|
|
|
```bash
|
|
cd /opt/ids/python_ml
|
|
sudo -u ids nohup python3.11 syslog_parser.py > /var/log/ids/syslog_parser.log 2>&1 &
|
|
```
|
|
|
|
Verifica log in arrivo:
|
|
```bash
|
|
tail -f /var/log/mikrotik/raw.log
|
|
tail -f /var/log/ids/syslog_parser.log
|
|
```
|
|
|
|
### Step 11: Configura Crontab e Avvio Automatico
|
|
|
|
```bash
|
|
cd /opt/ids/deployment
|
|
chmod +x setup_crontab.sh
|
|
./setup_crontab.sh
|
|
```
|
|
|
|
Questo configura:
|
|
- ✅ Backend Python (FastAPI) - avvio automatico
|
|
- ✅ Frontend Node.js - avvio automatico
|
|
- ✅ Training ML ogni 12 ore
|
|
- ✅ Detection ogni 5 minuti
|
|
- ✅ Monitoring e restart automatici
|
|
- ✅ Backup database giornaliero
|
|
|
|
### Step 12: Verifica Sistema
|
|
|
|
```bash
|
|
# Verifica processi in esecuzione
|
|
ps aux | grep -E 'python.*main|npm.*dev|syslog_parser'
|
|
|
|
# Verifica API backend
|
|
curl http://localhost:8000/health
|
|
|
|
# Verifica frontend
|
|
curl http://localhost:5000
|
|
|
|
# Verifica database popolato
|
|
psql -U ids_user -d ids_database -c "SELECT COUNT(*) FROM network_logs;"
|
|
```
|
|
|
|
## 🔄 Aggiornamenti da Git
|
|
|
|
### Procedura Standard di Aggiornamento
|
|
|
|
```bash
|
|
cd /opt/ids
|
|
sudo -u ids ./deployment/update_from_git.sh
|
|
```
|
|
|
|
Questo script:
|
|
1. ✅ Salva backup configurazione locale (.env)
|
|
2. ✅ Scarica aggiornamenti da git.alfacom.it
|
|
3. ✅ Ripristina configurazione locale
|
|
4. ✅ Aggiorna dipendenze Node.js e Python
|
|
5. ✅ Aggiorna schema database
|
|
6. ✅ Restart automatico servizi
|
|
|
|
### Aggiornamento Manuale (se necessario)
|
|
|
|
```bash
|
|
cd /opt/ids
|
|
|
|
# Backup configurazione
|
|
sudo -u ids cp .env .env.backup
|
|
|
|
# Pull da git
|
|
sudo -u ids git pull origin main
|
|
|
|
# Ripristina .env
|
|
sudo -u ids cp .env.backup .env
|
|
|
|
# Aggiorna dipendenze
|
|
sudo -u ids npm install
|
|
cd python_ml && sudo -u ids pip3.11 install -r requirements.txt
|
|
|
|
# Sync database
|
|
cd /opt/ids
|
|
sudo -u ids npm run db:push
|
|
|
|
# Restart servizi
|
|
./deployment/restart_all.sh
|
|
```
|
|
|
|
## 📊 Workflow Completo
|
|
|
|
```
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
│ ROUTER MIKROTIK │
|
|
│ (Generano log firewall via Syslog UDP:514) │
|
|
└────────────────────────┬────────────────────────────────────┘
|
|
│
|
|
▼
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
│ SERVER ALMALINUX 9 - RSYSLOG │
|
|
│ Riceve log → /var/log/mikrotik/raw.log │
|
|
└────────────────────────┬────────────────────────────────────┘
|
|
│
|
|
▼
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
│ SYSLOG_PARSER.PY │
|
|
│ Parsa log → Salva in PostgreSQL (network_logs) │
|
|
└────────────────────────┬────────────────────────────────────┘
|
|
│
|
|
▼
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
│ DATABASE POSTGRESQL │
|
|
│ Tabelle: network_logs, detections, routers, etc. │
|
|
└────────────────────────┬────────────────────────────────────┘
|
|
│
|
|
┌──────────────┴──────────────┐
|
|
│ │
|
|
▼ ▼
|
|
┌─────────────────────┐ ┌─────────────────────┐
|
|
│ PYTHON BACKEND │ │ NODE.JS BACKEND │
|
|
│ (FastAPI:8000) │ │ (Express:5000) │
|
|
│ - Training ML │ │ - API REST │
|
|
│ - Detection │ │ - Frontend serve │
|
|
│ - Auto-block IPs │ └──────────┬──────────┘
|
|
└──────────┬──────────┘ │
|
|
│ │
|
|
└─────────────┬───────────────┘
|
|
│
|
|
▼
|
|
┌──────────────────────┐
|
|
│ REACT DASHBOARD │
|
|
│ (Browser) │
|
|
│ - Monitoring │
|
|
│ - Gestione router │
|
|
│ - Detections │
|
|
└──────────────────────┘
|
|
```
|
|
|
|
## ⏰ Operazioni Automatiche
|
|
|
|
### Training ML (ogni 12h - 00:00 e 12:00)
|
|
```bash
|
|
POST http://localhost:8000/train
|
|
{
|
|
"max_records": 10000,
|
|
"hours_back": 24,
|
|
"contamination": 0.01
|
|
}
|
|
```
|
|
|
|
### Detection (ogni 5 minuti)
|
|
```bash
|
|
POST http://localhost:8000/detect
|
|
{
|
|
"max_records": 5000,
|
|
"auto_block": true,
|
|
"risk_threshold": 75
|
|
}
|
|
```
|
|
|
|
### Monitoring Processi (ogni 5 minuti)
|
|
- Controlla backend Python (porta 8000)
|
|
- Controlla frontend Node (porta 5000)
|
|
- Riavvia automaticamente se down
|
|
|
|
### Backup Database (ogni giorno alle 04:00)
|
|
```bash
|
|
/opt/ids/backups/ids_backup_YYYYMMDD_HHMMSS.sql.gz
|
|
```
|
|
Mantiene 7 giorni di backup.
|
|
|
|
### Restart Completo (ogni domenica 03:00)
|
|
```bash
|
|
./deployment/restart_all.sh
|
|
```
|
|
|
|
## 🔍 Monitoring e Log
|
|
|
|
### Log Principali
|
|
|
|
```bash
|
|
# Backend Python (FastAPI)
|
|
tail -f /var/log/ids/backend.log
|
|
|
|
# Frontend Node.js
|
|
tail -f /var/log/ids/frontend.log
|
|
|
|
# Syslog Parser
|
|
tail -f /var/log/ids/syslog_parser.log
|
|
|
|
# Training ML
|
|
tail -f /var/log/ids/training.log
|
|
|
|
# Detection
|
|
tail -f /var/log/ids/detect.log
|
|
|
|
# Crontab
|
|
tail -f /var/log/ids/cron.log
|
|
|
|
# Log router in arrivo
|
|
tail -f /var/log/mikrotik/raw.log
|
|
```
|
|
|
|
### Comandi Utili
|
|
|
|
```bash
|
|
# Stato processi
|
|
ps aux | grep -E 'python.*main|npm.*dev|syslog_parser'
|
|
|
|
# Stato database
|
|
psql -U ids_user -d ids_database
|
|
|
|
# Query utili database
|
|
psql -U ids_user -d ids_database -c "SELECT COUNT(*) FROM network_logs;"
|
|
psql -U ids_user -d ids_database -c "SELECT COUNT(*) FROM detections WHERE blocked = true;"
|
|
|
|
# Restart manuale servizi
|
|
/opt/ids/deployment/restart_all.sh
|
|
|
|
# Visualizza crontab
|
|
crontab -u ids -l
|
|
```
|
|
|
|
## 🔒 Sicurezza
|
|
|
|
### Password e Secrets
|
|
|
|
- ❌ **MAI** committare `.env` su git
|
|
- ✅ Usa `.env.example` come template (committabile)
|
|
- ✅ Genera password sicure con `openssl rand -base64 32`
|
|
- ✅ Configura `.env` solo sul server di produzione
|
|
- ✅ File `.env` ha permessi 600 (solo utente ids può leggere)
|
|
|
|
### Firewall
|
|
|
|
```bash
|
|
# Porta 514/UDP per Syslog (solo da router fidati)
|
|
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.0.0/16" port port="514" protocol="udp" accept'
|
|
|
|
# Porta 5000/TCP per frontend (accesso web)
|
|
firewall-cmd --permanent --add-port=5000/tcp
|
|
|
|
# Porta 8000/TCP per API backend (solo localhost)
|
|
# NON esporre pubblicamente
|
|
|
|
firewall-cmd --reload
|
|
```
|
|
|
|
### Backup
|
|
|
|
```bash
|
|
# Backup manuale database
|
|
/opt/ids/deployment/backup_db.sh
|
|
|
|
# Restore backup
|
|
gunzip /opt/ids/backups/ids_backup_YYYYMMDD_HHMMSS.sql.gz
|
|
psql -U ids_user -d ids_database < /opt/ids/backups/ids_backup_YYYYMMDD_HHMMSS.sql
|
|
```
|
|
|
|
## 🐛 Troubleshooting
|
|
|
|
### Log non arrivano dai router
|
|
|
|
```bash
|
|
# 1. Verifica rsyslog in ascolto
|
|
netstat -ulnp | grep 514
|
|
|
|
# 2. Test connettività da router
|
|
# Sul router MikroTik:
|
|
/tool fetch url="http://<IP_SERVER>:514" mode=udp
|
|
|
|
# 3. Verifica firewall server
|
|
firewall-cmd --list-all
|
|
|
|
# 4. Controlla log rsyslog
|
|
tail -f /var/log/messages | grep rsyslog
|
|
```
|
|
|
|
### Database non si popola
|
|
|
|
```bash
|
|
# 1. Verifica syslog_parser in esecuzione
|
|
ps aux | grep syslog_parser
|
|
|
|
# 2. Controlla log parser
|
|
tail -f /var/log/ids/syslog_parser.log
|
|
|
|
# 3. Verifica file log esiste
|
|
ls -la /var/log/mikrotik/raw.log
|
|
|
|
# 4. Test manuale parser
|
|
cd /opt/ids/python_ml
|
|
sudo -u ids python3.11 syslog_parser.py
|
|
```
|
|
|
|
### Backend non risponde
|
|
|
|
```bash
|
|
# 1. Verifica processo
|
|
ps aux | grep "python.*main"
|
|
|
|
# 2. Controlla log
|
|
tail -f /var/log/ids/backend.log
|
|
|
|
# 3. Test manuale
|
|
cd /opt/ids/python_ml
|
|
sudo -u ids python3.11 main.py
|
|
|
|
# 4. Verifica dipendenze
|
|
pip3.11 list | grep -E 'fastapi|psycopg2|scikit'
|
|
```
|
|
|
|
### Frontend non carica
|
|
|
|
```bash
|
|
# 1. Verifica processo Node
|
|
ps aux | grep "npm.*dev"
|
|
|
|
# 2. Controlla log
|
|
tail -f /var/log/ids/frontend.log
|
|
|
|
# 3. Test manuale
|
|
cd /opt/ids
|
|
sudo -u ids npm run dev
|
|
|
|
# 4. Verifica dipendenze
|
|
npm list
|
|
```
|
|
|
|
## 📝 Checklist Post-Installazione
|
|
|
|
- [ ] Server AlmaLinux 9 aggiornato
|
|
- [ ] PostgreSQL installato e configurato
|
|
- [ ] Database `ids_database` creato e accessibile
|
|
- [ ] File `.env` configurato con password sicure
|
|
- [ ] Dipendenze Node.js e Python installate
|
|
- [ ] Rsyslog in ascolto su porta 514
|
|
- [ ] Router MikroTik configurati per inviare log
|
|
- [ ] Log visibili in `/var/log/mikrotik/raw.log`
|
|
- [ ] Syslog parser in esecuzione
|
|
- [ ] Database `network_logs` si popola
|
|
- [ ] Backend Python risponde su porta 8000
|
|
- [ ] Frontend Node accessibile su porta 5000
|
|
- [ ] Crontab configurato per operazioni automatiche
|
|
- [ ] Firewall configurato correttamente
|
|
- [ ] Backup automatico funzionante
|
|
|
|
## ✅ Prima Esecuzione Training
|
|
|
|
Dopo 24h di raccolta log:
|
|
|
|
```bash
|
|
# Controlla quanti log hai raccolto
|
|
psql -U ids_user -d ids_database -c "SELECT COUNT(*) FROM network_logs;"
|
|
|
|
# Se hai almeno 10.000 log, esegui training
|
|
curl -X POST http://localhost:8000/train \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"max_records": 10000, "hours_back": 24}'
|
|
|
|
# Controlla log training
|
|
tail -f /var/log/ids/training.log
|
|
|
|
# Dopo training, abilita detection automatica (già configurata in crontab)
|
|
```
|
|
|
|
---
|
|
|
|
**Sistema IDS pronto per la produzione! 🛡️**
|
|
|
|
Per supporto: controllare log in `/var/log/ids/` e documentazione in `/opt/ids/deployment/`
|