fix(honeypot): 取证记精确地址、封禁用 /64 键,并补建封与拦截日志
线上实测:IPv6 访问蜜罐后收到「你的 IP 已被封禁」,邮件里却是 `2408:…::/64` —— 既不像自己的地址,也说不清封了多大范围;同时用户自己 仍能正常访问,看日志也无从判断到底有没有封。 根因是「取证地址」与「封禁键」混用同一个变量(都取 LimitKey 的折叠值): - 取证丢掉精确 IPv6 后 64 位,事后无法定位到具体主机、没法和其他日志对账; - 邮件只给聚合键,运维第一反应是「不是我」,排查方向直接被带偏。 改动: - Serve 拆 clientIP(ResolveClientIP,精确)与 banKey(LimitKey,/64): DB / 凭据文件 / 告警邮件用前者,封禁 / 探测采样 / 告警去重用后者; - autoban.Options 新增 OnBan 回调(保持纯标准库、不引入 logrus), route.newBanner 注入后每次建封打 WARN:ban_key / reason / duration / expires_at; 中间件 403 分支加 Debug 日志(被封者会持续请求,Info 会被刷屏); - 告警邮件新增「封禁范围」行,仅 ip != ban_key 时渲染(IPv4 不重复显示), 顺带把过时的「已入库」改为「写入取证文件」——凭据早就外置了; - honeypot stats 的来源排行改按 LimitKey 合并,否则 IPv6 轮换地址会把 排行碎成几十条各 1 次的记录;合并口径与封禁完全一致。 测试(每项都做了变异验证:改坏→确认变红→恢复): - TestHoneypotRecordsExactIPv6ButBansByPrefix:精确地址进 DB 与凭据文件、 /64 才是封禁键、同 /64 换地址仍 403; - TestOnBanCallback:建封回调带键/时长/原因,封禁期内重复命中不重复回调; - TestHoneypotAlertTemplateRenders:IPv6 渲染封禁范围、IPv4 不重复; - TestMergeSourcesByKey:同 /64 合并、IPv4 原样、limit 与空输入。
This commit is contained in:
@@ -55,8 +55,27 @@ type Options struct {
|
||||
HoneypotThreshold int
|
||||
// HoneypotDuration 蜜罐独立封禁时长,<= 0 时复用 Duration。
|
||||
HoneypotDuration time.Duration
|
||||
|
||||
// OnBan 在「新建一条封禁」时回调,供上层打日志 / 埋点。
|
||||
//
|
||||
// 存在的意义:封禁表是纯内存的,从外部完全看不见 —— 线上出现过
|
||||
// 「邮件说封了、实际没挡住」,而当时没有任何日志能说明封的是哪个键、
|
||||
// 到什么时候解除,排查只能靠猜。有了回调,每一次建封都会留下痕迹。
|
||||
//
|
||||
// ⚠️ 只在真正新建封禁时触发:已在封禁期内再次命中不会重复回调
|
||||
// (与告警去重口径一致)。回调在独立 goroutine 中执行,不要假设顺序,
|
||||
// 也不要在回调里调用本 Banner 的方法。
|
||||
OnBan func(key string, duration time.Duration, reason string)
|
||||
}
|
||||
|
||||
// 封禁原因(传给 Options.OnBan)。
|
||||
const (
|
||||
// ReasonProbe 常规通道:敏感路径 / 404 探测累计到阈值。
|
||||
ReasonProbe = "probe"
|
||||
// ReasonHoneypot 蜜罐通道:往假登录页提交了凭据。
|
||||
ReasonHoneypot = "honeypot"
|
||||
)
|
||||
|
||||
// sensitivePaths 常见后台/配置/漏洞探测路径特征(忽略大小写,完整段前缀匹配)。
|
||||
// 注意:前缀匹配抓不到 //xxx/wp-includes/wlwmanifest.xml 这类"目录枚举变体"(敏感段
|
||||
// 出现在路径中部),需要与 sensitiveFragments(任意位置子串)配合使用。
|
||||
@@ -114,6 +133,8 @@ type Banner struct {
|
||||
honeypotEnabled bool
|
||||
honeypotThreshold int
|
||||
honeypotDuration time.Duration
|
||||
|
||||
onBan func(key string, duration time.Duration, reason string)
|
||||
}
|
||||
|
||||
// New 按配置创建封禁器并注册全局清理。
|
||||
@@ -157,6 +178,7 @@ func New(opts Options) *Banner {
|
||||
honeypotEnabled: opts.HoneypotEnabled,
|
||||
honeypotThreshold: honeypotThreshold,
|
||||
honeypotDuration: honeypotDuration,
|
||||
onBan: opts.OnBan,
|
||||
}
|
||||
register(b)
|
||||
return b
|
||||
@@ -214,6 +236,7 @@ func (b *Banner) RecordHoneypot(ip, path string) bool {
|
||||
return true
|
||||
}
|
||||
b.banned[ip] = now.Add(b.honeypotDuration)
|
||||
b.notifyBan(ip, b.honeypotDuration, ReasonHoneypot)
|
||||
|
||||
// 告警(每 IP 仅一次)
|
||||
if b.alertEmail != "" && b.sendAlert != nil && !b.alerted[ip] {
|
||||
@@ -285,6 +308,7 @@ func (b *Banner) Record(ip, path string, kind int) {
|
||||
}
|
||||
until := now.Add(b.duration)
|
||||
b.banned[ip] = until
|
||||
b.notifyBan(ip, b.duration, ReasonProbe)
|
||||
|
||||
// 告警(每 IP 仅一次)
|
||||
if b.alertEmail != "" && b.sendAlert != nil && !b.alerted[ip] {
|
||||
@@ -297,6 +321,18 @@ func (b *Banner) Record(ip, path string, kind int) {
|
||||
}
|
||||
}
|
||||
|
||||
// notifyBan 触发 OnBan 回调。
|
||||
//
|
||||
// 调用点都在持锁区间内,因此这里只做「取函数 + 起 goroutine」:
|
||||
// 回调本身可能在写日志、发邮件,放到锁外执行才不会拖慢被判定的请求。
|
||||
func (b *Banner) notifyBan(key string, duration time.Duration, reason string) {
|
||||
fn := b.onBan
|
||||
if fn == nil {
|
||||
return
|
||||
}
|
||||
go fn(key, duration, reason)
|
||||
}
|
||||
|
||||
// IsBanned 判断 IP 是否处于封禁期;到期自动解除。
|
||||
//
|
||||
// 注意:即使 enabled(enable_auto_ban)关闭,蜜罐通道写入的封禁依然生效 ——
|
||||
|
||||
Reference in New Issue
Block a user