1Panel icon indicating copy to clipboard operation
1Panel copied to clipboard

[Bug] Fail2ban misses sshd-session logs on Debian 13 with OpenSSH 10, matching only 3.8% of SSH failures

Open 2286721642 opened this issue 2 weeks ago • 1 comments

Contact Information

No response

Product

1Panel

Version

v2.5.1

Problem Description

Debian 13 + OpenSSH 10 环境下,1Panel 托管的 Fail2ban 显示“已启用”、jail 正常运行,但几乎不封禁攻击 IP。24 小时内 12465 条 SSH 认证失败事件中,仅 476 条被计入(约 3.8%),其余全部静默丢弃。

用 fail2ban-regex 对同一批真实日志做对照:

面板默认(mode=normal) 匹配 476 忽略 10746 遗漏 1243 修复后(mode=aggressive 等) 匹配 12465 忽略 0 遗漏 0

日志按 _COMM 统计: 14161 _COMM=sshd-session 585 _COMM=sshd

共三个原因:

  1. journalmatch 漏掉 sshd-session(主因) OpenSSH 10 把每个连接的认证拆到独立 sshd-session 进程,绝大多数认证失败日志的 _COMM 是 sshd-session。fail2ban 1.1.0 自带的 filter.d/sshd.conf 已正确处理,但 Debian 包的 /etc/fail2ban/jail.d/defaults-debian.conf 中写的是: journalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshd 该文件会覆盖 filter 中的正确值,导致约 96% 的日志根本未被读取。 而 initLocalFile() 生成模板时未显式设置 backend/journalmatch,完全依赖发行版默认值。

  2. 模板未指定 mode,默认 normal 覆盖率过低 normal 模式把 Connection closed by authenticating ... [preauth] 标为 直接忽略,而这类日志正是 OpenSSH 10 在 PerSourcePenalties 抑制下的主要表现形式。此外 OpenSSH 10 措辞已变更: 旧: Timeout before authentication for 新: Timeout before authentication for connection from to <内网IP>, pid = N 内置正则无法匹配新措辞。

  3. banaction 探测误判 ufw CheckActive("ufw") 只要 ufw.service 处于 active 就选 ufw,但 service active ≠ ufw 已启用。实测该机器 systemctl is-active ufw 为 active,而 ufw status 为 inactive,导致写入 banaction = ufw 后封禁规则不落内核。

Steps to Reproduce

  1. 在 Debian 13 上通过 1Panel 工具箱安装并启用 Fail2ban;
  2. 等待服务器遭受 SSH 扫描(公网 22 端口通常数分钟内即有);
  3. 执行 fail2ban-client status sshd,观察 Total failed 与实际攻击量严重不符;
  4. 执行 fail2ban-client banned,封禁列表长期为空或仅 1~2 个 IP;
  5. 执行 fail2ban-client get sshd journalmatch,可见仅有 _COMM=sshd,缺少 _COMM=sshd-session。

The expected correct result

  1. 面板初始化 Fail2ban 后,sshd jail 应能实际拦截绝大多数 SSH 攻击,而非不足 4%;
  2. 在 OpenSSH 10 / Debian 13 环境下开箱即用,无需用户手工调整 filter 与 journalmatch;
  3. banaction 探测应基于防火墙实际生效状态,而非 service 是否 active。

Related log output

# fail2ban-client get sshd journalmatch
Current match filter:
_SYSTEMD_UNIT=ssh.service + _COMM=sshd

# systemctl is-active ufw   /   ufw status
active
ERROR: You need to be root to run this script

# 日志按 _COMM 分布(24h)
  14161  _COMM=sshd-session
    585  _COMM=sshd

# fail2ban-regex 对照(样本 12465 行)
normal       Lines: 12465 lines, 10746 ignored, 476 matched, 1243 missed
aggressive   Lines: 12465 lines, 0 ignored, 12167 matched, 298 missed
修复后       Lines: 12465 lines, 0 ignored, 12465 matched, 0 missed

# 典型未被计数的日志
Connection closed by authenticating user root 221.10.42.66 port 51184 [preauth]
Timeout before authentication for connection from 101.96.221.196 to 172.16.0.5, pid = 269850

Additional Information

【源码位置】

  1. agent/utils/toolbox/fail2ban.go → initLocalFile() 模板中 [sshd] 段只写了 logpath,未声明 backend / journalmatch / mode, 完全依赖发行版默认值,在 OpenSSH 10 + Debian 13 上直接失效。

    建议:

    • 显式写入 journalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshd + _COMM=sshd-session
    • 显式设置 mode = aggressive
    • 为 OpenSSH 10 新措辞补一条 failregex
  2. 同一函数中的 banaction 探测逻辑: if active, _ := controller.CheckActive("firewalld"); active { banaction = "firewallcmd-ipset" } else if active, _ := controller.CheckActive("ufw"); active { banaction = "ufw" } else { banaction = "iptables-allports" } 建议改用 ufw status 实际启用状态判断,或优先探测 nftables。

【附带发现(另开 Issue)】agent/app/service/fail2ban.go → UpdateConf()

其匹配逻辑使用 strings.HasPrefix: if strings.HasPrefix(line, req.Key) {

修改 bantime 时会把同前缀的键一并覆盖删除。例如 jail.local 中: bantime = 3600 bantime.increment = true bantime.factor = 2 bantime.maxtime = 1w 用户在面板点击“禁用时间 → 设置”保存后,这四行全部被改写成单行 bantime = 3600, 递增封禁配置被静默清除且无任何提示。logpath / port / maxretry / findtime 同样受影响。

同时 loadFailValue() 读取也用前缀匹配,会把 bantime.increment = true 解析成 .increment true, 若 [sshd] 段未重新定义 bantime,面板会显示异常值。

建议改为按 = 拆分后精确比较: if key, _, ok := strings.Cut(line, "="); ok && strings.TrimSpace(key) == req.Key {

【说明】 defaults-debian.conf 覆盖 filter 默认值本身属于 Debian 打包侧的滞后问题, 但 1Panel 作为面板层完全可以在生成模板时显式声明正确配置来规避, 目前实现依赖了不可靠的默认值。

2286721642 avatar Nov 18 '26 04:11 2286721642

感谢反馈,下个版本针对该部分进行优化

ssongliu avatar Nov 19 '26 07:11 ssongliu