Stealing HttpOnly cookies with the cookie sandwich technique
The cookie sandwich technique exploits how some servers parse legacy cookies with quotes and the $Version attribute, allowing an attacker to wrap an HttpOnly session cookie inside a reflected cookie value. Combined with an XSS and a tracking endpoint that reflects cookie values, an attacker could steal the HttpOnly PHPSESSID cookie.
portswigger.net
Published on portswigger.net
In this post, I will introduce the "cookie sandwich" technique which lets you bypass the HttpOnly flag on certain servers. This research follows on from Bypassing WAFs with the phantom $Version cookie.
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.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the server's cookie parser supports legacy RFC2109 syntax, where a cookie value starting with a double quote continues until the next unescaped double quote, so an attacker-controlled cookie can swallow subsequent cookies including HttpOnly ones.
Impact
An attacker could steal an HttpOnly session cookie, but only if an XSS exists and a suitable reflecting endpoint is present.
What the attacker needed
XSS on the target origin; Tracking endpoint reflects cookie value; Server uses legacy cookie parsing
The fix
Applications should avoid reflecting cookie values and should ensure cookie parsing is secure, while developers should understand their framework's cookie handling; the text does not state that a specific fix was applied.
How it was found
- Identify an XSS vulnerability that allows running JavaScript on the target origin.
- Find a tracking endpoint on a related domain that reflects a cookie value in its response and allows cross-origin requests with credentials.
- Use JavaScript to set a $Version cookie and a cookie with an opening quote, both with a path matching the tracking endpoint, to control cookie order.
- Append a closing quote cookie to complete the sandwich.
- Make a credentialed CORS request to the tracking endpoint, which reflects the manipulated cookie value including the HttpOnly session cookie.
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 write-up has not been written yet. The Original tab links to the source.
HttpOnly is a cookie flag that stops JavaScript from reading a cookie, protecting session tokens from theft via cross-site scripting (XSS). This research shows that on some servers, if you can run JavaScript on the page, you can create specially named cookies with quotes so that the server groups the protected cookie inside another cookie's value. If the application then reflects that value back, the script can read the protected cookie.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.