Dazu braucht man natürlich fail2ban: HOWTO fail2ban on gentoo
fail2ban ist ein daemon der Logfiles auf regular expressions prüft und dementsprechend Aktionen ausführt.
Zu beachten ist hierbei, dass fail2ban extrem flexibel und erweiterbar ist, es lässt sich denkbar viel realisieren, da es übersichtlich und modular konfigurierbar ist.
Als erstes brauchen wir eine filter Regel, diese "Regeln" liegen als einzelne .conf Dateien unter /etc/fail2ban/filter.d und sind nach ihrem jeweiligen Zweck bezeichnet.
Wir erstellen unter filter.d/ eine Datei webmin.conf mit folgendem Inhalt:
Code: Select all
# Fail2Ban configuration file
[Definition]
# Option: failregex
# Notes.: regex to match the password failure messages in the logfile. The
# host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>\S+)
# Values: TEXT
#
failregex = <HOST>.*"POST /session_login\.cgi.*401
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =Das Datum in der Zeile der zu prüfenden Logdatei findet fail2ban selbst, ebenso das Format des Datums.
ignoreregex steht für eine regular expression bei deren Auftreten die jeweilige Zeile ignoriert werden soll, das kann zum Beispiel dazu dienen falsche Logins von einer bestimmten IP auf jedenfall nicht zu verfolgen.
Als nächstes brauchen wir eine neue Section in jail.conf:
Code: Select all
[webmin-iptables]
enabled = true
filter = webmin
action = iptables[name=webmin, port=10000, protocol=tcp]
logpath = /var/log/webmin/miniserv.log
maxretry = 4filter steht für den Namen des jeweiligen Filters und ergibt sich aus dem Namen der .conf datei in filter.d/
action ist die Aktion die getätigt werden soll, iptables[name=webmin: webmin ist hierbei der Name der Chain die in Iptables erstellt werden soll.
port=10000, wir wollen die Chain nur für den Webmin port gültig machen,
protocol=tcp, selbsterklärend.
logpath, selbsterklärend
maxretry, ebenso
unter action.d/iptables.conf lässt sich nochmal einiges verändern.
wobei man dazu sagen muss, dass die "Out of the Box" Konfiguration von iptables.conf vollkommen ausreichen sollte.
Code: Select all
[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 INPUT -p <protocol> --dport <port> -j fail2ban-<name>Code: Select all
# Option: actionend
# Notes.: command executed once at the end of Fail2Ban
# Values: CMD
#
actionstop = iptables -D INPUT -p <protocol> --dport <port> -j fail2ban-<name>
iptables -F fail2ban-<name>
iptables -X fail2ban-<name>Code: Select all
# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck = iptables -n -L INPUT | grep -q fail2ban-<name>Code: Select all
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: <ip> IP address
# <failures> number of failures
# <time> unix timestamp of the ban time
# Values: CMD
#
actionban = iptables -I fail2ban-<name> 1 -s <ip> -j DROPhier lässt sich durchaus noch feintuning betreiben, wenn man das möchte.
Code: Select all
# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: <ip> IP address# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: <ip> IP address
# <failures> number of failures
# <time> unix timestamp of the ban time
# Values: CMD
#
actionunban = iptables -D fail2ban-<name> -s <ip> -j DROPDamit sind wir fertig, nach einem:
Code: Select all
/etc/init.d/fail2ban restartwer möchte kann sich das Verhalten von fail2ban noch genauer ansehen indem er in der fail2ban.conf das LogLevel auf 1 (DEBUG) setzt, dann zeichnet fail2ban unter /var/log/fail2ban.log alles auf was es tut.
Optional lässt sich mit /usr/bin/fail2ban-regex jede Regular Expression auf Strings oder Files testen und analysieren.
Zum Beispiel können wir mit:
Code: Select all
fail2ban-regex /var/log/webmin/miniserv.log filter.d/webminCode: Select all
Running tests
=============
Use regex file : filter.d/webmin.conf
Use log file : /var/log/webmin/miniserv.log
Results
=======
Failregex:
[1] <HOST>.*"POST /session_login\.cgi.*401
Number of matches:
[1] 11 match(es)
Addresses found:
[1]
212.183.xx.xx (Fri Aug 31 06:58:21 2007)
212.183.xx.xx (Fri Aug 31 06:58:30 2007)
83.65.xx.xx (Mon Sep 03 06:27:42 2007)
83.65.xx.xx (Mon Sep 03 06:29:32 2007)
83.65.xx.xx (Mon Sep 03 06:29:54 2007)
83.65.xx.xx (Mon Sep 03 06:30:47 2007)
83.65.xx.xx (Mon Sep 03 06:39:20 2007)
83.65.xx.xx (Mon Sep 03 06:39:56 2007)
83.65.xx.xx (Mon Sep 03 07:17:31 2007)
83.65.xx.xx (Mon Sep 03 07:19:14 2007)
83.65.xx.xx (Mon Sep 03 07:19:39 2007)
Date template hits:
0 hit: Month Day Hour:Minute:Second
0 hit: Weekday Month Day Hour:Minute:Second Year
0 hit: Weekday Month Day Hour:Minute:Second
0 hit: Year/Month/Day Hour:Minute:Second
11 hit: Day/Month/Year:Hour:Minute:Second
0 hit: Year-Month-Day Hour:Minute:Second
0 hit: TAI64N
0 hit: Epoch
Success, the total number of match is 11
However, look at the above section 'Running tests' which could contain important
information.Hier wird jeder Eintrag des File berücksichtigt auf den die Regular Expression in unserer webmin.conf zutrifft.
ACHTUNG: bei einem Neustart von fail2ban wird jeder zur Zeit gebannte Host gelöscht und ist somit nach dem Neustart nicht mehr ausgesperrt, fail2ban ist damit nach dem Neustart "frisch".
ANMERKUNG: Dieses Howto basiert auf Version v0.8.0 von fail2ban und ist nur ein Beispiel, bitte nicht per Copy&Paste alle Einstellungen übernehmen, da damit die einwandfreie und gewünschte Funktion nicht gewährleistet ist.
Ich hoffe es findet jemand Nutzen an meinem ersten Beitrag in diesem, im übrigen sehr hilfreichen, Forum.


