Awesome-POC/人工智能漏洞/Ollama 目录遍历致代码执行漏洞 CVE-2024-37032.md
2024-11-07 14:48:26 +08:00

89 lines
4.3 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Ollama 目录遍历致代码执行漏洞 CVE-2024-37032
## 漏洞描述
Ollama 是一款开源 AI 模型项目,可方便打包,加快部署 AI 模型流程。2024 年 6 月 24 日,互联网上披露 CVE-2024-37032 Ollama 目录遍历致代码执行漏洞。由于通常情况下 Ollama 没有认证授权,攻击者可利用相关 API 结合目录遍历漏洞造成远程代码执行,控制服务器。
要利用此漏洞,攻击者必须向 Ollama API 服务器发送特制的 HTTP 请求。在默认的 Linux 安装中API 服务器绑定到 `localhost`,这大大降低了远程利用的风险。但是,在 docker 部署中,服务器以 `root` 权限运行并默认侦听 `0.0.0.0`API 服务器是公开的,因此可以被远程利用。
通常,建议 [将 Ollama 部署在反向代理后](https://github.com/ollama/ollama/issues/849) 以强制进行身份验证。
参考链接:
- https://www.wiz.io/blog/probllama-ollama-vulnerability-cve-2024-37032
- https://github.com/ollama/ollama/issues/849
- https://github.com/Bi0x/CVE-2024-37032
## 漏洞影响
```
Ollama < 0.1.34
```
## 环境搭建
Docker 启动 Ollama 0.1.33 服务:
```
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama:0.1.33
```
环境启动后,访问 `http://your-ip:11434/`,此时 Ollma 0.1.33 已经成功运行。
![](images/Ollama%20目录遍历致代码执行漏洞%20CVE-2024-37032/image-20241107102342624.png)
## 漏洞复现
Ollama 公开了多个执行各种操作的 [API endpoints](https://github.com/ollama/ollama/blob/main/docs/api.md),其中, `/api/pull` 可用于从 Ollama 下载模型。
![](images/Ollama%20目录遍历致代码执行漏洞%20CVE-2024-37032/image-20241107094826037.png)
默认情况下,服务器将从 Ollama 官方仓库 `registry.ollama.com` 下载模型,但也允许从私有仓库中获取模型。当下载模型时,通常会得到一个包含 manifest 文件的下载包或资源。在一个正常的 manifest 文件中,给定 layer 的 `digest` 字段应该与该层的哈希值一致。该字段还作为模型文件存储在磁盘的标识符,例如:
```
/root/.ollama/models/blobs/sha256-2049f5674b1e92b4464e5729975c9689fcfbf0b0e4443ccf10b5339f370f9a54
```
![](images/Ollama%20目录遍历致代码执行漏洞%20CVE-2024-37032/image-20241107110501091.png)
但是,模型文件存储到文件系统时未对 `digest` 字段进行校验,如果在该字段中构造一个 payload将会导致服务器在处理 manifest 时读取并泄露 `digest` 字段指定的文件内容,通过该漏洞,攻击者可读取任意文件。
例如,当通过  `http://<VICTIM>:11434/api/pull` 从私有仓库中下载模型时,可能会获取一个恶意的 manifest 文件,该文件的 digest 字段中将包含路径遍历的 payload
```
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"digest": "../../../../../../../../../../../../../../../../../../../traversal",
"size": 5
},
"layers": [
{
"mediaType": "application/vnd.ollama.image.license",
"digest": "../../../../../../../../../../../../../../../../../../../../../traversal",
"size": 7020
}
]
}
```
因此,我们可以在服务器上部署一个恶意的 manifest 文件,例如 `/root/.ollama/models/manifests/<ATTACKER>/library/manifest/latest` ,当通过  `/api/push` 将该模型推送到远程仓库时,服务器将泄露 `digest` 字段中指定的文件内容。
参考这个项目 [CVE-2024-37032]( https://github.com/Bi0x/CVE-2024-37032)
![](images/Ollama%20目录遍历致代码执行漏洞%20CVE-2024-37032/image-20241107135451081.png)
> 实现远程代码执行的思路是破坏动态链接器/加载器 [ld.so](https://man7.org/linux/man-pages/man8/ld.so.8.html) ,修改 `/etc/ld.so.preload`。通过任意文件写入,将恶意 `.so` 写入文件系统,然后使 `ld.so.preload` 包含恶意库。最后,访问 `/api/chat` 创建一个新进程,从而加载恶意 `.so` 实现远程代码执行。
## 漏洞 POC
```shell
https://github.com/Bi0x/CVE-2024-37032
```
## 漏洞修复
官方已经发布 0.1.34 修复该漏洞,建议升级至 0.1.34 及其以上版本。