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:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user