6 Commits

7 changed files with 71 additions and 57 deletions

View File

@@ -17,7 +17,7 @@ Der **CrowdSec Manager** ist ein Bash-Script, das:
- **Interaktives Menü** als Standard-Modus (für manuelle Verwaltung)
- **CrowdSec Admin-Funktionen**: Decisions, Alerts, Bouncers, Machines, Metriken, IP-Info
- **Gotify** als zusätzlicher Benachrichtigungskanal
- **`--run`-Flag** für automatischen Cron-Betrieb
- **`--sync-allowlist`-Flag** für automatischen Cron-Betrieb
- **Eigene cscli-Befehle** direkt aus dem Menü ausführen
## Features
@@ -37,7 +37,7 @@ Der **CrowdSec Manager** ist ein Bash-Script, das:
| **Dry-Run** | Testmodus ohne tatsächliche Änderungen |
| **Lock-File** | Verhindert parallele Ausführung |
| **Backup** | Sichert den Zustand vor Änderungen |
| **Cron-fähig** | Optimiert für automatische Ausführung mit `--run` |
| **Cron-fähig** | Optimiert für automatische Ausführung mit `--sync-allowlist` |
## Schnellstart
@@ -79,10 +79,10 @@ nano config/crowdsec-manager.conf
./crowdsec-manager.sh
# Allowlist-Sync (für Cron)
./crowdsec-manager.sh --run
./crowdsec-manager.sh --sync-allowlist
# Testlauf ohne Änderungen
./crowdsec-manager.sh --run --dry-run
./crowdsec-manager.sh --sync-allowlist --dry-run
```
### 5. Cron einrichten
@@ -92,10 +92,10 @@ crontab -e
```
```cron
*/30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1
*/30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1
```
> **Wichtig:** `--run` muss angegeben werden, da ohne Parameter das interaktive Menü startet.
> **Wichtig:** `--sync-allowlist` muss angegeben werden, da ohne Parameter das interaktive Menü startet.
## Interaktives Menü
@@ -184,7 +184,7 @@ Verwendung:
Modi:
(ohne Optionen) Startet das interaktive Menü
--run Allowlist synchronisieren (für Cron)
--sync-allowlist Allowlist synchronisieren (für Cron)
Optionen:
-c, --config FILE Konfigurationsdatei angeben
@@ -195,6 +195,7 @@ Optionen:
-f, --flush Alle verwalteten Einträge entfernen
-t, --test Konfiguration testen
-i, --interactive Interaktives Menü starten
--sync-allowlist Allowlist-Sync ausführen (nicht-interaktiv)
-h, --help Hilfe anzeigen
-V, --version Version anzeigen
```
@@ -203,7 +204,7 @@ Optionen:
```
┌─────────────────────────┐
Script: --run
│ Script: --sync-allowlist
└────────────┬────────────┘
┌───────▼────────┐
@@ -214,7 +215,7 @@ Optionen:
┌───────▼────────┐
│ Aktuelle Liste │
│ abrufen + Backup
│ abrufen
└───────┬────────┘
┌───────▼────────┐
@@ -225,11 +226,13 @@ Optionen:
┌───────▼────────┐
│ Vergleich & │
│ Synchronisation │
│ (nur bei Änd.) │
└───────┬────────┘
┌───────▼────────┐
Report &
│ Notifications
Backup, Report
& Notifications │
│ (nur bei Änd.) │
└───────┬────────┘
┌───────▼────────┐

View File

