-
-
Save Vaarlion/c5aefaf283fd9a9fa3dd8ae90660ec8d to your computer and use it in GitHub Desktop.
Fail2ban action to build an Nginx geo of ip to block. See https://vaarlion.com/blog/how-to-use-fail2ban-when-there-is-a-proxy-in-the-way/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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; | |
systemctl reload nginx | |
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>; | |
systemctl reload nginx | |
echo "%(debug)s clear all" | |
# Option: actionstop | |
# Notes.: command executed at the stop of jail (or at the end of Fail2Ban) | |
# Values: CMD | |
# | |
actionstop = systemctl reload nginx | |
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> 1' <target>; then sed -i '/^ default 0;/a\ \ <ip> 1; # fail2ban\/$( date "+%%Y-%%m-%%d %%T" )' <target>; fi | |
systemctl reload nginx | |
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> 1' <target>; then sed -i '/ <ip> 1/d' <target>; fi | |
systemctl reload nginx | |
echo "%(debug)s unbanned <ip> (family: <family>)" | |
debug = [<name>] <actname> <target> -- | |
[Init] | |
init = geo \$bad_user {\n default 0;\n} | |
target = /etc/nginx/conf.d/banned-hosts.conf | |
to_target = >> <target> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment