HAProxy Erweiterungen: Unterschied zwischen den Versionen
| Zeile 162: | Zeile 162: | ||
==Modul 3: Coraza WAF via SPOE== | ==Modul 3: Coraza WAF via SPOE== | ||
| − | + | *[[Coraza WAF via SPOE]] | |
| − | Coraza | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Version vom 23. Mai 2026, 13:32 Uhr
HAProxy Middleware
HAProxy lässt sich durch verschiedene Middleware-Module erweitern, die zwischen Client und Backend Traffic inspizieren, filtern oder transformieren. Die folgenden Module können unabhängig voneinander eingesetzt werden.
Modul 1: Rate Limiting und Security Headers
Beide Funktionen sind nativ in HAProxy verfügbar – kein Zusatztool erforderlich.
Rate Limiting mit Stick Tables
HAProxy kann Requests pro IP begrenzen und bei Überschreitung mit HTTP 429 antworten.
Die folgende Konfiguration ergänzt den bestehenden frontend ft_https und backend backend_www:
- nano /etc/haproxy/haproxy.cfg
# Stick Table im Backend definieren
backend backend_www
stick-table type ip size 100k expire 30s store http_req_rate(10s)
http-request track-sc0 src
http-request deny deny_status 429 if { sc_http_req_rate(0) gt 50 }
server www 10.88.2XX.11:443 ssl verify none check
- Erklärung
stick-table– speichert Request-Raten pro IP im RAMhttp_req_rate(10s)– zählt Requests innerhalb von 10 Sekundengt 50– blockiert ab mehr als 50 Requests in 10 Sekundendeny_status 429– antwortet mit 429 Too Many Requests
Security Headers
Im frontend ft_https ergänzen:
frontend ft_https
# ... bestehende Konfiguration ...
http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains"
http-response set-header X-Frame-Options "SAMEORIGIN"
http-response set-header X-Content-Type-Options "nosniff"
http-response set-header Referrer-Policy "strict-origin-when-cross-origin"
Test
- curl -vk https://revproxy.it2XX.int
In der Ausgabe sollten die gesetzten Header sichtbar sein:
< strict-transport-security: max-age=31536000; includeSubDomains < x-frame-options: SAMEORIGIN < x-content-type-options: nosniff
Rate Limiting testen (z.B. mit ab oder einer Schleife):
- for i in $(seq 1 60); do curl -sk -o /dev/null -w "%{http_code}\n" https://revproxy.it2XX.int; done
Ab Request 51 sollte 429 erscheinen.
Modul 2: CrowdSec HAProxy Bouncer
CrowdSec läuft als IDS/IPS und kommuniziert über den SPOE-Mechanismus direkt mit HAProxy. Blockentscheidungen werden auf Proxy-Ebene getroffen, bevor der Request das Backend erreicht.
- Voraussetzung
- CrowdSec ist installiert und läuft (siehe CrowdSec)
Installation
- apt install crowdsec-haproxy-bouncer
SPOE-Konfiguration
Der Bouncer legt automatisch eine SPOE-Konfigurationsdatei an:
/etc/haproxy/spoe-crowdsec.conf
Inhalt prüfen:
- cat /etc/haproxy/spoe-crowdsec.conf
[config]
spoe-agent crowdsec
messages check-client-ip
option var-prefix crowdsec
timeout hello 100ms
timeout idle 30s
timeout processing 50ms
use-backend crowdsec_backend
spoe-message check-client-ip
args src
event on-client-session
HAProxy Konfiguration erweitern
- nano /etc/haproxy/haproxy.cfg
Im global-Block ergänzen:
global
# ... bestehende Einträge ...
lua-prepend-path /usr/lib/crowdsec/lua/haproxy/?.lua
lua-load /usr/lib/crowdsec/lua/haproxy/crowdsec.lua
setenv CROWDSEC_API_KEY <DEIN_BOUNCER_KEY>
setenv CROWDSEC_API_URL http://127.0.0.1:8080
Im frontend ft_https ergänzen:
frontend ft_https
# ... bestehende Einträge ...
filter spoe engine crowdsec config /etc/haproxy/spoe-crowdsec.conf
http-request lua.crowdsec_allow
http-request deny deny_status 403 if { var(txn.crowdsec.action) -m str "ban" }
Backend für den CrowdSec-Agent hinzufügen:
backend crowdsec_backend
server crowdsec 127.0.0.1:7422
Bouncer-Key generieren
- cscli bouncers add haproxy-bouncer
Den ausgegebenen Key in die HAProxy-Konfiguration unter CROWDSEC_API_KEY eintragen.
Konfiguration testen und neu laden
- haproxy -c -f /etc/haproxy/haproxy.cfg
- systemctl reload haproxy
Test
CrowdSec-Entscheidungen anzeigen:
- cscli decisions list
Eine Test-IP manuell bannen:
- cscli decisions add --ip 10.88.2XX.99 --duration 5m --reason "Test"
Vom gebannten Client aus sollte ein HTTP 403 zurückkommen. Ban wieder entfernen:
- cscli decisions delete --ip 10.88.2XX.99
Logs beobachten:
- journalctl -fu haproxy
- journalctl -fu crowdsec