CSRF Attacks: Bypassing SameSite Cookies
A comprehensive guide to understanding and exploiting CSRF vulnerabilities with SameSite bypass techniques
Introduction to CSRF
Cross-Site Request Forgery (CSRF) is an attack that forces authenticated users to submit malicious requests. Attackers exploit the trust a web application has in the user’s browser.
How CSRF Works
- User logs into a vulnerable website (e.g., bank.com)
- Session cookie is stored in the browser
- User visits attacker’s page containing malicious request
- Browser automatically includes the session cookie with the request
- Vulnerable server processes the request as legitimate
SameSite Cookies Protection
The SameSite cookie attribute was introduced to mitigate CSRF attacks by restricting when cookies are sent with cross-site requests.
SameSite Value | Description | CSRF Protection |
---|---|---|
Strict | Cookies only sent in first-party context | Strong |
Lax | Cookies sent with top-level navigation (GET requests) | Moderate |
None | Cookies always sent (requires Secure flag) | None |
Modern browsers default to SameSite=Lax
when no attribute is specified.
Bypassing SameSite Restrictions
1. Exploiting Lax Policy with GET Requests
The Lax policy allows cookies to be sent with top-level navigation via GET requests:
<img src="https://vulnerable.com/transfer?amount=1000&to=attacker">
When the user visits a page containing this image, the browser will include the session cookie if the domain matches.
2. Using 307/308 Redirects
HTTP 307/308 status codes preserve the method and body of the original request during redirects:
POST /malicious-form HTTP/1.1
Host: attacker.com
Content-Type: application/x-www-form-urlencoded
action=transfer&amount=1000&to=attacker
HTTP/1.1 308 Permanent Redirect
Location: https://vulnerable.com/api/transfer
The browser will follow the redirect and send the POST request with cookies.
3. Exploiting Subdomain Takeovers
If you can control a subdomain of the target site, cookies may be sent to it:
// If vulnerable.com sets cookies for *.vulnerable.com
document.cookie = "malicious=payload; domain=.vulnerable.com";
4. Browser Flaws and Inconsistencies
Some browsers may not properly enforce SameSite rules in certain scenarios:
- Older browser versions
- Non-standard port numbers
- IP addresses instead of domains
Testing Methodology
- Identify sensitive actions (password change, funds transfer)
- Check if SameSite protection is implemented
- Test with various HTTP methods (GET, POST, PUT)
- Try different content types (form, JSON, XML)
- Experiment with redirect chains
Mitigation Techniques
For Developers:
- Use
SameSite=Strict
for sensitive actions - Implement CSRF tokens
- Require re-authentication for sensitive operations
- Validate the
Origin
andReferer
headers
For Pentesters:
Always include SameSite bypass attempts in your CSRF testing methodology, especially for critical applications.
Conclusion
While SameSite cookies provide significant protection against CSRF, they are not foolproof. Understanding these bypass techniques is essential for both attackers and defenders.
Further Resources
Discover more from Cyber Samir
Subscribe to get the latest posts sent to your email.