fail2banを試す。

※IT系の記事は当方環境での実施内容となるため実施する場合は自己責任でお願いいたします。

これも日経Linux7月号の記事ですが、SSHやApache、DNSへのアタック元IPを自動でブロック、一定時間後に開放するfail2banが便利そうです。以前はswatchをいろいろ設定して同じ事をやっている記事を見たことがありますが、そういう仕組みを提供してくれて、最初から用意されているものをテンプレートに自分で独自の制限も追加できるみたいです。CentOSならリポジトリに「epel」を追加していれば、yumで入ります。

以下、actionにshorewallじゃなくてiptablesを使う場合の追加サンプル(CentOS)。
logpathのログの場所とかは読み替えてください。あと、sendmailの行は前の行のiptables・・・と始まりの位置を合わせて置いたほうがいいかも。


[apache-badbots]

enabled  = true
filter   = apache-badbots
action   = iptables-multiport[name=BadBots, port="http,https"]
           sendmail-buffered[name=BadBots, lines=5, dest=root]
logpath  = /var/www/*/logs/access_log
bantime  = 172800
maxretry = 1

[apache-noscript]

enabled  = true
filter   = apache-noscript
action   = iptables[name=NoScript, port=http, protocol=tcp]
           sendmail-whois[name=Noscript, dest=root]
logpath  = /var/www/*/logs/error_log
bantime  = 172800
maxretry = 3

[apache-overflows]

enabled  = true
filter   = apache-overflows
action   = iptables[name=OverFlows, port=http, protocol=tcp]
           sendmail-whois[name=Overflows, dest=root]
logpath  = /var/www/*/logs/error_log
bantime  = 172800
maxretry = 3

【php-url-fopen】←太い【】を細い[]に変えてください。
enabled = true
filter = php-url-fopen
action = iptables[name=php-url-fopen, port=http, protocol=tcp]
         sendmail-buffered[name=php-url-fopen, lines=5, dest=root]
logpath = /var/www/*/logs/access_log
maxretry = 1

動作結果は

iptables -L

で確認。

しばらく

tail -f /var/log/messages

で眺めていると・・・
おお!↓早速ブロックしてる。

Jul 14 11:46:15 fail2ban.actions: WARNING [apache-badbots] Ban 27.50.131.11

フィルタ状態確認

# fail2ban-client status apache-badbots
Status for the jail: apache-badbots
|- filter
|  |- File list:        /var/log/httpd/access_log
|  |- Currently failed: 0
|  `- Total failed:     0
`- action
   |- Currently banned: 0
   |  `- IP list:
   `- Total banned:     0

PDF

Linuxban,fail

Posted by suama