Skip to content

Instantly share code, notes, and snippets.

@Vaarlion
Last active March 6, 2023 21:35
Show Gist options
  • Save Vaarlion/b6abb7fedf6b94d0cee3bfd4b7af212b to your computer and use it in GitHub Desktop.
Save Vaarlion/b6abb7fedf6b94d0cee3bfd4b7af212b to your computer and use it in GitHub Desktop.
Fail2ban action to build an apache 2 list of ip to block. See https://vaarlion.com/blog/how-to-use-fail2ban-when-there-is-a-proxy-in-the-way/
# Fail2Ban configuration file
#
# Author: Vaarlion
#
[Definition]
# Option: actionstart
# Notes.: command executed on demand at the first ban (or at the start of Fail2Ban if actionstart_on_demand is set to false).
# Values: CMD
#
actionstart = if [ ! -f <target> ]; then touch <target>; chgrp www-data <target>; chmod g+r <target>; printf %%b "<init>\n" <to_target>; fi;
echo "%(debug)s started"
# Option: actionflush
# Notes.: command executed once to flush (clear) all IPS, by shutdown (resp. by stop of the jail or this action)
# Values: CMD
#
actionflush = \> <target>; printf %%b "<init>\n" <to_target>;
echo "%(debug)s clear all"
# Option: actionstop
# Notes.: command executed at the stop of jail (or at the end of Fail2Ban)
# Values: CMD
#
actionstop = echo "%(debug)s stopped"
# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck = [ -f <target> ]
# 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 = if ! grep -q '<ip> -' <target>; then printf %%b "<ip> - # fail2ban/$( date '+%%Y-%%m-%%d %%T' )\n" <to_target>; fi
echo "%(debug)s banned <ip> (family: <family>)"
# 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 = if grep -q '<ip> -' <target>; then sed -i '/<ip> -/d' <target>; fi
echo "%(debug)s unbanned <ip> (family: <family>)"
debug = [<name>] <actname> <target> --
[Init]
init = ### Block map for Apache2 ###
target = /etc/apache2/banned-hosts
to_target = >> <target>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment