build(etc): 完善 Fail2ban 防爆破配置

- 将 SSH 封禁改为端口无关的 allports 策略
- 新增 Gitea SSH 双链防护和失败日志过滤器
- 补充公共策略、再犯封禁及部署验证文档
This commit is contained in:
cnphpbb
2026-11-08 23:37:44 +08:00
parent db3749375c
commit a13785c6e2
9 changed files with 246 additions and 14 deletions
+36
View File
@@ -0,0 +1,36 @@
# fail2ban —— 所有 jail 共用的策略(公用版本)
#
# 文件名 00- 前缀是刻意为之:fail2ban 读取顺序是
# jail.conf -> jail.d/*.conf -> jail.local -> jail.d/*.local(按名排序)
# 所以 jail.d/*.local 反而会覆盖 jail.local。00- 保证本文件的 [DEFAULT]
# 先于 sshd.local / gitea-ssh.local / recidive.local 加载,让它们能按需覆盖单 jail 设置。
[DEFAULT]
# ── 端口无关封禁(本方案的核心)────────────────────────────────
# sshd 端口在各机器上落在 49152-65535 内且每台不同,写死 port 必然出错。
# nftables 的 type=allports 生成的规则是:
# meta l4proto { tcp } <family> saddr @<set> reject
# 完全不引用 <port>,因此端口是多少都不影响封禁是否生效。
# 对比 type=multiport 会生成 tcp dport { <port> },端口写错 = 检测到但封不住。
banaction = nftables[type=allports]
banaction_allports = nftables[type=allports]
# ── 安全默认 ──────────────────────────────────────────────
# usedns=no:不信任反向解析(默认 warn 会解析主机名,可被 DNS 欺骗/拖慢)
usedns = no
# ignoreself:永不封本机
ignoreself = true
# ── 白名单 ────────────────────────────────────────────────
# 必须包含管理出口 IP,否则管理员自己会被封(fail2ban-client set banip 还会绕过本项)。
# 保留 RFC1918:内网探测/监控/tailscale 等不应触发封禁。
ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 183.238.148.253/32
# ── 递增封禁:1d -> 2d -> 4d -> ... 上限 4w ─────────────────
bantime = 1d
findtime = 10m
maxretry = 3
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 4w
+21
View File
@@ -0,0 +1,21 @@
# Gitea 内置 SSH 服务(容器内监听 2222,宿主发布端口不固定)
#
# 日志路径必须稳定:默认的 docker json-file 路径内嵌容器 ID,
# 容器一重建就静默失效(fail2ban 不报错,只是再也不封)。
# 因此要求 Gitea 用 file 模式写到挂载卷,见 docs/gitea-fail2ban-app-ini.md。
[gitea-ssh]
enabled = true
filter = gitea-ssh
logpath = /var/lib/gitea/log/gitea.log
maxretry = 5
findtime = 10m
bantime = 1d
# 双 action:默认 nftables chain 只 hook input,而 Docker 对发布端口做 DNAT 后
# 走 forward 链,容器流量根本不经过 input。只挂 input 会出现「封禁列表里有 IP、
# 但攻击者照样能连容器端口」。
# 第二条把同一条黑名单再挂到 forward,priority -10 早于 Docker 的
# table ip filter FORWARD(priority 0),确保先于 docker 规则丢弃。
action = nftables[type=allports]
nftables[type=allports, table=f2b-fwd, chain=f2b-fwd-chain, chain_hook=forward, chain_priority=-10]
+13
View File
@@ -0,0 +1,13 @@
# 再犯 jail:只读 fail2ban 自己的日志,把「封过又重新犯」的 IP 长封,
# 作为 bantime.increment 之外的兜底。
#
# ⚠ 数据源是 fail2ban 自己的日志文件,不是 journald:
# 默认 fail2ban.conf 里 logtarget = /var/log/fail2ban.log,
# "Ban"/"Unban" 行只进这个文件,不进 journal。
# 若某台机器把 logtarget 改成了 SYSLOG/STDOUT,这里要相应改 backend。
[recidive]
enabled = true
logpath = /var/log/fail2ban.log
maxretry = 3
findtime = 1d
bantime = 4w
-8
View File
@@ -1,8 +0,0 @@
[sshd]
enabled = true
maxretry = 3
findtime = 10m
bantime = 1h
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 1w
+14 -5
View File
@@ -1,10 +1,19 @@
[sshd]
enabled = true
backend = systemd
journalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshd + _COMM=sshd-session
# ssh.service (Debian/Ubuntu) 与 sshd.service (RHEL/Fedora) 都要覆盖。
# OpenSSH 9.8+ 把会话进程拆成 sshd-session,只写 _COMM=sshd 会漏掉真正的认证失败行。
# journal 字段匹配:'+' = 组间 OR,空格 = 组内 AND。
journalmatch = _SYSTEMD_UNIT=ssh.service + _SYSTEMD_UNIT=sshd.service + _COMM=sshd + _COMM=sshd-session
# normal 足以覆盖密码/公钥爆破;aggressive 会把 "Connection closed by" 等
# 正常客户端抖动也算失败,配合 maxretry=3 容易误封,故不用。
mode = normal
maxretry = 3
findtime = 10m
bantime = 24h
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 7d
bantime = 1d
# 刻意不写 port:封禁走 allports(见 jail.d/00-common.local),
# 因此本机 sshd 端口落在 49152-65535 的哪个值都无需知道。