Debian GNU/Linux 11.6上のroundcube 1.6.1 をfail2banで扱う

やりたいこと

roundcubeのログインでブルートフォース攻撃をされると困るので、一定時間内に複数回アクセスをしてきたときにはしばらくの間アクセスできないようにしたい。そこで、fail2banを用いてそれを実現する。

fail2banのインストールや設定

こちらで既に行っている。
Debian 10 + nftables + fail2ban - 発声練習

ログ解析フィルタを作成する

Debian GNU/Linux 11.6のfail2banには標準でroundcube用のフィルタが存在したが、それを利用してroundcube 1.6.1のログをフィルタリングできなかった。ログイン失敗時のログは以下のようになっている。

[28-Mar-2023 09:19:27 +0000]:  <bmqu0ede> IMAP Error: Login failed for 「ログイン名」 against 「サーバのFQDN」 from 「サーバのIPアドレス」. AUTHENTICATE PLAIN: Authentication failed. in /var/lib/roundcube/program/lib/Roundcube/rcube_imap.php on line 211 (POST /roundcube/?_task=login&_action=login)

Debian GNU/Linux 11.6上でroundcube 1.6.1 - 発声練習でfail2banのフィルタのチェックの仕方が紹介されている。今回は/var/lib/roundcube/logs/errors.log がログファイルなので以下のようにコマンドを実行し、フィルタが機能しているかどうかを確かめることができる。

% cd /etc/fail2ban/filter.d
% sudo fail2ban-regex /var/lib/roundcube/logs/errors.log /etc/fail2ban/filter.d/roundcube-auth.conf --print-all-matched

デフォルトの正規表現はまったくヒットしない。そこで/etc/fail2ban/filter.d/roundcube-auth.confを以下のように書き換えた。

[INCLUDES]

[Definition]

prefregex = ^.*?IMAP Error:

failregex = %(prefregex)s Login failed for .* from <HOST>(\. .* Authentication failed\. in .*?/rcube_imap\.php on line \d+ \(\S+ \S+\))?$

ignoreregex = Could not connect to .* Connection refused

journalmatch = SYSLOG_IDENTIFIER=roundcube

続いて、/etc/fail2ban/jail.dにファイルを作成し、このフィルタを有効化する。

% cd /etc/fail2ban/jail.d
% sudo touch roundcube.conf

roundcube.confの中身は以下のようにする。

[DEFAULT]
roundcube_errors_log = /var/lib/roundcube/logs/errors.log

[roundcube-auth]
enabled = true
backend = auto

fail2banを再起動する。

% sudo systemctl restart fail2ban

エラーがあるかどうかは/var/log/fail2ban.log を確認する。

ログイン失敗状況は以下のコマンドで確認できる。一度、確認したとにわざとログインに失敗して、失敗した回数が増えていれば正しく動作している。

% sudo fail2ban-client status roundcube-auth
Status for the jail: roundcube-auth
|- Filter
|  |- Currently failed:	0
|  |- Total failed:	0
|  `- File list:	/var/lib/roundcube/logs/errors.log
`- Actions
   |- Currently banned:	0
   |- Total banned:	0
   `- Banned IP list: