David Ramsden
21Mar/16

Banning repeat offenders with fail2ban

More and more I see fail2ban banning the same hosts repeatedly. One way to tackle this could be to increase the ban time but you could also have fail2ban monitor itself to find "repeat offenders" and then ban them for an extended period of time.

Firstly, create a filter definition:

This will be used against the fail2ban log and will find any hosts that have been unbanned. We don't want to monitor hosts that have been banned because, er, they're already banned. We also want to ignore any log entries that are generated by the jail itself.

Next edit jail.local to add a new jail:

This jail will monitor the /var/log/fail2ban.log file and use the repeat-offender filter that was defined earlier. If 3 unban's are seen within 5 hours, the host will be banned for 48 hours. You could adjust the banaction to use the route action which may give some performance benefits on a very busy server.

Comments (3) Trackbacks (0)
  1. Here’s a fun 1 liner to find historical repeat offenders:
    grep -ho “Unban.*$” /var/log/fail2ban.log* | sort | uniq -c

  2. Thanks for the script. Unfortunately I could never get it to work. I believe it’s probably the failregex might be different – at least working with the version of fail2ban I am using (0.8.11). However, it looks like there’s already a precreated jail called recidive.conf that comes with fail2ban that seems to do the same thing.

  3. @durango99 – make sure you replace those fancy quotes with real quotes.


Leave a comment

No trackbacks yet.