Elevation of Privileges (EoP) vulnerabilities related to the some easy_options on Windows
A privileged Windows process using libcurl's cookie jar, HSTS, or alt-svc options with a user-writable path can be tricked into following a crafted link, causing arbitrary file deletion. An attacker can then escalate privileges to SYSTEM using known techniques.
Originally disclosed on HackerOne
- Disclosed after 6 months
Summary
An Elevation of Privileges (EoP) vulnerability can occur in a Windows privileged process that uses CURLOPT_COOKIEJAR, CURLOPT_HSTS, or CURLOPT_ALTSVC. This vulnerability arises due to the differences in the implementation of the unlink function between Windows and Linux, as well as the behavior of MoveFileEx, which follows specially crafted links. Given that many components, such as program updaters, frequently use curl with elevated privileges, this issue must be considered a serious concern.
Affected version
libcurl latest version (8.11.1)
Description
libcurl provides easy options such as CURLOPT_COOKIEJAR, CURLOPT_HSTS, and CURLOPT_ALTSVC. For example, when CURLOPT_COOKIEJAR is set, the library user can specify a file where cookie information will be stored.
Since libcurl is widely used for web communication, it is often utilized in privileged programs that need to communicate with web servers, such as program updaters.
The issue is that privileged programs using the aforementioned options may occur a Elevation of Privileges (EoP) vulnerability. Since all three options share a very similar code structure, this explanation will focus on CURLOPT_COOKIEJAR.
In the cookie_output function of lib/cookie.c, the output file's FILE pointer is obtained via Curl_fopen. The Curl_fopen function generates a random temporary file based on the file path specified by the library user through CURLOPT_COOKIEJAR and returns the FILE pointer to this temporary file.
Subsequently, cookie_output writes the cookie contents to the temporary file and then moves the temporary file to the user-specified file path using Curl_rename. Internally, Curl_rename calls the MoveFileExA API, and if this attempt fails, it tries to delete the temporary file using the unlink function.
(https://github.com/curl/curl/blob/c5bb4e77e414c1505d800a0091a6d57c7f75d416/lib/cookie.c#L1660)
If the file path for storing cookies is in a user-writable location, a specially crafted link in Windows can be used to redirect the calls to MoveFileExA or unlink to operate on an arbitrary file. The MoveFileExA function follows this special link as is, while the unlink function, which executes through the Windows C runtime (unlink() -> remove() -> DeleteFile(WINAPI)), also follows the link.
Through this mechanism, an attacker can leverage a privileged process to achieve arbitrary file deletion, which can ultimately lead to escalation of privilege (EoP) to SYSTEM using well-known exploitation techniques.
At first glance, it may seem that exploiting this issue requires a race condition. However, it can be exploited in a highly reliable manner using oplocks.
For more details on how arbitrary file deletion can be abused to escalate privileges, refer to the ZDI blog post linked below: (https://www.thezdi.com/blog/2022/3/16/abusing-arbitrary-file-deletes-to-escalate-privilege-and-other-great-tricks)
To help understand this issue better, a proof-of-concept (PoC) demonstrating the vulnerability will be provided, along with step-by-step instructions on how to reproduce it in the next section.
Steps To Reproduce:
-
To reproduce the issue described above, I created a simple program (
curl_EoP.sln) that sends a web request using libcurl and theCURLOPT_COOKIEJARoption.Additionally,
curl_EoP_Exp.slndemonstrates how this program can be exploited to achieve high-privilege arbitrary file deletion. This exploit modifies the deletion of"C:/ProgramData/curl_EoP/{temporary_file_name}.tmp"to delete"C:/Windows/test_file.txt"instead.Steps to Reproduce:
-
Open an administrator CMD and create
test_file.txtby running the following command:CMD echo "tempfile" > C:/Windows/test_file.txt -
Use Visual Studio C/C++ 2022 to build
curl_EoP_Exp.slnandcurl_EoP.sln(x64-Release).- Note:
curl_EoP.slnrequires libcurl.
- Note:
-
Run
curl_EoP_Exp.exewith normal user privileges.- Ignore any stdout output.
-
Run
curl_EoP.exewith administrator or SYSTEM privileges. -
As a result of the exploit,
C:/Windows/test_file.txtwill be deleted.
-
Patch Suggestion
The GetFinalPathNameByHandle API can be used to retrieve the final destination file path of a specific file handle.
By comparing this retrieved path with the expected file name, it is possible to determine whether the path has been manipulated via links.
Implement a secure wrapper around functions like MoveFileExA and unlink that incorporates this logic to prevent exploitation.
Supporting Material/References:
- curl_EoP.zip: source codes of curl_EoP.sln
- curl_EoP_Exp.zip: source codes of curl_EoP_Exp.sln
- PoC.mp4: A video of reproduce steps
Impact
Summary:
A medium-privileged attacker can achieve Escalation of Privilege (EoP) to SYSTEM by targeting any privileged program that uses the CURLOPT_COOKIEJAR, or CURLOPT_HSTS, or CURLOPT_ALTSVC options with a user writable path.
Attachments
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, libcurl's use of MoveFileExA and unlink on Windows follows symbolic links or junctions in user-writable directories, allowing a privileged process to operate on an arbitrary file.
Impact
A medium-privileged attacker can achieve escalation of privilege to SYSTEM by targeting any privileged program that uses the affected libcurl options with a user-writable path.
What the attacker needed
Windows system; privileged process using libcurl; user-writable cookie/HSTS/alt-svc path; attacker can create links in that path
The fix
The fix should use GetFinalPathNameByHandle to verify the final path of a file handle before performing operations like MoveFileExA or unlink, preventing link manipulation.
How it was found
- Create a test file in a protected location as administrator.
- Build the provided proof-of-concept programs.
- Run the exploit program as a normal user to set up the link.
- Run the vulnerable program as administrator or SYSTEM.
- Observe that the protected test file is deleted.
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 a program runs with high privileges on Windows, it can be tricked into deleting important system files if it saves data to a folder that normal users can write to. An attacker places a special link in that folder, and the program follows it, deleting a file it should not touch. This can let the attacker take full control of the computer.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.