Debian 13 で fail2ban の設定方法

背景

Debian archive mirror用の固定IPv4サーバがあり、このIPv4アドレスに対してssh攻撃が多少来ています。

昔は sshguardで攻撃者のIPを自動BANしていたのですが、Debian 11/12あたりの標準ファイアウォールの iptables から nftables への切り替えあたりでうまく設定ができず、パスワード攻撃無制限許可状態になっていたので、Debian 13 trixie のリリースに合わせて ssh 攻撃の自動ブロックを設定しましたので、参考までにログを。

重要、必読

ssh接続は特定ユーザかつ公開鍵認証しか許可しておらず、パスワード認証は利用不可にしています。ssh攻撃の99%はパスワード認証での攻撃ですので、sshd_configの PasswordAuthentication Noの設定は必須です。

  • sshd_configでパスワード認証を不可にしている環境であること(≒正常ログインのユーザは公開鍵認証で利用していること)
    • パスワード認証が必要になる公開サーバはそそもそもの設計に問題があるので、この設定を入れるよりも公開鍵認証化することを先に検討してください

現在のfail2ban状況(参考情報)

sshd_configのLoginGraceTime(ssh接続して認証完了になるまでの許容時間)を既定値の2分にしていた際には過疎サーバでもこれぐらいは自動ブロックしています。

LoginGraceTimeが2分のままでも十分に機能していますが、ルータのNATセッション消費が鬱陶しいので 10秒にしたところ、ssh攻撃自体がほとんど来なくなってしまったので、ブロックしてるログを確認するには2分のほうがよいかもしれません(危険)。

table inet f2b-table {
set addr-set-sshd-stage1 {
type ipv4_addr
elements = { 14.103.71.220, 115.78.226.174,
171.244.61.82, 191.96.235.47,
210.79.190.22 }
}

set addr-set-sshd-stage2 {
type ipv4_addr
elements = { 4.211.84.189, 20.107.168.239,
23.228.85.111, 24.55.171.147,
34.91.0.68, 35.244.25.124,
37.193.112.180, 43.225.158.4,
45.61.187.220, 45.78.196.250,
45.127.4.231, 45.134.26.79,
45.135.232.92, 45.135.232.177,
45.140.17.124, 45.148.10.240,
46.245.82.12, 49.207.186.17,
51.38.238.8, 54.38.242.11,
59.144.223.126, 60.199.224.2,
61.80.237.194, 62.60.193.127,
62.219.172.50, 74.208.111.131,
85.208.253.184, 91.205.219.185,
92.5.88.159, 101.36.109.176,
101.126.151.131, 103.49.238.99,
103.140.73.162, 103.172.204.219,
103.176.79.117, 103.186.1.120,
103.186.188.211, 103.187.146.107,
104.167.222.134, 115.21.183.150,
122.170.148.252, 128.199.252.176,
150.109.244.181, 152.32.218.149,
152.200.205.179, 154.221.18.60,
165.154.255.47, 167.71.245.96,
178.94.142.216, 181.228.68.232,
185.50.38.169, 188.166.191.39,
191.37.72.46, 196.0.120.211,
199.195.248.191, 202.39.251.216,
202.51.214.98, 209.38.34.79,
209.150.154.219, 211.201.163.70,
212.113.125.138, 221.213.129.46 }
}

set addr-set-sshd-recidive {
type ipv4_addr
elements = { 12.156.67.18, 27.79.0.161,
27.79.3.153, 27.79.4.114,
27.79.5.189, 27.79.6.170,
27.79.40.92, 27.79.44.165,
27.79.45.81, 27.79.45.188,
27.79.47.253, 43.245.249.251,
45.82.122.177, 45.134.26.79,
45.140.17.124, 59.172.153.82,
61.53.69.210, 61.78.62.85,
64.227.66.66, 64.227.134.126,
85.18.236.229, 85.208.253.156,
87.120.191.13, 92.118.39.92,
95.85.114.218, 96.78.175.36,
103.217.145.53, 104.214.117.63,
109.73.199.206, 116.110.23.150,
116.110.156.88, 116.110.157.189,
121.165.204.105, 121.237.180.222,
125.75.148.28, 134.199.174.250,
139.159.97.118, 142.93.135.131,
147.182.152.101, 157.255.155.179,
159.65.155.187, 159.65.157.71,
159.223.237.58, 165.22.195.40,
165.154.168.234, 170.64.168.190,
170.64.178.112, 171.231.176.95,
171.231.190.211, 171.231.192.5,
171.231.192.24, 171.231.198.90,
171.243.151.107, 172.245.240.21,
174.138.6.91, 176.65.148.27,
182.93.50.90, 185.203.236.212,
195.190.104.66, 196.251.70.115,
196.251.80.27, 196.251.83.84,
196.251.84.181, 202.51.214.99,
206.189.109.178, 209.38.111.217,
220.250.41.11 }
}

chain f2b-chain {
type filter hook input priority filter - 1; policy accept;
tcp dport 22 ip saddr @addr-set-sshd-stage1 counter packets 6 bytes 360 reject with icmp port-unreachable comment "fail2ban-sshd-stage1"
tcp dport 22 ip saddr @addr-set-sshd-stage2 counter packets 0 bytes 0 reject with icmp port-unreachable comment "fail2ban-sshd-stage2"
ip saddr @addr-set-sshd-recidive counter packets 0 bytes 0 reject with icmp port-unreachable comment "fail2ban-sshd-recidive"
}
}

