fail2banでmt-comments.cgiのコメントスパムをIP的にBANする
最近Movable Typeのコメントスパム投稿のbotが喧しいですね。Movable TypeをCGIで動かしている限り、POSTされていると反映しなくても負荷が高くなりますよね。というわけで、botをやっつけましょう!
CentOS 6.4とEPELのfail2ban v0.8.8です。もう既にfail2banによるアクセス制限が稼働しているとします。なお、fail2banはiptablesというLinux内蔵のファイアウォールを使いますので、fail2banを稼働させるまでにはちょっとハードルが高いかもしれません。
Apacheの設定
実ホストでApacheを動かしているなら、アクセスログは/var/log/httpd/access_log に入りますけれども、バーチャルホストでMovable Typeを提供していて、さらにログの書き出し場所も指定している場合は、ログの書き出しが分散されてしまいます。これでは、fail2banでログを監視するには具合が悪いので、旧来の /var/log/httpd/access_log にも書き込みます。
CustomLog行を2行書くと、1アクセスでそれぞれのCustomLog行で指定したログファイルに書き込みます。
※ストレージが遅いと、Apacheのパフォーマンスが落ちますので注意です。
# blog.dtpwiki.jp
<VirtualHost *:80>
ServerAdmin webmaster@dtpwiki.jp
DocumentRoot /home/www/blog.dtpwiki.jp/html
ServerName blog.dtpwiki.jp
CustomLog logs/access_log combined
CustomLog "|/usr/sbin/cronolog /var/log/virtualhosts/blog.dtpwiki.jp/%Y/%m/access_log.%Y%m%d" combined
</VirtualHost>
fail2banの設定変更
/etc/fail2ban/jail.conf に以下を追加
[apache-mtcommentsbot]
enabled = true
filter = apache-mtcommentsbot
action = iptables-multiport[name=mtcommentsbot, port="http,https"]
sendmail-buffered[name=BadBots, lines=10, dest=you@example.com]
logpath = /var/log/httpd/access_log
findtime = 420
bantime = 172800
maxretry = 6同じIPから7分間(420秒)で6回POSTされたら、そのIPからのアクセスを2日間(172800秒)禁じます。
/etc/fail2ban/filter.d/apache-mtcommentsbot.conf を新規作成
failregex = ^<HOST> -.*"POST /.*mt-comments.cgi.*$ ignoreregex =
fail2banを再起動
/sbin/service fail2ban restart
ログがちゃんと監査できるか確認
fail2ban-regexコマンドを使います。
コマンド:
fail2ban-regex /var/log/httpd/access_log /etc/fail2ban/filter.d/apache-mtcommentsbot.conf
出力:
Running tests
=============
Use regex file : /etc/fail2ban/filter.d/apache-mtcommentsbot.conf
Use log file : /var/log/httpd/access_log
Results
=======
Failregex: 119 total
|- #) [# of hits] regular expression
| 1) [119] ^<HOST> -.*"POST /.*mt-comments.cgi.*$
`-
Ignoreregex: 0 total
Summary
=======
Addresses found:
[1]
110.86.184.27 (Wed Apr 17 00:47:45 2013)
110.86.184.27 (Wed Apr 17 00:47:46 2013)
110.86.184.27 (Wed Apr 17 00:48:09 2013)
110.86.184.27 (Wed Apr 17 00:48:31 2013)
110.86.184.27 (Wed Apr 17 00:48:43 2013)
110.86.184.27 (Wed Apr 17 00:49:10 2013)
110.86.184.27 (Wed Apr 17 00:49:17 2013)
110.86.184.27 (Wed Apr 17 00:49:18 2013)
110.86.184.27 (Wed Apr 17 00:49:34 2013)
110.86.184.27 (Wed Apr 17 00:49:53 2013)
...
27.153.250.49 (Wed Apr 17 09:46:39 2013)
27.153.250.49 (Wed Apr 17 09:47:18 2013)
27.153.250.49 (Wed Apr 17 09:47:25 2013)
27.153.250.49 (Wed Apr 17 09:47:58 2013)
27.153.250.49 (Wed Apr 17 09:48:03 2013)
27.153.250.49 (Wed Apr 17 09:48:11 2013)
125.76.229.177 (Wed Apr 17 09:58:25 2013)
125.76.229.177 (Wed Apr 17 10:02:02 2013)
Date template hits:
26022 hit(s): Day/MONTH/Year:Hour:Minute:Second
Success, the total number of match is 119
However, look at the above section 'Running tests' which could contain important
information.
iptablesでBANできているか確認
コマンド:
/sbin/iptables-save
実行結果:
# Generated by iptables-save v1.4.7 on Wed Apr 17 10:23:44 2013 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [30593:51765437] :RH-Firewall-1-INPUT - [0:0] :fail2ban-mtcommentsbot - [0:0] -A INPUT -p tcp -m multiport --dports 80,443 -j fail2ban-mtcommentsbot ... -A fail2ban-mtcommentsbot -s 27.153.250.49/32 -j DROP -A fail2ban-mtcommentsbot -s 121.204.243.19/32 -j DROP -A fail2ban-mtcommentsbot -s 122.228.160.175/32 -j DROP -A fail2ban-mtcommentsbot -s 1.80.251.198/32 -j DROP -A fail2ban-mtcommentsbot -s 110.86.184.27/32 -j DROP -A fail2ban-mtcommentsbot -j RETURN COMMIT # Completed on Wed Apr 17 10:23:45 2013