Created
September 26, 2025 19:21
-
-
Save steffen-wirth/60858476f1a30f8372495c842088adca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Schritt-für-Schritt Konfiguration (503-Response für gebannte IPs) | |
| 1. Apache so konfigurieren, dass er Client-IPs korrekt sieht | |
| Installiere und aktiviere mod_remoteip: | |
| sudo a2enmod remoteip | |
| In /etc/apache2/conf-available/remoteip.conf (neu anlegen): | |
| RemoteIPHeader X-Forwarded-For | |
| RemoteIPTrustedProxy 127.0.0.1 | |
| Falls Varnish auf einer anderen IP (z. B. 195.201.0.85) läuft, diese ebenfalls hinzufügen: | |
| RemoteIPTrustedProxy 127.0.0.1 195.201.0.85 | |
| Dann: | |
| sudo a2enconf remoteip | |
| sudo systemctl reload apache2 | |
| Ab jetzt sieht Apache in Remote_Addr die echte Client-IP (Fail2Ban erkennt diese auch). | |
| 2. Fehlerseite für gebannte IPs | |
| Lege unter /var/www/errors/503.php an: | |
| <?php | |
| http_response_code(503); | |
| header('Retry-After: 3600'); // 1 Stunde | |
| echo "Service temporarily unavailable."; | |
| ?> | |
| Alias und Directory freigeben in deiner VHost-Datei (/etc/apache2/sites-available/000-default.conf oder deine Domain): | |
| Alias /errors/ /var/www/errors/ | |
| <Directory /var/www/errors> | |
| Require all granted | |
| </Directory> | |
| 3. Apache-Config: Banned-IPs erkennen und 503 ausliefern | |
| Erzeuge eine Include-Datei /etc/fail2ban/apache-banned.conf. | |
| Dort trägt Fail2Ban für jede IP automatisch sowas ein: | |
| SetEnvIf Remote_Addr "^1\.2\.3\.4$" IS_BANNED=1 | |
| In deinem VHost ganz oben einfügen: | |
| IncludeOptional /etc/fail2ban/apache-banned.conf | |
| SetEnvIfExpr "%{env:IS_BANNED} == '1'" is_banned | |
| ErrorDocument 403 /errors/503.php | |
| Das bedeutet: Wenn eine IP gebannt ist, setzt Apache IS_BANNED, behandelt sie als "forbidden" (403), aber liefert dank ErrorDocument die 503-Seite zurück. | |
| 4. Fail2Ban Action zum Befüllen der Datei | |
| Neues Script /usr/local/bin/f2b-apache-setenv.sh: | |
| #!/bin/bash | |
| MODE="$1" | |
| IP="$2" | |
| FILE="/etc/fail2ban/apache-banned.conf" | |
| TMP="/tmp/apache-banned.conf.$$" | |
| if [ -z "$MODE" ] || [ -z "$IP" ]; then | |
| echo "Usage: $0 ban|unban <IP>" | |
| exit 2 | |
| fi | |
| sudo touch "$FILE" | |
| sudo chown root:root "$FILE" | |
| sudo chmod 644 "$FILE" | |
| case "$MODE" in | |
| ban) | |
| grep -F "SetEnvIf Remote_Addr \"^${IP}$\" IS_BANNED=1" "$FILE" >/dev/null 2>&1 || \ | |
| echo "SetEnvIf Remote_Addr \"^${IP}$\" IS_BANNED=1" | sudo tee -a "$FILE" >/dev/null | |
| ;; | |
| unban) | |
| sudo grep -v -F "SetEnvIf Remote_Addr \"^${IP}$\" IS_BANNED=1" "$FILE" > "$TMP" || true | |
| sudo mv "$TMP" "$FILE" | |
| ;; | |
| *) | |
| echo "Unknown mode" | |
| exit 2 | |
| ;; | |
| esac | |
| sudo apachectl -k graceful | |
| Rechte setzen: | |
| sudo chmod +x /usr/local/bin/f2b-apache-setenv.sh | |
| 5. Fail2Ban Action anlegen | |
| Datei /etc/fail2ban/action.d/apache-setenv.conf: | |
| [Definition] | |
| actionstart = | |
| actionstop = | |
| actioncheck = | |
| actionban = /usr/local/bin/f2b-apache-setenv.sh ban <ip> | |
| actionunban = /usr/local/bin/f2b-apache-setenv.sh unban <ip> | |
| 6. Jail anpassen | |
| In deiner jail.local, z. B. bei [apache-badbots] oder [apache-ddos]: | |
| [apache-badbots] | |
| enabled = true | |
| filter = apache-badbots | |
| logpath = /var/log/apache2/access-le-groove.log | |
| action = apache-setenv | |
| maxretry = 10 | |
| findtime = 60 | |
| bantime = 3600 | |
| 7. Restart | |
| sudo systemctl restart fail2ban | |
| sudo apachectl configtest && sudo systemctl reload apache2 | |
| ✅ Ergebnis: | |
| Fail2Ban schreibt gebannte IPs in /etc/fail2ban/apache-banned.conf. | |
| Apache liefert bei Zugriffen dieser IPs einen 503 Service Unavailable zurück (nicht 403). | |
| Dank mod_remoteip siehst du die echte Client-IP auch hinter Varnish. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment