Files
tamabox/templates/mail/honeypot-alert.html
T
tamakyi 818fa74160 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 与空输入。
2026-10-23 10:53:03 +08:00

78 lines
4.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="format-detection" content="email=no"/>
<meta name="format-detection" content="date=no"/>
</head>
<body style="margin: 0; padding: 0;" bgcolor="#FFFFFF">
<table width="100%" height="100%" style="min-width: 348px;" border="0" cellspacing="0" cellpadding="0" lang="zh-CN">
<tbody>
<tr height="32" style="height: 32px;">
<td></td>
</tr>
<tr align="center">
<td>
<table border="0" cellspacing="0" cellpadding="0"
style="padding-bottom: 20px;max-width: 516px;min-width: 220px;">
<tbody>
<tr>
<td width="8" style="width: 8px;"></td>
<td>
<div style="border-style: solid; border-width: thin; border-color:#dadce0; border-radius: 8px; padding: 40px 20px;"
align="center">
<div style="font-family: 'Google Sans',Roboto,RobotoDraft,Helvetica,Arial,sans-serif;border-bottom: thin solid #dadce0; color: rgba(0,0,0,0.87); line-height: 32px; padding-bottom: 24px;text-align: center; word-break: break-word;">
<div style="font-size: 24px;">
蜜罐捕获到破解尝试
</div>
<div style="font-size: 13px; color: rgba(0,0,0,0.54); margin-top: 6px;">
{{.time}}
</div>
</div>
<div style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 14px; color: rgba(0,0,0,0.87); line-height: 22px; padding-top: 20px; text-align: left;">
<div style="padding: 12px 14px; border-radius: 6px; background-color: #fff4e5; border: 1px solid #ffe0b2; margin-bottom: 14px;">
<div><b>来源 IP:</b>{{.ip}}</div>
{{if ne .ip .ban_key}}
<div style="margin-top: 4px;"><b>封禁范围:</b>{{.ban_key}}(IPv6 按 /64 前缀聚合,整段一起封)</div>
{{end}}
<div style="margin-top: 4px;"><b>网络归属:</b>{{.provider}}</div>
<div style="margin-top: 4px;"><b>诱饵路径:</b>{{.path}}</div>
<div style="margin-top: 4px;"><b>处置:</b>{{.disposition}}</div>
{{if .proxy_hint}}
<div style="margin-top: 8px; font-size: 13px; color: #b71c1c;">⚠️ 该 IP 属于 CDN / 代理网络,攻击者可随时更换出口 IP。</div>
{{end}}
</div>
<div style="font-size: 13px; color: rgba(0,0,0,0.54);">捕获到的破解凭据:</div>
<div style="font-family: monospace; font-size: 12px; color: #333; background-color: #f7f7f7; padding: 10px 12px; border-radius: 6px; margin-top: 6px; word-break: break-all;">
用户名:{{.username}}<br/>
密码:{{.password}}
</div>
<div style="font-size: 12px; color: rgba(0,0,0,0.54); margin-top: 12px;">
攻击者在假登录页上提交了凭据(假页面只返回静态内容,从不校验、也从不返回任何真实信息)。
完整记录(含 User-Agent、Referer、原始 body)已写入取证文件,可用
<code>TamaBox honeypot hits</code> 命令查看;用户名与密码只存在该文件里,不写入数据库。
</div>
</div>
</div>
<div style="text-align: left;">
<div style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif;color: rgba(0,0,0,0.54);font-size: 11px; line-height: 18px; padding-top: 12px; text-align: center;">
<div>
此邮件由系统自动发送,请勿直接回复。<br/>
{{ .year }} 狼的提问箱
</div>
</div>
</div>
</td>
<td width="8" style="width: 8px;"></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr height="32" style="height: 32px;">
<td></td>
</tr>
</tbody>
</table>
</body>
</html>