19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

fail2banでブルートフォースアタックを防ぐ

Last updated at Posted at 2014-04-23

fail2banはログを監視し、マッチしたらiptablesでDROPする仕組み。
マッチの正規表現は自分で書く。

前準備

インストール(deb系)
$ sudo apt-get install -y fail2ban
インストール(CentOS5)
$ wget -q http://dl.fedoraproject.org/pub/epel/5/x86_64/fail2ban-0.8.10-4.el5.noarch.rpm
$ wget -q http://dl.fedoraproject.org/pub/epel/5/x86_64/python-inotify-0.9.1-1.el5.noarch.rpm
$ wget -q http://dl.fedoraproject.org/pub/epel/5/x86_64/python-ctypes-1.0.2-2.el5.x86_64.rpm
$ sudo rpm -ivh [fp]*rpm
監視したいログを確認。
$ grep "LOGIN FAILED" /var/log/mail.log
Apr 23 18:24:13 localhost courierpop3login: LOGIN FAILED, user=test1, ip=[::ffff:22.33.44.55]

設定記述

ignoreipの追記を忘れずに。

/etc/fail2ban/jail.conf
-ignoreip = 127.0.0.1
+ignoreip = 127.0.0.1 10.0.0.0/8 192.168.0.0/16 11.22.33.44/28

+[courierpop3login]
+enabled  = true
+port	 = pop3
+filter   = courierpop3login
+logpath  = /var/log/mail.log
/etc/fail2ban/filter.d/courierpop3login.conf
[Definition]
failregex = LOGIN FAILED, user=.*, ip=\[<HOST>\]$
ignoreregex =

リスタート

$ sudo /etc/init.d/fail2ban restart
Restarting authentication failure monitor: fail2ban.

動作確認

リモートからpop3で存在しないアカウントでのログインを試す。

/var/log/mail.log
Apr 24 07:41:09 localhost courierpop3login: LOGIN FAILED, user=test1, ip=[::ffff:123.123.123.123]
Apr 24 07:41:16 localhost courierpop3login: LOGIN FAILED, user=test1, ip=[::ffff:123.123.123.123]
Apr 24 07:41:26 localhost courierpop3login: LOGIN FAILED, user=test1, ip=[::ffff:123.123.123.123]

4回目から/var/log/mail.logにログ出力されない。
これは3回の失敗時点でiptablesにDROP登録されるからです。

 # iptables -L
 Chain INPUT (policy ACCEPT)
 target     prot opt source               destination
 fail2ban-courierpop3login  tcp  --  anywhere             anywhere            tcp dpt:pop3
 
 Chain FORWARD (policy ACCEPT)
 target     prot opt source               destination
 
 Chain OUTPUT (policy ACCEPT)
 target     prot opt source               destination
 
 Chain fail2ban-courierpop3login (1 references)
 target     prot opt source               destination
+DROP       0    --  123.123.123.123.sonet.ne.jp  anywhere
 RETURN     0    --  anywhere             anywhere

ログ

  • CentOSだと初期値はsyslog(messages)に出力される。
/var/log/fail2ban.log
2014-04-24 07:57:16,154 fail2ban.actions: WARNING [courierpop3login] Ban 123.123.123.123
2014-04-24 08:07:16,760 fail2ban.actions: WARNING [courierpop3login] Unban 123.123.123.123

fail2banをrestartしてもDROP状態は維持される。(初期化されない。)

デフォルトは3回失敗で10分間ドロップ

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?