This lab guide teaches Cross-Site Request Forgery (CSRF) attacks and defences through practical exercises on DVWA at four security levels. It demonstrates how browsers automatically send cookies with cross-origin requests, enabling attackers to forge authenticated actions (password changes, fund transfers) without user knowledge. The guide progresses from building a working CSRF proof-of-concept to analyzing why referer checks fail, token-based protection works, and requiring current password knowledge eliminates the attack entirely.
- Root cause: automatic cookie inclusionCSRF works because browsers automatically send session cookies with every request to a domain, including requests initiated by completely different websites. The application cannot distinguish a forged request from a legitimate one unless it requires a secret token only the real form possesses. This fundamental browser behavior is what makes CSRF possible.
- Low security: building working PoCThe lab demonstrates creating a malicious HTML page with a hidden 1×1 pixel image tag that sends a GET request to DVWA's password change endpoint. When a logged-in victim visits this page, the browser automatically fires the forged password-change request with their valid session cookie, changing the admin password without any visible user action or clicking suspicious links.
- Medium security: referer check failuresDVWA Medium security checks if the server name appears in the HTTP Referer header, but this is insufficient because: the stripos() check uses 'contains' rather than 'equals', allowing bypass by naming attacker pages to include the target domain in the path; browsers may not send Referer headers due to privacy settings or HTTPS-to-HTTP transitions; and proxies/VPNs can strip the header entirely. Referer checking is partial mitigation, not reliable defence.
- High security: anti-CSRF tokens explainedDVWA High security adds a unique, unpredictable user_token hidden field issued per page load that must match server-side session state. Attackers cannot guess the 128-bit random token or read it via cross-origin JavaScript due to same-origin policy. However, stored XSS on the DVWA domain can steal the token and include it in a forged request—this XSS-CSRF chaining is the only bypass for High security.
- Impossible level: current password requirementDVWA Impossible security requires users to enter their current password before changing it, validated via PDO parameterized queries. This completely defeats CSRF because attackers cannot know the victim's current password, making forged requests always fail regardless of token or cookie presence. This approach requires knowledge the attacker cannot possess—the cleanest defence for sensitive operations.
- SameSite cookie attribute: browser-level defenceModern browsers support the SameSite cookie attribute, which prevents cookies from being sent with cross-site requests entirely. Setting SameSite=Strict blocks cookies on all cross-site requests; Lax allows them only on top-level navigation GET requests. Chrome defaults to Lax since 2021, but explicit configuration is required; SameSite should be used alongside tokens and current-password requirements for defence in depth.
- Defence in depth: three-layer approachThe lab emphasizes that CSRF protection requires multiple layers: anti-CSRF tokens protect against forged requests; SameSite cookies prevent cookie transmission on cross-site requests in modern browsers; requiring current password makes CSRF useless by requiring knowledge only the legitimate user possesses. This layered approach ensures protection across older browsers, newer browsers, and non-browser clients.
- Practical exercises: from HTML to source reviewThe lab includes three hands-on exercises: building a working CSRF PoC HTML file that changes DVWA admin password; analyzing why referer checks fail; and reviewing DVWA source code at all four security levels using docker commands to identify specific protection mechanisms and their gaps. This progression teaches both attack and defence implementation.
Bottom line: CSRF attacks succeed because browsers automatically send cookies with cross-site requests, making applications unable to distinguish forged requests from legitimate ones without additional controls—the most robust solution combines unpredictable per-request tokens, SameSite=Strict cookie attributes, and requiring current-password knowledge for sensitive operations. Understanding why the Impossible level's current-password approach works (attacker cannot know what they do not have) is more import
🎯 What You’ll Master in Lab 4
⏱️ 22 min read · 3 hands-on exercises
📋 What This Lab Covers
Complete Lab 1 (DVWA Setup) before starting this lab. Navigate to http://127.0.0.1/dvwa/vulnerabilities/csrf/ with Security Level set to Low.
How CSRF Works — The Browser as a Weapon
CSRF exploits a fundamental browser behaviour: when your browser loads any resource — an image, a page, a form submission — it automatically sends all relevant cookies for that domain with the request. This is what keeps you logged in as you navigate between pages. But it also means any page you visit can cause your browser to make authenticated requests to other sites you are currently logged into, without your knowledge or consent.
⏱️ Time: 10 minutes · Target: DVWA at 127.0.0.1 — Low security
The DVWA CSRF module password change form sends a GET request:
?password_new=X&password_conf=X&Change=Change
No token. No referer check. Any page can fire this.
Create a file called csrf_poc.html on your Kali desktop with this content:
<html>
<head><title>Win a Prize!</title></head>
<body>
<h1>Congratulations! You won!</h1>
<p>Click here to claim your prize…</p>
<!– Hidden CSRF attack — fires automatically on page load –>
<img src=”http://127.0.0.1/dvwa/vulnerabilities/csrf/
?password_new=hacked&password_conf=hacked&Change=Change”
width=”1″ height=”1″>
</body>
</html>
Steps to test:
2. Open csrf_poc.html in the browser (File → Open File)
3. The img tag fires automatically — you will see a 1×1 pixel request
4. Navigate to DVWA CSRF module — it should confirm “Password Changed”
5. Log out and try logging in with password “hacked”
6. Reset the database (DVWA Setup page) to restore the original password
📸 Screenshot the “Password Changed” DVWA confirmation and share in #dvwa-csrf on Discord.
🧠 QUICK CHECK
Medium Security — Referer Check and Its Limits
At Medium security, DVWA adds a check: it reads the HTTP Referer header and rejects requests that do not appear to come from the DVWA domain. This represents a developer who has heard that referer checking prevents CSRF — but the implementation has significant weaknesses.
⏱️ Time: 3 minutes · No tools required
1. Browser privacy settings (can you rely on browsers sending Referer?)
2. HTTP vs HTTPS transitions (what happens to Referer across protocols?)
3. The stripos bypass shown above (contains vs equals check)
4. Corporate proxies and VPNs (do they strip Referer?)
5. What would happen if you used a POST form vs GET request?
For each point: does the referer check protect against it?
📸 Write your answers and share in #dvwa-csrf on Discord.
High Security — Token Protection
At High security, DVWA adds a user_token hidden field to the password change form. This is an anti-CSRF token — a unique, unpredictable value tied to the user’s session. The server rejects any request that does not include the correct token value.
An attacker cannot directly forge a valid token because the same-origin policy prevents cross-origin JavaScript from reading the token from DVWA’s page. However, if there is a stored XSS vulnerability on the DVWA domain, that XSS can be used to steal the token and include it in a subsequent forged request — this is the classic XSS-CSRF chained attack.
Impossible Security — What Actually Works
The Impossible level takes a different approach that makes CSRF attacks completely useless: it requires the user to enter their current password before setting a new one. An attacker forging a request cannot know the victim’s current password, so the forged request will always fail — regardless of whether tokens are used.
→ Any page can fire this. Zero friction for attacker.
→ Bypass: name attacker page to contain target domain in path
→ Bypass only possible with same-domain XSS to steal token
→ CSRF useless — attacker cannot know victim’s current password
⏱️ Time: 10 minutes · Target: Your DVWA Docker container
📸 Screenshot the Impossible-level source showing current_password check and share in #dvwa-source-review on Discord. Tag #dvwalab4
Modern CSRF Defence — SameSite Cookies
Modern browsers now support the SameSite cookie attribute, which is the browser-level defence against CSRF. When set to Strict or Lax, the browser simply does not send the cookie with cross-site requests at all — eliminating the root cause of CSRF rather than trying to detect or reject forged requests after the fact.
🧠 QUICK CHECK — Lab 4 Final
📋 Lab 4 Reference — CSRF Key Concepts
🏆 Lab 4 Complete — CSRF Mastered
From PoC HTML to token analysis — all 4 security levels done.
❓ Frequently Asked Questions – DVWA CSRF Lab
What is a CSRF attack?
What is the difference between CSRF and XSS?
How do anti-CSRF tokens prevent this attack?
Does HTTPS prevent CSRF?
What makes DVWA Impossible level safe from CSRF?
What DVWA lab comes after CSRF?
Lab 3: Command Injection
Lab 5: File Inclusion (LFI/RFI)
📚 Further Reading
- DVWA Lab 3: Command Injection 2026 — The previous lab — inject OS commands through web forms, establish a reverse shell, and analyse all four security levels.
- Bug Bounty Day 7: XSS Hunting — XSS is CSRF’s most dangerous enabler — a stored XSS on the target domain can steal anti-CSRF tokens and bypass High security.
- DVWA Labs Hub — The complete 30-lab DVWA series covering every vulnerability module from brute force through to advanced chaining techniques.
- PortSwigger: CSRF Complete Guide — The definitive CSRF reference with free interactive labs covering token bypass, SameSite edge cases, and multi-step CSRF attacks.
- OWASP: Cross-Site Request Forgery — OWASP’s authoritative CSRF reference covering attack mechanics, prevention techniques, and framework-specific implementations.

