Skip to content

sshd — alert on auth-fail spike (before fail2ban bans)

fail2ban bans sources by threshold — but a campaign hits from thousands of IPs at 1 attempt each. None get banned individually, but overall noise on the ssh port is huge.

Recipe

bash
#!/usr/bin/env bash
# /etc/cron.d/sshd-spike
# */5 * * * * root /opt/sshd-spike.sh

WINDOW=${WINDOW:-5min}
THRESH=${THRESH:-100}                 # fails / 5 min

COUNT=$(journalctl -u ssh --since "$WINDOW ago" --no-pager 2>/dev/null \
  | grep -cE 'Failed password|Invalid user|maximum authentication attempts')

if [ "${COUNT:-0}" -gt "$THRESH" ]; then
  UNIQUE=$(journalctl -u ssh --since "$WINDOW ago" --no-pager 2>/dev/null \
    | grep -oE 'from [0-9.]+' | sort -u | wc -l)
  curl -fsS "$HEARTBEAT_URL" --data "ssh_fails=$COUNT,unique_ips=$UNIQUE"
  exit 2
fi
echo "OK ($COUNT auth-fails / 5m)"

Same thing in Enterno.io

Wrap in an Enterno heartbeat — see a distributed brute-force campaign before it lucks into one weak ssh account.

Set up Heartbeat monitor → ← All recipes

Related recipes