@@ -1,6 +1,6 @@
#!/bin/bash
# ============================================================================
# CrowdSec Manager v0.1.0
# CrowdSec Manager
# ============================================================================
# Verwaltet automatisch eine CrowdSec-Allowlist und bietet interaktive
# CrowdSec-Administration über ein Menü.
@@ -17,7 +17,7 @@ set -euo pipefail
# ----------------------------------------------------------------------------
SCRIPT_NAME="$(basename "$0")"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SCRIPT_VERSION="0.1.0"
SCRIPT_VERSION="0.2.1"
CONFIG_FILE="${SCRIPT_DIR}/config/crowdsec-manager.conf"
# Farben (werden ggf. deaktiviert wenn nicht im Terminal)
@@ -410,8 +410,8 @@ get_current_allowlist() {
return 0
fi
# IPs/CIDRs aus dem JSON extrahieren (Feld "items" -> "ip_or_range")
echo "$inspect_output" | grep -oP '"ip_or_range"\s*:\s*"\K[^"]+' | sort -u > "$CURRENT_IPS_FILE" 2>/dev/null || true
# IPs/CIDRs aus dem JSON extrahieren (Feld "items" -> "value")
echo "$inspect_output" | grep -oP '"value"\s*:\s*"\K[^"]+' | sort -u > "$CURRENT_IPS_FILE" 2>/dev/null || true
local count
count=$(wc -l < "$CURRENT_IPS_FILE" 2>/dev/null || echo "0")
@@ -481,6 +481,12 @@ sync_allowlist() {
STAT_UNCHANGED=$(echo "$unchanged" | wc -l)
fi
# Keine Änderungen erkannt → frühzeitig zurückkehren
if [[ -z "$to_add" ]] && [[ -z "$to_remove" ]]; then
log "INFO" "Keine Änderungen erkannt - Allowlist ist bereits aktuell"
return 1
fi
if [[ -n "$to_add" ]]; then
log "INFO" "Neue IPs zum Hinzufügen gefunden:"
while IFS= read -r ip; do
@@ -488,8 +494,6 @@ sync_allowlist() {
log "INFO" " + $ip"
add_to_allowlist "$ip"
done <<< "$to_add"
else
log "INFO" "Keine neuen IPs zum Hinzufügen"
fi
if [[ -n "$to_remove" ]]; then
@@ -499,9 +503,9 @@ sync_allowlist() {
log "INFO" " - $ip"
remove_from_allowlist "$ip"
done <<< "$to_remove"
else
log "INFO" "Keine veralteten IPs zum Entfernen"
fi
return 0
}
# ============================================================================
@@ -1408,11 +1412,15 @@ run_allowlist_sync() {
fi
get_current_allowlist
create_backup
resolve_all_entries
sync_allowlist
if sync_allowlist; then
# Änderungen erkannt → Backup erstellen & benachrichtigen
create_backup
send_notifications
fi
print_summary
send_notifications
cleanup_temp
release_lock
@@ -1472,7 +1480,7 @@ ${BOLD}Verwendung:${NC}
${BOLD}Modi:${NC}
(ohne Optionen) Startet das interaktive Menü
--run Allowlist synchronisieren (für Cron)
--sync-allowlist Allowlist synchronisieren (für Cron)
${BOLD}Optionen:${NC}
-c, --config FILE Konfigurationsdatei angeben
@@ -1483,20 +1491,21 @@ ${BOLD}Optionen:${NC}
-f, --flush Alle verwalteten Einträge entfernen
-t, --test Konfiguration testen
-i, --interactive Interaktives Menü starten
--run Allowlist-Sync ausführen (nicht-interaktiv)
--sync-allowlist Allowlist-Sync ausführen (nicht-interaktiv)
-h, --help Diese Hilfe anzeigen
-V, --version Version anzeigen
${BOLD}Beispiele:${NC}
$SCRIPT_NAME # Interaktives Menü
$SCRIPT_NAME --run # Allowlist-Sync (Cron-Modus)
$SCRIPT_NAME --run --dry-run # Testlauf
$SCRIPT_NAME --sync-allowlist # Allowlist-Sync (Cron-Modus)
$SCRIPT_NAME --sync-allowlist --dry-run # Testlauf
$SCRIPT_NAME --sync-allowlist --quiet # Sync ohne Ausgabe (nur Fehler)
$SCRIPT_NAME --interactive # Interaktives Menü
$SCRIPT_NAME --list # Allowlist anzeigen
$SCRIPT_NAME --config /etc/conf.conf # Eigene Config
${BOLD}Cron-Beispiel:${NC}
*/30 * * * * ${SCRIPT_DIR}/${SCRIPT_NAME} --run 2>&1
*/30 * * * * ${SCRIPT_DIR}/${SCRIPT_NAME} --sync-allowlist --quiet 2>&1
${BOLD}Konfiguration:${NC}
Standard: config/crowdsec-manager.conf
@@ -1683,8 +1692,8 @@ main() {
action="interactive"
shift
;;
--run)
action="run"
--sync-allowlist)
action="sync-allowlist"
shift
;;
-h|--help)
@@ -1727,9 +1736,11 @@ main() {
interactive_menu
exit 0
;;
sync-allowlist)
;;
esac
# === Automatischer Sync (--run, für Cron) ===
# === Automatischer Sync (--sync-allowlist) ===
load_config
[[ -n "${DRY_RUN:-}" ]] || DRY_RUN=false

View File

@@ -22,7 +22,7 @@ Ab Version 0.1.0 startet das Script standardmäßig ein **interaktives Menü**,
```
╔═══════════════════════════════════════════════════════╗
║ CrowdSec Manager v0.1.0
CrowdSec Manager
╚═══════════════════════════════════════════════════════╝
── ALLOWLIST MANAGEMENT ──────────────────────────────

View File

@@ -24,8 +24,10 @@ Das Script arbeitet nach dem **Desired-State-Prinzip**: Du definierst WAS allowg
3. Synchronisation
├── Vergleich: current_ips.txt vs. new_ips.txt
├── Bei keinen Änderungen: Frühzeitiger Abbruch (kein Backup, keine Benachrichtigung)
├── Neue IPs hinzufügen (in new, nicht in current)
├── Veraltete IPs entfernen (in current, nicht in new)
├── Backup der vorherigen Liste erstellen
└── Statistiken aktualisieren
4. Abschluss
@@ -44,16 +46,16 @@ Das Script arbeitet nach dem **Desired-State-Prinzip**: Du definierst WAS allowg
| Aufruf | Beschreibung |
|--------|-------------|
| `./script.sh` | Startet das **interaktive Menü** (Standard) |
| `./script.sh --run` | Führt Allowlist-Sync aus (für **Cron**) |
| `./script.sh --sync-allowlist` | Führt Allowlist-Sync aus (für **Cron**) |
| `./script.sh --interactive` | Startet das interaktive Menü (explizit) |
> **Wichtig für Cron:** Ab v0.1.0 muss `--run` angegeben werden, da ohne Parameter das interaktive Menü startet.
> **Wichtig für Cron:** Ab v0.2.0 muss `--sync-allowlist` angegeben werden, da ohne Parameter das interaktive Menü startet.
### Alle Optionen
| Option | Kurz | Beschreibung |
|--------|------|-------------|
| `--run` | | Allowlist-Sync ausführen (nicht-interaktiv, für Cron) |
| `--sync-allowlist` | | Allowlist-Sync ausführen (nicht-interaktiv, für Cron) |
| `--interactive` | `-i` | Interaktives Menü starten |
| `--config FILE` | `-c` | Eigene Konfigurationsdatei laden |
| `--dry-run` | `-d` | Simuliert alle Aktionen ohne CrowdSec zu ändern |
@@ -72,7 +74,7 @@ Das Script arbeitet nach dem **Desired-State-Prinzip**: Du definierst WAS allowg
Im Dry-Run Modus werden alle Schritte normal durchgeführt (DNS-Auflösung, Vergleich), aber die CrowdSec-Befehle werden **nicht** ausgeführt:
```bash
./crowdsec-manager.sh --run --dry-run
./crowdsec-manager.sh --sync-allowlist --dry-run
```
Ausgabe:
@@ -148,7 +150,7 @@ crontab -e
Empfohlener Eintrag:
```cron
# CrowdSec Manager - alle 30 Minuten
*/30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1
*/30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1
```
### Cron-spezifisches Verhalten
@@ -175,10 +177,10 @@ Du kannst mehrere Konfigurationsdateien für verschiedene Allowlists verwenden:
```bash
# Webserver-Allowlist
./crowdsec-manager.sh --run --config config/webserver.conf
./crowdsec-manager.sh --sync-allowlist --config config/webserver.conf
# Monitoring-Allowlist
./crowdsec-manager.sh --run --config config/monitoring.conf
./crowdsec-manager.sh --sync-allowlist --config config/monitoring.conf
```
Jede Konfiguration sollte einen eigenen `ALLOWLIST_REASON` verwenden:

View File

@@ -1,7 +1,5 @@
# CrowdSec Manager Dokumentation
## Version 0.1.0
Umfassende Dokumentation für den CrowdSec Manager.
---
@@ -65,7 +63,7 @@ Mindestens setzen:
./crowdsec-manager.sh
# Allowlist-Sync (für Cron)
./crowdsec-manager.sh --run
./crowdsec-manager.sh --sync-allowlist
```
### 6. Cron einrichten
@@ -75,7 +73,7 @@ crontab -e
```
```cron
*/30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1
*/30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1
```
---
@@ -157,7 +155,7 @@ grep -E "Hinzugefügt|Entfernt" /var/log/crowdsec-manager.log
cron:
name: "CrowdSec Manager"
minute: "*/30"
job: "/opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1"
job: "/opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1"
```
### Docker Compose Sidecar
@@ -176,7 +174,7 @@ services:
command: >
sh -c "apk add --no-cache bash bind-tools curl docker-cli &&
while true; do
/app/crowdsec-manager.sh --run;
/app/crowdsec-manager.sh --sync-allowlist;
sleep 1800;
done"
```

View File

@@ -90,14 +90,14 @@ sudo chown $(whoami) /var/backup/crowdsec-manager
### Interaktives Menü startet statt Cron-Sync
Ab v0.1.0 startet das Script standardmäßig das interaktive Menü. Für den Cron-Betrieb **muss** `--run` angegeben werden:
Ab v0.1.0 startet das Script standardmäßig das interaktive Menü. Für den Cron-Betrieb **muss** `--sync-allowlist` angegeben werden:
```bash
# Falsch (startet interaktives Menü):
*/30 * * * * /opt/.../crowdsec-manager.sh
# Richtig:
*/30 * * * * /opt/.../crowdsec-manager.sh --run
*/30 * * * * /opt/.../crowdsec-manager.sh --sync-allowlist
```
---
@@ -129,7 +129,7 @@ A: Kein festes Limit. Die DNS-Auflösung wird linear pro Eintrag durchgeführt.
A: Ja, solange `dig` und Bash 4+ verfügbar sind. macOS liefert standardmäßig Bash 3.2 installiere Bash 4+ z.B. via Homebrew.
**F: Was ist der Unterschied zwischen dem interaktiven Menü und dem CLI?**
A: Das interaktive Menü (Standard) bietet eine grafische Auswahl aller Funktionen. Der CLI-Modus (`--run`, `--list`, etc.) ist für automatisierten/Cron-Betrieb gedacht.
A: Das interaktive Menü (Standard) bietet eine grafische Auswahl aller Funktionen. Der CLI-Modus (`--sync-allowlist`, `--list`, etc.) ist für automatisierten/Cron-Betrieb gedacht.
**F: Können im interaktiven Menü auch direkte cscli-Befehle ausgeführt werden?**
A: Ja, Menüpunkt 40 ermöglicht die Ausführung beliebiger cscli-Befehle über den konfigurierten Basisbefehl.

View File

@@ -1,32 +1,32 @@
# ============================================================================
# CrowdSec Manager v0.1.0 - Crontab Beispiele
# CrowdSec Manager - Crontab Beispiele
# ============================================================================
# Installation: crontab -e (und gewünschte Zeile einfügen)
#
# WICHTIG: Das Script startet ohne Parameter das interaktive Menü.
# Für den Cron-Betrieb MUSS der Parameter --run angegeben werden!
# Für den Cron-Betrieb MUSS --sync-allowlist angegeben werden!
# ============================================================================
# --- Standard: Alle 30 Minuten ausführen ---
*/30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1
*/30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1
# --- Alle 15 Minuten ---
# */15 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1
# */15 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1
# --- Stündlich ---
# 0 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1
# 0 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1
# --- Alle 6 Stunden ---
# 0 */6 * * * /opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1
# 0 */6 * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1
# --- Täglich um 03:00 Uhr ---
# 0 3 * * * /opt/crowdsec-manager/crowdsec-manager.sh --run >> /var/log/crowdsec-manager-cron.log 2>&1
# 0 3 * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist >> /var/log/crowdsec-manager-cron.log 2>&1
# --- Mit eigenem Config-Pfad ---
# */30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run --config /etc/crowdsec-manager/meine-config.conf >> /var/log/crowdsec-manager-cron.log 2>&1
# */30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist --config /etc/crowdsec-manager/meine-config.conf >> /var/log/crowdsec-manager-cron.log 2>&1
# --- Im Quiet-Modus (nur Fehler loggen) ---
# */30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run --quiet >> /var/log/crowdsec-manager-cron.log 2>&1
# */30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist --quiet >> /var/log/crowdsec-manager-cron.log 2>&1
# --- Dry-Run zum Testen ---
# */30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --run --dry-run >> /var/log/crowdsec-manager-cron.log 2>&1
# */30 * * * * /opt/crowdsec-manager/crowdsec-manager.sh --sync-allowlist --dry-run >> /var/log/crowdsec-manager-cron.log 2>&1