1. sshd_configの設定

2点、修正します。sshd_configを修正してください。( /etc/ssh/sshd_config 、/etc/ssh/ssh_configではないので注意)

  • LoginGraceTimeを 2m から 10 へ
    • 私のサーバだと10秒にした時点でssh攻撃がほとんど止まるので、正しく設定できているか確認する為にこの設定は後でもよいかも。
    • 後にする場合は LoginGraceTime が既定では 「#LoginGrace Time 2m」とコメントアウトになっているので、先頭の「#」を削除してください。
  • PasswordAuthentication no を追加する

こんな感じになるはず。修正箇所を赤太字にしてます。中略あり。

#       $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
LoginGraceTime 10
#PermitRootLogin prohibit-password

<中略>

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

編集方法は自由ですが、既定のコメントアウト位置の近くにこだわりがないなら sshd_config 末尾に LoginGraceTime 10とPasswordAuthentication noを追加するだけでもOKです。

設定ができたらここで sshd を再起動させましょう。Debian 13 は systemd で行うので以下のコマンドです。なお、接続中のsshセッションは切断されないので、設定不備がある場合はセッションを切断せずにエラー箇所を修正してください。

sudo systemctl restart ssh.service

sshdの動作状況確認

systemdでの捕捉状況

% sudo systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/usr/lib/systemd/system/ssh.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-09-25 11:39:36 JST; 49s ago
 Invocation: 80a333807aee4d749b1a70bb21914d18
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 1619 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 1621 (sshd)
      Tasks: 1 (limit: 2317)
     Memory: 1.3M (peak: 2.3M)
        CPU: 11ms
     CGroup: /system.slice/ssh.service
             mq1621 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Sep 25 11:39:36 yordeb2 systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Sep 25 11:39:36 yordeb2 sshd[1621]: Server listening on 0.0.0.0 port 22.
Sep 25 11:39:36 yordeb2 sshd[1621]: Server listening on :: port 22.
Sep 25 11:39:36 yordeb2 systemd[1]: Started ssh.service - OpenBSD Secure Shell server.

systemdのsshサービスのログの確認方法(参考)。rsyslogをインストールして /var/log/syslog を見るほうが慣れてる場合はそっちでもOK

% sudo journalctl -u ssh.service -n 100

