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:
@@ -5,6 +5,7 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -14,6 +15,8 @@ import (
|
||||
|
||||
"github.com/flamego/flamego"
|
||||
|
||||
"github.com/tamakyi/TamaBox/internal/conf"
|
||||
"github.com/tamakyi/TamaBox/internal/db"
|
||||
"github.com/tamakyi/TamaBox/internal/security/autoban"
|
||||
"github.com/tamakyi/TamaBox/internal/security/honeypot"
|
||||
)
|
||||
@@ -360,3 +363,147 @@ func TestHoneypotBanBlocksBusinessRoutes(t *testing.T) {
|
||||
t.Errorf("status = %d, want 403(被封禁的 IP 不应还能访问业务路由)", resp2.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHoneypotRecordsExactIPv6ButBansByPrefix 验证「取证地址」与「封禁键」是两个东西。
|
||||
//
|
||||
// 线上踩过:用 IPv6 访问蜜罐后收到「你的 IP 已被封禁」,邮件里却是
|
||||
// 2408:…::/64 —— 既不像自己的地址,也说不清到底封了多大范围,
|
||||
// 排查方向直接被带偏。现在取证与邮件给精确地址,封禁仍用 /64 聚合键
|
||||
// (抗「单个 /64 内轮换地址」绕过)。两个断言都必须成立,否则说明又混用了。
|
||||
func TestHoneypotRecordsExactIPv6ButBansByPrefix(t *testing.T) {
|
||||
oldTrust := conf.Security.TrustProxy
|
||||
conf.Security.TrustProxy = true
|
||||
t.Cleanup(func() { conf.Security.TrustProxy = oldTrust })
|
||||
|
||||
cs, err := honeypot.NewFileCredStore(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 落库路径也要断言:文件与 DB 用的是同一个 clientIP,但任一处写错
|
||||
// 都会让取证「只剩前缀」,而单测只有把它抓在手里才知道。
|
||||
store := &captureStore{}
|
||||
|
||||
f := flamego.New()
|
||||
hp := honeypot.New(honeypot.Options{Enabled: true})
|
||||
ab := autoban.New(autoban.Options{
|
||||
Enabled: true,
|
||||
HoneypotEnabled: true,
|
||||
ThresholdSensitive: 10,
|
||||
Window: time.Minute,
|
||||
Duration: time.Hour,
|
||||
HoneypotThreshold: 1,
|
||||
HoneypotDuration: 24 * time.Hour,
|
||||
})
|
||||
hph := newHoneypotHandler(hp, ab, store, cs)
|
||||
f.Use(autobanMiddleware(ab, hp))
|
||||
f.NotFound(func(ctx flamego.Context) {
|
||||
if hp.Match(ctx.Request().URL.Path) {
|
||||
hph.Serve(ctx)
|
||||
return
|
||||
}
|
||||
ctx.Redirect("/")
|
||||
})
|
||||
ts := httptest.NewServer(f)
|
||||
t.Cleanup(ts.Close)
|
||||
|
||||
const clientIP = "2408:8207:1234:5678::abcd"
|
||||
const banKey = "2408:8207:1234:5678::/64"
|
||||
|
||||
post, err := http.NewRequest(http.MethodPost, ts.URL+"/wp-login.php",
|
||||
strings.NewReader("log=admin&pwd=hunter2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
post.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
post.Header.Set("CF-Connecting-IP", clientIP)
|
||||
resp, err := http.DefaultClient.Do(post)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
recs, err := cs.ListSince(time.Now().Add(-time.Hour), "", 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(recs) != 1 {
|
||||
t.Fatalf("凭据记录数 = %d, want 1", len(recs))
|
||||
}
|
||||
if recs[0].IP != clientIP {
|
||||
t.Errorf("取证 IP = %q, want 精确地址 %q(不能写 /64 聚合键)", recs[0].IP, clientIP)
|
||||
}
|
||||
|
||||
if len(store.hits) != 1 {
|
||||
t.Fatalf("落库记录数 = %d, want 1", len(store.hits))
|
||||
}
|
||||
if store.hits[0].IP != clientIP {
|
||||
t.Errorf("落库 IP = %q, want 精确地址 %q(元数据表同样是取证物)", store.hits[0].IP, clientIP)
|
||||
}
|
||||
|
||||
if !ab.IsBanned(banKey) {
|
||||
t.Errorf("封禁键 %q 未生效:IPv6 必须按 /64 聚合", banKey)
|
||||
}
|
||||
if ab.IsBanned(clientIP) {
|
||||
t.Errorf("精确地址 %q 不应单独成为封禁键:写入与判定必须是同一口径", clientIP)
|
||||
}
|
||||
|
||||
// 同 /64 内换个地址仍应被拦 —— 这才是按前缀聚合的实际价值。
|
||||
get, err := http.NewRequest(http.MethodGet, ts.URL+"/anything", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
get.Header.Set("CF-Connecting-IP", "2408:8207:1234:5678::9999")
|
||||
resp2, err := http.DefaultClient.Do(get)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
if resp2.StatusCode != http.StatusForbidden {
|
||||
t.Errorf("同 /64 内换地址 status = %d, want 403", resp2.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// captureStore 是 db.HoneypotStore 的极简实现:只把 Create 的记录留下,
|
||||
// 其余方法返回零值。真库测试有 DB 门控、不适合放进这组结构性用例,
|
||||
// 而「落库的 IP 是不是精确地址」又必须被钉住(混用聚合键正是曾经的缺陷)。
|
||||
type captureStore struct {
|
||||
hits []*db.HoneypotHit
|
||||
}
|
||||
|
||||
func (c *captureStore) Create(_ context.Context, hit *db.HoneypotHit) error {
|
||||
c.hits = append(c.hits, hit)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *captureStore) ListSince(context.Context, time.Time, int) ([]*db.HoneypotHit, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *captureStore) CountSince(context.Context, time.Time) (int64, error) { return 0, nil }
|
||||
|
||||
func (c *captureStore) TopSources(context.Context, time.Time, int) ([]*db.HoneypotSourceStat, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *captureStore) TopPaths(context.Context, time.Time, int) ([]*db.HoneypotPathStat, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *captureStore) DeleteBefore(context.Context, time.Time) (int64, error) { return 0, nil }
|
||||
|
||||
func (c *captureStore) DeleteOldestBeyond(context.Context, int) (int64, error) { return 0, nil }
|
||||
|
||||
func (c *captureStore) Compact(context.Context) error { return nil }
|
||||
|
||||
func (c *captureStore) Size(context.Context) (db.HoneypotSize, error) {
|
||||
return db.HoneypotSize{}, nil
|
||||
}
|
||||
|
||||
func (c *captureStore) LegacyCredentialColumns(context.Context) ([]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *captureStore) PurgeLegacyCredentialColumns(context.Context) ([]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user