Debian 9 Stretch で連続接続を Fail2ban ではじく

Fail2ban は、ログを参照して認証エラーを繰返しているホストを ban するための Python 製のツールです。iptables で接続制御をするのは ufw と同じですが、Fail2ban はより柔軟にログを分析して接続を制御します。

インストール

$ sudo apt install fail2ban
$ sudo fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd

設定

/etc/fail2ban/jail.conf の中には次のような記述があります。

# YOU SHOULD NOT MODIFY THIS FILE.
#
# It will probably be overwritten or improved in a distribution update.
#
# Provide customizations in a jail.local file or a jail.d/customisation.local.
# For example to change the default bantime for all jails and to enable the
# ssh-iptables jail the following (uncommented) would appear in the .local file.
# See man 5 jail.conf for details.

jail.cof は更新時に上書きされる可能性があるので、jail.local 等の別名ファイルを作成して差分を記述するようにしましょう、ということです。

/etc/fail2ban/jail.conf 次のように書きます。ufw と組合せているので、ufw の limit を考慮しないとずっと ban されないことになります。

[DEFAULT]
bantime = 1800
findtime = 600
maxretry = 5

[postfix-sasl]
enabled = true

[dovecot]
enabled = true

設定を再読込したら完了です。

$ sudo systemctl reload fail2ban
$ sudo fail2ban-client status
Status
|- Number of jail: 3
`- Jail list: dovecot, postfix-sasl, sshd
$ sudo fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 1
| |- Total failed: 7
| `- File list: /var/log/auth.log
`- Actions
 |- Currently banned: 0
 |- Total banned: 0
 `- Banned IP list:

自分でフィルタやアクションを定義することもできますが、とりあえずはこれで様子を見てその都度変更していこうと思います。

bantime

接続を拒否する時間を秒で指定します。

# "bantime" is the number of seconds that a host is banned.
bantime = 600

findtime と maxtretry

findtime 秒間の間に maxretry 回の認証失敗があったときに拒否対象となります。標準の findtime = 600, maxretry = 5 であれば 600秒間に 5 回の試行で拒否対象となります。

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 600

# "maxretry" is the number of failures before a host get banned.
maxretry = 5

参考