Sep 25 11:07:22 yordeb2 systemd[1]: Stopping ssh.service - OpenBSD Secure Shell server...
Sep 25 11:07:23 yordeb2 systemd[1]: ssh.service: Deactivated successfully.
Sep 25 11:07:23 yordeb2 systemd[1]: Stopped ssh.service - OpenBSD Secure Shell server.
-- Boot --
Sep 25 11:07:37 yordeb2 systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Sep 25 11:07:37 yordeb2 sshd[794]: Server listening on 0.0.0.0 port 22.
Sep 25 11:07:37 yordeb2 sshd[794]: Server listening on :: port 22.
Sep 25 11:07:37 yordeb2 systemd[1]: Started ssh.service - OpenBSD Secure Shell server.
Sep 25 11:07:43 yordeb2 sshd-session[1382]: Accepted publickey for [username] from [IP_ADDRESS] port [PORT] ssh2: ED25519 SHA256:[KEY_FINGERPRINT]
Sep 25 11:07:43 yordeb2 sshd-session[1382]: pam_unix(sshd:session): session opened for user [username](uid=[uid]) by [username](uid=[uid])
Sep 25 11:08:22 yordeb2 sshd[794]: Timeout before authentication for connection from 115.190.24.246 to 192.168.100.12, pid = 1423
Sep 25 11:10:09 yordeb2 sshd-session[1431]: Received disconnect from 80.94.93.233 port 52220:11:  [preauth]
Sep 25 11:10:09 yordeb2 sshd-session[1431]: Disconnected from authenticating user root 80.94.93.233 port 52220 [preauth]
Sep 25 11:12:07 yordeb2 sshd-session[1437]: Invalid user system from 185.156.73.233 port 51746
Sep 25 11:12:07 yordeb2 sshd-session[1437]: Connection closed by invalid user system 185.156.73.233 port 51746 [preauth]
Sep 25 11:15:30 yordeb2 sshd-session[1455]: Connection closed by authenticating user root 140.246.80.125 port 59910 [preauth]
Sep 25 11:15:57 yordeb2 sshd-session[1458]: Received disconnect from 193.46.255.217 port 50480:11:  [preauth]
Sep 25 11:15:57 yordeb2 sshd-session[1458]: Disconnected from authenticating user root 193.46.255.217 port 50480 [preauth]
Sep 25 11:21:40 yordeb2 sshd-session[1478]: Invalid user pi from 80.94.95.115 port 48622
Sep 25 11:21:41 yordeb2 sshd-session[1478]: Connection closed by invalid user pi 80.94.95.115 port 48622 [preauth]
Sep 25 11:35:31 yordeb2 sshd-session[1567]: Invalid user user from 194.0.234.19 port 48858
Sep 25 11:35:31 yordeb2 sshd-session[1567]: Connection closed by invalid user user 194.0.234.19 port 48858 [preauth]
Sep 25 11:38:13 yordeb2 sshd-session[1586]: Received disconnect from 80.94.93.176 port 10732:11:  [preauth]
Sep 25 11:38:13 yordeb2 sshd-session[1586]: Disconnected from authenticating user root 80.94.93.176 port 10732 [preauth]
Sep 25 11:39:36 yordeb2 systemd[1]: Stopping ssh.service - OpenBSD Secure Shell server...
Sep 25 11:39:36 yordeb2 systemd[1]: ssh.service: Deactivated successfully.
Sep 25 11:39:36 yordeb2 systemd[1]: Stopped ssh.service - OpenBSD Secure Shell server.
Sep 25 11:39:36 yordeb2 systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Sep 25 11:39:36 yordeb2 sshd[1621]: Server listening on 0.0.0.0 port 22.
Sep 25 11:39:36 yordeb2 sshd[1621]: Server listening on :: port 22.
Sep 25 11:39:36 yordeb2 systemd[1]: Started ssh.service - OpenBSD Secure Shell server.
Sep 25 11:40:46 yordeb2 sshd-session[1638]: Connection closed by authenticating user root 14.225.198.72 port 36510 [preauth]

2. fail2ban の設定

ようやく、fail2banの設定です。fail2banはDebian標準リポジトリにあるので、apt でインストールしてください。

% apt install fail2ban

記事執筆時点のバージョン

sudo dpkg --list | grep fail2ban
ii fail2ban 1.1.0-8 all ban hosts that cause multiple authentication errors

いきなりですが、fail2ban既定設定と競合するので、既定設定側を無効にします。既定設定は検証していませんが、何か自動でブロックしてくれてるので、fail2banをインストールするだけでも効果があるかも?

/etc/fail2ban/jail.d/defaults-debian.conf の [sshd] の3行下の enabled = true を enabled = false にしてください。

% sudo 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 = false

新しく、fail2banの設定を追加します。現在私の設定値をそのまま貼り付けます。パラメータはよく検証してください。設定内容の品質は一切保証できません。

動作の概要としては、stage1が3分3回失敗で30分BAN、stage2が24時間15回失敗で1週間BAN、stage3はstage1/stage2で2回BANした場合は90日BANとする設定です。
stage1/stage2は systemd からストリームで情報を取得して効率化(ファイルをtail -fして監視するより効率的)し、stage3は fail2banのサービスが作成するログファイルを tail -fしてBANする設定です。よって、このfail2banのログはlogrotateが必要なのでこの章の最後に設定例を記載します。

% sudo vi /etc/fail2ban/jail.d/sshd-custom.conf
# ステージ1: 短期間での連続攻撃対策
[sshd-stage1]
enabled = true
backend = systemd
journalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshd
port = ssh
filter = sshd
action = nftables-counter[name=sshd-stage1, port=ssh]
# 3回失敗で
maxretry = 3
# 3分間(180秒)で判定
findtime = 180
# 30分間ban
bantime = 1800
ignoreip = 127.0.0.1/8 ::1

# ステージ2: 1日での攻撃回数制限
[sshd-stage2]
enabled = true
backend = systemd
journalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshd
port = ssh
filter = sshd
action = nftables-counter[name=sshd-stage2, port=ssh]
# 15回失敗で
maxretry = 15
# 24時間(86400秒)で判定
findtime = 86400
# 7days ban(604800秒)
bantime = 604800
ignoreip = 127.0.0.1/8 ::1

# ステージ3: 繰り返し攻撃者への長期ban
[sshd-recidive]
enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
action = nftables-recidive-counter[name=sshd-recidive]
# 2回banされたら
maxretry = 2
# 7日間で判定
findtime = 604800
# 90日間ban
bantime = 7776000
ignoreip = 127.0.0.1/8 ::1

どれだけブロックしているか、カウンター情報を取りたいので、action = nftables-counter[name=sshd-stage1, port=ssh] と action = nftables-recidive-counter[name=sshd-recidive] を使っています。このカウンター情報が不要な場合はこの action のスクリプトが不要になるので、先頭に「#」を追加してコメントアウトしてください。私と同じカウンターを取る場合は次のファイルを新規追加してください。

/etc/fail2ban/action.d% ls -ltr  | tail -n 5
-rw-r--r-- 1 root root 1413 Apr 26 2024 apprise.conf
-rw-r--r-- 1 root root 587 Apr 26 2024 apf.conf
-rw-r--r-- 1 root root 3748 Apr 26 2024 abuseipdb.conf
-rw-r--r-- 1 root root 1050 Sep 6 00:42 nftables-counter.conf
-rw-r--r-- 1 root root 968 Sep 6 00:42 nftables-recidive-counter.conf
/etc/fail2ban/action.d% cat nftables-counter.conf
[Definition]

# テーブル・セット・チェーン・ルールを一度だけ作成
actionstart = nft add table inet f2b-table 2>/dev/null || true
              nft add chain inet f2b-table f2b-chain '{ type filter hook input priority filter - 1; policy accept; }' 2>/dev/null || true
              nft add set inet f2b-table addr-set-<name> '{ type ipv4_addr; }' 2>/dev/null || true
              nft add rule inet f2b-table f2b-chain tcp dport <port> ip saddr @addr-set-<name> counter reject comment \"fail2ban-<name>\" 2>/dev/null || true

