Skip to main content
KnowHow DB
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Reverse proxy & WAF

A reverse proxy can be used to forward requests to the appropriate backend service like shown in this diagram:

reverse proxy diagram

The same could’ve been achieved by using two different ports, but we want the services to be easily accessible without specifying a port.

Docker

Traefik is an easy to use reverse-proxy with an docker image that is also supported by arm devices (e.g. raspberry pi).

Setup

  1. Create a network
docker network create server
  1. Create an empty acme.json file and set permissions
touch acme.json
chmod 600 acme.json
  1. Create configuration file traefik.yaml`
entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: "websecure"
          scheme: "https"

  websecure:
    address: ":443"
    forwardedHeaders:
      trustedIPs:
        - "172.16.0.0/12"
    http:
      tls:
        certResolver: "lets-encrypt"
      middlewares:
        - "crowdsec@file"

api:
  dashboard: true

certificatesResolvers:
  lets-encrypt:
    acme:
      email: "your@email.com"
      storage: "acme.json"
      tlsChallenge: {}

providers:
  docker:
    watch: true
    network: "server"
    exposedByDefault: false
  file:
    filename: "traefik_dynamic.yaml"

log:
  level: "ERROR"

accessLog:
  filePath: "/var/log/traefik/access.log"
  format: json

experimental:
  plugins:
    bouncer:
      moduleName: "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
      version: "v1.8.1"
  1. Create configuration file traefik_dynamic.yaml (replacing redacted htpasswd)
http:
  middlewares:
    authelia: #only required if authelia sso is used
      forwardAuth:
        address: "http://authelia:9091/api/authz/forward-auth"
        trustForwardHeader: true
        maxResponseBodySize: 8192
        authResponseHeaders:
          - 'Remote-User'
          - 'Remote-Groups'
          - 'Remote-Email'
          - 'Remote-Name'

    crowdsec:
      plugin:
        bouncer:
          enabled: true
          logLevel: ERROR
          crowdsecMode: stream
          updateIntervalSeconds: 10
          crowdsecLapiScheme: http
          crowdsecLapiHost: crowdsec:8080
          crowdsecLapiKey: '{{ env "BOUNCER_KEY_TRAEFIK" }}'
          crowdsecAppsecEnabled: true
          crowdsecAppsecHost: crowdsec:7422
          forwardedHeadersTrustedIPs:
            - "172.16.0.0/12"

  routers:
    dashboard:
      rule: 'Host(`traefik.{{ env "DOMAIN" }}`)'
      entrypoints:
        - "websecure"
      middlewares:
        - "authelia@file"
      service: "api@internal"
  1. Create waf/acquis.d/appsec.yaml file:
appsec_config: crowdsecurity/appsec-default
labels:
  type: appsec
listen_addr: 0.0.0.0:7422
source: appsec
  1. Create waf/acquis.d/traefik.yaml file:
filenames:
  - /var/log/traefik/access.log
poll_without_inotify: true
labels:
  type: traefik
  1. Create docker-compose.yml file:
services:
  traefik:
    image: 'traefik:3.7.16'
    container_name: 'traefik'
    restart: 'unless-stopped'
    depends_on: [crowdsec]
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock:ro'
      - './traefik.yaml:/traefik.yaml'
      - './traefik_dynamic.yaml:/traefik_dynamic.yaml'
      - './acme.json:/acme.json'
      - "./logs:/var/log/traefik"
    networks: ["server"]
    environment:
      DOMAIN: "${DOMAIN}"
      BOUNCER_KEY_TRAEFIK: "${BOUNCER_KEY_TRAEFIK}"

  crowdsec:
    image: 'crowdsecurity/crowdsec:v1.8.1'
    restart: 'unless-stopped'
    environment:
      COLLECTIONS: "crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules crowdsecurity/traefik crowdsecurity/http-cve crowdsecurity/http-dos"
      BOUNCER_KEY_traefik: "${BOUNCER_KEY_TRAEFIK}"
    volumes:
      - "./waf/acquis.d:/etc/crowdsec/acquis.d"
      - "./logs:/var/log/traefik"
      - crowdsec-data:/var/lib/crowdsec/data/
      - crowdsec-config:/etc/crowdsec/
    networks: ["server"]

networks:
  server:
    external: true
volumes:
  crowdsec-config:
  crowdsec-data:

Configure docker-compose webservice

Each webservice that wants to use the reverse proxy must use the same network (here “server”) and set these labels:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
services:
    webservice1:
        ...
        labels:
         - "traefik.enable=true"
         - "traefik.http.routers.webservice1.rule=Host(`webservice1.domain.org`)"

networks:
    server:
        external: true