1
0

bash-fail2ban.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env bash
  2. # fail2ban action shim: post the banned IP to IRDB.
  3. #
  4. # Wire it up by dropping a fail2ban action file in /etc/fail2ban/action.d/:
  5. #
  6. # [Definition]
  7. # actionban = /usr/local/bin/irdb-fail2ban.sh <ip> brute_force '{"jail":"<name>"}'
  8. # actionunban = true
  9. #
  10. # [Init]
  11. # name = default
  12. #
  13. # And set IRDB_URL + IRDB_TOKEN in fail2ban's environment (typically
  14. # /etc/default/fail2ban or a systemd drop-in).
  15. #
  16. # This script is intentionally tiny — fail2ban actions execute often,
  17. # in restricted environments, and any sleep/retry logic belongs in a
  18. # higher layer.
  19. set -euo pipefail
  20. IP="${1:?ip required}"
  21. CATEGORY="${2:?category required}"
  22. METADATA="${3:-{\}}"
  23. : "${IRDB_URL:?must be set}"
  24. : "${IRDB_TOKEN:?must be set}"
  25. # Best-effort: 5 second timeout, no retries. fail2ban won't block on
  26. # the action; if IRDB is briefly unreachable we lose this report
  27. # rather than holding the ban.
  28. exec curl -fsS --max-time 5 -X POST \
  29. -H "Authorization: Bearer $IRDB_TOKEN" \
  30. -H "Content-Type: application/json" \
  31. -d "{\"ip\":\"$IP\",\"category\":\"$CATEGORY\",\"metadata\":$METADATA}" \
  32. "$IRDB_URL/api/v1/report" >/dev/null