security fail2ban tutorial
Fail2Ban richtig konfigurieren
Jeder Server mit SSH ist Ziel von Brute-Force-Angriffen. Fail2Ban sperrt IPs automatisch nach zu vielen Fehlversuchen.
Installation
apt install fail2ban
Konfiguration verstehen
/etc/fail2ban/jail.conf- Defaults (nicht bearbeiten!)/etc/fail2ban/jail.local- Deine Anpassungen/etc/fail2ban/jail.d/- Modulare Configs
Immer jail.local erstellen, nie jail.conf editieren:
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Basis-Konfiguration
/etc/fail2ban/jail.local:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
banaction = ufw
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
Erklärung:
bantime- Wie lange wird gebanntfindtime- Zeitfenster für Fehlversuchemaxretry- Erlaubte Fehlversuchebanaction = ufw- Nutzt UFW statt iptables
Fail2Ban starten
systemctl enable fail2ban
systemctl start fail2ban
Status prüfen
fail2ban-client status
fail2ban-client status sshd
Gebannte IPs anzeigen
fail2ban-client status sshd
Output:
Status for the jail: sshd
|- Filter
| |- Currently failed: 2
| |- Total failed: 147
| `- File list: /var/log/auth.log
`- Actions
|- Currently banned: 3
|- Total banned: 42
`- Banned IP list: 1.2.3.4 5.6.7.8 9.10.11.12
IP manuell entbannen
fail2ban-client set sshd unbanip 1.2.3.4
Eigene IP whitelisten
Damit du dich nicht selbst aussperrst:
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24 deine.feste.ip
Nginx/Apache schützen
[nginx-http-auth]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log
[nginx-limit-req]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log
Aggressive Einstellungen
Für Server mit vielen Angriffen:
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 2
bantime = 24h
findtime = 1h
Weniger Versuche, längerer Ban.
Recidive - Wiederholungstäter
Extra-Jail für IPs die immer wieder gebannt werden:
[recidive]
enabled = true
logpath = /var/log/fail2ban.log
banaction = ufw
bantime = 1w
findtime = 1d
maxretry = 3
Nach 3 Bans in 24h: 1 Woche Sperre.
Logs checken
tail -f /var/log/fail2ban.log
Fazit
Fail2Ban + UFW + SSH-Keys = Solide Grundsicherung. Kein Server sollte ohne laufen.