How I found the Grafana zero-day Path Traversal exploit that gave me access to your logs
A path traversal vulnerability in Grafana allowed an unauthenticated attacker to read arbitrary files on the server, including the database and configuration files. This could lead to access to sensitive data such as auth tokens, plaintext passwords, and potentially admin access.
labs.detectify.com
Published on labs.detectify.com
TwitterLinkedIn
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Grafana is an open-source analytics and dashboarding solution. The vulnerability described here is CVE-2021-43798, a critical path traversal flaw that received a 7.5 CVSS score and allowed remote, unauthenticated access to local files on the server. Grafana released an emergency patch on 2 December 2021; the vulnerability later leaked publicly and became a zero-day. The author, Jordy Versmissen, a Detectify Crowdsource security researcher, reported the issue to Grafana before it became public.
The flaw lived in the getPluginAssets method in pkg/api/plugins.go, which serves plugin assets under a public URL path.
Setup
The work was a source code audit of the open-source Grafana project on GitHub. To test the finding, the author ran Grafana in a Docker container and browsed the container's file system to find a file that should not be publicly reachable. Fuzzing used a commonly known wordlist of path traversal notations, and specifically the traversals-8-deep-exotic-encoding.txt wordlist from the swisskyrepo/PayloadsAllTheThings GitHub repository, with {FILE} replaced by VERSION on every line.
Finding it
During the audit the author searched for typical ways in Golang reads files, looking for functions that commonly cause bugs such as path traversals, along with methods like exec.Command, and then worked up the tree of method calls.
One such function is os.Open. A call to os.Open from the getPluginAssets method in pkg/api/plugins.go drew attention. The value passed to it went through the filepath.Clean function. According to Go's documentation, filepath.Clean removes inner .. elements, and also removes .. elements when the path starts with a forward slash. When the value does not start with a slash, the traversal is not removed. That asymmetry is what made the call look wrong, and a quick script to test a few cases suggested it could be a bug in Grafana.
Exploitation
Step 1: Fuzz for a working traversal
With Grafana running in a Docker container, the author used a wordlist of path traversal notations to fuzz for a traversal that would read a file which should not be public. Many proxies and WAFs normalise or block a typical ../ traversal, so alternatives were needed. The traversals-8-deep-exotic-encoding.txt wordlist from swisskyrepo/PayloadsAllTheThings was used, with {FILE} replaced by VERSION on every line. After a few hits, payloads were tested manually to reach other system files.
Step 2: Confirm and report
The fuzzing produced a working traversal, a new bug in Grafana that went on to receive a CVE. The author notified Grafana before the issue was misused.
Root cause
The developer used Golang's filepath.Clean to normalise the path before opening the file. The function does remove .. elements, but only strips them in the cases its documentation describes; when the value does not start with a forward slash, the traversal survives. The report frames the lesson as reading the documentation carefully and adding unexpected cases to unit tests.
Impact
An unauthenticated attacker can read any file on the server that is readable by Grafana. Because many Grafana instances use the default Sqlite database, which sits at a predictable location, a specially crafted GET request can download the database file. That database contains a lot of data, including users' auth tokens and data sources.
The Grafana config file can also be read. It may contain plain text passwords for databases, the admin user, OAuth credentials or LDAP credentials, which can give an attacker access to the database or admin access to the Grafana instance. With admin access, the attacker can create a new datasource and use the datasource proxy API endpoint to use Grafana as an HTTP proxy.
Grafana confirmed that its cloud-hosted Grafana dashboards were not impacted.
Fix
Grafana released versions 8.3.1, 8.2.7, 8.1.8 and 8.0.7. All installations between v8.0.0-beta1 and v8.3.0 should be upgraded as soon as possible. If upgrading is not possible, running a reverse proxy in front of Grafana that normalises the PATH of the request mitigates the vulnerability; the report gives the normalize_path setting in envoy as an example.
Grafana stated that the vulnerable URL path is:
<grafana_host_url>/public/plugins/<"plugin-id">
where <"plugin-id"> is the plugin ID for any installed plugin.
More generally, path traversal attacks rely on two vulnerable elements: the web application code and the web server configuration. Avoiding vulnerabilities in both, and not storing sensitive configuration files inside the web root, mitigates the majority of such attacks. The report points to OWASP for more information on path traversals.
Timeline
- 2021-12-02: Security researcher sends the initial report
- 2021-12-02: Confirmed for 8.0.0-beta1 through 8.3.0
- 2021-12-02: Confirmed that Grafana Cloud is not vulnerable
- 2021-12-02: Security fix determined and committed to Git
- 2021-12-02: Release timeline determined: 2021-12-07 for private customer release, 2021-12-14 for public release
- 2021-12-06: Second report about the vulnerability received
- 2021-12-07: Grafana received information that the vulnerability CVE-2021-43798 has been leaked to the public, turning it into a zero-day
- 2021-12-07: Private release with reduced 2-hour grace period
Variants to test
The report notes that filepath.Clean is used a lot in Golang projects, and that many applications and libraries likely use the method without ensuring the passed value starts with a forward slash. In other languages, developers can forget to normalise the path or to test the long list of possible bypasses and encodings.
Concrete things to try on other targets:
- A positive result is a request that returns a file outside the intended directory.
- A positive result is a response containing content from a file that should not be public.
- A positive result is a traversal that reaches the file system while the plain form is blocked.
- A positive result is a download of either file without authentication.
The report also cites earlier examples of the same class: a critical directory traversal in Atlassian's Jira Service Desk Server and Jira Service Desk Data Center (September 2019), and a relative path traversal in Citrix ShareFile Storage Zones Controller (CTX328123/CVE-2021-22941) exploitable by unauthenticated users for remote code execution.
What happened, in plain words
In December 2021 a security researcher named Jordy Versmissen found a serious bug in Grafana, a popular open-source tool for building dashboards and charts from data. The bug let anyone on the internet read files stored on the server running Grafana, without logging in at all. That kind of bug is called a path traversal, and this particular one was tracked as CVE-2021-43798. It was given a severity score of 7.5 on the CVSS scale, which is a standard way of rating how bad a security flaw is.
Grafana fixed the problem in an emergency patch on 2 December 2021; the vulnerability later leaked publicly and became a zero-day. The researcher had reported it to Grafana before that happened. The bug affected Grafana versions from v8.0.0-beta1 up to and including v8.3.0.
This article walks through how the researcher found the bug, what an attacker could do with it, how Grafana fixed it, and what the rest of us can learn from it. Everything is explained from scratch, so you only need to know how to use a browser and a computer.
Background: what Grafana is, and what a plugin is
Grafana is software you install on a server. It connects to databases and other data sources, then draws charts and dashboards from that data so people can look at it in a web browser. It is open source, which means its source code (the human-written instructions that make the program work) is published on GitHub for anyone to read, copy and improve.
Grafana has a feature called plugins. A plugin is an add-on that extends what Grafana can do, for example by adding a new type of chart or a connection to a new kind of database. Each plugin has an identifier, called a plugin ID, and Grafana serves plugin files under a public URL path; the code handling those assets is where the bug lived.
The idea behind the bug: path traversal
Every file on a computer has a path, which is like a postal address for the file. On a Linux server a path might look like /var/lib/grafana/grafana.db. The slashes separate folders, and the last part is the file name.
Two special pieces of notation matter here:
.means "this folder"...means "the folder above this one".
So if a program is told to open plugins/myplugin/logo.png, and someone instead sends plugins/../../etc/passwd, the .. parts tell the operating system to climb up out of the plugins folder and keep going. The result is a completely different file, one the program never meant to hand out. That is a path traversal: tricking a program into walking out of the folder it was supposed to stay in, using .. in the path.
A useful everyday comparison: imagine a hotel receptionist who is told to fetch "room 204, second door on the left". If a guest says "room 204, then go back to the lobby, then into the manager's office", a receptionist who follows instructions literally will end up in the manager's office. The fix is for the receptionist to refuse any instruction that leaves the corridor.
How the researcher found it
The researcher downloaded the latest version of Grafana's source code and started reading it, looking for anything unusual. This is called a source code audit: reading a program's instructions by hand, the way an editor reads a manuscript, instead of just poking at the running program from outside.
When doing a first code review, the researcher always searches for typical functions that commonly cause bugs, such as path traversals, but also methods like exec.Command (a way for a program to run another program on the system). The technique is to find a suspicious function call, then work upwards through the tree of method calls (the chain of functions that call each other) to see where the input comes from and whether an attacker can control it.
One of the functions you can use in Golang (the programming language Grafana is written in) is os.Open. It opens a file so the program can read it. A call to os.Open from the getPluginAssets method in the file pkg/api/plugins.go caught the researcher's attention. A method is simply a named block of code that does one job; getPluginAssets is the block responsible for serving plugin files.
The value passed to os.Open went through a function called filepath.Clean. That function is supposed to tidy up a path, removing things like .. so the path cannot escape its folder. According to Go's documentation, filepath.Clean removes inner .. elements, and also removes .. elements when the path starts with a forward slash. But when the value does not start with a slash, the traversal is not removed. That asymmetry is what made the call look wrong: the same input is treated differently depending on whether it begins with /.
A quick script to test a few cases suggested this could be a real bug in Grafana. A script here just means a short program written to try many inputs automatically instead of typing them by hand.
Setting up a test environment
To confirm the finding safely, the researcher started Grafana in a Docker container. Docker is a tool that runs a program inside an isolated box on your computer, with its own tiny file system, so experiments cannot damage the real machine. Think of it as a sandbox in a playground: you can dig and build inside it without affecting anything outside.
Inside that container the researcher browsed the file system to find a file that should not be publicly reachable, so there would be something to prove the bug with.
Step 1: Fuzzing for a working traversal
With Grafana running in the container, the researcher used a wordlist of path traversal notations to fuzz for a traversal that would read a file which should not be public. Fuzzing means sending a large number of automatically generated variations of a request and watching which ones produce an interesting response. A wordlist is simply a text file with one candidate per line.
Many proxies and WAFs normalise or block a typical ../ traversal, so alternatives were needed. A proxy is a server that sits between you and the real website and forwards your requests; a WAF (web application firewall) is a filter that inspects requests and blocks ones that look malicious. Normalise means rewriting a request into a standard form before passing it on, which can neutralise tricks. Because these defences often catch the plain ../ form, the researcher tried many different encodings, meaning ways of writing the same characters so they look different to a filter but are still understood by the program.
The wordlist used was traversals-8-deep-exotic-encoding.txt from the swisskyrepo/PayloadsAllTheThings GitHub repository, with {FILE} replaced by VERSION on every line. PayloadsAllTheThings is a public collection of example attack inputs maintained on GitHub; the file name says it contains traversal patterns that are eight levels deep and use unusual encodings. Replacing {FILE} with VERSION means every line in the wordlist was turned into a request asking for a file called VERSION, so the researcher could see which pattern actually reached it.
After a few hits, payloads were tested manually to reach other system files. A payload is the part of a request that carries the attack; testing manually means sending individual requests by hand, one at a time, to confirm and extend what the automated run found.
Step 2: Confirm and report
The fuzzing produced a working traversal: a new bug in Grafana. A zero-day is a vulnerability that has been leaked or exploited before defenders can prepare. The bug went on to receive a CVE, which is a public catalogue number for a known vulnerability, like a case number for a security incident.
The researcher notified Grafana before the issue was misused. This is the ethical approach: tell the people who can fix it, give them time, and do not publish details that would help attackers.
What an attacker could actually do
An unauthenticated attacker can read any file on the server that is readable by Grafana. That is a big deal because servers hold far more than the website's pictures.
Many Grafana instances use the default Sqlite database for storage. Sqlite is a simple database that keeps all its data in a single file on disk, and by default that file sits at a predictable location. Because the attacker can read files, a specially crafted GET request can download the database file. A GET request is the ordinary kind of request your browser sends when you open a web page; "specially crafted" means the address has been built to include a traversal. The database contains a lot of data, including users' auth tokens and data sources. An auth token is a short piece of text that proves who you are, a bit like a wristband at an event: whoever holds it is treated as the logged-in user. Stealing tokens means impersonating people.
The Grafana config file can also be read. A config file is where a program keeps its settings. It may contain plain text passwords for databases, the admin user, OAuth credentials or LDAP credentials. OAuth and LDAP are two common ways of logging in through a central account system; credentials are the username and password (or equivalent) used to prove identity. With those, an attacker can get access to the database or admin access to the Grafana instance.
If the attacker has admin access, they can create a new datasource in Grafana and use the datasource proxy API endpoint to use Grafana as an HTTP proxy. A datasource is a connection Grafana uses to fetch data. An API is a way for one program to talk to another over the network, like a service counter where you make a request and get a response. A proxy is something that forwards requests on your behalf. So the attacker can turn Grafana into a relay that fetches other things for them, hiding their real origin.
Grafana confirmed that its cloud-hosted Grafana dashboards were not impacted.
The fix
Grafana released versions 8.3.1, 8.2.7, 8.1.8 and 8.0.7. All installations between v8.0.0-beta1 and v8.3.0 should be upgraded as soon as possible. Upgrading means installing the newer version, which contains the corrected code.
If upgrading is not possible, running a reverse proxy in front of Grafana that normalises the PATH of the request mitigates the vulnerability. A reverse proxy is a server placed in front of the real application that receives every request first and can clean it up before passing it on. The PATH is the part of a web address after the domain name, for example /public/plugins/.... Normalising it means rewriting it into a standard form, which removes the tricks. The report gives the normalize_path setting in envoy as an example; Envoy is one such proxy product.
Grafana stated that the vulnerable URL path is:
<grafana_host_url>/public/plugins/<"plugin-id">
Line by line: <grafana_host_url> is the address of the Grafana server, for example the domain you type in your browser. /public/plugins/ is the public folder where plugin files are served from. <"plugin-id"> is a placeholder for the identifier of any installed plugin, and the > at the end is how the greater-than sign was written in the published text.
Why this keeps happening
Path traversal attacks rely on two vulnerable elements: the web application code and the web server configuration. Avoiding vulnerabilities in both, and not storing sensitive configuration files inside the web root, mitigates the majority of such attacks. The web root is the folder the web server is allowed to serve files from; anything inside it can potentially be downloaded, so secrets should live elsewhere.
Most of the time the programming language or framework will handle these kinds of situations, or provide a function to normalise a file path and remove the typical path traversal cases. In this case the developer used Golang's filepath.Clean to normalise the path before opening the file. The function does remove .. elements, but only strips them in the cases its documentation describes; when the value does not start with a forward slash, the traversal survives. A sanitiser is a function whose job is to clean dangerous input before it is used.
The report frames the lesson as reading the documentation carefully and adding unexpected cases to unit tests. A unit test is a small automatic check that verifies one piece of code behaves correctly; adding odd inputs to those tests is how you catch a function that only half does its job.
The report points to OWASP for more information on path traversals. OWASP is a well-known non-profit project that publishes free guidance on web security.
Timeline
- 2021-12-02: Security researcher sends the initial report
- 2021-12-02: Confirmed for 8.0.0-beta1 through 8.3.0
- 2021-12-02: Confirmed that Grafana Cloud is not vulnerable
- 2021-12-02: Security fix determined and committed to Git
- 2021-12-02: Release timeline determined: 2021-12-07 for private customer release, 2021-12-14 for public release
- 2021-12-06: Second report about the vulnerability received
- 2021-12-07: Grafana received information that the vulnerability CVE-2021-43798 has been leaked to the public, turning it into a zero-day
- 2021-12-07: Private release with reduced 2-hour grace period
Git, mentioned in the timeline, is the version-control system used to track changes to source code. A grace period is the window between telling customers about a fix and making it public, giving them time to update before attackers learn the details.
Variants to test
The report notes that filepath.Clean is used a lot in Golang projects, and that many applications and libraries likely use the method without ensuring the passed value starts with a forward slash. In other languages, developers can forget to normalise the path or to test the long list of possible bypasses and encodings.
Concrete things to try on other targets:
- A positive result is a request that returns a file outside the intended directory. Grep is a command-line tool for searching text; here it means searching the source code for those function names.
- A positive result is a response containing content from a file that should not be public. An endpoint is a specific address on a server that accepts requests.
- A positive result is a traversal that reaches the file system while the plain form is blocked.
- A positive result is a download of either file without authentication.
The report also cites earlier examples of the same class: a critical directory traversal in Atlassian's Jira Service Desk Server and Jira Service Desk Data Center (September 2019), and a relative path traversal in Citrix ShareFile Storage Zones Controller (CTX328123/CVE-2021-22941) exploitable by unauthenticated users for remote code execution. Remote code execution means the attacker can make the server run their own commands, which is generally the most severe outcome of all.
Glossary
- API: a defined way for one program to ask another program for something over a network, like a service counter with fixed request forms.
- Auth token: a short piece of text that proves who you are, so the server does not ask for your password again; whoever holds it is treated as you.
- Config file: a file where a program stores its settings, sometimes including passwords.
- Container: an isolated box on a computer that runs a program with its own file system, used to test safely.
- Credentials: the username and password (or equivalent) used to prove identity.
- CVE: a public catalogue number assigned to a known security vulnerability, like a case number.
- CVSS: a standard scoring system for how severe a vulnerability is; 7.5 is a high score.
- Datasource: a connection Grafana uses to fetch data from a database or other system.
- Docker: a tool for running programs inside isolated containers.
- Encoding: a way of writing the same characters differently so a filter may not recognise them, while the program still understands them.
- Endpoint: a specific address on a server that accepts requests.
- Envoy: a proxy product that can sit in front of an application and rewrite requests.
- Fuzzing: automatically sending many variations of a request to see which produce an interesting response.
- GET request: the ordinary request a browser sends when you open a web page.
- Git: the version-control system used to track changes to source code.
- Golang (Go): the programming language Grafana is written in.
- Grace period: the window between telling customers about a fix and making it public.
- Grep: a command-line tool for searching text, often used to search source code.
- LDAP: a common central system for logging in and managing accounts.
- Method: a named block of code that does one job.
- Mitigate: to reduce the impact of a problem, here by blocking the attack rather than fixing the code.
- Normalise: rewrite a request into a standard form so tricks no longer work.
- OAuth: a common way of logging in through a central account system.
- Open source: software whose source code is published for anyone to read and reuse.
- OWASP: a non-profit project that publishes free guidance on web security.
- Path: the address of a file on a computer, made of folder names separated by slashes.
- Path traversal: tricking a program into reading a file outside the folder it was meant to stay in, using
..in the path. - Payload: the part of a request that carries the attack.
- Plugin: an add-on that extends what a program can do.
- Proof of concept: a small demonstration showing that a bug can really be abused.
- Proxy: a server that forwards requests between you and the real website.
- Reverse proxy: a proxy placed in front of an application that receives every request first.
- Remote code execution: making a server run commands chosen by the attacker.
- Sanitiser: a function whose job is to clean dangerous input before it is used.
- Script: a short program written to automate a task.
- Source code: the human-written instructions that make a program work.
- Source code audit: reading a program's source code by hand to look for bugs.
- Sqlite: a simple database that keeps all its data in a single file on disk.
- Unit test: a small automatic check that one piece of code behaves correctly.
- WAF (web application firewall): a filter that inspects incoming requests and blocks ones that look malicious.
- Web root: the folder a web server is allowed to serve files from.
- Wordlist: a text file with one candidate input per line, used for fuzzing.
- Zero-day: a vulnerability the vendor does not yet know about and has no fix for.