fail2ban Logfile sortieren

Fail2Ban Logfile sortieren

Fail2ban ist wie ich finde ein beeindruckendes Tool um seinen Server gegen böse Jungs ein wenig sicherer zu machen.
Man kann seine Eigenen Patterns schreiben und so gut wie jedes Logfile live danach durchkämmen lassen.

Mit diesem kleinen Bash Schnippel kann man sich auch noch ganz einfach die bösen Jungs gezählt und sortiert anschauen.

Ein einfacher cat auf das Logfile ergibt dies.

[stextbox id=“info“]2014-08-08 21:09:44,464 fail2ban.actions: WARNING [apache] Ban 188.193.123.145
2014-08-08 21:13:13,728 fail2ban.actions: WARNING [apache] Ban 218.6.2.195
2014-08-08 21:13:36,778 fail2ban.actions: WARNING [apache] Ban 218.6.2.177
2014-08-08 21:14:48,882 fail2ban.actions: WARNING [apache] Ban 175.42.95.51
2014-08-08 21:18:20,136 fail2ban.actions: WARNING [apache] Ban 120.43.9.73
2014-08-08 21:19:45,260 fail2ban.actions: WARNING [apache] Unban 188.193.123.145
2014-08-08 21:21:39,436 fail2ban.actions: WARNING [apache] Ban 175.44.52.236
2014-08-08 21:23:08,556 fail2ban.actions: WARNING [apache] Ban 175.42.95.6
2014-08-08 21:23:14,593 fail2ban.actions: WARNING [apache] Unban 218.6.2.195
2014-08-08 21:23:19,632 fail2ban.actions: WARNING [apache] Ban 218.6.2.195
2014-08-08 21:23:37,676 fail2ban.actions: WARNING [apache] Unban 218.6.2.177
2014-08-08 21:24:09,732 fail2ban.actions: WARNING [apache] Ban 218.6.2.177
2014-08-08 21:24:49,800 fail2ban.actions: WARNING [apache] Unban 175.42.95.51
2014-08-08 21:28:21,060 fail2ban.actions: WARNING [apache] Unban 120.43.9.73
2014-08-08 21:29:19,184 fail2ban.actions: WARNING [apache] Ban 175.42.95.51
2014-08-08 21:31:40,406 fail2ban.actions: WARNING [apache] Unban 175.44.52.236
2014-08-08 21:33:09,533 fail2ban.actions: WARNING [apache] Unban 175.42.95.6
2014-08-08 21:33:20,569 fail2ban.actions: WARNING [apache] Unban 218.6.2.195
2014-08-08 21:33:27,602 fail2ban.actions: WARNING [apache] Ban 218.6.2.195
2014-08-08 21:34:10,676 fail2ban.actions: WARNING [apache] Unban 218.6.2.177
2014-08-08 21:34:31,725 fail2ban.actions: WARNING [apache] Ban 218.6.2.177
2014-08-08 21:36:33,891 fail2ban.actions: WARNING [apache] Ban 120.43.9.73
2014-08-08 21:39:20,092 fail2ban.actions: WARNING [apache] Unban 175.42.95.51
2014-08-08 21:41:45,312 fail2ban.actions: WARNING [apache] Ban 175.42.95.6
2014-08-08 21:41:55,352 fail2ban.actions: WARNING [apache] Ban 175.44.52.236
2014-08-08 21:43:28,483 fail2ban.actions: WARNING [apache] Unban 218.6.2.195
2014-08-08 21:43:36,524 fail2ban.actions: WARNING [apache] Ban 218.6.2.195
2014-08-08 21:44:32,614 fail2ban.actions: WARNING [apache] Unban 218.6.2.177
2014-08-08 21:45:09,909 fail2ban.actions: WARNING [apache] Ban 218.6.2.177
2014-08-08 21:45:11,936 fail2ban.actions: WARNING [apache] Ban 120.37.240.99
2014-08-08 21:46:34,052 fail2ban.actions: WARNING [apache] Unban 120.43.9.73
2014-08-08 21:51:45,438 fail2ban.actions: WARNING [apache] Unban 175.42.95.6
2014-08-08 21:51:49,490 fail2ban.actions: WARNING [apache] Ban 110.89.9.116
2014-08-08 21:51:55,520 fail2ban.actions: WARNING [apache] Unban 175.44.52.236
2014-08-08 21:53:15,631 fail2ban.actions: WARNING [apache] Ban 120.43.9.73
2014-08-08 21:53:36,680 fail2ban.actions: WARNING [apache] Unban 218.6.2.195
2014-08-08 21:53:43,716 fail2ban.actions: WARNING [apache] Ban 218.6.2.195
2014-08-08 21:55:10,861 fail2ban.actions: WARNING [apache] Unban 218.6.2.177
2014-08-08 21:55:12,890 fail2ban.actions: WARNING [apache] Unban 120.37.240.99
2014-08-08 21:55:44,952 fail2ban.actions: WARNING [apache] Ban 218.6.2.177
2014-08-08 21:58:25,166 fail2ban.actions: WARNING [apache] Ban 120.37.240.99
2014-08-08 22:00:18,318 fail2ban.actions: WARNING [apache] Ban 175.44.52.236
2014-08-08 22:01:18,411 fail2ban.actions: WARNING [apache] Ban 175.42.95.6
2014-08-08 22:01:50,472 fail2ban.actions: WARNING [apache] Unban 110.89.9.116[/stextbox]

Da das natürlich nicht ganz einfach zu lesen ist wenn man jetzt wissen will welche IP wie oft geblockt wurde.

Mit diesem einfachen Befehl bekommt man das ganze schon etwas leserlich ausgegeben.
[stextbox id=“bash“]grep Ban /var/log/fail2ban.log | awk ‚{print $NF}‘ |sort | uniq -c | sort -nr[/stextbox]

[stextbox id=“info“]
44 218.6.2.177
32 175.42.95.6
23 120.37.240.99
21 218.6.2.195
20 175.44.52.236
19 120.43.9.73
17 175.42.95.131
14 175.44.25.120
11 175.42.95.51
6 27.153.207.204
5 110.89.10.115
4 110.89.9.116
3 222.77.225.206
3 117.26.76.69
2 59.58.158.228
2 27.153.138.90
2 27.150.246.11
2 188.193.123.145
2 121.205.247.49
2 120.37.230.82
2 110.89.8.124
1 36.248.170.93
1 27.153.203.187
1 27.153.161.68
1 27.150.228.25
1 222.77.225.98
1 222.77.205.246
1 121.205.246.7
1 110.89.9.12
1 110.89.41.229
1 110.82.132.44[/stextbox]
Die erste Zahl ist wie oft die IP vorkommt, und der Rest sollte selbsterklärend sein.

Eine kurze Erläuterung zu den einzelnen Befehlen:

[stextbox id=“black“]grep Ban /var/log/fail2ban.log[/stextbox]
Mit grep gibt man nur die Zeilen aus, die den gesuchten String oder regex enthalten.

Man kann das natürlich auch mit cat machen.
[stextbox id=“black“]cat /var/log/fail2ban.log | grep Ban[/stextbox]
Das Ergebnis ist das gleiche.
Manche Admins regen sich bei so was auf und erzählen einem was vom „useless use of cat award“.
Bei manchen Cat´s würde ich ihnen auch recht geben. Aber ich persönlich finde diese Variante besser da man

  • den cat schnell durch ein tail austauschen kann.
  • und beim grep auch schnell die Parameter ändern kann.

Als einzige Argument das ich hier zählen lasse ist die Zeit die die Befehle zum ausführen brauchen.

GREP  vs CAT
[stextbox id=“bash“]time grep Ban /var/log/fail2ban.log | awk ‚{print $7}‘ |sort | uniq -c | sort -nr[/stextbox]
[stextbox id=“info“]real    0m0.012s
user    0m0.008s
sys     0m0.020s[/stextbox]

[stextbox id=“bash“]time cat /var/log/fail2ban.log | grep Ban |awk ‚{print $7}‘ |sort | uniq -c | sort -nr[/stextbox]
[stextbox id=“info“]real    0m0.016s
user    0m0.008s
sys     0m0.008s[/stextbox]

Wow ganze 0.04 Sekunden langsamer. Naja

Zurück zum wesentlichen.
[stextbox id=“black“]awk ‚{print $NF}'[/stextbox]
Mit Awk können wir uns einfach Teile einer Ausgabe aus einem Text herausfiltern.
In diesem Fall nutzen wir NF was die letzte Stelle ausgibt.
Man kann auch zb. $7 angeben was dann der siebten Stelle entsprechen würde.
Das Standard-Trennzeichen von awk ist das Leerzeichen, was aus diesem Grund nicht mit angegeben werden muss.

[stextbox id=“black“]sort[/stextbox]
Mit dem sort Befehl sortiert man die aktuelle Ausgabe.
Also gleiche Vorkommnisse werden untereinander geschrieben.

[stextbox id=“black“]uniq -c[/stextbox]
Mit uniq werden Doppelte Einträge entfernt.
Und der Parameter -c schreibt gleichzeitig die Anzahl der Vorkommnisse als Prefix vor jede Zeile.

[stextbox id=“black“]sort -nr[/stextbox]
Jetzt nochmal sortieren, aber diesmal nach Nummern und rekursiv.
So steht das was am öftesten vorkommt ganz oben.

Das war es im Großen und ganzen schon.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

*