forked from DevOps/deploy.stack
fix(etc): 修复 Fail2ban 双链配置冲突
- 为 Gitea SSH 的 input 和 forward action 设置独立名称 - 固定 sshd 过滤器的 publickey=invalid 语义 - 补充 jail 数量检查、action 重名检测和实测说明
This commit is contained in:
@@ -151,7 +151,7 @@ docker compose -p <名称> --env-file ./builder/golang/env.cfg -f ./builder/gola
|
||||
| --- | --- |
|
||||
| `etc/fail2ban/fail2ban.d/00-common.local` | 服务端选项:`dbpurgeage` |
|
||||
| `etc/fail2ban/jail.d/00-common.local` | `[DEFAULT]` 公共策略:allports 封禁 / usedns / ignoreip / 递增封禁 |
|
||||
| `etc/fail2ban/jail.d/sshd.local` | 系统 OpenSSH jail(**不写 `port`**) |
|
||||
| `etc/fail2ban/jail.d/sshd.local` | 系统 OpenSSH jail(**不写 `port`**,`publickey=invalid`) |
|
||||
| `etc/fail2ban/jail.d/gitea-ssh.local` | Gitea 内置 SSH jail(input + forward 双链) |
|
||||
| `etc/fail2ban/jail.d/recidive.local` | 再犯长封 |
|
||||
| `etc/fail2ban/filter.d/gitea-ssh.conf` | Gitea SSH 过滤器 |
|
||||
@@ -163,8 +163,11 @@ docker compose -p <名称> --env-file ./builder/golang/env.cfg -f ./builder/gola
|
||||
- **不要写死 `port`**。sshd 端口每台机器不同、落在 49152-65535。`banaction = nftables[type=allports]` 生成 `meta l4proto { tcp } saddr @<set> reject`,**完全不引用 `<port>`**,从根上消除端口依赖;反过来 `type=multiport` 生成 `tcp dport { <port> }`,端口写错就是「检测得到、封不到」。
|
||||
- **`dbpurgeage` 必须放 `fail2ban.d/`**。它是服务端选项,写在 `jail.d/[DEFAULT]` 里会被**静默忽略**(实测仍为 `1d`),只有 `fail2ban.d/*.local` 生效。默认 1d 会让 `bantime.increment` 的历史被提前清掉,递增涨不到位。
|
||||
- **Docker 里的 Gitea 必须再挂一条 forward 链**。默认 nftables 链只 `hook input`,而 Docker 对发布端口做 DNAT 后走 `forward`,容器流量**不经过 input**;只挂 input 会出现「封禁列表里有 IP、攻击者照样能连容器端口」。
|
||||
- **同一 jail 里的多个 action 必须写不同的 `name=`**。fail2ban 的 `ActionReader` 中 `name` 不写时默认取 **jail 名**,而 `actname` 只在 `name != jailName` 时才加后缀去重——两个 action 同名会撞成同一个 `actname`,`Actions.add()` 直接抛 `ValueError('Action nftables already exists')`,**整个 server-stream 中断**(实测 fail2ban 1.1.0),forward 链根本不会建出来,且现象极隐蔽:`fail2ban-client status` 里 jail 仍在运行。`name` 同时决定 nft set 名(`addr-set-<name>`),不同 `name` 才能落在不同 set。写法:`nftables[type=allports, name=gitea-ssh-in]` + `nftables[type=allports, name=gitea-ssh-fwd, ...]`。
|
||||
- **一个 jail 初始化失败 = 全部 jail 都不启动**。实测 `backend = systemd` 但 `python3-systemd` 缺失或与 python 版本不匹配时报 `Backend 'systemd' failed to initialize`,会**直接中断整个 server-stream**,结果是 **0 个 jail 运行**(`fail2ban-client status` 显示 `Number of jail: 0`)——不是少一个 jail,而是全都不封。部署后必须核对 jail 数量,并把 `Number of jail: 0` 纳入告警。
|
||||
- **改配置后必须 `systemctl restart fail2ban`**,`reload` 不会重建 nftables 链。
|
||||
- **`ignoreip` 必须包含管理出口 IP**(`183.238.148.253/32`)。fail2ban 1.1.0 的 sshd 过滤器把 `Failed publickey`、`pam_unix ... authentication failure`、`Connection closed by invalid user` 全部标为 NOFAIL,真正计入失败的只有 `Failed password` 与 `User X from IP not allowed because not listed in AllowUsers`;在密码登录关闭后,**唯一触发器就是「用非 AllowUsers 用户名登录」**,管理员误用 `root` 登录 3 次即会被自封。
|
||||
- **`ignoreip` 必须包含管理出口 IP**(`183.238.148.253/32`)。fail2ban 1.1.0 的 sshd 过滤器实测:**计入失败**的有 `Failed password for ...`、`Failed publickey for invalid user ...`、`Invalid user ...`、`User X ... not allowed because not listed in AllowUsers`、`maximum authentication attempts exceeded ...`;**标为 NOFAIL**(不计失败)的有 `Failed publickey for <有效用户>`、`pam_unix ... authentication failure`、`Connection closed by invalid user`。在密码登录关闭后,触发点是「用非 AllowUsers 用户名登录」和「对不存在用户名的公钥尝试」——管理员误用 `root` 登录 3 次即会被自封。
|
||||
- **`sshd.local` 显式写 `filter = sshd[mode=normal, publickey=invalid]`**。`publickey` 四种取值的实测命中数(8 行样本):`nofail` 6、**`invalid` 6**、`any` 7、`ignore` 4。`invalid` 与默认值 `nofail` 封禁集合相同(两者都把 `Failed publickey for invalid user` 计入失败),区别只是「有效用户用错 key」那行从 NOFAIL 记录变成完全不匹配;显式写是为了锁定语义、防上游改默认值。**不要用 `any`**(有效用户用错 key 3 次即被封),**不要用 `ignore`**(漏掉 invalid user 的公钥爆破)。
|
||||
- **`fail2ban-client set <jail> banip <IP>` 会绕过 `ignoreip`**(管理员强制操作)。切勿用它测试白名单,否则会把管理出口直接封死。验证白名单请用 `fail2ban-client -d | grep ignoreip`。
|
||||
- `journalmatch = _SYSTEMD_UNIT=ssh.service + _SYSTEMD_UNIT=sshd.service + _COMM=sshd + _COMM=sshd-session`:systemd 对同一字段的多个值按 **OR** 合并(不同字段之间才是 AND)。`ssh.service`/`sshd.service` 覆盖 Debian/RHEL 差异;OpenSSH 9.8+ 把会话进程拆成 `sshd-session`,只写 `_COMM=sshd` 会漏掉真正的认证失败行。
|
||||
- **`gitea-ssh.local` 的 `logpath` 必须指向稳定文件**。docker json-file 路径内嵌容器 ID,容器重建后**静默失效**(不报错,只是不再封)。要求 Gitea 用 `[log] MODE=file` 写到挂载卷,见 README。
|
||||
@@ -175,10 +178,11 @@ docker compose -p <名称> --env-file ./builder/golang/env.cfg -f ./builder/gola
|
||||
|
||||
```bash
|
||||
fail2ban-client -t # 配置校验
|
||||
fail2ban-client status # 已启用 jail
|
||||
fail2ban-client status # 已启用 jail(Number of jail 应为 3:sshd / gitea-ssh / recidive)
|
||||
fail2ban-client status sshd # 单 jail 状态
|
||||
fail2ban-client -d | grep dbpurgeage # 确认服务端选项真的生效
|
||||
fail2ban-client -d | grep -c dport # 应为 0(说明走的是 allports)
|
||||
fail2ban-client -d | grep -c "already exists" # 应为 0(多 action 同名会导致 server-stream 中断)
|
||||
nft list table inet f2b-table # input 链 + IP 集合
|
||||
nft list table inet f2b-fwd # forward 链(仅 Docker 场景)
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user