HAProxy Erweiterungen: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
 
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt)
Zeile 5: Zeile 5:


==Modul 1: Rate Limiting und Security Headers==
==Modul 1: Rate Limiting und Security Headers==
 
*[[HAProxy Rate Limiting und Security Header]]
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 <code>frontend ft_https</code> und <code>backend backend_www</code>:
 
*nano /etc/haproxy/haproxy.cfg
 
<syntaxhighlight lang="text">
# 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
</syntaxhighlight>
 
;Erklärung:
*<code>stick-table</code> – speichert Request-Raten pro IP im RAM
*<code>http_req_rate(10s)</code> – zählt Requests innerhalb von 10 Sekunden
*<code>gt 50</code> – blockiert ab mehr als 50 Requests in 10 Sekunden
*<code>deny_status 429</code> – antwortet mit ''429 Too Many Requests''
 
===Security Headers===
 
Im <code>frontend ft_https</code> ergänzen:
 
<syntaxhighlight lang="text">
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"
</syntaxhighlight>
 
===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 <code>ab</code> 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 <code>429</code> erscheinen.
 
----


==Modul 2: CrowdSec HAProxy Bouncer==
==Modul 2: CrowdSec HAProxy Bouncer==
 
*[[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
 
<syntaxhighlight lang="text">
[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
</syntaxhighlight>
 
===HAProxy Konfiguration erweitern===
 
*nano /etc/haproxy/haproxy.cfg
 
Im <code>global</code>-Block ergänzen:
 
<syntaxhighlight lang="text">
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
</syntaxhighlight>
 
Im <code>frontend ft_https</code> ergänzen:
 
<syntaxhighlight lang="text">
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" }
</syntaxhighlight>
 
Backend für den CrowdSec-Agent hinzufügen:
 
<syntaxhighlight lang="text">
backend crowdsec_backend
    server crowdsec 127.0.0.1:7422
</syntaxhighlight>
 
===Bouncer-Key generieren===
 
*cscli bouncers add haproxy-bouncer
 
Den ausgegebenen Key in die HAProxy-Konfiguration unter <code>CROWDSEC_API_KEY</code> 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
 
----


==Modul 3: Coraza WAF via SPOE==
==Modul 3: Coraza WAF via SPOE==
*[[Coraza WAF via SPOE]]
*[[Coraza WAF via SPOE]]

Aktuelle Version vom 23. Mai 2026, 13:34 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

Modul 2: CrowdSec HAProxy Bouncer

Modul 3: Coraza WAF via SPOE