Understanding CSRF: How Attackers Trick Users Into Performing Unauthorized Actions
Imagine you're logged into your bank account in one browser tab.
You open another tab and visit a website that contains malicious content.
Without realizing it, your browser sends a request to your bank using your existing login session.
What if that request transfers money?
This is the basic idea behind Cross-Site Request Forgery (CSRF).
CSRF is a web application vulnerability where an attacker tricks an authenticated user's browser into sending an unwanted request to a website where the user is already logged in.
Let's understand how it works.
1. First, Understand How Authentication Cookies Work
Before understanding CSRF, we need to understand cookies.
Imagine you log in to:
https://bank.example.com
After successful authentication, the server sends a session cookie:
HTTP/1.1 200 OK
Set-Cookie: sessionid=abc123; Secure; HttpOnly
Your browser stores this cookie.
Whenever you make another request to the same website, the browser automatically includes the cookie.
For example:
POST /account HTTP/1.1
Host: bank.example.com
Cookie: sessionid=abc123
The server recognizes your session and knows you are authenticated.
This automatic cookie behavior is an important part of understanding CSRF.
2. How Does a CSRF Attack Work?
Let's use a simple example.
Suppose a banking application has an endpoint:
POST /transfer HTTP/1.1
Host: bank.example.com
Cookie: sessionid=abc123
amount=500&to=attacker
The application checks the session cookie and processes the transfer.
Now imagine an attacker creates a malicious webpage containing:
<form action="https://bank.example.com/transfer" method="POST">
<input type="hidden" name="amount" value="500">
<input type="hidden" name="to" value="attacker">
</form>
<script>
document.forms[0].submit();
</script>
The attacker tricks an authenticated victim into visiting that page.
If the browser sends the bank's session cookie with the cross-site request, and the application lacks effective CSRF protections, the transfer might be processed.
The victim may not even realize that the request happened.
Important: This example assumes the application accepts the request without additional CSRF validation and that cookie settings allow the cross-site request.
3. Understanding the Attack Flow
Let's visualize the complete process.
1. Victim logs into bank.example.com
|
v
2. Browser stores authentication cookie
|
v
3. Victim visits attacker's website
|
v
4. Malicious webpage triggers a request
|
v
5. Browser may automatically attach
the bank's authentication cookie
|
v
6. Bank receives the request
|
v
7. If CSRF protection is missing,
the action may be executed
The important thing to understand is that the attacker does not necessarily need to know the victim's password or session cookie.
The browser already has the authenticated session.
4. Why Does CSRF Happen?
The fundamental problem is that the application assumes:
"If a request contains a valid session cookie, it must have been intentionally initiated by the authenticated user."
But this assumption is not always correct.
A browser can send authenticated requests that were triggered by another website.
The server needs additional mechanisms to verify that the request was legitimately initiated.
5. Types of CSRF
A. GET-Based CSRF
Imagine an application performs a sensitive action through a GET request:
GET /change-email?email=attacker@example.com HTTP/1.1
Host: example.com
An attacker might create:
<img src="https://example.com/change-email?email=attacker@example.com">
When the browser loads the image, it sends a GET request.
If the application performs the sensitive action through GET and accepts the victim's session cookie, the action might succeed.
Sensitive state-changing operations should not use GET requests.
B. POST-Based CSRF
POST-based CSRF uses an HTML form to submit data to a target website.
For example:
<form action="https://example.com/change-password" method="POST">
<input type="hidden" name="password" value="newpassword123">
</form>
<script>
document.forms[0].submit();
</script>
Whether this works depends on the application's CSRF protections, cookie policy, and other browser security controls.
C. Login CSRF
Login CSRF is another interesting variation.
An attacker attempts to make a victim log into an application using an account controlled by the attacker.
This can cause the victim to unknowingly interact with the attacker's account.
6. How Do Developers Prevent CSRF?
There are several important defensive mechanisms.
A. CSRF Tokens
A server generates a unique, unpredictable token and associates it with the user's session.
For example:
<form method="POST" action="/transfer">
<input type="hidden" name="csrf_token" value="random-token">
<input type="text" name="amount">
<button type="submit">Transfer</button>
</form>
When the form is submitted, the server validates the token.
An attacker on another website generally cannot read the legitimate page's token because of the browser's same-origin policy.
Without a valid token, the server rejects the request.
B. SameSite Cookies
Cookies support the SameSite attribute.
Example:
Set-Cookie: sessionid=abc123; Secure; HttpOnly; SameSite=Lax
Common values:
| Attribute | Behavior |
|---|---|
| Strict | Restricts cookies to same-site contexts |
| Lax | Allows cookies in certain cross-site navigations |
| None | Allows cross-site cookie use, requiring Secure |
SameSite helps reduce CSRF risk, but it should not automatically replace appropriate CSRF protections.
C. Origin and Referer Validation
Applications can validate the Origin header and, where appropriate, the Referer header to check where a request originated.
For example:
Origin: https://attacker.example
If the application only accepts requests originating from its trusted website, it can reject unexpected origins.
These headers should be validated carefully, with a safe policy for missing or malformed values.
7. How to Test CSRF Using Burp Suite
When testing an application you own or are authorized to assess:
- Log in to the application.
- Identify sensitive actions such as changing an email address or updating account settings.
- Intercept the corresponding request in Burp Suite.
- Look for CSRF tokens.
- Investigate whether the token is validated server-side.
- Test whether the application checks Origin or Referer.
- Examine the authentication cookie's SameSite configuration.
- Verify whether an unauthorized cross-site request can actually perform the action.
Pay attention to:
- Missing CSRF tokens.
- Tokens that are not validated.
- Tokens that can be reused incorrectly.
- Sensitive GET endpoints.
- Inadequate Origin validation.
- Cookie configurations that permit unwanted cross-site requests.
Remember: a missing CSRF token alone does not always establish an exploitable CSRF vulnerability. Modern applications may have other effective protections.
8. CSRF vs XSS: What Is the Difference?
These two vulnerabilities are often confused.
| CSRF | XSS |
|---|---|
| Tricks a victim's browser into sending unwanted requests | Injects attacker-controlled JavaScript into a trusted page |
| Abuses the victim's authenticated session | Executes scripts in the context of the vulnerable website |
| Does not inherently require JavaScript execution on the target website | Requires script execution in the target website's origin |
| CSRF tokens and SameSite cookies help prevent it | Contextual output encoding and safe sanitization help prevent it |
One important relationship:
XSS can often bypass CSRF protections because malicious JavaScript executing within the trusted origin can access page content and submit legitimate requests.
Conclusion
CSRF teaches us something important about web application security:
Authentication alone does not prove that a user intentionally initiated a request.
Browsers automatically manage cookies and sessions, and attackers can sometimes abuse this behavior to trigger unwanted actions.
Understanding cookies, HTTP requests, browser security policies, and server-side validation is essential for identifying and preventing CSRF vulnerabilities.
Don't just memorize payloads. Understand why the browser sends the request and why the server accepts it.
Top comments (0)