fix(honeypot): 蜜罐封禁与 enable_auto_ban 解耦 + 泄露扫描后缀匹配

- autoban: 新增 RecordHoneypot 独立通道(不受 enabled 约束)+ 独立阈值/时长;
  IsBanned 不再因 enable_auto_ban=false 而忽略已有封禁
- honeypot: 新增 honeypotSuffixes 后缀匹配,覆盖 /backup.zip、/db.sql、/id_rsa.pem
- 新增配置 honeypot_threshold / honeypot_ban_minutes
- 补测试:autoban/honeypot_test.go、route 侧禁用自动封禁仍封禁
This commit is contained in:
tamakyi
2026-10-21 12:36:33 +08:00
parent 79051acae8
commit 7d52155411
12 changed files with 402 additions and 10 deletions
+104 -1
View File
@@ -37,6 +37,24 @@ type Options struct {
Duration time.Duration // 封禁时长
AlertEmail string // 告警收件人(留空不发送)
SendAlert func(ip string, paths []string, durationMinutes int) error // 告警发送函数(nil 则不发送)
// HoneypotEnabled 独立于 Enabled:蜜罐(假后台登录页 + 破解凭据取证)是显式
// 开启的功能,命中即应封禁,不应被 enable_auto_ban 连坐。
//
// 为什么必须独立:蜜罐命中的是「有人在真的往假登录表单里填账号密码」,
// 证据强度远高于「访问了一个可疑路径」。若两者共用同一个 enabled 开关,
// 运维关掉 enable_auto_ban(因为它对共享出口误伤多)就会连带让蜜罐变成
// 只记录不封禁的旁观者 —— 蜜罐的全部价值(威慑 + 阻断)就消失了。
//
// 开启后蜜罐命中仍走同一套计数/阈值/告警机制,只是绕开 enabled 早退。
HoneypotEnabled bool
// HoneypotThreshold 蜜罐独立阈值:窗口内蜜罐命中次数达到即封禁。
// 默认 1(一次即封)—— 蜜罐路径与业务路由零交集,正常访客不可能命中,
// 一次命中就足以判定为扫描器,无需等 autoban 的 10 次阈值。
// 留空或 <= 0 时兜底为 1。
HoneypotThreshold int
// HoneypotDuration 蜜罐独立封禁时长,<= 0 时复用 Duration。
HoneypotDuration time.Duration
}
// sensitivePaths 常见后台/配置/漏洞探测路径特征(忽略大小写,完整段前缀匹配)。
@@ -91,6 +109,11 @@ type Banner struct {
alertEmail string
sendAlert func(ip string, paths []string, durationMinutes int) error
enabled bool
// 蜜罐独立通道(见 Options.HoneypotEnabled 的说明)。
honeypotEnabled bool
honeypotThreshold int
honeypotDuration time.Duration
}
// New 按配置创建封禁器并注册全局清理。
@@ -111,6 +134,14 @@ func New(opts Options) *Banner {
if notFound <= 0 {
notFound = DefaultThreshold404
}
honeypotThreshold := opts.HoneypotThreshold
if honeypotThreshold <= 0 {
honeypotThreshold = 1
}
honeypotDuration := opts.HoneypotDuration
if honeypotDuration <= 0 {
honeypotDuration = duration
}
b := &Banner{
probes: make(map[string]*probeStat),
banned: make(map[string]time.Time),
@@ -123,11 +154,79 @@ func New(opts Options) *Banner {
alertEmail: strings.TrimSpace(opts.AlertEmail),
sendAlert: opts.SendAlert,
enabled: opts.Enabled,
honeypotEnabled: opts.HoneypotEnabled,
honeypotThreshold: honeypotThreshold,
honeypotDuration: honeypotDuration,
}
register(b)
return b
}
// RecordHoneypot 记录一次蜜罐命中,命中即封禁。
//
// 与 Record 的区别:
// - 不受 enabled(enable_auto_ban)约束 —— 蜜罐是显式开启的功能,
// 开启即意味着要对命中者封禁(详见 Options.HoneypotEnabled);
// - 走独立的阈值(默认 1 次即封)与时长(默认复用常规时长);
// - 键与 Record 完全一致(同样经 shared.LimitKey 归一化),
// 否则 IsBanned 判不出来,封禁形同虚设。
//
// 告警仍走既有的 sendAlert 通道(每 IP 一次),与蜜罐自己的告警邮件互补:
// 前者说明「已封禁」,后者说明「捕获到了什么凭据」。
func (b *Banner) RecordHoneypot(ip, path string) bool {
if !b.honeypotEnabled || ip == "" {
return false
}
b.mu.Lock()
defer b.mu.Unlock()
now := time.Now()
cutoff := now.Add(-b.window)
st := b.probes[ip]
if st == nil {
st = &probeStat{}
b.probes[ip] = st
}
// 滚动清理过期事件(只统计窗口内的蜜罐命中)
valid := st.events[:0]
hits := 0
for _, e := range st.events {
if e.at.After(cutoff) {
valid = append(valid, e)
hits++
}
}
valid = append(valid, probeEvent{at: now, kind: KindSensitive})
hits++
st.events = valid
st.sensitive = hits
if len(st.paths) < pathSampleMax && path != "" {
st.paths = append(st.paths, path)
}
if hits < b.honeypotThreshold {
return false
}
// 已在封禁中则跳过
if until, ok := b.banned[ip]; ok && until.After(now) {
return true
}
b.banned[ip] = now.Add(b.honeypotDuration)
// 告警(每 IP 仅一次)
if b.alertEmail != "" && b.sendAlert != nil && !b.alerted[ip] {
b.alerted[ip] = true
sample := append([]string{}, st.paths...)
dur := int(b.honeypotDuration.Minutes())
go func() {
_ = b.sendAlert(ip, sample, dur)
}()
}
return true
}
// Record 记录一次探测事件,达到阈值时封禁 IP 并触发一次告警。
func (b *Banner) Record(ip, path string, kind int) {
if !b.enabled || ip == "" || kind == KindNone {
@@ -199,8 +298,12 @@ func (b *Banner) Record(ip, path string, kind int) {
}
// IsBanned 判断 IP 是否处于封禁期;到期自动解除。
//
// 注意:即使 enabled(enable_auto_ban)关闭,蜜罐通道写入的封禁依然生效 ——
// 否则「关了自动封禁就等于连蜜罐的封禁一起关了」,与 Options.HoneypotEnabled
// 的设计意图相悖。enabled 只控制「是否主动创建封禁」,不控制「是否尊重已有封禁」。
func (b *Banner) IsBanned(ip string) bool {
if !b.enabled || ip == "" {
if ip == "" {
return false
}
b.mu.Lock()
+123
View File
@@ -0,0 +1,123 @@
// Copyright 2026 tamakyi. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package autoban
import (
"testing"
"time"
)
// TestHoneypotBanIndependentOfAutoBan 覆盖 P0:蜜罐封禁不得被 enable_auto_ban 连坐。
//
// 背景(线上实测发现):蜜罐命中 70+ 次,站点一次都没封禁。
// 根因是蜜罐复用了 Record(),而 Record() 第一行就 `if !b.enabled { return }` ——
// 运维一旦关掉 enable_auto_ban(它对共享出口误伤多,常被关掉),
// 蜜罐就退化成「只记录不封禁」的旁观者,全部价值(威慑 + 阻断)消失。
//
// 修复:蜜罐走独立的 RecordHoneypot 通道,只看 HoneypotEnabled。
func TestHoneypotBanIndependentOfAutoBan(t *testing.T) {
b := New(Options{
Enabled: false, // 自动封禁关闭
HoneypotEnabled: true, // 但蜜罐开启
Window: 60 * time.Second,
Duration: 15 * time.Minute,
})
// 常规敏感路径不再计数(enable_auto_ban 关闭)
b.Record("1.2.3.4", "/wp-admin", KindSensitive)
if b.IsBanned("1.2.3.4") {
t.Error("enable_auto_ban 关闭时,常规敏感路径不应产生封禁")
}
// 蜜罐命中必须立即封禁
if !b.RecordHoneypot("5.6.7.8", "/wp-login.php") {
t.Error("RecordHoneypot 应在首次命中时封禁")
}
if !b.IsBanned("5.6.7.8") {
t.Error("蜜罐命中后 IsBanned 应为 true(蜜罐封禁不得被 enable_auto_ban 连坐)")
}
// 其它 IP 不受影响
if b.IsBanned("9.9.9.9") {
t.Error("无关 IP 不应被封禁")
}
}
// TestHoneypotDisabledNoBan 覆盖反向:蜜罐关闭时不应封禁。
func TestHoneypotDisabledNoBan(t *testing.T) {
b := New(Options{
Enabled: true,
HoneypotEnabled: false,
Window: 60 * time.Second,
Duration: 15 * time.Minute,
})
if b.RecordHoneypot("1.2.3.4", "/wp-login.php") {
t.Error("蜜罐关闭时不应封禁")
}
if b.IsBanned("1.2.3.4") {
t.Error("蜜罐关闭时不应产生封禁")
}
}
// TestHoneypotThreshold 覆盖蜜罐独立阈值:默认 1 次即封,可配为 N 次。
func TestHoneypotThreshold(t *testing.T) {
b := New(Options{
Enabled: false,
HoneypotEnabled: true,
HoneypotThreshold: 3,
Window: 60 * time.Second,
Duration: 15 * time.Minute,
})
if b.RecordHoneypot("1.2.3.4", "/wp-login.php") {
t.Error("第 1 次不应封禁(阈值 3)")
}
if b.RecordHoneypot("1.2.3.4", "/wp-login.php") {
t.Error("第 2 次不应封禁(阈值 3)")
}
if !b.RecordHoneypot("1.2.3.4", "/wp-login.php") {
t.Error("第 3 次应封禁(阈值 3)")
}
if !b.IsBanned("1.2.3.4") {
t.Error("达到阈值后 IsBanned 应为 true")
}
}
// TestHoneypotDurationIndependent 覆盖蜜罐独立封禁时长。
func TestHoneypotDurationIndependent(t *testing.T) {
b := New(Options{
Enabled: false,
HoneypotEnabled: true,
Duration: 15 * time.Minute, // 常规时长
HoneypotDuration: 5 * time.Minute, // 蜜罐时长
Window: 60 * time.Second,
})
b.RecordHoneypot("1.2.3.4", "/wp-login.php")
b.mu.Lock()
until := b.banned["1.2.3.4"]
b.mu.Unlock()
if until.IsZero() {
t.Fatal("未写入封禁记录")
}
remaining := time.Until(until)
if remaining > 6*time.Minute {
t.Errorf("蜜罐封禁时长应约 5 分钟,实际剩余 %v", remaining)
}
}
// TestIsBannedRespectsExistingBan 覆盖:enabled 关闭时,已有的封禁依然被尊重。
// 这是蜜罐封禁能真正挡住后续请求的前提。
func TestIsBannedRespectsExistingBan(t *testing.T) {
b := New(Options{
Enabled: false,
HoneypotEnabled: true,
Window: 60 * time.Second,
Duration: 15 * time.Minute,
})
b.RecordHoneypot("1.2.3.4", "/wp-login.php")
if !b.IsBanned("1.2.3.4") {
t.Error("enabled 关闭时仍应尊重已存在的蜜罐封禁")
}
}