--- name: csrf description: > Cross-Site Request Forgery (CSRF) tricks authenticated users into submitting forged requests to a target application by exploiting browser automatic cookie attachment. Detect via missing or predictable CSRF tokens in state-changing requests (POST/PUT/DELETE), absent `SameSite` cookie attributes, and JSON endpoints accepting `text/plain` Content-Type. Test using HTML auto-submitting forms, XHR requests, and CORS-enabled fetch. Tools: Burp Suite (Generate CSRF PoC), OWASP ZAP. license: MIT compatibility: Designed for Claude Code. Requires Burp Suite or OWASP ZAP, HTML PoC hosting. metadata: category: client-side version: "0.1" source: https://owasp.org/www-project-web-security-testing-guide/stable/ source_types: framework wstg: WSTG-SESS-05 --- # Cross-Site Request Forgery (CSRF) ## What Is Broken and Why CSRF exploits the browser's automatic inclusion of credentials (cookies, HTTP Basic auth) with every request to a given origin. An attacker-controlled page on a different origin can cause the victim's browser to send authenticated requests to the target application. Since the browser automatically attaches the session cookie, the server cannot distinguish the forged request from a legitimate one — unless it validates a secret token that only the legitimate page would know. Absent CSRF tokens, `SameSite=Strict/Lax` cookie attributes, or origin validation, any state-changing operation is potentially exploitable. ## Key Signals - State-changing requests (account settings, password change, fund transfer, email change) without a CSRF token - CSRF token present but predictable, static, or not validated server-side - Cookie lacks `SameSite=Strict` or `SameSite=Lax` attribute - `Content-Type: application/json` endpoints that also accept `text/plain` (allows form-based CSRF) - API endpoints not verifying `Origin` or `Referer` headers - WebSocket handshakes without origin validation ## Methodology 1. Identify all state-changing requests (POST, PUT, PATCH, DELETE) in the application. 2. Check for CSRF token in request parameters or headers (`X-CSRF-Token`, `_token`, `csrf_token`). 3. If token is present: test whether it is validated (remove it, use an invalid value, reuse an old token, use another user's token). 4. If no token: confirm the action is exploitable by crafting a PoC HTML page. 5. Check `SameSite` attribute of session cookie; `Lax` provides partial protection (GET only, top-level nav). 6. Test JSON endpoints: try submitting the same payload as `text/plain` or `application/x-www-form-urlencoded`. 7. Test whether `Referer` header is validated and if it can be stripped or spoofed. 8. Use Burp's "Generate CSRF PoC" feature (right-click on request in Proxy history). ## Payloads & Tools ```html
POST /change-password HTTP/1.1 Host: TARGET Cookie: session=TOKEN new_password=attacker123 POST /change-password HTTP/1.1 csrf_token=AAAAAAAAAAAAAAAA ``` ## Bypass Techniques - Remove CSRF token entirely (some servers skip validation when token is absent) - Submit empty/null CSRF token value - Reuse a valid token from another session (if tokens are not per-user) - Use attacker's own valid CSRF token for victim's request (if not tied to session) - Strip Referer header via `` or HTTPS → HTTP downgrade - For `SameSite=Lax`: exploit GET-based state changes; or use top-level navigation (window.open) - JSON endpoint: attempt `Content-Type: text/plain` with JSON body shaped as `name=value` form data - Subdomain takeover → subdomain can set/read parent domain cookies (bypasses SameSite in some configs) - Flash-based CSRF (legacy): Flash objects bypass SameSite for cross-origin requests in old browsers ## Exploitation Scenarios **Scenario 1 — Account Email Takeover** Setup: `/account/change-email` accepts POST with `email` parameter; no CSRF token; session cookie lacks SameSite. Trigger: Victim visits attacker page containing auto-submitting form POSTing to the email change endpoint. Impact: Victim's email changed to attacker's address; attacker uses "forgot password" to gain full account control. **Scenario 2 — Fund Transfer via JSON Endpoint** Setup: Banking app's transfer API accepts JSON but does not enforce `Content-Type` (accepts `text/plain`). Trigger: Attacker hosts page with form using `enctype="text/plain"` where input name contains valid JSON prefix. Impact: Victim unknowingly authorizes fund transfer; attacker receives funds. **Scenario 3 — Admin Action via GET Request** Setup: Admin panel uses GET requests for user deletion: `/admin/delete-user?id=123`; no CSRF protection. Trigger: Attacker embeds `