fail2banで遊ぶ
fail2banは某MLで偶然知った最近のお気に入り。任意のログを常時監視し、不正なアクセス元を見つけるとiptablesで通信をブロックする。ssh、http、ftp 向けのフィルタが最初から用意されているので、とりあえず動かすだけでもそこそこ遊べる。sendmailとdovecot向けのフィルタも標準装備なら文句なしなのだが、なぜかこれは用意されてない。
配布元:http://www.fail2ban.org/wiki/index.php/Main_Page
■インストール
CentOS5向けにはrpmパッケージが用意されているので、これをそのまま使わせてもらう。CentOS4向けのrpmパッケージはバージョンが古く、CentOS5向けのsrpmをリビルドしてみたらうまく動かなかった。CentOS4の人は素直にソースから入れることを推奨(その場合、ログローテーションの設定を忘れずに)。
# wget http://download.fedora.redhat.com/pub/epel/5/x86_64/fail2ban-0.8.3-18.el5.noarch.rpm
# rpm -ivh fail2ban-0.8.3-18.el5.noarch.rpm
警告: fail2ban-0.8.3-18.el5.noarch.rpm: ヘッダ V3 DSA signature: NOKEY, key ID 217521f6
エラー: 依存性の欠如:
shorewall は fail2ban-0.8.3-18.el5.noarch に必要とされています
いきなり依存性でこける。shorewallはiptablesと同じくパケットフィルタだが、CentOS標準ではない。fail2banの動作に不可欠ではないのに、なんで依存関係つけてしまうのかな。別にiptablesでもいいじゃん。shorewallを探してきて入れるのが面倒なので、さくっと依存関係を無視。
# rpm --nodeps -ivh fail2ban-0.8.3-18.el5.noarch.rpm 警告: fail2ban-0.8.3-18.el5.noarch.rpm: ヘッダ V3 DSA signature: NOKEY, key ID 217521f6 準備中... ########################################### [100%] 1:fail2ban ########################################### [100%]
設定ファイルは /etc/fail2ban/ 配下にインストールされる。fail2ban.conf は通常触る必要なし。変更する必要があるのは、動作確認等で loglevel を上げたい時だけ。監視対象はjail.conf で定義する。ひな形がいくつか書かれているので、デフォルトのフィルタ(filter.d/ 配下にある)をそのまま使うなら enabled = falseを enabled = true に書き換えればOK。注意点としては、何かアクションが発生するたびにメールで通知してくる仕様なので、メールを受け取るために dest=you@mail.com の箇所を有効なメールアドレスに変更すること。
とりあえず、最初から有効な[ssh-iptables]に加えて、[vsftpd-iptables]と[apache-badbots]のフィルタを有効にし、ついでに見よう見まねで[devecot-iptables]を追加してみる。
# vi /etc/fail2ban/jail.conf [/etc/fail2ban/jail.conf] 16c16 < ignoreip = 127.0.0.1 --- > ignoreip = 127.0.0.1 192.168.1.0/24 48c48 < sendmail-whois[name=SSH, dest=root, sender=fail2ban@mail.com] --- > sendmail-whois[name=SSH, dest=root] 60a61,70 > [dovecot-iptables] > > enabled = true > filter = dovecot > action = iptables[name=dovecot, port=pop3, protocol=tcp] > sendmail-whois[name=dovecot, dest=root] > logpath = /var/log/maillog > maxretry = 5 > findtime = 180 > 124c134 < enabled = false --- > enabled = true 127c137 < sendmail-whois[name=VSFTPD, dest=you@mail.com] --- > sendmail-whois[name=VSFTPD, dest=root] 137c147 < enabled = false --- > enabled = true 140,141c150,151 < sendmail-buffered[name=BadBots, lines=5, dest=you@mail.com] < logpath = /var/www/*/logs/access_log --- > sendmail-buffered[name=BadBots, lines=5, dest=root] > logpath = /home/*/logs/access_log
dovecotのフィルタは適当に自分で書く。
# vi /etc/fail2ban/filter.d/dovecot.conf [/etc/fail2ban/filter.d/dovecot.conf] [Definition] # Option: failregex # Notes.: regex to match the password failures messages in the logfile. # Values: TEXT # failregex = Disconnected \(auth failed, [0-9.] attempts\): user=<\S+>, method=PLAIN, rip=, \S+$ # Option: ignoreregex # Notes.: regex to ignore. If this regex matches, the line is ignored. # Values: TEXT # ignoreregex =
しかる後に起動する。
# service fail2ban start Starting fail2ban: [ OK ]
起動したら、iptablesのルールを確認。
# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination fail2ban-VSFTPD tcp -- anywhere anywhere tcp dpt:ftp fail2ban-BadBots tcp -- anywhere anywhere multiport dports http,https fail2ban-SSH tcp -- anywhere anywhere tcp dpt:ssh fail2ban-dovecot tcp -- anywhere anywhere tcp dpt:pop3 (中略) Chain fail2ban-BadBots (1 references) target prot opt source destination RETURN all -- anywhere anywhere Chain fail2ban-SSH (1 references) target prot opt source destination RETURN all -- anywhere anywhere Chain fail2ban-VSFTPD (1 references) target prot opt source destination RETURN all -- anywhere anywhere Chain fail2ban-dovecot (1 references) target prot opt source destination RETURN all -- anywhere anywhere
ChainができていればOK。iptablesの起動に失敗する場合、経験則では一度fail2banを再起動すると2度目からは大丈夫なようだ。なぜかは不明。
動作原理は単純で、任意のログを監視してトリガーとなる文字列が指定時間内に指定回数発現したら接続元のIPアドレスをiptables等で指定時間(デフォルトは10分)だけ遮断するというもの。パスワードアタック等に対しては10分ブロックしてくれるだけで十分効果がある。同じところから繰り返し何度も来るようであれば別途恒久的にブロックしてやればOK。フィルタのルールを工夫すれば様々な攻撃に対応できそうな気がする。ただし、正規表現をきちんと書ければ、の話(そこがなにげに厳しい。。。)。
フィルタが発動するたびにメールで通知してくれるので、誤動作した場合の発見も容易。誤動作を解除するには、jail.conf の ignoreip = の項目に当該IPアドレスを追加してfail2banを再起動すればいい。
devecotのアクセス制限でお悩みの方にもお勧め。
2010/9/18 土曜日 at 1:07 PM
[...] –nodepsを付けているのは、不要な依存関係があるため。そして設定ファイルをちょこちょこいじくってpostfixとdovecotに対して監視を有効にした。ここら辺については詳しい事がこことかこことかここに書いてあるので参考にすれば比較的簡単に設定出来る。 [...]
2011/10/30 日曜日 at 11:34 AM
「iptablesの起動に失敗する場合、経験則では一度fail2banを再起動すると2度目からは大丈夫なようだ」が参考になりました。これを読んでいたお陰でfail2banの設定で迷うところがありませんでした。
拙宅のfail2banは1週間ごとに勝手に再起動しています。fail2banのログを見るとそこに必ずERRORがあってiptablesへの設定に失敗してます。そのため手動で2回目の再起動をかける羽目になっていますがfail2banは便利なので手放せません。手動さえなければ最高なんですけどね。
fail2banに関する情報の提供をありがとうございました。