# セットのみ削除(ルールは残す)
actionstop = nft delete set inet f2b-table addr-set-<name> 2>/dev/null || true

# 存在確認
actioncheck = nft list set inet f2b-table addr-set-<name> >/dev/null 2>&1

# IPを追加/削除するだけ(ルールは触らない)
actionban = nft add element inet f2b-table addr-set-<name> '{ <ip> }' 2>/dev/null || true

actionunban = nft delete element inet f2b-table addr-set-<name> '{ <ip> }' 2>/dev/null || true

[Init]
name = default
port = ssh
/etc/fail2ban/action.d% cat nftables-recidive-counter.conf
[Definition]

# テーブル・セット・チェーン・ルールを一度だけ作成
actionstart = nft add table inet f2b-table 2>/dev/null || true
              nft add chain inet f2b-table f2b-chain '{ type filter hook input priority filter - 1; policy accept; }' 2>/dev/null || true
              nft add set inet f2b-table addr-set-<name> '{ type ipv4_addr; }' 2>/dev/null || true
              nft add rule inet f2b-table f2b-chain ip saddr @addr-set-<name> counter reject comment \"fail2ban-<name>\" 2>/dev/null || true

# セットのみ削除
actionstop = nft delete set inet f2b-table addr-set-<name> 2>/dev/null || true

# 存在確認
actioncheck = nft list set inet f2b-table addr-set-<name> >/dev/null 2>&1

# IPを追加/削除するだけ
actionban = nft add element inet f2b-table addr-set-<name> '{ <ip> }' 2>/dev/null || true

actionunban = nft delete element inet f2b-table addr-set-<name> '{ <ip> }' 2>/dev/null || true

[Init]
name = default

nftables-counter.conf が stage1/stage2 (短期的な攻撃者をブロック) する為の action スクリプトで、
nftables-recidive-counter.conf は しつこい攻撃者を長期的にブロックする用の action スクリプトです。

ここまで設定できましたら、fail2banを再起動させます。

% sudo systemctl restart fail2ban.service

fail2banの自動起動設定がdisableになっている場合は自動起動を有効にします。

% sudo systemctl is-enabled fail2ban.service
enabled
↑disableの場合は以下を実行する
% sudo systemctl enabled fail2ban.service

/var/log/fail2ban.log のログローテート設定を追加します。

% sudo vi /etc/logrotate.d/fail2ban
/var/log/fail2ban.log {

    weekly
    rotate 4
    compress
    # Do not rotate if empty
    notifempty

    delaycompress
    missingok
    postrotate
        fail2ban-client flushlogs 1>/dev/null
    endscript

    # If fail2ban runs as non-root it still needs to have write access
    # to logfiles.
    # create 640 fail2ban adm
    create 640 root adm
}

3. nftables の確認/設定

既定値でnftablesが導入されていない、自動起動していない場合がありますので、確認します。

systemdのワードとして、enabledが自動起動の有無、active/inactiveがプロセスの存在有無という感じです。

現在の設定を確認

% sudo systemctl status nftables
● nftables.service - nftables
     Loaded: loaded (/usr/lib/systemd/system/nftables.service; enabled; preset: enabled)
     Active: active (exited) since Thu 2025-09-25 11:07:36 JST; 56min ago
 Invocation: 489f397c980845f9bf54bdae9a357153
       Docs: man:nft(8)
             http://wiki.nftables.org
    Process: 289 ExecStart=/usr/sbin/nft -f /etc/nftables.conf (code=exited, status=0/SUCCESS)
   Main PID: 289 (code=exited, status=0/SUCCESS)
   Mem peak: 4M
        CPU: 6ms

Sep 25 11:07:36 yordeb2 systemd[1]: Finished nftables.service - nftables.
Notice: journal has been rotated since unit was started, output may be incomplete.

自動起動するようになっているか?

% sudo systemctl is-enabled nftables
enabled

この値が「disable」になっている場合は以下で自動起動を有効にしてください。

% sudo systemctl enabled nftables

4. ブロック状況確認

