Shorewall, Ipsets, Fail2Ban and Recidive Jail v.2

This is my second post on the above subject.  I have now started using ipsets to handle blocking of ip addresses at my firewalls.  For the purposes of this post, the ipsets will be call F2BLIST for ipv4 and F2BLIST6 for ipv6 address.  In the shorewall init file, place the following code:

ipset create F2BLIST hash:ip timeout 300 -exist

This creates an ipset with a default timeout of 300 seconds, and won’t throw an error if it already exists (if you happen to restart shorewall).

Next add a rule to the shorewall rules file like:

?SECTION ALL

DROP:info net:+F2BLIST all

Next, create a new action file under /etc/fail2ban/action.d/ called shorewall-ipset.conf

[Definition]
actionstart =
actionstop =
actioncheck =
actionban = ipset add F2BLIST <ip> timeout <bantime> -exist
actionunban =
[Init]
blocktype = logdrop
bantime = 600

The last line sets the default ban time to 600 seconds.  Next, create a jail in /etc/fail2ban/jail.local like the following:

[ssh-shorewall]

enabled = true
filter = sshd
action = shorewall-ipset[bantime=3600]
sendmail-geoip-lines[name=SSH, dest=youremail, sender=anotheremail, logpath=/var/log/messages]
logpath = /var/log/messages
maxretry = 3

This jail sets the ban time to 3600 seconds, and uses the default findtime.  A recidive jail could look like the following:

[recidive]

enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
action = shorewall-ipset[bantime=172800]
sendmail-geoip-lines[name=recidive, logpath=/var/log/fail2ban.log]
findtime = 172800 ; 2 day
maxretry = 2

The beauty of the ipsets is that the kernel handles expiring the entry, so you don’t have to worry about fail2ban expirying an ip that is still supposed to be blocked in the recidive jail.  If you issue the following command:

ipset list

it will show the ipset and all the entries in it with the corresponding seconds left for each ip to expire.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.