线上实测: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 与空输入。
268 lines
11 KiB
Go
268 lines
11 KiB
Go
// Copyright 2022 E99p1ant. All rights reserved.
|
||
// Use of this source code is governed by a MIT-style
|
||
// license that can be found in the LICENSE file.
|
||
|
||
package mail
|
||
|
||
import (
|
||
"bytes"
|
||
"crypto/tls"
|
||
"embed"
|
||
"fmt"
|
||
"html/template"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/pkg/errors"
|
||
"gopkg.in/gomail.v2"
|
||
|
||
"github.com/tamakyi/TamaBox/internal/conf"
|
||
"github.com/tamakyi/TamaBox/templates"
|
||
)
|
||
|
||
func SendNewQuestionMail(email, domain string, questionID uint, questionContent string, token string) error {
|
||
params := map[string]string{
|
||
"link": fmt.Sprintf("%s/_/%s/%d?t=%s", conf.App.ExternalURL, domain, questionID, token),
|
||
"question": questionContent,
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】您有一个新的提问", templates.FS, "mail/new-question.html", params)
|
||
}
|
||
|
||
func SendNewQuestionMailToUser(email, domain string, questionID uint, questionContent string, token string) error {
|
||
params := map[string]string{
|
||
"link": fmt.Sprintf("%s/_/%s/%d?t=%s", conf.App.ExternalURL, domain, questionID, token),
|
||
"question": questionContent,
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】您发送了一个新的提问", templates.FS, "mail/new-question-to-user.html", params)
|
||
}
|
||
|
||
func SendNewAnswerMail(email, domain string, questionID uint, question, answer string, token string) error {
|
||
params := map[string]string{
|
||
"link": fmt.Sprintf("%s/_/%s/%d?t=%s", conf.App.ExternalURL, domain, questionID, token),
|
||
"question": question,
|
||
"answer": answer,
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】您的提问有了回复", templates.FS, "mail/new-answer.html", params)
|
||
}
|
||
|
||
func SendPasswordRecoveryMail(email, code string) error {
|
||
params := map[string]string{
|
||
"link": fmt.Sprintf("%s/recover-password?code=%s", conf.App.ExternalURL, code),
|
||
"email": email,
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】账号密码找回", templates.FS, "mail/password-recovery.html", params)
|
||
}
|
||
|
||
// SendNewFollowUpMail 通知提问箱主人:有人对某条已回答的提问进行了追问。
|
||
// 链接带 token,与 SendNewQuestionMail 对齐——主人于未登录设备(手机邮件客户端)点开私密提问时
|
||
// 也能凭 token 查看,否则会被 Questioner 中间件重定向到首页。
|
||
func SendNewFollowUpMail(email, domain string, questionID uint, questionContent string, followUpContent string, token string) error {
|
||
params := map[string]string{
|
||
"link": fmt.Sprintf("%s/_/%s/%d?t=%s", conf.App.ExternalURL, domain, questionID, token),
|
||
"question": questionContent,
|
||
"followup": followUpContent,
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】您的提问有了追问", templates.FS, "mail/new-follow-up.html", params)
|
||
}
|
||
|
||
// SendNewConversationMail 通知有新私聊发起(使用 token 而非 conversationID)
|
||
func SendNewConversationMail(email, askerName, domain string, token string, firstMessage string) error {
|
||
params := map[string]string{
|
||
"link": fmt.Sprintf("%s/_/%s/chat/%s", conf.App.ExternalURL, domain, token),
|
||
"asker": askerName,
|
||
"content": firstMessage,
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】有人发起了私聊", templates.FS, "mail/new-conversation.html", params)
|
||
}
|
||
|
||
// SendNewMessageMail 通知私聊中有新消息(使用 token 而非 conversationID)
|
||
func SendNewMessageMail(email, senderName, domain string, token string, content string) error {
|
||
params := map[string]string{
|
||
"link": fmt.Sprintf("%s/_/%s/chat/%s", conf.App.ExternalURL, domain, token),
|
||
"sender": senderName,
|
||
"content": content,
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】私聊新消息", templates.FS, "mail/new-message.html", params)
|
||
}
|
||
|
||
// SendNewVoteSubmissionMail 通知箱主:表单投票收到新回复(链接指向管理后台结果页)。
|
||
func SendNewVoteSubmissionMail(email, voteTitle string, voteID uint) error {
|
||
params := map[string]string{
|
||
"link": fmt.Sprintf("%s/user/votes/%d/results", conf.App.ExternalURL, voteID),
|
||
"title": voteTitle,
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】您的投票收到了新回复", templates.FS, "mail/new-vote.html", params)
|
||
}
|
||
|
||
// SendSecurityAlertMail 发送安全告警:检测到疑似批量扫描/探测并触发自动封禁时通知管理员。
|
||
// provider 为来源 IP 的网络归属标签(见 internal/security/ipinfo),proxied 表示
|
||
// IP 属于 CDN/WAF/代理网络(此时封禁出口 IP 可能无法拦截真实攻击者,邮件中额外提示)。
|
||
func SendSecurityAlertMail(ip, provider string, proxied bool, samplePaths []string, durationMinutes int) error {
|
||
proxyHint := ""
|
||
if proxied {
|
||
proxyHint = "1"
|
||
}
|
||
params := map[string]string{
|
||
"ip": ip,
|
||
"provider": provider,
|
||
"proxy_hint": proxyHint,
|
||
"paths": strings.Join(samplePaths, "、"),
|
||
"duration": fmt.Sprintf("%d", durationMinutes),
|
||
"time": time.Now().Format("2006-01-02 15:04:05"),
|
||
}
|
||
return sendTemplateMail(conf.Security.SecurityAlertEmail, "【狼的提问箱】检测到疑似批量扫描,已自动封禁 IP", templates.FS, "mail/security-alert.html", params)
|
||
}
|
||
|
||
// HoneypotAlert 蜜罐捕获到破解尝试时的告警载荷。
|
||
type HoneypotAlert struct {
|
||
// IP 是精确客户端地址,正文里展示的就是它(人要对得上自己的地址)。
|
||
IP string
|
||
// BanKey 是实际写进封禁表的键:IPv4 与 IP 相同,IPv6 是折叠后的 /64 前缀。
|
||
// 单独给出,是为了让运维能分清「源地址」与「被封的范围」——
|
||
// 曾经只给键,收到「你的 IP 已被封禁」的人发现邮件里的地址不是自己的,
|
||
// 排查方向直接被带偏。
|
||
BanKey string
|
||
Provider string
|
||
Proxied bool
|
||
Path string
|
||
Username string
|
||
Password string
|
||
Banned bool
|
||
DurationMin int
|
||
}
|
||
|
||
// SendHoneypotAlertMail 发送「蜜罐捕获到破解尝试」告警。
|
||
//
|
||
// 与 SendSecurityAlertMail 共用 conf.Security.SecurityAlertEmail 收件人,不新增配置项。
|
||
// 邮件中直接给出捕获到的用户名/密码:这是本功能的核心价值(拿到攻击者正在用的字典),
|
||
// 但也意味着这封邮件是敏感物 —— 收件人应当只有运维本人,不要转发到公共群组。
|
||
func SendHoneypotAlertMail(a HoneypotAlert) error {
|
||
proxyHint := ""
|
||
if a.Proxied {
|
||
proxyHint = "1"
|
||
}
|
||
disposition := "已记录,未触发封禁"
|
||
if a.Banned {
|
||
disposition = fmt.Sprintf("已记录并自动临时封禁 %d 分钟", a.DurationMin)
|
||
}
|
||
password := a.Password
|
||
if password == "" {
|
||
password = "(空)"
|
||
}
|
||
username := a.Username
|
||
if username == "" {
|
||
username = "(空)"
|
||
}
|
||
params := map[string]string{
|
||
"ip": a.IP,
|
||
"ban_key": a.BanKey,
|
||
"provider": a.Provider,
|
||
"proxy_hint": proxyHint,
|
||
"path": a.Path,
|
||
"username": username,
|
||
"password": password,
|
||
"disposition": disposition,
|
||
"time": time.Now().Format("2006-01-02 15:04:05"),
|
||
}
|
||
return sendTemplateMail(conf.Security.SecurityAlertEmail, "【狼的提问箱】蜜罐捕获到后台破解尝试", templates.FS, "mail/honeypot-alert.html", params)
|
||
}
|
||
|
||
// SendAbuseAlertMail 发送「某箱主收到集中举报」的阈值告警。
|
||
//
|
||
// 与 SendSecurityAlertMail 共用 conf.Security.SecurityAlertEmail 收件人,不新增配置项。
|
||
// 邮件只给计数与下一步命令,不贴举报内容原文:邮件可能被转发/归档,贴原文等于把挂人内容
|
||
// 又复制了一份出去;要看内容请用 reports --domain。
|
||
func SendAbuseAlertMail(domain, name string, count24, count7d int64) error {
|
||
params := map[string]string{
|
||
"domain": domain,
|
||
"name": name,
|
||
"count24": fmt.Sprintf("%d", count24),
|
||
"count7d": fmt.Sprintf("%d", count7d),
|
||
"time": time.Now().Format("2006-01-02 15:04:05"),
|
||
}
|
||
return sendTemplateMail(conf.Security.SecurityAlertEmail, "【狼的提问箱】某箱主收到集中举报", templates.FS, "mail/abuse-alert.html", params)
|
||
}
|
||
|
||
// SendQuestionReportedMail 通知箱主「你的某条内容被多人举报」。
|
||
//
|
||
// 与给运维的告警不同:这封是给**箱主**的自查提醒,语气必须中性 ——
|
||
// 明确写出「不代表平台已判定违规」,避免变成变相的处罚通知。
|
||
func SendQuestionReportedMail(email, domain string, questionID uint, count int64, link string) error {
|
||
params := map[string]string{
|
||
"domain": domain,
|
||
"question_id": fmt.Sprintf("%d", questionID),
|
||
"count": fmt.Sprintf("%d", count),
|
||
"link": link,
|
||
"time": time.Now().Format("2006-01-02 15:04:05"),
|
||
}
|
||
return sendTemplateMail(email, "【狼的提问箱】你的内容被举报了", templates.FS, "mail/question-reported.html", params)
|
||
}
|
||
|
||
// RestrictionNotice 处置通知邮件的内容。
|
||
//
|
||
// 全部文案由调用方按「箱主自己的界面语言」预先翻译好再传进来:
|
||
// 邮件模板本身是单语言的,而箱主的语言存在 users.language 里,只能在调用侧解析。
|
||
//
|
||
// 刻意不包含 --reason 原文:那是运维的审计备注(可能写得很直白),
|
||
// 对外只用「级别 + 到期时间 + 规则链接」,与公告模板的口径一致。
|
||
type RestrictionNotice struct {
|
||
To string
|
||
Heading string
|
||
Greeting string
|
||
Body string
|
||
Note string
|
||
RulesText string
|
||
RulesURL string
|
||
Appeal string
|
||
}
|
||
|
||
// SendRestrictionNoticeMail 告知箱主「你的提问箱已被限制」。
|
||
func SendRestrictionNoticeMail(n RestrictionNotice) error {
|
||
params := map[string]string{
|
||
"heading": n.Heading,
|
||
// time 由邮件包统一注入:调用方(CLI / 未来可能的自动流程)不必各自拼时间,
|
||
// 也保证与其他通知邮件(question-reported / abuse-alert)的头部格式一致。
|
||
"time": time.Now().Format("2006-01-02 15:04:05"),
|
||
"greeting": n.Greeting,
|
||
"body": n.Body,
|
||
"note": n.Note,
|
||
"rulesText": n.RulesText,
|
||
"rulesURL": n.RulesURL,
|
||
"appeal": n.Appeal,
|
||
}
|
||
return sendTemplateMail(n.To, n.Heading, templates.FS, "mail/restriction-notice.html", params)
|
||
}
|
||
|
||
func sendTemplateMail(email, title string, templateFS embed.FS, templatePath string, params map[string]string) error {
|
||
var content bytes.Buffer
|
||
t, err := template.ParseFS(templateFS, templatePath)
|
||
if err != nil {
|
||
return errors.Wrap(err, "parse template file")
|
||
}
|
||
params["year"] = time.Now().Format("2006")
|
||
if err := t.Execute(&content, params); err != nil {
|
||
return errors.Wrap(err, "execute template")
|
||
}
|
||
return sendMail(email, title, content.String())
|
||
}
|
||
|
||
func sendMail(to, title, content string) error {
|
||
m := gomail.NewMessage()
|
||
m.SetHeader("From", fmt.Sprintf("TamaBox <%s>", conf.Mail.Account))
|
||
m.SetHeader("To", to)
|
||
m.SetHeader("Subject", title)
|
||
m.SetBody("text/html", content)
|
||
|
||
d := gomail.NewDialer(
|
||
conf.Mail.SMTP,
|
||
conf.Mail.Port,
|
||
conf.Mail.Account,
|
||
conf.Mail.Password,
|
||
)
|
||
// 默认校验证书;仅当配置了 skip_tls_verify=true(自签名/内网 SMTP)时跳过。
|
||
if conf.Mail.SkipTLSVerify {
|
||
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
||
}
|
||
return d.DialAndSend(m)
|
||
}
|