basforge

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

    RU

    Protection: brute-force bans and closed ports

    CrowdSec in a container spots SSH brute force in auth.log, and a bouncer on the host bans the address in nftables on every port, containers included. Then check from outside that only 22, 80 and 443 are open. The why is in the manuals, linked from every stage.

    Start froma server after level 1, “Base”: key login as example-user, ufw lets in SSH, 80 and 443, Docker installed with capped logs.

    What you get

    sshd ──▶ /var/log/auth.log ──▶ crowdsec (container) ── decision “ban 90d”
                                           │ LAPI on 127.0.0.1:8080
                                           ▼
             crowdsec-firewall-bouncer (host) ──▶ nftables: input and forward hooks
    /opt/crowdsec/    the stack: compose, data/config — sources and profiles, data/db — decisions
    outside           only 22, 80 and 443
    • You’ll needA phone on mobile data: it has a different address, so it’s where you test the ban from
    • You’ll neednmap on your computer to check ports from outside; on Windows, the installer from nmap.org

    Your values

    Filled into the commands and files of this and other recipes on the path; stored in this browser only.
    Server address
    203.0.113.10placeholder
    User
    example-userplaceholder

    Without the script the commands show placeholder values: replace them with yours.

    Steps

    The same recipe, short: commands from the stages, files as one line. For a second server and for those who already know what each command does.

    CrowdSec in a container

    Why — CrowdSec against SSH brute force
    Stack directory
    Command What it does
    sudo mkdir -p /opt/crowdsec the stack directory
    /opt/crowdsec/docker-compose.yamlCrowdSec with scenario sets for syslog and SSH; LAPI only on 127.0.0.1
    services:
      crowdsec:
        image: crowdsecurity/crowdsec:latest
        container_name: crowdsec
        restart: unless-stopped
        mem_limit: 256M
        memswap_limit: 384M
        environment:
          COLLECTIONS: "crowdsecurity/linux crowdsecurity/sshd"
        volumes:
          - ./data/config:/etc/crowdsec
          - ./data/db:/var/lib/crowdsec/data
          - /var/log:/var/log:ro
        ports:
          - "127.0.0.1:8080:8080"
    First start
    Command What it does
    cd /opt/crowdsec && sudo docker compose up -d starts it; the first time takes about a minute: it downloads scenarios and registers with CAPI
    sudo docker logs crowdsec 2>&1 | tail -5 the last lines of the log

    The first start created data/config. Put the source — auth.log — and the ban duration there.

    Sources directory
    Command What it does
    sudo mkdir -p /opt/crowdsec/data/config/acquis.d the log sources directory
    /opt/crowdsec/data/config/acquis.d/auth.yamlCrowdSec reads login attempts from auth.log
    filenames:
      - /var/log/auth.log
    labels:
      type: syslog
    /opt/crowdsec/data/config/profiles.yamla 90-day ban instead of 4 hours; replaces the image’s file entirely
    name: default_ip_remediation
    filters:
     - Alert.Remediation == true && Alert.GetScope() == "Ip"
    decisions:
     - type: ban
       duration: 90d
    on_success: break
    ---
    name: default_range_remediation
    filters:
     - Alert.Remediation == true && Alert.GetScope() == "Range"
    decisions:
     - type: ban
       duration: 90d
    on_success: break
    Restart
    Command What it does
    cd /opt/crowdsec && sudo docker compose restart CrowdSec rereads sources and profiles
    Done when
    Command What you should see
    sudo docker exec crowdsec cscli collections list crowdsecurity/sshd and crowdsecurity/linux in the list
    sudo docker exec crowdsec cscli metrics show acquisition a file:/var/log/auth.log line; numbers appear with the first login attempt
    Not like this — see “If it doesn’t work”, stage cases.

    Bouncer on the host

    Why — CrowdSec against SSH brute force

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

    Package
    Command What it does
    curl -s https://install.crowdsec.net | sudo sh adds the CrowdSec repository and signing key; installs nothing by itself
    sudo apt install -y crowdsec-firewall-bouncer-nftables the bouncer: pulls decisions from CrowdSec and bans in nftables
    Key
    Command What it does
    sudo sed -i "s#^api_key:.*#api_key: $(sudo docker exec crowdsec cscli bouncers add firewall-bouncer -o raw)#" /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml creates the bouncer key and writes it straight into the config: it never reaches the screen or the shell history
    sudo chmod 600 /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml only root can read the config with the key
    sudo systemctl restart crowdsec-firewall-bouncer the bouncer starts with the key
    Done when
    Command What you should see
    sudo docker exec crowdsec cscli bouncers list firewall-bouncer, with Last API pull seconds ago
    sudo nft list table ip crowdsec | grep hook two lines: hook input and hook forward
    Not like this — see “If it doesn’t work”, stage cases.

    Test the ban by doing it

    Why — CrowdSec against SSH brute force

    ifconfig.me shows the phone’s address on mobile data. Ban it for 2 minutes: if you got the address wrong, the ban lifts by itself (Test your server’s protection by action). Replace <phone-address> in the command with it.

    A 2-minute ban
    Command What it does
    sudo docker exec crowdsec cscli decisions add --ip <phone-address> --duration 2m --reason test bans the phone’s address; the bouncer picks up the decision within 10 s

    On the phone, open http://203.0.113.10 in Chrome. Without a ban it says ERR_CONNECTION_REFUSED right away: port 80 is open, but nothing listens on it yet. With the ban the page keeps loading with no answer: the packets are dropped.

    Done when
    Command What you should see
    sudo docker exec crowdsec cscli decisions list the phone’s address, test in reason
    sudo nft list table ip crowdsec | grep -c <phone-address> not zero: the address is in the bouncer’s table
    Not like this — see “If it doesn’t work”, stage cases.

    After 2 minutes the ban lifts by itself, and Chrome on the phone says ERR_CONNECTION_REFUSED right away again.

    Ports from outside

    Why — Docker ports bypass the input chain

    A port only the server needs listens on 127.0.0.1 (A host-only port goes on 127.0.0.1); reach it through an SSH tunnel (SSH tunnels). That’s why CrowdSec’s LAPI on 127.0.0.1:8080 isn’t visible from outside.

    On the server
    Command What it does
    sudo ss -tlnH | awk '{print $4}' | grep -v -e '^127\.' -e '^\[::1\]' what listens on addresses visible from outside
    sudo docker ps --format '{{.Names}}\t{{.Ports}}' which ports Docker published
    From your computer
    Command What it does
    nmap -Pn --top-ports 1000 203.0.113.10 what’s visible from outside: the 1000 most common ports
    Done when
    Command What you should see
    sudo ss -tlnH | awk '{print $4}' | grep -v -e '^127\.' -e '^\[::1\]' only :22
    sudo docker ps --format '{{.Names}}\t{{.Ports}}' crowdsec 127.0.0.1:8080->8080/tcp
    nmap -Pn --top-ports 1000 203.0.113.10 22/tcp open; 80/tcp and 443/tcp are closed: ufw lets them through, but nothing listens there yet; the rest are filtered
    Not like this — see “If it doesn’t work”, stage cases.

    If it doesn’t work

    Stage 1

    cscli metrics show acquisition shows no auth.log line

    Cause
    The file isn’t in the container: compose has no /var/log volume, or there’s no auth.log at all — minimal images without rsyslog keep logins only in the systemd journal.
    Fix
    sudo docker exec crowdsec ls -l /var/log/auth.log. Not on the host either — sudo apt install rsyslog and sudo docker compose restart.
    Stage 1

    The auth.log line is there, but the numbers are zeros

    Cause
    CrowdSec reads the file from the end: it doesn’t count old lines, and there have been no login attempts since the restart.
    Fix
    Wait a few minutes — bots knock on SSH all the time — or log in yourself from a new window.
    Stage 1

    Bind for 127.0.0.1:8080 failed: port is already allocated

    Cause
    Another service on the server already holds port 8080.
    Fix
    sudo ss -tlnp | grep 8080 shows which. Pick another port — 127.0.0.1:8081:8080 in compose and api_url: http://127.0.0.1:8081/ in the bouncer config.
    Stage 2

    cscli says firewall-bouncer already exists, and api_key in the config is empty

    Cause
    The key command ran a second time: the name is taken, so no new key was issued.
    Fix
    sudo docker exec crowdsec cscli bouncers delete firewall-bouncer, then the commands from the “Key” table again.
    Stage 2

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

    Cause
    api_key is empty or wrong: the package registers a key by itself only when CrowdSec runs on the host, and here it’s in a container.
    Fix
    The commands from the “Key” table; if the name is taken, see the case above.
    Stage 2

    nft list table ip crowdsec | grep hook shows only input

    Cause
    The bouncer config lacks forward in nftables_hooks: without it the ban doesn’t cover containers with published ports (Docker ports bypass the input chain).
    Fix
    In /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml set nftables_hooks to input and forward, then sudo systemctl restart crowdsec-firewall-bouncer.
    Stage 3

    The ban decision is there, but Chrome on the phone still says ERR_CONNECTION_REFUSED right away

    Cause
    The bouncer isn’t pulling decisions: cscli bouncers list shows an old Last API pull. Or the ban hit the wrong address: the phone has two, IPv4 and IPv6.
    Fix
    sudo systemctl status crowdsec-firewall-bouncer; check the address against ifconfig.me in the phone’s browser and ban that one.
    Stage 3

    You banned your own address: SSH from your computer says Connection timed out

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

    ss lists more addresses than :22

    Cause
    A service from the provider’s image listens on every address. ufw closes it from outside, but it’s still extra.
    Fix
    sudo ss -tlnp shows the process; not needed — sudo systemctl disable --now <service>, needed only by the server — set its address to 127.0.0.1.

    How to remove it

    sudo apt purge -y crowdsec-firewall-bouncer-nftables
    cd /opt/crowdsec && sudo docker compose down
    sudo rm -r /opt/crowdsec

    A stopped bouncer removes its tables: sudo nft list tables shows no crowdsec or crowdsec6. If the bouncer crashed instead of stopping (kill -9), the tables and their bans stay — delete them by hand: sudo nft delete table ip crowdsec, sudo nft delete table ip6 crowdsec6. The CrowdSec repository stays in apt — it installs nothing on its own.

    Next on the path