Skip to content
CSRF Write-up Intermediate

How I leveraged an interesting CSRF vulnerability to turn self XSS into a persistent attack?

The application encoded angle brackets in the name field but allowed a payload without them, causing self-XSS. A CSRF vulnerability in the logout endpoint and a global CSRF token pool allowed an attacker to log the victim out and then log them into the attacker's account, triggering the stored XSS and enabling account takeover.

Akash Methani Follow

Published on medium.com

{{media:0}}

Akash Methani, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

The target was a web application with user profiles and a project collaboration feature. Two parts of the product matter here. The first is the edit profile page, where a user sets a display name. The second is the project invite flow: a user can invite other people to a project, and the application sends an email about the invite with a link to the project. Even if the recipient ignores the joining request, they still see the inviter's name and email.

The researcher's goal was to turn a self XSS in the name field into something that fires in someone else's session.

Setup

The report does not describe specific tooling, versions or a test environment. The work was done against a live application, using two roles implicitly: an attacker account whose profile name carries the payload, and a victim account that receives the invite. The only precondition that matters is that the attacker can invite arbitrary users to a project.

Finding it

On the edit profile page, the name field encoded angular brackets. A conventional <script> style payload was therefore not usable. The researcher tried a payload without any angular brackets:

Text
Akash'-alert()-

This was intended to concatenate the alert method into the surrounding JavaScript without breaking the context. It worked, and the payload executed.

Image for post
Image for post From the original on medium.com

On its own this is a harmless self XSS: the payload only runs in the session of the person who typed it. The interesting part was the invite flow. If an attacker invites someone to his project, the application emails that person about the invite with a link to the project, and the inviter's name and email are visible to the recipient even if they decline.

The plan was therefore: store the payload in the name field of an attacker account, invite the victim, and wait for the victim to view the inviter's details. The payload would fire in the victim's session, and the attacker could take over the account either by exfiltrating cookies to a server he controls or by programmatically changing the victim's account email.

That plan failed. The name field was only vulnerable in the attacker's own profile. On the victim's side the application rendered the attacker's name securely, so there was no execution in the victim's session. Escalation required chaining the self XSS with another vulnerability.

Exploitation

The next idea was login and logout CSRF. If the victim could be logged out of his own account and then logged into the attacker's account, the attacker's profile would be loaded in the victim's browser and the stored XSS in the name field would fire. That turns the self XSS into a persistent attack against anyone who loads the page.

Step 1: Test the logout and login endpoints for CSRF

The logout endpoint was vulnerable to CSRF. The login endpoint was not exploitable at first. On closer inspection, the token was being sent both in the cookies and in the POST request body. Developers sometimes forget to keep the two tokens in sync, so the researcher checked whether they matched. They did not, but the login endpoint still rejected the attempt because it expected the token to be an actual valid token.

Step 2: Test whether any valid token is accepted

Some applications keep a global pool of tokens and accept any valid token from that pool, which means any user's token can be used to craft a successful CSRF attack. The researcher checked whether an unauthenticated user's token, that is a token issued at the stage when a user is not logged in to any account, was valid. It was.

Step 3: Build the chain

The PoC page does two things. An img tag sends a GET request to the logout endpoint, which logs the victim out of his account. A JavaScript submit then posts the login form automatically with the attacker's credentials and the unauthenticated token. Once the attacker's account is logged in inside the victim's browser, the stored XSS payload in the name field triggers.

HTML
<!DOCTYPE html><html><head>    <title>PoC</title></head><body><img src="https://redacted.com/logout"><form method="POST" id="csrfForm" action=LOGIN-ENDPOINT>    <input type="hidden" name="csrf" value=OLD-CSRF>    <input type="hidden" name="email" value=ATTACKER-EMAIL>    <input type="hidden" name="password" value="ATTACKER-PASSWORD>    <input type="submit" value="Submit"></form><script type="text/javascript">    document.getElementById("csrfForm").submit()</script></body></html>

The img element issues the GET to the logout endpoint without needing a response to be rendered. The hidden csrf field carries the token obtained while unauthenticated, which the application accepts because it accepts any valid token from a global pool. The email and password fields carry the attacker's credentials, and the script submits the form as soon as the page loads. The victim ends up authenticated as the attacker, and the payload stored in the attacker's name field executes in that session.

Root cause

