fail2ban を導入

いつだったか、Twitterのタイムライン上でSMTPサーバー建てるならのオススメは?という話が流れで出てきた Fail2ban というツールを入れてみた。

指定したログに対して指定したキーワードやフレースで監視を行い、iptables と連携してアクセスを遮断する、っていうツールなんですな。
コレを使うことで、POP3 への絨毯爆撃のようなアクセスを防ぐことが出来るということらしい。

インストール

入れると言っても、Debianだとdeb化されているので、おなじみの "aptitude install fail2ban" でインストール自体は終了〜。

# aptitude install fail2ban
以下の新規パッケージがインストールされます:
  fail2ban python-pyinotify{a}
更新: 0 個、新規インストール: 2 個、削除: 0 個、保留: 0 個。
164 k バイトのアーカイブを取得する必要があります。展開後に 505 k バイトのディスク領域が新たに消費されます。
先に進みますか? [Y/n/?] y
取得: 1 http://debian-mirror.sakura.ne.jp/debian/ sid/main fail2ban all 0.8.10-3 [135 kB]
取得: 2 http://debian-mirror.sakura.ne.jp/debian/ sid/main python-pyinotify all 0.9.3-2 [29.1 kB]
Fetched 164 kB in 0秒 (313 kB/s)
以前に未選択のパッケージ fail2ban を選択しています。
(データベースを読み込んでいます ... 現在 86811 個のファイルとディレクトリがインストールされています。)
(.../fail2ban_0.8.10-3_all.deb から) fail2ban を展開しています...
以前に未選択のパッケージ python-pyinotify を選択しています。
(.../python-pyinotify_0.9.3-2_all.deb から) python-pyinotify を展開しています...
man-db のトリガを処理しています ...
fail2ban (0.8.10-3) を設定しています ...
[ ok ] Starting authentication failure monitor: fail2ban.
python-pyinotify (0.9.3-2) を設定しています ...

設定して、起動

設定を変更したポイントとしては、次の2つ。
まず、POP3/IMAP4 を処理している dovecot 向けのfilter (/etc/fail2ban/filter.d/dovecot.conf) があるので、それを下のように変更。

# grep -n failregex /etc/fail2ban/filter.d/dovecot.conf
9:# Option:  failregex
16:#failregex = .*(?:pop3-login|imap-login):.*(?:Authentication failure|Aborted login \(auth failed|Aborted login \(tried to use disabled|Disconnected \(auth failed).*\s+rip=(?P\S*),.*
17:failregex = .*(?:pop3-login|imap-login):.*(?:Authentication failure|Aborted login \(auth failed|Aborted login \(tried to use disabled|Disconnected \(auth failed|tried to use disallowed plaintext auth).*\s+rip=(?P\S*),.*

"failregex =" で始まる行が、検知条件になるわけですが、16 行目が元々書かれていた条件で、17 行目が直した条件。
mail.log に残っていた POP3/IMAP4 へのアクセスに失敗しているログを見ると

Aug 12 05:45:25 nexus01 dovecot: pop3-login: Aborted login (tried to use disallowed plaintext auth): user=<>, rip=66.85.173.13, lip=218.219.149.234, session=<N7xUFrLjMgBCVa0N>

というのが、多かったので、このログの () の中の部分で引っかかるようにした次第。

もう一つは、/etc/fail2ban/jail.conf 中の "ignoreip = 127.0.0.1" の後ろに、自ネットワークのIPアドレスを書いておくこと。
#ここに、IPv6 アドレス書いたらエラーでたんだけど、対応してないのかね?

で、fail2ban を ”/etc/init.d/fail2ban start” で起動して上げれば、完了〜。
正しく設定されていれば、iptable の設定に "fail2ban" で始まるチェインが追加されます。(デフォルトでは、dovecot 周りと ssh のフィルタが有効になります。)

# iptables -nL | grep fail2ban
fail2ban-dovecot  tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 25,465,587,143,220,993,110,995
fail2ban-ssh  tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 22
Chain fail2ban-dovecot (1 references)
Chain fail2ban-ssh (1 references)

暫くほっとくと、思い出した頃に、"[Fail2Ban] dovecot: banned 192.168.0.2" という subject のメールがやってきて、ブロックされたんだ、というのが判ります。

で、他にもイロイロ出来る

実際、 /etc/fail2ban/filter.d の下を見ると判るのだけど、いろんなフィルタが準備されているので、

# ls /etc/fail2ban/filter.d/
apache-auth.conf       couriersmtp.conf       pam-generic.conf     sogo-auth.conf
apache-badbots.conf    cyrus-imap.conf        php-url-fopen.conf   sshd-ddos.conf
apache-common.conf     dovecot.conf           postfix.conf         sshd.conf
apache-nohome.conf     dropbear.conf          proftpd.conf         vsftpd.conf
apache-noscript.conf   exim.conf              pure-ftpd.conf       webmin-auth.conf
apache-overflows.conf  gssftpd.conf           qmail.conf           wuftpd.conf
assp.conf              lighttpd-auth.conf     recidive.conf        xinetd-fail.conf
asterisk.conf          lighttpd-fastcgi.conf  roundcube-auth.conf
common.conf            mysqld-auth.conf       sasl.conf
courierlogin.conf      named-refused.conf     sieve.conf

使いたいものがあれば、フィルタの方を必要に応じて修正した後に、/etc/fail2ban/jail.conf にある該当するフィルタの "enabled = false" を ”enabled = true” に書き換えて、再起動すれば、OK。
ログが出るものだったら、準備されているフィルタ以外のものでも、割と簡単にアクセスの遮断が出来るんじゃないでしょうか。

うちでは、今のところ POP3 と IMAP と ssh だけで事足りそうなので、ずっとこのままだな、多分。

トラックバック(1)

fail2banを使い始めて暫く経ったのだけど、どうもアップデートをしたタイミン... 続きを読む

コメントする