RousselTMLEARNING

Proxy & networking

Traefik plugins: the example of CrowdSec to block attacks

Extend Traefik with plugins (official catalog, Yaegi interpreter), then protect your applications with CrowdSec: detecting malicious behavior, shared decisions and the blocking plugin (bouncer).

1. Traefik plugins #

Adding middlewares and providers.

How it works #

Traefik plugins are written in Go and run by the built-in Yaegi interpreter: no need to recompile Traefik. They are published in the Plugin Catalog and declared in the static configuration, then used like middlewares.

# configuration statique
experimental:
  plugins:
    crowdsec:
      moduleName: github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
      version: v1.3.5        # fixez une version testée
Common mistake: A plugin runs inside the Traefik process: choose maintained plugins, pin their version and test them before production.

2. CrowdSec #

Collaborative detection.

Architecture #

Traefik and CrowdSec
Blocking happens in Traefik, before the request reaches the application.
  1. Traefik writes its access logs to a shared file.
  2. The CrowdSec agent analyzes them with scenarios (brute force, scans, exploitation of known vulnerabilities) and sends alerts.
  3. The local API creates decisions (ban an IP for 4 h) and also receives the lists shared by the community.
  4. The bouncer plugin queries the local API and responds 403 to banned IPs.

3. Setup with Docker Compose #

The essential elements.

Configuration #

services:
  crowdsec:
    image: crowdsecurity/crowdsec
    environment:
      COLLECTIONS: "crowdsecurity/traefik crowdsecurity/http-cve"
    volumes:
      - ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
      - traefik-logs:/var/log/traefik:ro
  traefik:
    image: traefik:v3
    command:
      - --accesslog.filepath=/var/log/traefik/access.log
      - --experimental.plugins.crowdsec.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
      - --experimental.plugins.crowdsec.version=v1.3.5
    volumes: [traefik-logs:/var/log/traefik]
# middleware dynamique : crowdsecLapiKey = clé créée par « cscli bouncers add traefik »
docker compose exec crowdsec cscli bouncers add traefik   # clé pour le plugin
docker compose exec crowdsec cscli metrics                 # lignes lues, scénarios déclenchés
docker compose exec crowdsec cscli decisions list          # IP bloquées
docker compose exec crowdsec cscli decisions add --ip 203.0.113.7 --duration 1h   # test
Good practice: Add your own addresses (monitoring, offices) to a CrowdSec allow list so you never block yourself.

Related articles

Traefik: securing and transforming requests with middlewares (BasicAuth, ForwardAuth…)

Traefik middlewares modify requests and responses between the router and the service: BasicAuth and ForwardAu…

Traefik: providers (Docker, Kubernetes, Nomad, Consul, files, key-value stores)

Providers feed Traefik's dynamic configuration: service discovery for Docker, Swarm, Kubernetes, Nomad and Co…

Traefik as an Ingress Controller for Kubernetes (Helm, IngressRoute, Gateway API)

Install Traefik in Kubernetes with Helm and publish applications with Ingress, the IngressRoute and Middlewar…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!