tuxvador@blog:~/blog/crowdsec-slow-brute-force$

~/blog/crowdsec-slow-brute-force

The Brute Force That Stayed Under CrowdSec

4 min readLire en français →

#security#crowdsec#linux#self-hosting

My homelab dashboard has an authentication history page: every failed login on every machine, grouped by source IP, with a flag showing whether CrowdSec has banned it. The first time I opened it, the top line was an address with 167 failed SSH attempts on the public server in 24 hours, and no ban.

CrowdSec was working. This attacker was just slower than CrowdSec’s SSH scenarios expect.

Why the hub scenarios let it through

CrowdSec’s SSH scenarios are leaky buckets. Each failure pours one event into a bucket per IP, the bucket leaks at a fixed rate, and it overflows into a ban when it fills:

Scenario Capacity Leak What gets through
ssh-bf 5 one every 10 s anything slower than one try every ~10 s
ssh-slow-bf 10 one every 60 s anything slower than one try a minute
ssh-time-based-bf — — detects it, but ships with remediation: false: an alert, no ban

One attempt every 3.5 minutes never fills either bucket: each event has leaked out long before the next one arrives. The scenario that does catch it is deliberately alert-only.

The same applies to mail and web logins: a patient password sprayer stays under every rate-based threshold.

The rule I wanted

Simple to say, and the same for every service:

Ban any IP with more than 5 authentication failures in an hour, more than 10 in six hours, or more than 20 in a day.

Three sliding windows, counted across SSH, Postfix SASL, Dovecot, the webmail, the WorkItOut app’s login and my dashboard.

A conditional bucket

A leaky bucket can’t express “more than N in the last H hours” exactly. A conditional bucket can. It keeps every event in its queue, and on each new event it evaluates an expression over the queue:

type: conditional
name: local/auth-fail-ban
description: "Auth failures: >5 in 1h, >10 in 6h or >20 in 24h"
filter: >-
  evt.Meta.log_type in ['ssh_failed-auth', 'homelab_dash_failed_auth', 'roundcube_failed_auth']
  || (evt.Meta.log_type == 'http_access-log' && evt.Meta.http_verb == 'POST'
      && evt.Meta.http_path startsWith '/api/login' && evt.Meta.http_status == '401')
  || (evt.Meta.log_type == 'postfix' && evt.Parsed.message contains 'authentication failed')
  || (evt.Meta.log_type == 'dovecot_logs' && evt.Parsed.message contains 'auth failed')
distinct: "evt.Meta.log_type == 'ssh_failed-auth' ? 'ssh-' + evt.Parsed.pid : evt.Line.Raw"
groupby: evt.Meta.source_ip
capacity: -1
leakspeed: 24h
condition: |
  let newest = queue.Queue[-1].Time;
  len(filter(queue.Queue, { newest.Sub(#.Time) <= duration("1h") })) > 5 ||
  len(filter(queue.Queue, { newest.Sub(#.Time) <= duration("6h") })) > 10 ||
  len(filter(queue.Queue, { newest.Sub(#.Time) <= duration("24h") })) > 20
blackhole: 10m
labels:
  remediation: true

The parts that matter:

  • capacity: -1 and leakspeed: 24h keep a day of events per IP, so the condition sees the full 24-hour window.
  • The condition counts from the newest event, not from “now”, so replaying old logs gives the same answer as live traffic.
  • distinct fixes double counting. One failed SSH attempt writes two lines (“Invalid user” then “Connection closed by invalid user”) with the same sshd PID. Keyed on the PID, one attempt is one failure, which is also how the dashboard counts.
  • The ban itself comes from the central API’s default profile: seven days, enforced by every bouncer in the lab.

The web logins needed one local parser (webmail failures go to syslog in a format the hub doesn’t know). The WorkItOut app needed nothing new: a POST /api/login answered with 401 in nginx’s access log is already a parsed event.

Not banning myself

Before deploying anything that bans on failed logins, I needed to be sure my own addresses would never be banned. Allowlists on the central API apply to every agent’s alerts, on top of each agent’s parser whitelists:

cscli allowlists create own-ips -d "home, public server, tailnet, private"
cscli allowlists add own-ips 192.0.2.10 198.51.100.20 100.64.0.0/10 fd7a:115c:a1e0::/48 \
    10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 127.0.0.0/8 ::1

My commercial VPN’s exit addresses are deliberately not on it. Six mistyped logins from the laptop on that VPN will ban that exit for a week, and I’ve decided I can live with that.

The test that banned real addresses

I tested the thresholds offline by replaying synthetic log files with documentation-range IPs (TEST-NET, 203.0.113.0/24) at the exact boundaries: 5 in an hour, 10 in six hours, 20 in a day gave no ban, and 6, 11, 21 did. Twelve SSH lines from six attempts counted as six. Every case passed.

Then I listed the decisions on the central API, and the three test addresses were there, banned for seven days, everywhere.

crowdsec -dsn file://... -no-api only disables the agent’s local API. This agent reports to a remote one, so the replay’s alerts went to it, and the central API’s profile turned them into real bans. Because I had used documentation addresses, nothing real was affected. With real IPs from a production log, I would have banned whoever they belonged to.

cscli decisions delete --range 203.0.113.0/24 --contained

Backfill

The rule only sees events from the moment it’s loaded. A short script ran the same three windows over the previous 24 hours of logs and found ten addresses over a threshold. One was already banned, and the other nine got a manual seven-day ban, tagged with the scenario name so they’re easy to find. The slow brute forcer from the dashboard was one of them.

Pitfalls

  • Leaky buckets have a speed floor. Anything slower than the leak rate never overflows, however long it keeps going.
  • Check remediation on hub scenarios. “Detected” and “banned” are different settings.
  • Count attempts, not lines. One SSH attempt can write two or three lines. Dedupe on something per-attempt, like the PID.
  • Replay pushes alerts to a remote API. -no-api doesn’t stop it. Test with documentation-range IPs, and clean up afterwards.
  • Never put a # comment inside a multi-line filter:. YAML folds it into the expression, the scenario fails to compile, and CrowdSec refuses to start. It did, for about a minute, on the public server. Run crowdsec -t before every reload.
  • last is a reserved word in conditions. Call your variable something else.

cd ~/blog