Two weaknesses combine. The first is a stored XSS in the name field that is only rendered unsafely in the owner's own profile view, which is why it behaves as self XSS until the session is swapped. The second is the CSRF design of the login endpoint: the token is present in both the cookie and the request body, the two are not synced, and the server accepts any valid token from a global pool, including one issued to an unauthenticated user. Because the token is not tied to the session it was issued for, an attacker can obtain a token before authentication and reuse it in a cross-site form submission. The logout endpoint was vulnerable to CSRF, which makes the forced logout step possible.

Impact

An attacker who can get a victim to load the PoC page can log the victim out and log him into the attacker's account, at which point the stored payload runs in the victim's browser. From there the attacker could take over the victim's account by sending cookies to a server he controls or by programmatically changing the victim's account email. The attack relies on the login CSRF succeeding, which depends on the global token pool accepting an unauthenticated token. The report does not describe the outcome on the live target.

Fix

Variants to test

  1. Check whether a token issued before login is accepted after login on other endpoints. A positive result is a state-changing request succeeding with a token obtained while unauthenticated.
  2. Compare the CSRF token in the cookie with the token in the request body and try mismatched pairs. A positive result is the request being processed despite the mismatch.
  3. Test the logout endpoint for CSRF. A positive result is a cross-site GET or POST logging the user out.
  4. Look for self XSS that renders unsafely only in the owner's view, then look for any flow that makes another user load that view, such as invites, shared projects or public profiles. A positive result is the payload executing in the second user's session.
  5. Try payloads without angular brackets in fields that are reflected into JavaScript contexts. A positive result is execution without any < or > characters.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

A security researcher found a small bug in a website where users have profiles and can invite other people to work on projects together. The bug was a type of cross-site scripting (XSS), which means the site let some text a user typed run as code in the browser. On its own this bug was harmless: it only ran in the browser of the person who typed it, which is called self XSS. The researcher wanted to make it hurt someone else, so he looked for a second bug that could be combined with the first. He found that the site's login form had a weakness called cross-site request forgery (CSRF), which lets an attacker make a victim's browser send a request the victim did not intend. By chaining the two bugs, he could force a victim to log out of their own account and log into the attacker's account, and then the stored XSS would run in the victim's browser. This article walks through each step in the same order the researcher took them, explaining every concept from scratch.

The product and the two features that matter

The target is a web application, which is just a website you use through a browser where you can create an account and do things like edit your profile or work on projects. Two features are important here.

The first is the edit profile page. Every user has a display name, the name other people see. You type it into a form and save it, and the site stores it.

The second is the project invite flow. A user can invite other people to join a project. The site sends an email about the invite with a link to the project. Even if the person who receives the email ignores the invite, they still see the inviter's name and email address.

The researcher's goal was to take a self XSS in the name field and make it fire in someone else's browser session. A session is the period during which you are logged in; the browser holds a small piece of data, usually a cookie, that tells the site who you are. If code runs in someone else's session, it can act as that person.

Setup

The report does not describe specific tools, software versions or a test environment. The work was done against a live application, using two roles implicitly: an attacker account whose profile name carries the payload, and a victim account that receives the invite. The only precondition that matters is that the attacker can invite arbitrary users to a project.

Finding the first bug

On the edit profile page, the name field encoded angular brackets. Angular brackets are the < and > characters. Encoding means the site replaces them with safe text so the browser shows them as characters instead of treating them as the start and end of an HTML tag. A conventional <script> style payload was therefore not usable, because the site would not let the < through as a real tag.

The researcher tried a payload without any angular brackets:

Text
Akash'-alert()-

This was intended to concatenate the alert method into the surrounding JavaScript without breaking the context. It worked, and the payload executed.

Image for post
Image for post From the original on medium.com

Let us unpack that payload. The payload was designed to concatenate the alert method into the surrounding code without breaking the context. The ' in the payload closes that string early. Then -alert()- is added to the code. alert() is a built-in browser function that shows a popup box; it is the classic harmless proof that code ran. The word concatenate means joining pieces of text or code together. Because the payload contains no < or >, the site's filter did not stop it.

On its own this is a harmless self XSS: the payload only runs in the session of the person who typed it. Self XSS means the code only executes for the user who entered it, so an attacker cannot use it against anyone else directly.

The interesting part was the invite flow. If an attacker invites someone to his project, the application emails that person about the invite with a link to the project, and the inviter's name and email are visible to the recipient even if they decline.

The plan was therefore: store the payload in the name field of an attacker account, invite the victim, and wait for the victim to view the inviter's details. The payload would fire in the victim's session, and the attacker could take over the account either by exfiltrating cookies to a server he controls or by programmatically changing the victim's account email.

