devtools "Copy as cURL (Windows)" allows custom code execution in CMD
Firefox's 'Copy as cURL (Windows)' feature did not properly escape carriage return characters in request data, so a crafted request could cause arbitrary commands to execute when the copied command is pasted into cmd.exe. An attacker could achieve automatic command execution on Windows 7 and Windows 10 (with an extra Enter press on Windows 10).
- Fixed in 5 months
- Paid in 5 months
- Disclosed after 5 months
It is possible to construct a request which, when copied as a cURL command on Windows, will automatically execute arbitrary commands when pasted in cmd.exe.
Affected: devtools "Copy as cURL (Windows)" -> cmd.exe (not Powershell)
Version: Firefox 102 and older
For example, the following request body will, upon pasting in cmd.exe:
1. Automatically execute curl with query evil
2. Auto start cmd.exe with custom parameters
3. Auto start calc.exe
query=evil\r\rcmd\" /c timeout /t 3 & calc.exe\r\r
This turns into:
curl command ... --data-raw "query=evil"^
More?
More?
<executes curl>
"cmd"" /c timeout /t 3 & calc.exe"^
More?
More?
<executes attacker's cmd command>
-----------
History:
Exists since April 2019, current code: https://hg.mozilla.org/mozilla-central/rev/c3f507baa3dea6e86274de6d57d3e638f2b3bb47#l2.89
This attempt tried to fix the older code, but didn't account for single \r characters.
----------
Background:
The \r\n new-line is the "native" new-line form on Windows, but CMD seems to have its own opinion on that.
Clipboard API: passed as-is, \r\n and \r and \n can roundtrip.
Writing to a file in text mode: \r\n turns into \r\r\n, \n -> \r\n, \r -> \r
Now comes the CMD:
\r -> as if <ENTER> is pressed
\r\n -> like <ENTER>
\n -> Win10: no character at all
\n -> Win7: like <ENTER>
I repeated this in the .html file descriptions below.
---------
PoC descriptions:
Win7-LFLF.html: Only automatically executes on Windows 7's CMD. With Win10 CMD the newline characters are lost
-> Automatic command execution when pasted
Win10-LFLF.html: Due to newline chars being gobbled, the ^ escape now escapes a double-quote. We can abuse this in a different way to work on Win10.
-> User must press ENTER to execute our command
Windows-CRCR.html: Due to Windows 10 CMD bug/feature of \n handling, this is the only variant that works on older and newer systems at the same time.
-> Automatic command execution when pasted
----------
Suggested fix:
1. Split "Copy as cURL (Windows)" in two separate commands: for CMD and for Powershell. Their parsers are completely different. While I couldn't come up with a variant that'd work for PS, the current escapeStringWin() code produces garbage data for either shell when a string is supposed to be escaped.
2. A separate Powershell escape implementation
3. CMD escape impl: Only " \ % \r \n must be escaped, when using the current double-quote method.
3.1: Step 1: Escape double-quotes "", percent signs "%". Escape backslashes when followed by double quotes \"
3.2: Step 2: Normalize all new-line characters (they are inevitably (and already) mangled when using CMD)
3.2.1: replace \r\n -> \n
3.2.2: replace \r -> \n
3.2.3: replace \n -> \r\n
3.2.4: escape new-lines
Note on 3.2.3: When testing ^\r\n\r\n new-lines through console.log, while displayed correctly, new-lines ended up duplicated when copied to clipboard. The ^\n\n construction displays and pastes correctly from FF console. However I think devtools' copy code doesn't modify the data, so there it must still be ^\r\n\r\n. Please test.
Further reading:
https://daviddeley.com/autohotkey/parameters/parameters.htm
http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-line-into-individual-arguments/
http://www.windowsinspired.com/understanding-the-command-line-string-and-arguments-received-by-a-windows-program/
http://www.windowsinspired.com/summary-of-important-concepts-for-quoting-and-escaping-command-line-arguments/
----------
escapeStringWinCmd
While describing the escape behaviour above, I ended up implementing it myself:
function escapeStringWinCmd(str) {
return (
'"' +
str
.replaceAll('"', '""')
.replaceAll("%", '"%"')
.replace(/([\\]+)(")/g, "$1$1$2") // escape backslashes only when followed by "
.replaceAll("\r\n", '\n')
.replaceAll("\r", '\n')
.replaceAll("\n", '"^\r\n\r\n"') +
'"'
);
}
// The testStr must paste and execute cleanly after escaping:
// curl --trace trace.txt https://example.com/404 --data-raw <here>
// testStr = 'Lets CRLF \r\n then LF \n then CR \r then doublequote" then singlequote \' then percent % then env %os% then ampersand & then backslash \\ 2bs \\\\ 3bs \\\\\\ 1bsQ \\" 2bsQ \\\\" 3bsQ \\\\\\" then special chars ~!@#$%^&*()_+=-0987654321`,./<?>}]{[ |\\/ end'
----------
Testing
To test this, use curl --trace log.txt option, to see what curl actually receives on the command-line. That's because each Windows program does its own parsing for argv.
Example in CMD on Win10 (press ENTER for "More?"):
curl --trace trace.txt http://example.com/404 --data-raw "doublequote"" and percent "%" and backslash \\ and new"^
More?
More? "line"
Here the new line is passed as "new\nline" to the server.
=> Send data, 56 bytes (0x38)
0000: 64 6f 75 62 6c 65 71 75 6f 74 65 22 20 61 6e 64 doublequote" and
0010: 20 70 65 72 63 65 6e 74 20 25 20 61 6e 64 20 62 percent % and b
0020: 61 63 6b 73 6c 61 73 68 20 5c 5c 20 61 6e 64 20 ackslash \\ and
0030: 6e 65 77 0a 6c 69 6e 65 new.line
Backslash escape:
curl --trace trace.txt http://example.com/404 --data-raw "backslash escape \\"
=> Send data, 18 bytes (0x12)
0000: 62 61 63 6b 73 6c 61 73 68 20 65 73 63 61 70 65 backslash escape
0010: 20 5c \
Other special cases:
curl --trace trace.txt http://example.com/404 --data-raw "doublequote"" and percent "%" and backslash \ double slash \\ and triple slash \\\ and bsq \\"" and dbsq \\\\"" and tbsq\\\\\\"" end"
=> Send data, 120 bytes (0x78)
0000: 64 6f 75 62 6c 65 71 75 6f 74 65 22 20 61 6e 64 doublequote" and
0010: 20 70 65 72 63 65 6e 74 20 25 20 61 6e 64 20 62 percent % and b
0020: 61 63 6b 73 6c 61 73 68 20 5c 20 64 6f 75 62 6c ackslash \ doubl
0030: 65 20 73 6c 61 73 68 20 5c 5c 20 61 6e 64 20 74 e slash \\ and t
0040: 72 69 70 6c 65 20 73 6c 61 73 68 20 5c 5c 5c 20 riple slash \\\
0050: 61 6e 64 20 62 73 71 20 5c 22 20 61 6e 64 20 64 and bsq \" and d
0060: 62 73 71 20 5c 5c 22 20 61 6e 64 20 74 62 73 71 bsq \\" and tbsq
0070: 5c 5c 5c 22 20 65 6e 64 \\\" end
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 the escapeStringWin() function did not account for single carriage return (\r) characters, which cmd.exe treats as pressing Enter, allowing command injection.
Impact
An attacker could execute arbitrary commands on a victim's Windows machine if the victim copies a malicious request as cURL and pastes it into cmd.exe; on Windows 10, some variants require the user to press Enter.
What the attacker needed
Victim uses Firefox devtools; Victim copies request as cURL (Windows); Victim pastes into cmd.exe; Windows 7 or 10 (some variants need Enter)
The fix
The report suggests splitting the copy feature for CMD and PowerShell, implementing separate escaping, and properly escaping carriage returns and newlines; it does not state that a fix was applied.
How it was found
- Craft a request with a body containing carriage return characters and a command injection payload.
- In Firefox devtools, copy the request as cURL (Windows).
- Paste the copied command into cmd.exe.
- The injected command executes automatically (on Windows 7 and Windows 10 with the CRCR variant).
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.
When you use a browser's developer tools to copy a network request as a cURL command, the browser builds a text string that you can paste into a terminal. If that text contains special characters that the terminal interprets as commands, pasting it can run those commands. Here, a request containing carriage return characters caused the copied command to break out and execute arbitrary programs.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.