Debian 13 trixie
Sponsored Link

Fail2Ban : 侵入防止システム2025/08/17

 

侵入防止システム (Intrusion Prevention System) Fail2Ban のインストールと設定です。

[1] Fail2Ban をインストールします。
root@dlp:~#
apt -y install fail2ban ufw
[2] デフォルトの設定は [/etc/fail2ban/jail.conf] で定義されています。
デフォルト値はパッケージのアップデートで変更となる可能性があるため、設定を変更したい場合は [jail.local] ファイルを作成して変更します。
root@dlp:~#
vi /etc/fail2ban/jail.conf
# 87行目 : 自身のローカル IP は無視する
#ignoreself = true

# 92行目 : 無視するネットワークを追加可能
#ignoreip = 127.0.0.1/8 ::1

# 103行目 : 対象ホストを禁止する時間
# - 単位は秒
# - 1m ⇒ 1 分
# - 1h ⇒ 1 時間
# - 1d ⇒ 1 日
# - 1mo ⇒ 1 か月
# - 1y ⇒ 1 年
bantime  = 10m

# 107行目 : 設定時間内にしきい値を超えると対象ホストを禁止する
findtime  = 10m

# 110行目 : 失敗回数が設定値を超えると対象ホストを禁止する
maxretry = 5

# 132行目 : ログを読み込むためのバックエンド
backend = auto

# 178行目 : メール通知する場合の宛先アドレス
destemail = root@localhost

# 181行目 : メール通知する場合の送信元アドレス
sender = root@<fq-hostname>

# 263行目 : デフォルトのアクション
# - %(action_)s ⇒ 禁止のみ
# - %(action_mw)s ⇒ 禁止とメール通知 (Whois 情報含む)
# - %(action_mwl)s ⇒ 禁止とメール通知 (Whois 情報とログ含む)
action = %(action_)s


root@dlp:~#
vi /etc/fail2ban/jail.local
# 新規作成
# デフォルト値を上書きしたい場合は設定可
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1
bantime  = 1d
findtime  = 5m
maxretry = 5
# Debian を [Standard system utilities] のみでインストールした場合は
# ログ管理は Journald のみで Rsyslog はインストールされていないため
# backend は auto ではなく systemd に要変更
backend = systemd
destemail = root@localhost
sender = root@dlp.srv.world

root@dlp:~#
systemctl enable --now fail2ban
[3] デフォルトでは SSH サービスのみが監視対象となっています。
root@dlp:~#
fail2ban-client status

Status
|- Number of jail:      1
`- Jail list:   sshd

root@dlp:~#
ll /etc/fail2ban/jail.d

total 4
-rw-r--r-- 1 root root 171 May  9 19:19 defaults-debian.conf

root@dlp:~#
vi /etc/fail2ban/jail.d/defaults-debian.conf
[DEFAULT]
banaction = nftables
banaction_allports = nftables[type=allports]

[sshd]
backend = systemd
journalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshd
enabled = true
# サービス毎にデフォルト値を上書き可
bantime  = 600
findtime  = 3m
maxretry = 5
action = %(action_mw)s

root@dlp:~#
systemctl reload fail2ban
# サービスの状態を表示

root@dlp:~#
fail2ban-client status sshd

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     0
|  `- Journal matches:  _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
   |- Currently banned: 0
   |- Total banned:     0
   `- Banned IP list:

# しきい値を超えたホストが禁止された状態

root@dlp:~#
fail2ban-client status sshd

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- Journal matches:  _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   10.0.0.230

# 実際の禁止アクションは iptables-nft で制御

root@dlp:~#
nft list ruleset

table inet f2b-table {
        set addr-set-sshd {
                type ipv4_addr
                elements = { 10.0.0.230 }
        }

        chain f2b-chain {
                type filter hook input priority filter - 1; policy accept;
                tcp dport 22 ip saddr @addr-set-sshd reject with icmp port-unreachable
        }
}

# メール通知を有効にしている場合は以下のようなメールが送信される

root@dlp:~#
mail

"/var/mail/root": 1 messages 1 new 1 unread
>N  4 root@dlp.srv.worl  Sat Aug 16 11:42   92/3350  [Fail2Ban] sshd: banned 10.0.0.230 from dlp.srv.world
? 4
Message 4:
From root@dlp.srv.world  Sat Aug 16 11:42:35 2025
X-Original-To: root@localhost
Subject: [Fail2Ban] sshd: banned 10.0.0.230 from dlp.srv.world
Date: Sat, 16 Aug 2025 11:42:35 +0900
From: Fail2Ban <root@dlp.srv.world>
To: root@localhost

Hi,

The IP 10.0.0.230 has just been banned by Fail2Ban after
5 attempts against sshd.


Here is more information about 10.0.0.230 :
.....
.....
[4] 禁止ホストを手動で追加/削除したい場合は以下のように実行します。
root@dlp:~#
fail2ban-client status sshd

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- Journal matches:  _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
   |- Currently banned: 1
   |- Total banned:     2
   `- Banned IP list:   10.0.0.230

# [10.0.0.230] の禁止を解除する

root@dlp:~#
fail2ban-client set sshd unbanip 10.0.0.230

1
root@dlp:~#
fail2ban-client status sshd

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- Journal matches:  _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
   |- Currently banned: 0
   |- Total banned:     2
   `- Banned IP list:

# [10.0.0.192/28] の禁止を追加する

root@dlp:~#
fail2ban-client set sshd banip 10.0.0.192/28

1
root@dlp:~#
fail2ban-client status sshd

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- Journal matches:  _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
   |- Currently banned: 1
   |- Total banned:     3
   `- Banned IP list:   10.0.0.192/28

# 禁止ホストを全て解除する場合は以下

root@dlp:~#
fail2ban-client unban --all

[5] [jail.conf] には SSH 以外のサービスも多数の事前定義があるため、定義名を指定することで監視対象に設定可能です。
root@dlp:~#
grep '^\[' /etc/fail2ban/jail.conf | tail -n +3

[sshd]
[dropbear]
[selinux-ssh]
[apache-auth]
[apache-badbots]
[apache-noscript]
[apache-overflows]
[apache-nohome]
[apache-botsearch]
[apache-fakegooglebot]
[apache-modsecurity]
[apache-shellshock]
[openhab-auth]
.....
.....

# 例として Apache2 の Basic 認証を設定

root@dlp:~#
vi /etc/fail2ban/jail.d/apache-auth.conf
# 新規作成

[apache-auth]
enabled = true
bantime  = 600
findtime  = 3m
maxretry = 5
action = %(action_mw)s

# 例として Vsftpd を設定

root@dlp:~#
vi /etc/fail2ban/jail.d/vsftpd.conf
# 新規作成

[vsftpd]
enabled = true
action = %(action_mw)s

# 例として Postfix SASL を設定

root@dlp:~#
vi /etc/fail2ban/jail.d/postfix-sasl.conf
# 新規作成

[postfix-sasl]
enabled = true
action = %(action_mw)s

root@dlp:~#
systemctl reload fail2ban
root@dlp:~#
fail2ban-client status

Status
|- Number of jail:      4
`- Jail list:   apache-auth, postfix-sasl, sshd, vsftpd

# 適当に認証失敗して動作確認

root@dlp:~#
fail2ban-client status apache-auth

Status for the jail: apache-auth
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- File list:        /var/log/apache2/error.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   10.0.0.5

root@dlp:~#
fail2ban-client status vsftpd

Status for the jail: vsftpd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- File list:        /var/log/vsftpd.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   10.0.0.203
関連コンテンツ