Two terms there. Exfiltrating cookies means stealing the cookie that proves who you are and sending it to a server the attacker controls, so the attacker can pretend to be the victim. Programmatically changing the email means using code to submit the site's change-email form automatically, without the victim clicking anything.

That plan failed. The name field was only vulnerable in the attacker's own profile. On the victim's side the application rendered the attacker's name securely, so there was no execution in the victim's session. Escalation required chaining the self XSS with another vulnerability. Chaining means using one bug to enable another, so the combined effect is worse than either alone.

Exploitation

The next idea was login and logout CSRF. If the victim could be logged out of his own account and then logged into the attacker's account, the attacker's profile would be loaded in the victim's browser and the stored XSS in the name field would fire. That turns the self XSS into a persistent attack against anyone who loads the page.

CSRF, or cross-site request forgery, is a trick where a malicious page makes your browser send a request to another site where you are logged in. Your browser automatically attaches your cookies to that request, so the other site thinks the request came from you. A persistent attack is one that keeps working for every future visitor, rather than needing to be triggered once by hand.

Step 1: Test the logout and login endpoints for CSRF

The logout endpoint was vulnerable to CSRF. The login endpoint was not exploitable at first. On closer inspection, the token was being sent both in the cookies and in the POST request body. Developers sometimes forget to keep the two tokens in sync, so the researcher checked whether they matched. They did not, but the login endpoint still rejected the attempt because it expected the token to be an actual valid token.

Some definitions. An endpoint is a specific address on a site that does one job, such as /logout or the login form handler. A token here is a random string the site gives you to prove a form submission came from its own pages, not from an attacker's page; this is called a CSRF token. A cookie is a small piece of data the browser stores for a site and sends back with every request. A POST request is a request that sends data to the site, usually from a form, as opposed to a GET request that just asks for a page. The request body is the part of a POST request that carries the form fields.

So the login form carried the token in two places: the cookie and the form body. The researcher checked whether the two matched. In other words, a made-up or mismatched token was not enough.

Step 2: Test whether any valid token is accepted

Some applications keep a global pool of tokens and accept any valid token from that pool, which means any user's token can be used to craft a successful CSRF attack. The researcher checked whether an unauthenticated user's token, that is a token issued at the stage when a user is not logged in to any account, was valid. It was.

A global pool of tokens means the site generates tokens and accepts any of them, without checking which user or session the token was issued to. Unauthenticated means not logged in. The idea is simple: if the site accepts a token that was handed out before anyone logged in, then an attacker can visit the site, get a token without an account, and use it in a forged form. The site has no way to tell that the token was not issued to the victim.

Step 3: Build the chain

The PoC page does two things. An img tag sends a GET request to the logout endpoint, which logs the victim out of his account. A JavaScript submit then posts the login form automatically with the attacker's credentials and the unauthenticated token. Once the attacker's account is logged in inside the victim's browser, the stored XSS payload in the name field triggers.

A PoC, or proof of concept, is a small demonstration that the attack works. An img tag is normally used to show a picture, but browsers fetch whatever address you give it, so an attacker can use it to make the browser send a request without showing anything. Credentials here means the attacker's email and password.

HTML
<!DOCTYPE html><html><head>    <title>PoC</title></head><body><img src="https://redacted.com/logout"><form method="POST" id="csrfForm" action=LOGIN-ENDPOINT>    <input type="hidden" name="csrf" value=OLD-CSRF>    <input type="hidden" name="email" value=ATTACKER-EMAIL>    <input type="hidden" name="password" value="ATTACKER-PASSWORD>    <input type="submit" value="Submit"></form><script type="text/javascript">    document.getElementById("csrfForm").submit()</script></body></html>

Line by line:

  1. <!DOCTYPE html><html><head> <title>PoC</title></head><body> starts a normal web page with the title PoC. Nothing special happens yet.
  2. <img src="https://redacted.com/logout"> is the forced logout. The browser tries to load an image from the logout address. The site's logout endpoint acts on the request, so the victim is logged out. The browser does not need to display anything for this to work.
  3. <form method="POST" id="csrfForm" action=LOGIN-ENDPOINT> starts a form that will send a POST request to the login endpoint. The id gives the form a name so the script can find it later.
  4. <input type="hidden" name="csrf" value=OLD-CSRF> carries the CSRF token that was obtained while unauthenticated. type="hidden" means the field is not shown on the page, but it is still submitted.
  5. <input type="hidden" name="email" value=ATTACKER-EMAIL> carries the attacker's email address as the login name.
  6. <input type="hidden" name="password" value="ATTACKER-PASSWORD> carries the attacker's password. Note the quote before ATTACKER-PASSWORD is not closed in the report's code; it is reproduced exactly as written.
  7. <input type="submit" value="Submit"> is an ordinary submit button. The script does not need a human to press it.
  8. <script type="text/javascript"> document.getElementById("csrfForm").submit()</script> is the automation. It finds the form by its id and submits it as soon as the page loads. The victim never sees or clicks anything.