% sudo fail2ban-client status
Status
|- Number of jail:      3
`- Jail list:   sshd-recidive, sshd-stage1, sshd-stage2

% sudo fail2ban-client status | grep "Jail list" | sed 's/.*Jail list://' | tr ',' '\n' | while read jail; do echo "=== $jail ==="; sudo fail2ban-client status $jail; done
=== sshd-recidive ===
Status for the jail: sshd-recidive
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     0
|  `- File list:        /var/log/fail2ban.log
`- Actions
   |- Currently banned: 67
   |- Total banned:     67
   `- Banned IP list:   103.217.145.53 104.214.117.63 109.73.199.206 116.110.156.88 116.110.157.189 116.110.23.150 12.156.67.18 121.165.204.105 121.237.180.222 125.75.148.28 134.199.174.250 139.159.97.118 142.93.135.131 147.182.152.101 157.255.155.179 159.223.237.58 159.65.155.187 159.65.157.71 165.154.168.234 165.22.195.40 170.64.168.190 170.64.178.112 171.231.176.95 171.231.190.211 171.231.192.24 171.231.192.5 171.231.198.90 171.243.151.107 172.245.240.21 174.138.6.91 176.65.148.27 182.93.50.90 185.203.236.212 195.190.104.66 196.251.70.115 196.251.80.27 196.251.83.84 196.251.84.181 202.51.214.99 206.189.109.178 209.38.111.217 220.250.41.11 27.79.0.161 27.79.3.153 27.79.4.114 27.79.40.92 27.79.44.165 27.79.45.188 27.79.45.81 27.79.47.253 27.79.5.189 27.79.6.170 43.245.249.251 45.134.26.79 45.140.17.124 45.82.122.177 59.172.153.82 61.53.69.210 61.78.62.85 64.227.134.126 64.227.66.66 85.18.236.229 85.208.253.156 87.120.191.13 92.118.39.92 95.85.114.218 96.78.175.36
=== sshd-stage1 ===
Status for the jail: sshd-stage1
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     6
|  `- Journal matches:  _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
   |- Currently banned: 0
   |- Total banned:     5
   `- Banned IP list:
=== sshd-stage2 ===
Status for the jail: sshd-stage2
|- Filter
|  |- Currently failed: 5
|  |- Total failed:     6
|  `- Journal matches:  _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
   |- Currently banned: 56
   |- Total banned:     62
   `- Banned IP list:   101.126.151.131 101.36.109.176 103.140.73.162 103.186.1.120 103.186.188.211 103.187.146.107 103.49.238.99 104.167.222.134 115.21.183.150 122.170.148.252 128.199.252.176 150.109.244.181 152.200.205.179 152.32.218.149 154.221.18.60 165.154.255.47 167.71.245.96 178.94.142.216 181.228.68.232 185.50.38.169 188.166.191.39 191.37.72.46 196.0.120.211 199.195.248.191 20.107.168.239 202.39.251.216 202.51.214.98 209.38.34.79 211.201.163.70 221.213.129.46 24.55.171.147 34.91.0.68 35.244.25.124 37.193.112.180 4.211.84.189 43.225.158.4 45.127.4.231 45.134.26.79 45.135.232.177 45.135.232.92 45.140.17.124 45.148.10.240 45.61.187.220 45.78.196.250 46.245.82.12 49.207.186.17 51.38.238.8 54.38.242.11 59.144.223.126 60.199.224.2 61.80.237.194 62.60.193.127 74.208.111.131 85.208.253.184 91.205.219.185 92.5.88.159

nftables(ファイアウォール)の状況確認

