昨今は北朝鮮のミサイル実験がらみで、
一方、
Fail2banについて
インターネット上でメールやWebのサーバを運用している人は、
Aug 20 10:42:23 nsv sshd[29206]: connect from 59.63.188.36 (59.63.188.36)
Aug 20 10:42:23 nsv sshd[29208]: connect from 59.63.188.36 (59.63.188.36)
Aug 20 10:43:18 nsv sshd[29210]: connect from 59.63.188.36 (59.63.188.36)
Aug 20 10:43:20 nsv sshd[29212]: connect from 59.63.188.36 (59.63.188.36)
...
ユーザの認証履歴を残しているファイルにはこのような記録も残っていました。
Aug 20 10:42:25 nsv sshd[29206]: pam_unix(sshd:auth): authentication failure;
logname= uid=0 euid=0 tty=ssh ruser= rhost=59.63.188.36 user=root
Aug 20 10:42:25 nsv sshd[29206]: Failed password for root from 59.63.188.36 port 28643 ssh2
Aug 20 10:42:26 nsv sshd[29206]: last message repeated 2 times
...
これはいわゆる
サーバの動作が重いなぁ…、
"Fail2ban"はそのような問題を解決するために開発されたソフトウェアで、
インターネット上でもっとも広く行われているのが、
このような攻撃に対し、
LinuxカーネルのNetfilter機能は、
そのようなNetfilter機能を操作するためのiptablesコマンドには、
"Fail2ban"はそのような問題を補うように設計されており、

"Fail2ban"はwww.
Fail2banの仕組み
Fail2banの実体はPythonで書かれたスクリプトで、
Fail2banの設定ファイルには、
jail.{conf,local}では、
たとえば、
[ssh-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
sendmail-whois[name=SSH, [email protected], [email protected], sendername="Fail2Ban"]
logpath = /var/log/secure
maxretry = 5
この設定は、
filter行に指定している各種フィルタは/etc/
# Fail2Ban filter for openssh
#
...
[Definition]
_daemon = sshd
failregex = ^%(__prefix_line)s(?:error: PAM: )?[aA]uthentication (?:failure|error) for .* from <HOST>( via \S+)?\s*$
^%(__prefix_line)s(?:error: PAM: )?User not known to the underlying authentication module for .* from <HOST>\s*$
^%(__prefix_line)sFailed \S+ for .*? from <HOST>(?: port \d*)?(?: ssh\d*)?(: (ruser .*|(\S+ ID \S+ \(serial \d+\) CA )?\S+ %(__md5hex)s(, client user ".*", client host ".*")?))?\s*$
^%(__prefix_line)sROOT LOGIN REFUSED.* FROM <HOST>\s*$
^%(__prefix_line)s[iI](?:llegal|nvalid) user .* from <HOST>\s*$
^%(__prefix_line)sUser .+ from <HOST> not allowed because not listed in AllowUsers\s*$
^%(__prefix_line)sUser .+ from <HOST> not allowed because listed in DenyUsers\s*$
...
このファイルのfailregex行に指定されているのが、
筆者が"Fail2ban"に感心したのはこのフィルタ用ファイルの豊富さで、
これらのフィルタにより総当たり攻撃が検出されるとjail.
今回の例でactionとして指定したのはiptablesとsendmail-whoisで、
前者の設定は/etc/
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
...
# Option: actionstart
# Notes.: command executed once at the start of Fail2Ban.
# Values: CMD
#
actionstart = <iptables> -N f2b-<name>
<iptables> -A f2b-<name> -j <returntype>
<iptables> -I <chain> -p <protocol> --dport <port> -j f2b-<name>
# Option: actionstop
# Notes.: command executed once at the end of Fail2Ban
# Values: CMD
#
actionstop = <iptables> -D <chain> -p <protocol> --dport <port> -j f2b-<name>
<iptables> -F f2b-<name>
<iptables> -X f2b-<name>
...
# 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 f2b-<name> 1 -s <ip> -j <blocktype>
...
これらの処理は設定ファイルで指定した[name=SSH, port=ssh, protocol=tcp]という引数を使って実行されます。Fail2banが起動されると、
sendmail-whoisの処理は/etc/
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
...
[Definition]
...
actionban = printf %%b "Subject: [Fail2Ban] <name>: banned <ip> from `uname -n`
Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
From: <sendername> <<sender>>
To: <dest>\n
Hi,\n
The IP <ip> has just been banned by Fail2Ban after
<failures> attempts against <name>.\n\n
Here is more information about <ip> :\n
`/usr/bin/whois <ip> || echo missing whois program`\n
Regards,\n
Fail2Ban" | /usr/sbin/sendmail -f <sender> <dest>
...
このsendmail-whois.

なお、
以上見てきたようにFail2banは、
次回は、