From 307bd94ed1ea7512487dbdff5d1b1faf5d7d06c7 Mon Sep 17 00:00:00 2001 From: tamakyi Date: Fri, 23 Oct 2026 12:58:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(honeypot):=20/xmlrpc.php=20=E8=B5=B0?= =?UTF-8?q?=E7=9C=9F=E5=AE=9E=20XML-RPC=EF=BC=8C=E5=8A=A8=E6=80=81=20token?= =?UTF-8?q?=20=E4=B8=8D=E5=86=8D=E6=81=92=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 线上验收发现两个「能被指纹识别」的破绽,都不影响原有功能但削弱伪装: 一、/xmlrpc.php 回的是 HTML 登录页(P1) 我们自己发的响应头里就有 X-Pingback: /xmlrpc.php —— 扫描器顺着跟过来, 一看是 HTML 就知道整站是假的(真实 WordPress 在这里回 text/xml)。 更可惜的是 system.multicall 这条通道被浪费:一次请求能带上千组用户名/密码、 且绕开登录页限速插件,是 WP 爆破里产出最高的入口。 新增 xmlrpc.go: - 非 POST → 405 + fault「XML-RPC services are disabled on this site.」(与真实 WP 一致) - POST methodCall → 200 + fault「Incorrect username or password.」(不区分用户是否存在) - POST system.listMethods → 200 + 方法列表(枚举类工具靠它确认对面是真 WP) - 凭据提取刻意不上 encoding/xml(蜜罐被高频命中):抓所有 、剔除 methodName 与站点 URL 后两两配对。wp.getUsersBlogs 的 blog_id 若是短数字会让 配对错位一位,故「奇数长度 + 首项短数字」时丢首项;另外原始串全量写进 fields(xmlrpc.0/1/…),配对失败也不丢原文。 multicall 的第一组进标准字段,其余进 cred2/cred3… —— 一个用户名配一长串 密码是常见字典形态,只留第一组等于丢掉整份字典。 - 封禁与表单提交同规则:带凭据走 RecordHoneypot(立即封),否则走常规阈值。 二、进入页面的隐藏值每次请求都一样(P2) 两次请求拿到同一个 pma token / csrf_token / X-Jenkins-Session,任何脚本都能 判定对面是伪造的。改为按请求生成 32 位 hex(math/rand/v2 —— 这些是假 token, 不需要不可预测,只需要每次不同;crypto/rand 在 Windows 要走系统调用,贵一个量级), 页头、hidden 与 Set-Cookie 共用同一套。 顺带: - X-Pingback 改绝对地址($base/xmlrpc.php)。真实 WP 给绝对 URL,相对路径本身 就是破绽;没配 external_url 时干脆不发这个头。 - Cache-Control 从 no-store 改 no-cache, must-revalidate, max-age=0 (no-store 更像安全设备/代理加的,不像登录页)。 - Grafana / GitLab 补上真实产品会种的会话 cookie(grafana_sess、_gitlab_session)。 - Match 增加 .php 变体兜底:清单里是 /phpinfo,扫描器打 /phpinfo.php, 原来漏掉 → 302 回首页,等于明确告诉对方「这个文件不存在」。 测试:xmlrpc_test(IsXMLRPCPath / getUsersBlogs / multicall / 数字 blog_id / 非 XML-RPC body / fault 与 listMethods 输出)、family_test(动态 token 两次不同且 是合法 hex、X-Jenkins-Session 与 correlation_id 动态、Pingback 绝对地址与缺省不发、 所有家族渲染后不残留 $ 占位符)、route 层(POST multicall 捕获凭据 + 立即封 + 回 text/xml;GET 回 405 fault 而非登录页)、honeypot(.php 变体命中且不牵连业务路径)。 四处变异验证均按预期变红:固定 token、xmlrpc 分支失效、Pingback 退化成相对路径、 .php 兜底失效。 性能:RenderFamily 3.7µs→4.18µs、2→3 allocs(多的一次是 hex 编码), AssetFor 仍 0 分配。 --- internal/route/honeypot.go | 61 +++++- internal/route/honeypot_test.go | 83 ++++++++ internal/security/honeypot/family.go | 12 +- internal/security/honeypot/family_test.go | 115 ++++++++++ internal/security/honeypot/honeypot.go | 10 + internal/security/honeypot/honeypot_test.go | 19 ++ internal/security/honeypot/page.go | 78 ++++++- internal/security/honeypot/xmlrpc.go | 222 ++++++++++++++++++++ internal/security/honeypot/xmlrpc_test.go | 152 ++++++++++++++ 9 files changed, 740 insertions(+), 12 deletions(-) create mode 100644 internal/security/honeypot/xmlrpc.go create mode 100644 internal/security/honeypot/xmlrpc_test.go diff --git a/internal/route/honeypot.go b/internal/route/honeypot.go index 25f30c3..db1ca0a 100644 --- a/internal/route/honeypot.go +++ b/internal/route/honeypot.go @@ -164,6 +164,13 @@ func (h *honeypotHandler) Serve(ctx flamego.Context) { return } + // XML-RPC 端点单独处理:真实 WordPress 在这里回 XML,而不是登录页。 + // 我们自己的 X-Pingback 头正指着这个地址,回 HTML 等于自曝。 + if honeypot.IsXMLRPCPath(path) { + h.serveXMLRPC(ctx, clientIP, banKey, path) + return + } + // 家族决定用哪种后台的外观与字段名:WordPress / phpMyAdmin / Jenkins / // Tomcat / Grafana / GitLab / OpenWrt / Joomla / 通用后台。 fam := honeypot.FamilyOf(path) @@ -282,12 +289,60 @@ func isLoopbackIP(ip string) bool { // 现在响应头也归 page.go 统一设置,改一处即可。 func (h *honeypotHandler) writeLogin(ctx flamego.Context, fam honeypot.Family, path string, failed bool) { honeypot.WriteFamilyPage(ctx.ResponseWriter(), fam, honeypot.LoginPageVars{ - Action: path, - Failed: failed, - Redir: ctx.Request().URL.Query().Get("redirect_to"), + Action: path, + Failed: failed, + Redir: ctx.Request().URL.Query().Get("redirect_to"), + BaseURL: conf.App.ExternalURL, }) } +// serveXMLRPC 处理 /xmlrpc.php。 +// +// 为什么单独做:system.multicall 一次请求能带上千组用户名/密码,且绕开登录页的 +// 限速插件 —— 这是 WordPress 爆破里产出最高的一条通道,xxer、WPScan 的 xmlrpc +// 模式都走它。原来的实现把这个路径当普通诱饵,回一个 HTML 登录页: +// 既拿不到字典,又因为 X-Pingback 指着它而暴露「整个后台是假的」。 +// +// 响应形态对齐真实 WordPress: +// - 非 POST(GET/PUT/…)→ 405 + fault「XML-RPC services are disabled on this site.」 +// - POST + 凭据 → 200 + fault「Incorrect username or password.」(不区分用户是否存在) +// - POST system.listMethods → 200 + 方法列表(枚举类工具靠它确认对面是真 WP) +func (h *honeypotHandler) serveXMLRPC(ctx flamego.Context, clientIP, banKey, path string) { + req := ctx.Request().Request + w := ctx.ResponseWriter() + + if req.Method != http.MethodPost { + // 纯探测:计入常规敏感路径统计(阈值同其它诱饵),不立即封。 + if h.ab != nil && banKey != "" { + h.ab.Record(banKey, path, autoban.KindSensitive) + } + h.record(clientIP, banKey, req, path, honeypot.Credentials{}, false) + honeypot.WriteXMLRPCFault(w, http.StatusMethodNotAllowed, 405, honeypot.XMLRPCFaultDisabled) + return + } + + // body 只能读一次,提取完就不能再给后面的通用逻辑用了。 + creds := honeypot.ExtractXMLRPCCredentials(req) + + // 与表单提交同一套判定:带凭据 → 蜜罐通道立即封;否则走常规阈值。 + banned := false + if h.ab != nil && banKey != "" { + if creds.HadFields { + banned = h.ab.RecordHoneypot(banKey, path) + } else { + h.ab.Record(banKey, path, autoban.KindSensitive) + } + } + + if !creds.HadFields && creds.Fields["xmlrpc.method"] == "system.listMethods" { + honeypot.WriteXMLRPCMethods(w) + } else { + honeypot.WriteXMLRPCFault(w, http.StatusOK, 403, honeypot.XMLRPCFaultBadLogin) + } + + h.record(clientIP, banKey, req, path, creds, banned) +} + // record 追加一条命中记录(列宽对齐 + 探测采样都在这里收口)。 // // clientIP 是精确客户端地址(写进取证数据),banKey 是聚合键(只用于采样去重)。 diff --git a/internal/route/honeypot_test.go b/internal/route/honeypot_test.go index e6e4b44..7873f3d 100644 --- a/internal/route/honeypot_test.go +++ b/internal/route/honeypot_test.go @@ -644,3 +644,86 @@ func TestHoneypotAssetRequestsAreNotRecorded(t *testing.T) { t.Errorf("资源请求不应落库,实际写了 %d 条", len(store.hits)) } } + +// TestXMLRPCPostCapturesCredentials 守住 /xmlrpc.php 是真正的 XML-RPC 端点。 +// +// 之前它回的是 HTML 登录页,而响应头 X-Pingback 又指着这个地址 —— +// 扫描器跟过来一看是 HTML 就知道整站是假的;更可惜的是 system.multicall +// 这条「一次上千组凭据」的通道被白白浪费。 +func TestXMLRPCPostCapturesCredentials(t *testing.T) { + cs, err := honeypot.NewFileCredStore(t.TempDir()) + if err != nil { + t.Fatal(err) + } + ts := newHoneypotTestServerFull(t, true, false, cs) + + body := `wp.getUsersBlogs` + + `http://127.0.0.1/xmlrpc.php` + + `root` + + `toor` + + `` + + resp, err := http.Post(ts.URL+"/xmlrpc.php", "text/xml", strings.NewReader(body)) + if err != nil { + t.Fatal(err) + } + payload, _ := io.ReadAll(resp.Body) + resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Errorf("status = %d, want 200(真实 WordPress 用 200 + fault 表达登录失败)", resp.StatusCode) + } + if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/xml") { + t.Errorf("Content-Type = %q, want text/xml", ct) + } + if strings.Contains(string(payload), "