Coraza WAF via SPOE: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
Zeile 1: Zeile 1:
 
===Was ist das?===
 
===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.
 
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===
 
===Schaubild===
 
{{#drawio:Coraza WAF}}
 
{{#drawio:Coraza WAF}}
  
 
===Installation des SPOE-Agents===
 
===Installation des SPOE-Agents===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt install golang git
+
apt install -y golang-go git
 
 
 
git clone https://github.com/corazawaf/coraza-spoa.git /opt/coraza-spoa
 
git clone https://github.com/corazawaf/coraza-spoa.git /opt/coraza-spoa
 
cd /opt/coraza-spoa
 
cd /opt/coraza-spoa
go build -o coraza-spoa ./...
+
go build -o /usr/local/bin/coraza-spoa .
cp coraza-spoa /usr/local/bin/
+
cd /
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===OWASP Core Rule Set herunterladen===
+
;Hinweis: <code>go build -o /usr/local/bin/coraza-spoa .</code> – nur das Hauptpackage, Binary direkt ans Ziel. <code>./...</code> funktioniert hier nicht da das Repo mehrere Packages enthält.
 
 
<syntaxhighlight lang="bash">
 
mkdir -p /etc/coraza/crs
 
git clone https://github.com/coreruleset/coreruleset.git /etc/coraza/crs
 
cp /etc/coraza/crs/crs-setup.conf.example /etc/coraza/crs/crs-setup.conf
 
</syntaxhighlight>
 
  
 
===Coraza-Konfiguration===
 
===Coraza-Konfiguration===
 +
Das Verzeichnis und die YAML-Konfigurationsdatei anlegen:
  
*nano /etc/coraza/coraza.conf
+
*mkdir -p /etc/coraza-spoa
 +
*mkdir -p /var/log/coraza
 +
*chown haproxy:haproxy /var/log/coraza
 +
*nano /etc/coraza-spoa/coraza-spoa.yaml
  
<syntaxhighlight lang="text">
+
<syntaxhighlight lang="yaml">
Include /etc/coraza/crs/crs-setup.conf
+
bind: 127.0.0.1:9000
Include /etc/coraza/crs/rules/*.conf
+
log_level: info
 +
log_file: /var/log/coraza/coraza-spoa.log
 +
log_format: json
 +
default_application: sample_app
  
SecRuleEngine On
+
applications:
SecRequestBodyAccess On
+
  - name: sample_app
SecResponseBodyAccess On
+
    directives: |
SecAuditLog /var/log/coraza/audit.log
+
      Include @coraza.conf-recommended
SecAuditLogType Serial
+
      Include @crs-setup.conf.example
SecAuditEngine RelevantOnly
+
      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
 
</syntaxhighlight>
 
</syntaxhighlight>
  
*mkdir -p /var/log/coraza
+
;Hinweis: Die OWASP-Regeln (<code>@owasp_crs/</code>, <code>@coraza.conf-recommended</code>) sind im Binary eingebaut – kein separater CRS-Clone nötig.
  
 
===SPOE-Agent als Systemd-Service===
 
===SPOE-Agent als Systemd-Service===
 
 
*nano /etc/systemd/system/coraza-spoa.service
 
*nano /etc/systemd/system/coraza-spoa.service
  
<syntaxhighlight lang="text">
+
<syntaxhighlight lang="ini">
 
[Unit]
 
[Unit]
 
Description=Coraza SPOA for HAProxy
 
Description=Coraza SPOA for HAProxy
Zeile 51: Zeile 61:
  
 
[Service]
 
[Service]
ExecStart=/usr/local/bin/coraza-spoa -config /etc/coraza/coraza.conf
+
ExecStart=/usr/local/bin/coraza-spoa -config /etc/coraza-spoa/coraza-spoa.yaml
 
Restart=on-failure
 
Restart=on-failure
 
User=haproxy
 
User=haproxy
Zeile 65: Zeile 75:
  
 
===SPOE-Konfiguration für HAProxy===
 
===SPOE-Konfiguration für HAProxy===
 +
*nano /etc/haproxy/coraza.cfg
  
*nano /etc/haproxy/spoe-coraza.conf
+
<syntaxhighlight lang="text">
 +
[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
  
<syntaxhighlight lang="text">
+
spoe-message coraza-req
[config]
+
     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
  spoe-agent coraza
+
     event on-frontend-http-request
     messages    coraza-req
 
     option      var-prefix      coraza
 
    timeout    hello          100ms
 
    timeout    idle            30s
 
    timeout    processing      500ms
 
    use-backend coraza_backend
 
  
  spoe-message coraza-req
 
    args        src                    method=method    path=path    query=query    ver=req.ver    hdrs_bin=req.hdrs.bin    body_size=req.body_size    body=req.body
 
    event      on-frontend-http-request
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
;Wichtig: Der Section-Header <code>[coraza]</code> muss exakt mit dem engine-Namen in <code>haproxy.cfg</code> übereinstimmen. Leerzeile am Dateiende ist zwingend, sonst meldet HAProxy ''Missing LF on last line''.
  
 
===HAProxy Konfiguration erweitern===
 
===HAProxy Konfiguration erweitern===
 
 
Im <code>frontend ft_https</code> ergänzen:
 
Im <code>frontend ft_https</code> ergänzen:
  
Zeile 90: Zeile 102:
 
frontend ft_https
 
frontend ft_https
 
     # ... bestehende Einträge ...
 
     # ... bestehende Einträge ...
     filter spoe engine coraza config /etc/haproxy/spoe-coraza.conf
+
     filter spoe engine coraza config /etc/haproxy/coraza.cfg
     http-request deny deny_status 403 if { var(txn.coraza.action) -m str "deny" }
+
     http-request deny deny_status 403 hdr waf-block "request" if { var(txn.coraza.action) -m str deny }
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Backend für den Coraza-Agent hinzufügen:
+
Backend für den Coraza-SPOA hinzufügen:
  
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
backend coraza_backend
+
backend coraza-spoa
 +
    mode tcp
 
     server coraza 127.0.0.1:9000
 
     server coraza 127.0.0.1:9000
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
;Wichtig: <code>mode tcp</code> ist zwingend – ohne das schlägt der Config-Check fehl.
  
 
===Konfiguration testen und neu laden===
 
===Konfiguration testen und neu laden===
 
 
*haproxy -c -f /etc/haproxy/haproxy.cfg
 
*haproxy -c -f /etc/haproxy/haproxy.cfg
 
*systemctl reload haproxy
 
*systemctl reload haproxy
  
 
===Test===
 
===Test===
 
 
Klassischer WAF-Test mit einem OWASP-typischen Angriff (SQL Injection):
 
Klassischer WAF-Test mit einem OWASP-typischen Angriff (SQL Injection):
  
Zeile 120: Zeile 133:
 
Erwartet: HTTP 403
 
Erwartet: HTTP 403
  
Audit-Log beobachten:
+
Logs beobachten:
  
 +
*journalctl -fu coraza-spoa
 
*tail -f /var/log/coraza/audit.log
 
*tail -f /var/log/coraza/audit.log

Version vom 23. Mai 2026, 15:28 Uhr

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