Skip to content
RCE Disclosed report Intermediate CVE-2022-31739

Possible download files like exe to user Startup folder on windows, which may cause RCE

The browser's download attribute accepted percent-encoded environment variable names in the suggested filename, and when the user saved the file, Windows expanded those variables into a path inside the Startup folder. An attacker could therefore trick a user into saving an executable that would run at the next login, potentially achieving remote code execution.

Chaobin Zhang Follow

Originally disclosed on Mozilla

  • Fixed in 24 days
  • Paid in 24 days
  • Disclosed after 24 days
Text
Steps to reproduce:

Pre Requirements:

1. Settings → Files and Applications → Downloads → “Always ask you where to save files” is checked
2. Environment Variable: %HOMEPATH% resolves to \ . See link: https://docs.microsoft.com/en-us/troubleshoot/windows-server/networking/variables-resolved-incorrectly , when user's home folder is on a DfS share, %HOMEPATH% should resolve to \. Besides that, in some companies, IT department will configure their employee’s computer home directory to a network driver, which will also result in %HOMEPATH% resolving to \. See link:
    1. https://superuser.com/questions/255776/overriding-homedrive-and-homepath-as-a-windows-7-user/
    2. https://superuser.com/questions/246731/how-do-i-change-homedrive-homepath-and-homeshare-in-windows-xp
    3. https://stackoverflow.com/questions/40077826/windows-10-system-environment-variable-path-back-slash-meaning
    4. ...

Steps:
1. Run the app.py: python app.py. Note: I didn't attach calc.exe in the attached file: firefox-startup-poc.zip 
2. Visit the http://127.0.0.1:5000/, by holding for 2 seconds ENTER button on the website, the hacker can trick the user download a file like exe to his Startup folder, which will lead to RCE when system reboot.

Details:
<a id="link" href="/poc.exe" download="%APPDATA%%HOMEPATH%Microsoft%HOMEPATH%Windows%HOMEPATH%Start Menu%HOMEPATH%Programs%HOMEPATH%Startup%HOMEPATH%poc.exe">POC</a>

HTML <a> download attribute accept “%” in the file name, when the file save dialog show up and user click “Save”,  %APPDATA%%HOMEPATH%Microsoft%HOMEPATH%Windows%HOMEPATH%Start Menu%HOMEPATH%Programs%HOMEPATH%Startup%HOMEPATH%poc.exe will be resolved to  C:\Users\{username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\poc.exe 

Another attack scenario
User has python installed and “PYTHONPATH” is configured to a path ends with backslash(\). For example: PYTHONPATH=C:\MyPYTHON\. For more details about PYTHONPATH, see link: https://bic-berkeley.github.io/psych-214-fall-2016/using_pythonpath.html

Python imports should be grouped in the following order:

1. sys.modules
2. built-in modules
3. import path: sys.path A list of strings that specifies the search path for modules. Initialized from the environment variable `PYTHONPATH`, plus an installation-dependent default.

So if we put string.py under the %PYTHONPATH% directory, it will hijack the “import string”.

<a id="link" href="/pystr" download="%PYTHONPATH%string.py">POC</a>

If user’s PYTHONPATH is configured without endings with backslash, we can still concatenate %HOMEPATH% to attack.

<a id="link" href="/pystr" download="%PYTHONPATH%%HOMEPATH%string.py">POC</a>


Actual results:

Website can trick user download file to startup directory, which may cause RCE.


Expected results:

% should be stripped in the file save dialog.

The original report as published on Mozilla's Bugzilla, by Chaobin Zhang. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 browser passed the download attribute's filename to the Windows save dialog without stripping or escaping percent signs, allowing environment variable expansion to alter the save path.

Impact

An attacker could trick a user into saving an executable to the Startup folder, which may lead to remote code execution after a reboot; the attack depends on specific environment variable configurations and user interaction.

What the attacker needed

Windows with %HOMEPATH% resolving to backslash; Browser setting to always ask where to save; User holds Enter for two seconds; User clicks Save in the dialog

How it was found

  1. Enable the setting that always asks where to save files.
  2. Ensure the %HOMEPATH% environment variable resolves to a backslash.
  3. Run the provided local web server.
  4. Visit the local page and hold the Enter key for two seconds to trigger the download.
  5. Save the suggested file, which lands in the Startup folder.

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 download a file, the browser suggests a name and folder. This report shows that if the suggested name contains things like %APPDATA%, Windows can expand them into a real folder path, such as the Startup folder. Files in the Startup folder run automatically when you log in, so an attacker could get their program to run without you noticing.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More by Chaobin Zhang

View all

1 more disclosed report, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.