蜜罐告警: 修复去重口径与空收件人短路
审阅蜜罐邮件提醒功能发现的问题,三项一并修: 1. autoban.alerted 从 map[string]bool(永不过期)改为带时间戳的 map[string]time.Time,冷却期 24h 与蜜罐侧对齐。cleanup 里清理过期项, 长期盯防同一来源不再漏掉后续攻击活动(此前一生只报一次)。 2. 蜜罐告警在 security_alert_email 为空时短路:route.go 注入 sendAlert 前判空,收件人缺失置 nil,避免每次凭据提交白做 SMTP 拨号 + 刷 Warn。 3. 强化 security_alert_email 的配置警示(蜜罐邮件含明文密码, 必须是个人邮箱、禁群组/分发列表)。
This commit is contained in:
@@ -101,6 +101,52 @@ func TestAlertOncePerIP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestAlertCooldownExpires 回归「告警冷却期」而非「一生只发一次」:
|
||||
// 冷却期(alertCooldown = 24h)过后,同一来源再次触发封禁应能再告警。
|
||||
//
|
||||
// 早先是 map[string]bool 永久去重,长期盯防同一来源会漏掉后续攻击活动
|
||||
// (换路径再来、隔几天再来都收不到提醒)。这里直接回拨 alerted 的时间戳,
|
||||
// 模拟「冷却期已过」,断言能再次发出告警。
|
||||
func TestAlertCooldownExpires(t *testing.T) {
|
||||
var calls int64
|
||||
b := newTestBanner(1, 5, time.Minute, time.Minute, "[email protected]", &calls)
|
||||
|
||||
// 第一次封禁 + 告警
|
||||
b.Record("8.8.8.8", "/admin", KindSensitive)
|
||||
waitFor(&calls, 1, t)
|
||||
|
||||
// 冷却期内再次命中:不应重复告警(此时已在封禁中,先手动解除封禁以走到告警判定)
|
||||
b.mu.Lock()
|
||||
delete(b.banned, "8.8.8.8")
|
||||
b.mu.Unlock()
|
||||
b.Record("8.8.8.8", "/config", KindSensitive)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
if got := atomic.LoadInt64(&calls); got != 1 {
|
||||
t.Fatalf("冷却期内不应重复告警,got %d", got)
|
||||
}
|
||||
|
||||
// 回拨 alerted 时间戳到冷却期之前,模拟「24 小时后攻击者再来」
|
||||
b.mu.Lock()
|
||||
b.alerted["8.8.8.8"] = time.Now().Add(-alertCooldown - time.Minute)
|
||||
delete(b.banned, "8.8.8.8")
|
||||
b.mu.Unlock()
|
||||
b.Record("8.8.8.8", "/.env", KindSensitive)
|
||||
|
||||
waitFor(&calls, 2, t)
|
||||
if got := atomic.LoadInt64(&calls); got != 2 {
|
||||
t.Fatalf("冷却期过后应再次告警,got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
// waitFor 轮询等待 atomic 计数器达到 want(sendAlert 是异步 goroutine)。
|
||||
func waitFor(c *int64, want int64, t *testing.T) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(500 * time.Millisecond)
|
||||
for atomic.LoadInt64(c) < want && time.Now().Before(deadline) {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisabled(t *testing.T) {
|
||||
b := New(Options{Enabled: false})
|
||||
b.Record("3.3.3.3", "/admin", KindSensitive)
|
||||
|
||||
Reference in New Issue
Block a user