The img element issues the GET to the logout endpoint without needing a response to be rendered. The hidden csrf field carries the token obtained while unauthenticated, which the application accepts because it accepts any valid token from a global pool. The email and password fields carry the attacker's credentials, and the script submits the form as soon as the page loads. The victim ends up authenticated as the attacker, and the payload stored in the attacker's name field executes in that session.

Root cause

Two weaknesses combine. The first is a stored XSS in the name field that is only rendered unsafely in the owner's own profile view, which is why it behaves as self XSS until the session is swapped. Stored XSS means the malicious text is saved on the server and runs whenever the page is viewed. The second is the CSRF design of the login endpoint: the token is present in both the cookie and the request body, the two are not synced, and the server accepts any valid token from a global pool, including one issued to an unauthenticated user. Because the token is not tied to the session it was issued for, an attacker can obtain a token before authentication and reuse it in a cross-site form submission. The logout endpoint was vulnerable to CSRF, which makes the forced logout step possible.

Impact

An attacker who can get a victim to load the PoC page can log the victim out and log him into the attacker's account, at which point the stored payload runs in the victim's browser. From there the attacker could take over the victim's account by sending cookies to a server he controls or by programmatically changing the victim's account email. The attack relies on the login CSRF succeeding, which depends on the global token pool accepting an unauthenticated token. The report does not describe the outcome on the live target.

Fix

Variants to test

  1. Check whether a token issued before login is accepted after login on other endpoints. A positive result is a state-changing request succeeding with a token obtained while unauthenticated.
  2. Compare the CSRF token in the cookie with the token in the request body and try mismatched pairs. A positive result is the request being processed despite the mismatch.
  3. Test the logout endpoint for CSRF. A positive result is a cross-site GET or POST logging the user out.
  4. Look for self XSS that renders unsafely only in the owner's view, then look for any flow that makes another user load that view, such as invites, shared projects or public profiles. A positive result is the payload executing in the second user's session.
  5. Try payloads without angular brackets in fields that are reflected into JavaScript contexts. A positive result is execution without any < or > characters.

Glossary

  • Angular brackets: the < and > characters, used to open and close HTML tags.
  • Chaining: combining two or more bugs so the combined effect is worse than either alone.
  • Concatenate: join pieces of text or code together end to end.
  • Cookie: a small piece of data a site stores in your browser and gets back with every request, often used to keep you logged in.
  • Credentials: the email and password (or similar) used to log in.
  • CSRF (cross-site request forgery): a trick where a malicious page makes your browser send a request to another site where you are logged in.
  • CSRF token: a random string a site puts in its forms to prove the submission came from its own pages.
  • Encoding: replacing special characters with safe text so the browser shows them instead of treating them as code.
  • Endpoint: a specific address on a site that performs one job, such as logging out.
  • Exfiltration: stealing data, such as cookies, and sending it to a server the attacker controls.
  • GET request: a request that asks a site for a page or resource.
  • Global pool of tokens: a set of tokens where the site accepts any valid one, without checking which user or session it was issued to.
  • img tag: an HTML element normally used to show a picture, but which makes the browser fetch any address given to it.
  • Persistent attack: an attack that keeps working for future visitors because the malicious content is stored on the site.
  • PoC (proof of concept): a small demonstration that an attack works.
  • POST request: a request that sends data to a site, usually from a form.
  • Request body: the part of a POST request that carries the form fields.
  • Self XSS: cross-site scripting that only runs in the browser of the person who entered the payload.
  • Session: the period during which you are logged in, identified by a cookie.
  • Stored XSS: cross-site scripting where the malicious text is saved on the server and runs whenever the page is viewed.
  • Token: a random string used to prove identity or that a form came from the site's own pages.
  • Unauthenticated: not logged in to any account.
  • XSS (cross-site scripting): a bug where a site lets text a user typed run as code in the browser.

More by Akash Methani

View all

2 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site request forgery (CSRF)

The lesson behind this report.

Another site makes a signed-in user's browser perform an action they did not intend.