basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    CrowdSec AppSec: blocking web attacks before they reach the app (WAF)

    Standard CrowdSec reads the access log after a request has already hit your app — the first exploit payload gets through. The AppSec component acts as a WAF in front of the container: the Traefik plugin sends each request to CrowdSec before proxying and rejects attacks on the spot with 403.

    The usual CrowdSec + Traefik setup (CrowdSec with Traefik behind Cloudflare) works off logs: a request hits the container, Traefik writes a line to access.log, CrowdSec parses it and bans the IP. That covers brute force and scanners, but an SQL injection or CVE exploit fires on the first request — before the IP lands on the ban list.

    AppSec puts inspection right in the request path:

    log-based:    request ──→ Traefik ──→ app ──→ access.log ──→ CrowdSec (ban after the fact)
    with AppSec:  request ──→ Traefik ──→ CrowdSec:7422 (WAF) ── ok ──→ app
                                                └── attack ──→ HTTP 403

    The Traefik plugin already speaks to AppSec — no separate WAF container needed.

    Enable AppSec in CrowdSec and Traefik

    1. In /opt/crowdsec/docker-compose.yaml, append two rule collections to COLLECTIONS — one for known CVEs and one for generic web-attack signatures:
    environment:
      COLLECTIONS: "crowdsecurity/linux crowdsecurity/sshd crowdsecurity/base-http-scenarios crowdsecurity/http-cve crowdsecurity/traefik crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules"
    1. Create the acquisition source /opt/crowdsec/data/config/acquis.d/appsec.yaml so CrowdSec listens on port 7422 inside the shared proxy Docker network (no need to publish it on the host via ports:):
    source: appsec
    listen_addr: 0.0.0.0:7422
    appsec_config: crowdsecurity/appsec-default
    labels:
      type: appsec

    Restart the stack: cd /opt/crowdsec && sudo docker compose up -d.

    1. In Traefik’s dynamic config /opt/traefik/data/dynamic/crowdsec.yml, enable AppSec on the crowdsec middleware:
    crowdsecAppsecEnabled: true
    crowdsecAppsecHost: crowdsec:7422
    crowdsecAppsecFailureBlock: true
    crowdsecAppsecUnreachableBlock: false
    • crowdsecAppsecFailureBlock: true — block the request if AppSec returns an error.
    • crowdsecAppsecUnreachableBlock: false — let traffic through if the crowdsec container is restarting or down. Setting true is stricter, but any CrowdSec outage takes down every site using this middleware.

    Traefik picks up edits under dynamic/ without a restart.

    What to weigh before turning it on

    • Every request hits crowdsec:7422. In regular stream mode, the plugin keeps bans in memory and polls every 15 s. With AppSec on, every HTTP request adds a local call over the Docker network — latency stays in milliseconds, but crowdsec container load rises; watch its memory if Compose caps it at mem_limit: 256M.
    • False positives. crowdsecurity/appsec-generic-rules can block normal POST requests carrying code snippets, SQL, or HTML (such as note editors and CMS admin panels). If a service handles that kind of input regularly, keep only crowdsecurity/appsec-virtual-patching — it targets specific CVEs with almost no false positives.
    • You still need log parsing. AppSec inspects single requests; slow brute force, 404 scanning, and SSH attacks are still caught by log-based scenarios and banned in nftables at the host level.

    How to check

    Confirm the AppSec component loaded its rules:

    docker exec crowdsec cscli metrics show appsec

    Send a test request matching a .env read signature to any service behind crowdsec@file:

    curl -i "https://whoami.example.org/.env"

    Traefik should return 403 Forbidden, and running cscli metrics show appsec again should show the hit in the blocked-requests counter.

    If it doesn’t work

    Enabled crowdsecAppsecEnabled: true in crowdsec.yml, yet requests to /.env still reach the app

    Cause
    The acquis.d/appsec.yaml source or AppSec collections are missing in CrowdSec, or the router doesn’t attach the crowdsec@file middleware. With crowdsecAppsecUnreachableBlock: false, the plugin lets traffic through whenever port 7422 is unreachable.
    Fix
    Run docker exec crowdsec cscli metrics show appsec (a rule table should appear) and check Traefik logs for connection errors to crowdsec:7422.

    Restarting the crowdsec container makes sites behind Traefik return 500 or 403

    Cause
    crowdsec.yml sets crowdsecAppsecUnreachableBlock: true: while CrowdSec restarts, port 7422 stops answering and the plugin blocks all traffic.
    Fix
    Set crowdsecAppsecUnreachableBlock: false so a CrowdSec restart doesn’t take sites offline.

    A legitimate user request (for instance, saving a note containing code or SQL) gets 403

    Cause
    A generic rule from crowdsecurity/appsec-generic-rules matched the request body or query parameters.
    Fix
    Check which rule fired via docker exec crowdsec cscli metrics show appsec and the CrowdSec logs, then disable that rule or keep only crowdsecurity/appsec-virtual-patching for known CVEs.