--- name: csrf-testing description: Cross-site request forgery testing for state-changing operations origin: RedteamOpencode --- # CSRF Testing (Cross-Site Request Forgery) ## When to Activate - State-changing operations (POST, PUT, DELETE, PATCH) - Cookie-based authentication - Forms or API calls that modify user data, settings, or permissions ## Tools - `run_tool curl` (manual request replay) - Burp Suite (generate CSRF PoC) - Browser DevTools (inspect cookies, headers) - Custom HTML PoC pages ## Methodology ### 1. Identify CSRF Protections - [ ] Check for CSRF token in forms (hidden field `csrf_token`, `_token`, `authenticity_token`) - [ ] Check for CSRF token in headers (`X-CSRF-Token`, `X-XSRF-Token`) - [ ] Check `SameSite` attribute on session cookies - [ ] Check `Referer` / `Origin` header validation - [ ] Check custom headers requirement (e.g., `X-Requested-With`) - [ ] Check `Content-Type` enforcement (JSON only = partial protection) ### 2. Test Token Validation - [ ] Remove CSRF token entirely — does request succeed? - [ ] Submit empty token value - [ ] Use token from another session / different user - [ ] Reuse old/expired token - [ ] Change token to arbitrary value - [ ] Check if token is tied to session or independent - [ ] Swap HTTP method: POST → GET (may skip token check) ### 3. Test SameSite Cookie Bypass - [ ] `SameSite=None` — no protection, full CSRF possible - [ ] `SameSite=Lax` — test top-level GET navigation (form method=GET) - [ ] `SameSite=Lax` — 2-minute window after cookie set (Chrome) - [ ] No SameSite set — defaults vary by browser (Lax in Chrome) - [ ] Check if API uses cookies at all (vs Bearer tokens) ### 4. Test Referer/Origin Validation - [ ] Remove `Referer` header entirely (use ``) - [ ] Set Origin to `null` (sandboxed iframe, data: URI) - [ ] Subdomain spoofing: `https://target.com.attacker.com` - [ ] Prefix bypass: `https://attacker.com/target.com` - [ ] Check regex flaws in validation ### 5. Build CSRF PoC - [ ] Auto-submit form: ```html
``` - [ ] Image tag for GET: `