basforge

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

    RU

    CrowdSec against SSH brute force: a ban across the whole server

    CrowdSec in a container reads /var/log/auth.log, recognizes password and key guessing and decides on a ban, and crowdsec-firewall-bouncer on the host drops packets from that address in nftables — on every port, sites in Docker included. The setup comes from production servers; Traefik and Cloudflare aren’t needed here — with them, see CrowdSec with Traefik behind Cloudflare.

    How it works

    sshd ── login attempts ──→ /var/log/auth.log (rsyslog)
                                    │ /var/log volume, read-only
                                    ▼
                         crowdsec (container)  crowdsecurity/sshd scenarios: password guessing, user enumeration,
                                    │          slow brute force → decision "ban 90d"
                                    │ LAPI on 127.0.0.1:8080
                                    ▼
                    crowdsec-firewall-bouncer (host)  pulls decisions every 10 s
                                    │
                                    ▼
                         nftables: crowdsec and crowdsec6 tables, input and forward hooks
                         packets from the address are dropped on every port

    Detection and enforcement are separate. CrowdSec only decides whom to ban — it blocks nothing itself. The bouncer blocks. That way one CrowdSec can feed both the firewall and a Traefik plugin (CrowdSec with Traefik behind Cloudflare).

    Besides its own decisions, CrowdSec gets the community blocklist (CAPI): addresses that attacked other servers running CrowdSec. The bouncer bans those too — before they knock on yours.

    Other ways to stop brute force

    With How it works When to use
    CrowdSec + firewall-bouncer scenarios over logs, a ban in nftables on every port, the community blocklist an internet-facing server with sites and SSH — this manual’s setup
    fail2ban regexes over logs, bans through iptables, nftables or ufw a single server without Docker; a familiar tool, no external service
    PerSourcePenalties in sshd sshd itself pushes back an address that fails to log in Ubuntu 26.04 (OpenSSH 10.2): on by default, SSH only
    ufw limit at most 6 connections per 30 s from an address nothing to install; gets in the way of your own scripts (The ufw firewall)

    With key-only login (sshd_config) guessing is useless anyway. The ban is for something else: an address that probes SSH usually goes on to scan the sites, and the community blocklist cuts it off in advance.

    Setup

    Docker is already on the server (Docker on Ubuntu).

    1. The CrowdSec stack

      /opt/crowdsec/docker-compose.yaml
      services:
        crowdsec:
          image: crowdsecurity/crowdsec:latest
          container_name: crowdsec
          restart: unless-stopped
          mem_limit: 256M
          memswap_limit: 384M

      mem_limit caps the container’s memory, memswap_limit caps memory plus swap. Without a cap, big logs and many scenarios make CrowdSec eat hundreds of megabytes, which shows on a 1–2 GB VPS.

          environment:
            COLLECTIONS: "crowdsecurity/linux crowdsecurity/sshd"

      Which scenario bundles to download at start: linux parses syslog, sshd brings the SSH brute force scenarios.

          volumes:
            - ./data/config:/etc/crowdsec
            - ./data/db:/var/lib/crowdsec/data
            - /var/log:/var/log:ro
      Key part

      Everything CrowdSec writes lives in the stack’s data/: config holds the config and the LAPI and CAPI keys, db the decision database. CrowdSec reads the host’s /var/log but can’t change it (:ro). Only your own files from data/ go into git — sources and profiles (Server configs in git).

          ports:
            - "127.0.0.1:8080:8080"

      LAPI on 127.0.0.1:8080: the bouncer on the host talks to it, and there’s no port from outside (A host-only port goes on 127.0.0.1).

    2. The source: auth.log

      The image puts a placeholder with the path /does/not/exist into acquis.yaml. Real sources go in separate files in acquis.d/.

      /opt/crowdsec/data/config/acquis.d/auth.yaml
      filenames:
        - /var/log/auth.log
      labels:
        type: syslog

      Which file to read and with which parser: type: syslog means syslog lines, and the sshd scenarios take login attempts from them.

      The data/config directory appears after the first start, so add this file after step 4 and restart the container.

    3. Ban duration

      /opt/crowdsec/data/config/profiles.yaml
      name: default_ip_remediation
      filters:
       - Alert.Remediation == true && Alert.GetScope() == "Ip"
      decisions:
       - type: ban
         duration: 90d
      on_success: break
      Key part

      Ban an address for 90 days instead of the default 4 hours: an address that was guessing SSH won’t become harmless in a week. The duration is written into the decision when it’s created, so an edit affects only new bans.

      ---
      name: default_range_remediation
      filters:
       - Alert.Remediation == true && Alert.GetScope() == "Range"
      decisions:
       - type: ban
         duration: 90d
      on_success: break

      The second profile does the same for subnets, when a decision covers an address range.

      The file replaces the image’s profiles.yaml entirely. If your own address gets banned, remove the ban by hand.

    4. Start it

      cd /opt/crowdsec && docker compose up -d
      docker logs crowdsec 2>&1 | tail -5

      The first start takes about a minute: CrowdSec downloads the collections and registers with CAPI.

    5. Install the bouncer on the host

      The package comes from CrowdSec’s repository. The script adds the repository and the signing key and installs nothing itself:

      curl -s https://install.crowdsec.net | sudo sh
      sudo apt install crowdsec-firewall-bouncer-nftables

      The commands follow the CrowdSec documentation as of 2026-10-18; on production servers the bouncer was installed earlier, and the steps weren’t repeated from scratch.

      The bouncer runs on the host, not in a container: it needs access to the host’s nftables.

    6. Give the bouncer a key

      docker exec crowdsec cscli bouncers add firewall-bouncer
      # API key for 'firewall-bouncer':
      #
      #    Kx9…
      #
      # Please keep this key since you will not be able to retrieve it!

      The key is shown once. Put it in the bouncer’s config without leaving it in shell history: open the file in an editor instead of using echo on the command line.

      sudo nano /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml
      mode: nftables
      api_url: http://127.0.0.1:8080/
      api_key: Kx9…
      update_frequency: 10s
      sudo chmod 600 /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml
      sudo systemctl restart crowdsec-firewall-bouncer
    7. Check the hooks

      sudo nft list table ip crowdsec | grep hook
      # type filter hook input priority filter - 10; policy accept;
      # type filter hook forward priority filter - 10; policy accept;

      Both hooks, input and forward. Without forward a ban doesn’t cover containers with published ports (Docker ports bypass the input chain). If the forward hook is missing, look at nftables_hooks in the same yaml. The config shipped with the package lists input and forward; if the line is missing altogether, the bouncer uses only input. The bouncer’s documentation doesn’t mention this setting — only its config and code do.

    If it doesn’t work

    cscli metrics shows no lines from auth.log

    Cause
    The container can’t see the file: no /var/log:/var/log:ro volume, or the source isn’t in acquis.d/. Sometimes there’s no auth.log at all — on a server without rsyslog, login events exist only in the systemd journal.
    Fix
    docker exec crowdsec ls -l /var/log/auth.log — the file must be there. Missing on the host — sudo apt install rsyslog: the regular Ubuntu image has it, minimal images and some hosting providers don’t.

    The bouncer won’t start; journalctl -u crowdsec-firewall-bouncer shows a key error or 403

    Cause
    api_key in crowdsec-firewall-bouncer.yaml is empty or wrong. The package registers a key itself only when CrowdSec runs on the host; with CrowdSec in a container, write the key in by hand.
    Fix
    docker exec crowdsec cscli bouncers add firewall-bouncer, put the key into api_key, then sudo systemctl restart crowdsec-firewall-bouncer.

    Banned your own address; SSH gives Connection timed out

    Cause
    Several failed logins in a row: the wrong key, or ssh went through every key in the agent.
    Fix
    From your phone on mobile data or through the provider console: docker exec crowdsec cscli decisions delete --ip <address>. For next time — IdentitiesOnly yes (~/.ssh/config).

    Ban decisions exist, but the address still connects

    Cause
    The bouncer doesn’t pull decisions (cscli bouncers list shows an old Last API pull), or it sits only on the input hook while the address reaches a container through forward.
    Fix
    sudo systemctl status crowdsec-firewall-bouncer and sudo nft list table ip crowdsec | grep hook — you need both hooks (Docker ports bypass the input chain).

    The CrowdSec container eats hundreds of megabytes and keeps growing

    Cause
    Without a limit the container’s memory is unbounded; with big logs and many scenarios that shows on a 1–2 GB VPS.
    Fix
    mem_limit: 256M in compose — that’s what production servers use.

    How to check

    CrowdSec reads auth.log and parses the lines:

    docker exec crowdsec cscli metrics show acquisition
    # │ file:/var/log/auth.log │ 1.24k │ 1.24k │ - │ - │

    The numbers in the first two columns are lines read and parsed. Zeros mean the source doesn’t work.

    The bouncer is connected:

    docker exec crowdsec cscli bouncers list
    # Name              IP Address  …  Last API pull         Type
    # firewall-bouncer  172.17.0.1  …  2026-10-18T19:41:02Z  crowdsec-firewall-bouncer

    Last API pull should be seconds ago.

    Test the ban by action, from a phone on mobile data. Its address shows on ifconfig.me. Ban it for 2 minutes: if you got the address wrong, the ban lifts itself.

    docker exec crowdsec cscli decisions add --ip <phone address> --duration 2m --reason test
    # from the phone: SSH and the sites on the server time out
    docker exec crowdsec cscli decisions delete --ip <phone address>

    Who’s banned already: docker exec crowdsec cscli decisions list; the other commands — cscli and bouncers.