Skip to content

Instantly share code, notes, and snippets.

@Belphemur
Last active October 10, 2022 18:29
Show Gist options
  • Save Belphemur/82d27b1b6dfd675d15f2 to your computer and use it in GitHub Desktop.
Save Belphemur/82d27b1b6dfd675d15f2 to your computer and use it in GitHub Desktop.
Tarpit Action for Fail2ban with rate limit
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
# Modified: Yaroslav O. Halchenko <[email protected]>
# made active on all ports from original iptables.conf
# Modified: Antoine Aflalo <[email protected]>
# Used the iptables-allports.conf as base for TARPIT.
#
#
[Definition]
# Option: actionstart
# Notes.: command executed once at the start of Fail2Ban.
# Values: CMD
#
actionstart = iptables -N fail2ban-<name>
iptables -A fail2ban-<name> -j RETURN
iptables -I <chain> -p <protocol> -j fail2ban-<name>
# set up TARPIT chain with rate limit
iptables -N TAR
iptables -A TAR -m limit --limit 10/sec -j TARPIT --tarpit -p tcp
iptables -A TAR -j DROP
# set up from the static file
cat /etc/fail2ban/ip.blocklist.<name> |grep -v ^\s*#|awk '{print $1}' | while read IP; do iptables -I fail2ban-<name> 1 -s $IP -j TAR; done
# Option: actionstop
# Notes.: command executed once at the end of Fail2Ban
# Values: CMD
#
actionstop = iptables -D <chain> -p <protocol> -j fail2ban-<name>
iptables -F fail2ban-<name>
iptables -X fail2ban-<name>
iptables -F TAR
iptables -X TAR
# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck = iptables -n -L <chain> | grep -q 'fail2ban-<name>[ \t]'
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionban = iptables -I fail2ban-<name> 1 -s <ip> -j TAR
# also put into the static file to re-populate after a restart
! grep -Fq <ip> /etc/fail2ban/ip.blocklist.<name> && echo "<ip> # fail2ban/$( date '+%%Y-%%m-%%d %%T' ): auto-add for repeat offender" >> /etc/fail2ban/ip.blocklist.<name>
# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
#actionunban = iptables -D fail2ban-<name> -s <ip> -j TAR
actionunban = /bin/true
[Init]
# Default name of the chain
#
name = default
# Option: protocol
# Notes.: internally used by config reader for interpolations.
# Values: [ tcp | udp | icmp | all ] Default: tcp
#
protocol = tcp
# Option: chain
# Notes specifies the iptables chain to which the fail2ban rules should be
# added
# Values: STRING Default: INPUT
chain = INPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment