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

[Bug] Fail2ban basic settings overwrite prefixed keys such as bantime.increment and create duplicate entries

Open 2286721642 opened this issue 2 weeks ago • 1 comments

Contact Information

No response

Product

1Panel

Version

v2.5.1

Problem Description

在 1Panel 工具箱的 Fail2ban「基础配置」中修改任意参数(如禁用时间),会破坏 jail.local 中所有同前缀的配置键。

这是因为 agent/app/service/fail2ban.go 的 UpdateConf() 使用 strings.HasPrefix 做键匹配:

if strings.HasPrefix(line, req.Key) {
    hasKey = true
    newFile += fmt.Sprintf("%s = %s\n", req.Key, req.Value)
    continue
}

以 bantime 为例,Fail2ban 官方支持 bantime.increment / bantime.factor / bantime.maxtime 等扩展键(用于递增封禁)。用户在 [sshd] 段写入:

bantime = 3600
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 1w

在面板点击「禁用时间 → 设置」改为 7200 后,这 4 行会全部被替换成 4 个内容相同的行:

bantime = 7200
bantime = 7200
bantime = 7200
bantime = 7200

即:① 递增封禁配置被静默清除;② 产生重复键(fail2ban 后值覆盖前值,虽不报错但语义已丢失);③ 全程无任何提示或警告。

同样问题存在于 logpath / port / maxretry / findtime / banaction。

此外读取端 loadFailValue() 也使用相同的前缀匹配:

if strings.HasPrefix(line, "bantime") {
    itemValue := strings.ReplaceAll(line, "bantime", "")
    itemValue = strings.ReplaceAll(itemValue, "=", "")
    baseInfo.BanTime = strings.TrimSpace(itemValue)
}

因此 bantime.increment = true 会被解析成 ".increment true",bantime.factor = 2 解析成 ".factor 2"。若 [sshd] 段未重新定义 bantime(依赖 [DEFAULT] 继承),面板的「禁用时间」输入框会显示这些异常字符串,若用户此时点击保存,异常值会被写回配置文件。

Steps to Reproduce

  1. 进入 1Panel → 工具箱 → Fail2ban;

  2. 切换到「全部配置」,在 [sshd] 段写入以下内容并保存:

    [sshd] enabled = true filter = sshd bantime = 3600 bantime.increment = true bantime.factor = 2 bantime.maxtime = 1w findtime = 300 maxretry = 5

  3. 切回「基础配置」,点击「禁用时间」右侧的「设置」,改为 7200 并保存;

  4. 再切回「全部配置」,可见 [sshd] 段的 4 行 bantime 相关配置已变成 4 个重复的 "bantime = 7200";

  5. 执行 grep -c "^bantime" /etc/fail2ban/jail.local 可确认键已重复。

The expected correct result

  1. 修改 bantime 时,只应替换精确匹配的键 bantime,不应影响 bantime.increment / bantime.factor / bantime.maxtime;

  2. 不产生重复键;

  3. 读取端应正确跳过 bantime.* 扩展键,面板「禁用时间」显示 3600 而非 ".increment true";

  4. 建议改为按 = 拆分后精确比较,例如:

    if key, _, ok := strings.Cut(line, "="); ok && strings.TrimSpace(key) == req.Key {

    读取端同理。

Related log output

# 复现:将 [sshd] 段的 bantime 由 3600 改为 7200

--- 改写前
+++ 改写后
@@ -9,9 +9,9 @@
 [sshd]
 enabled = true
 filter = sshd
-bantime = 3600
-bantime.increment = true
-bantime.factor = 2
-bantime.maxtime = 1w
+bantime = 7200
+bantime = 7200
+bantime = 7200
+bantime = 7200
 findtime = 300
 maxretry = 5

# 读取端解析异常(loadFailValue)
[DEFAULT] 原文: 'bantime = 3600'          ->  面板解析为: '3600'
[DEFAULT] 原文: 'bantime.increment = true' ->  面板解析为: '.increment  true'
[DEFAULT] 原文: 'bantime.factor = 2'       ->  面板解析为: '.factor  2'
[DEFAULT] 原文: 'bantime.maxtime = 1w'     ->  面板解析为: '.maxtime  1w'

Additional Information

【源码位置】

agent/app/service/fail2ban.go

  1. UpdateConf() 写入端,约第 137 行: if strings.HasPrefix(line, req.Key) { HasPrefix 会命中 bantime.increment / bantime.factor / bantime.maxtime 等所有同前缀键。

  2. loadFailValue() 读取端,约第 210 行起: if strings.HasPrefix(line, "bantime") { 同样命中扩展键,且 ReplaceAll("bantime","") 会残留 ".increment" 字符串。

【影响面】

Fail2ban 1.x 的 bantime.increment(递增封禁)是官方推荐用于对抗持续暴力破解的配置, 被静默清除后封禁时长退化为固定值,攻击者可在 bantime 到期后继续尝试,防护效果显著下降。 由于面板不报错、不提示,用户难以察觉配置已被破坏。

【建议修复】

// 写入端
if key, _, ok := strings.Cut(line, "="); ok && strings.TrimSpace(key) == req.Key {

// 读取端
if key, _, ok := strings.Cut(line, "="); ok && strings.TrimSpace(key) == "bantime" {

另建议在改写前对重复键做一次检测,避免历史遗留的重复键继续累积。

2286721642 avatar Nov 18 '26 04:11 2286721642

感谢反馈,下个版本优化该问题

ssongliu avatar Nov 19 '26 05:11 ssongliu