Coraza WAF via SPOE

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen

Was ist das?

Coraza ist eine moderne WAF-Engine (Go), die das OWASP Core Rule Set (CRS) ausführt. Die Integration in HAProxy erfolgt über SPOE – denselben Mechanismus wie bei CrowdSec.

Schaubild

Installation des SPOE-Agents

apt install -y golang-go git
git clone https://github.com/corazawaf/coraza-spoa.git /opt/coraza-spoa
cd /opt/coraza-spoa
go build -o /usr/local/bin/coraza-spoa .
cd /
Hinweis
go build -o /usr/local/bin/coraza-spoa . – nur das Hauptpackage, Binary direkt ans Ziel. ./... funktioniert hier nicht da das Repo mehrere Packages enthält.

Coraza-Konfiguration

Das Verzeichnis und die YAML-Konfigurationsdatei anlegen:

  • mkdir -p /etc/coraza-spoa
  • mkdir -p /var/log/coraza
  • chown haproxy:haproxy /var/log/coraza
  • nano /etc/coraza-spoa/coraza-spoa.yaml
bind: 127.0.0.1:9000
log_level: info
log_file: /var/log/coraza/coraza-spoa.log
log_format: json
default_application: sample_app

applications:
  - name: sample_app
    directives: |
      Include @coraza.conf-recommended
      Include @crs-setup.conf.example
      Include @owasp_crs/*.conf
      SecRuleEngine On
      SecRequestBodyAccess On
      SecResponseBodyAccess On
      SecAuditLog /var/log/coraza/audit.log
      SecAuditLogType Serial
      SecAuditEngine RelevantOnly
    response_check: false
    transaction_ttl_ms: 60000
    log_level: info
    log_file: /var/log/coraza/coraza-spoa.log
    log_format: json
Hinweis
Die OWASP-Regeln (@owasp_crs/, @coraza.conf-recommended) sind im Binary eingebaut – kein separater CRS-Clone nötig.

SPOE-Agent als Systemd-Service

  • nano /etc/systemd/system/coraza-spoa.service
[Unit]
Description=Coraza SPOA for HAProxy
After=network.target

[Service]
ExecStart=/usr/local/bin/coraza-spoa -config /etc/coraza-spoa/coraza-spoa.yaml
Restart=on-failure
User=haproxy

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now coraza-spoa

SPOE-Konfiguration für HAProxy

  • nano /etc/haproxy/coraza.cfg
[coraza]
spoe-agent coraza-agent
    messages        coraza-req
    option          var-prefix      coraza
    option          set-on-error    error
    timeout         hello           2s
    timeout         idle            2m
    timeout         processing      500ms
    use-backend     coraza-spoa

spoe-message coraza-req
    args app=str(sample_app) id=unique-id src-ip=src src-port=src_port dst-ip=dst dst-port=dst_port method=method path=path query=query version=req.ver headers=req.hdrs body=req.body
    event on-frontend-http-request
Wichtig
Der Section-Header [coraza] muss exakt mit dem engine-Namen in haproxy.cfg übereinstimmen. Leerzeile am Dateiende ist zwingend, sonst meldet HAProxy Missing LF on last line.

HAProxy Konfiguration erweitern

Im frontend ft_https ergänzen:

frontend ft_https
    # ... bestehende Einträge ...
    filter spoe engine coraza config /etc/haproxy/coraza.cfg
    http-request deny deny_status 403 hdr waf-block "request" if { var(txn.coraza.action) -m str deny }

Backend für den Coraza-SPOA hinzufügen:

backend coraza-spoa
    mode tcp
    server coraza 127.0.0.1:9000
Wichtig
mode tcp ist zwingend – ohne das schlägt der Config-Check fehl.

Konfiguration testen und neu laden

  • haproxy -c -f /etc/haproxy/haproxy.cfg
  • systemctl reload haproxy

Test

Klassischer WAF-Test mit einem OWASP-typischen Angriff (SQL Injection):

Erwartet: HTTP 403

XSS-Test:

Erwartet: HTTP 403

Logs beobachten:

  • journalctl -fu coraza-spoa
  • tail -f /var/log/coraza/audit.log