外部からの執拗なアタックを軽減させるツール
本家様 https://www.fail2ban.org/wiki/index.php/Main_Page
sshサーバへのddosアタックらを軽減できるみたい. 他にもwebサイトへのF5アタックとか.
本家様でも書かれているが、これを仕込んでもssh接続が安全になるわけではない.
sshd_configの「PasswordAuthentication yes」(/etc/passwdでも認証ok)を「PasswordAuthentication no」として
公開鍵を作ってより効果的にすべきかと.
インストール †
[root@c ~]# yum install epel-release
[root@c ~]# yum install fail2ban fail2ban-server fail2ban-sendmail fail2ban-firewalld fail2ban-systemd fail2ban-selinux
設定方法 †
「/etc/fail2ban/jail.conf」に
# Changes: in most of the cases you should not modify this
# file, but provide customizations in jail.local file,
# or separate .conf files under jail.d/ directory, e.g.:
とあるので「/etc/fail2ban/jail.local」を新規作成します。
方針としては ssh のみ防げばいいので
[sshd]
enabled = true
logpath = /var/log/secure
[DEFAULT]
ignoreip = 127.0.0.1/8 192.168.0.0/24
bantime = 600
findtime = 10
maxretry = 1
[recidive]
enabled = true
logpath = /var/log/fail2ban.log
banaction = iptables-allports
bantime = 4w ;604800 ; 1 week
findtime = 1d ;86400 ; 1 day
maxretry = 2
とする。
そして下記のようにして起動する
[root@c ~]# systemctl enable fail2ban.service
[root@c ~]# systemctl start fail2ban.service
*初回は[recidive]をわざと無効にして流して /var/log/fail2ban.log を作らせて、それから[recidive]を有効にしないと起動に失敗するみたい
動作確認 †
運用後暫くすればBanされた方々が下記コマンドで見れます。
[root@c ~]#
[root@c ~]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 5308
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 24
|- Total banned: 2578
`- Banned IP list: <BanされたIPアドレスが列挙されます>
[root@c ~]#
RHEL8/Rocky8 †
| --- /etc/fail2ban/fail2ban.conf.orig 2020-11-24 05:43:03.000000000 +0900
+++ /etc/fail2ban/fail2ban.conf 2021-10-27 13:03:32.723439882 +0900
@@ -46,14 +46,14 @@
# communicate with the server afterwards.
# Values: [ FILE ] Default: /var/run/fail2ban/fail2ban.sock
#
-socket = /var/run/fail2ban/fail2ban.sock
+socket = /run/fail2ban/fail2ban.sock
# Option: pidfile
# Notes.: Set the PID file. This is used to store the process ID of the
# fail2ban server.
# Values: [ FILE ] Default: /var/run/fail2ban/fail2ban.pid
#
-pidfile = /var/run/fail2ban/fail2ban.pid
+pidfile = /run/fail2ban/fail2ban.pid
# Options: dbfile
# Notes.: Set the file for the fail2ban persistent data to be stored.
|
selinux †
「/var/log/fail2ban.log」を参照して[recidive]を定義すると動かない事態が発生することがある
原因はSElinuxへの対応不足
[root@n1 ~]# ls -lZ /var/log/fail2ban.log
-rw-------. root root system_u:object_r:fail2ban_log_t:s0 /var/log/fail2ban.log
[root@n1 ~]#
「fail2ban-selinux」パッケージを入れる
解除方法 †
間違ってbanされたらそれを解除するには
fail2ban-client set sshd unbanip XX.XX.XX.XX
とする