% sudo nft list table inet f2b-table
table inet f2b-table {
        set addr-set-sshd-stage1 {
                type ipv4_addr
        }

        set addr-set-sshd-stage2 {
                type ipv4_addr
                elements = { 4.211.84.189, 20.107.168.239,
                             24.55.171.147, 34.91.0.68,
                             35.244.25.124, 37.193.112.180,
                             43.225.158.4, 45.61.187.220,
                             45.78.196.250, 45.127.4.231,
                             45.134.26.79, 45.135.232.92,
                             45.135.232.177, 45.140.17.124,
                             45.148.10.240, 46.245.82.12,
                             49.207.186.17, 51.38.238.8,
                             54.38.242.11, 59.144.223.126,
                             60.199.224.2, 61.80.237.194,
                             62.60.193.127, 74.208.111.131,
                             85.208.253.184, 91.205.219.185,
                             92.5.88.159, 101.36.109.176,
                             101.126.151.131, 103.49.238.99,
                             103.140.73.162, 103.186.1.120,
                             103.186.188.211, 103.187.146.107,
                             104.167.222.134, 115.21.183.150,
                             122.170.148.252, 128.199.252.176,
                             150.109.244.181, 152.32.218.149,
                             152.200.205.179, 154.221.18.60,
                             165.154.255.47, 167.71.245.96,
                             178.94.142.216, 181.228.68.232,
                             185.50.38.169, 188.166.191.39,
                             191.37.72.46, 196.0.120.211,
                             199.195.248.191, 202.39.251.216,
                             202.51.214.98, 209.38.34.79,
                             211.201.163.70, 221.213.129.46 }
        }

        set addr-set-sshd-recidive {
                type ipv4_addr
                elements = { 12.156.67.18, 27.79.0.161,
                             27.79.3.153, 27.79.4.114,
                             27.79.5.189, 27.79.6.170,
                             27.79.40.92, 27.79.44.165,
                             27.79.45.81, 27.79.45.188,
                             27.79.47.253, 43.245.249.251,
                             45.82.122.177, 45.134.26.79,
                             45.140.17.124, 59.172.153.82,
                             61.53.69.210, 61.78.62.85,
                             64.227.66.66, 64.227.134.126,
                             85.18.236.229, 85.208.253.156,
                             87.120.191.13, 92.118.39.92,
                             95.85.114.218, 96.78.175.36,
                             103.217.145.53, 104.214.117.63,
                             109.73.199.206, 116.110.23.150,
                             116.110.156.88, 116.110.157.189,
                             121.165.204.105, 121.237.180.222,
                             125.75.148.28, 134.199.174.250,
                             139.159.97.118, 142.93.135.131,
                             147.182.152.101, 157.255.155.179,
                             159.65.155.187, 159.65.157.71,
                             159.223.237.58, 165.22.195.40,
                             165.154.168.234, 170.64.168.190,
                             170.64.178.112, 171.231.176.95,
                             171.231.190.211, 171.231.192.5,
                             171.231.192.24, 171.231.198.90,
                             171.243.151.107, 172.245.240.21,
                             174.138.6.91, 176.65.148.27,
                             182.93.50.90, 185.203.236.212,
                             195.190.104.66, 196.251.70.115,
                             196.251.80.27, 196.251.83.84,
                             196.251.84.181, 202.51.214.99,
                             206.189.109.178, 209.38.111.217,
                             220.250.41.11 }
        }

        chain f2b-chain {
                type filter hook input priority filter - 1; policy accept;
                tcp dport 22 ip saddr @addr-set-sshd-stage1 counter packets 20 bytes 1200 reject with icmp port-unreachable comment "fail2ban-sshd-stage1"
                tcp dport 22 ip saddr @addr-set-sshd-stage2 counter packets 0 bytes 0 reject with icmp port-unreachable comment "fail2ban-sshd-stage2"
                ip saddr @addr-set-sshd-recidive counter packets 0 bytes 0 reject with icmp port-unreachable comment "fail2ban-sshd-recidive"
        }
}

chain f2b-chain で packets 20 bytes 1200 reject が action スクリプトによるカウンターの確認ができます。

5. 再起動

自動起動設定などの状況確認のためにOSを再起動して、4.のブロック状況確認を実行してください。fail2banは過去ログを自動でさかのぼる設定が既定値ですので、OS再起動しても、しっかりブロックしてくれるはずです。

コメント

タイトルとURLをコピーしました