Making Users Do Things They Never Asked For
Imagine you're logged into your bank. Now imagine clicking a link that looks harmless - maybe "cute cat pictures" - but behind the scenes, that link made your browser send a money transfer request to your bank. Your bank sees a valid request from your logged-in session and processes it. You just got robbed by a cat picture.
That's Cross-Site Request Forgery (CSRF or XSRF). The attacker doesn't steal your credentials - they make you use them unknowingly. It's like someone grabbing your hand while you're holding your credit card and making you tap it on their payment terminal.
How CSRF Actually Works
Let's break down the attack step by step:
CSRF Attack Flow
A Concrete Example
CSRF Attack Vectors
There are several ways to make a victim's browser send requests:
1. Auto-Submitting Form
2. Image Tag (GET requests)
3. XHR/Fetch (Limited by CORS)
4. Hidden Iframe
Identifying CSRF Vulnerabilities
Here's how to test for CSRF:
CSRF Testing Methodology
csrf_token, _token,authenticity_token. If none exist, it might be vulnerable.- Removing the token entirely
- Using an empty token
- Using a different user's token
- Using an old/expired token
- Changing one character of the token
What to Look For
CSRF Protection Bypass Techniques
Sometimes CSRF protections are implemented poorly:
Token Not Validated for All Methods
Token Validation Depends on Presence
Token Not Tied to Session
Token in Cookie (Double Submit)
Referer Header Validation
Crafting a CSRF Proof of Concept
Here's a template for CSRF PoCs:
Advanced: CSRF with JSON Body
Key Takeaways
- CSRF tricks users into making unwanted requests while authenticated
- It exploits the browser's automatic inclusion of cookies
- The attacker doesn't need to steal credentials - they use yours
- Auto-submitting forms and image tags are common attack vectors
- Anti-CSRF tokens, SameSite cookies, and Referer checking prevent it
- Tokens must be tied to sessions to be effective
- Test by removing, modifying, or reusing tokens to find weaknesses