Story of Stored Xss
Published on medium.com
Walid Hossain a bug hunter and web penetration tester from Bangladesh.This is my first write up to the medium community!I am sharing a write up about one of my recent finding(stored xss).
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.
Context
This is a stored cross-site scripting (XSS) finding on a private site, referred to throughout as redacted.com. The report does not describe the product beyond one feature: a search bar that suggests users, and a display name that each account can set. The behaviour that matters is that the search bar shows other users to you, and the display name of those users is rendered in the results.
The author is Walid Hossain (@NoobWalid), a bug hunter and web penetration tester from Bangladesh, and this is his first write-up for the Medium community. The issue was found on 15 November, submitted the same day, marked wont fix on 21 November, triaged on 25 November and a bounty was awarded on 26 November.
Setup
The only precondition is the ability to create accounts on the target. The researcher created two accounts on redacted.com. While creating them he set the display name to the payload below:
TEST"><svg onload=alert(1)//
The idea behind the leading TEST string comes from a tip the author read on Twitter: always put something before the XSS payload, so that the injected string can be found later by searching for that word. If the input is not properly filtered, the payload fires when the TEST word is displayed.
Finding it
The researcher typed the display name into the search bar and the XSS fired. The input was not filtered, and the tip worked. He reported the issue to the private program without thinking much further about it.
Two days later the program replied and marked the report as wont fix, calling it self XSS: the victim could not find the attacker's name in the search bar, so the victim could only attack himself.
Exploitation
After the wont fix, the researcher spent a day understanding the functionality of redacted.com and established two rules for the search bar:
- If user1 is following user2, then user2 will appear in user1's search bar.
- If user1 is not following user2, then user2 will not appear.
That turns the self XSS into a stored XSS that reaches another user, provided the victim follows the attacker. The attack scenario submitted was:
Step 1: set the payload as the display name
The attacker sets his display name to:
Attacker1"><svg onload=alert(1)//
Step 2: follow the victim
The attacker, Attacker1, follows victim1. The attacker follows the victim as the first move in the scenario; the report does not say this step is required for the name to appear.
Step 3: get the victim to follow back
Victim1 gets interested and also follows Attacker1. This is the social step: the victim has to follow the attacker's account for the stored name to be shown to him.
Step 4: the victim searches
Victim1 wants to see the attacker's profile, so he starts typing Attacker1 into the search bar.
Step 5: the payload fires
The XSS pops out in the victim's browser, because the stored display name is rendered in the search results without being filtered.
The researcher submitted the report with this scenario. Some days later an analyst triaged it, thanking him for a detailed POC.
Root cause
The display name is stored as supplied and later rendered in the search results without correct filtering, so the payload fires. The first report was closed as self XSS because the victim could not find the attacker's name in the search bar, so the victim could only attack himself. The missing piece was the follow relationship: once the victim follows the attacker, the attacker's stored name is displayed to the victim, and the payload executes in the victim's session.
Impact
The payload runs in the browser of a user who follows the attacker and then searches for the attacker's name, as in the submitted scenario. Under those conditions an attacker can execute script in the victim's session on redacted.com. The report does not go beyond alert(1) and does not describe what an attacker could do with the execution, such as reading data or acting as the victim.
Fix
The general fix is to filter or encode the display name on output, so that it is rendered as text in the search results rather than as markup, and to apply the same treatment wherever a user-supplied name is displayed.
Variants to test
- Put a unique marker before the payload, as in
TEST"><svg onload=alert(1)//, then search for the marker. A positive result is the payload firing when the marker is displayed. - Test every place a display name is rendered, not only the search bar: profile pages, follower lists, mentions and notifications. A positive result is the payload firing in any of those views.
- Test the follow relationship rule on other targets: create two accounts, follow from one to the other, and check whether the followed account's stored fields appear in the follower's search results. A positive result is the stored payload being shown to the second account.
- Try the same payload in other profile fields that are shown to other users, such as a bio or a username. A positive result is the payload firing when another account views the field.
What happened, in plain words
A security researcher found a way to make a website run his own code in someone else's browser. The trick was to put a small piece of code into his profile name. The site saved that name and later showed it to other people without cleaning it up. When another person searched for him, the code ran. The site first said it would not fix the problem, because it looked like the researcher could only attack himself. He then worked out that the site only shows you people you follow, so he got a victim to follow him back. After that, the code ran in the victim's browser. The report was later triaged and a bounty was awarded.
This article explains the whole story from the beginning, assuming you know how to use a browser and nothing more.
The basics: what is stored XSS?
XSS stands for cross-site scripting. It is a type of bug where a website takes something a user typed and later shows it to someone else as if it were part of the page, instead of showing it as plain text. When that happens, the thing the user typed can include instructions that the browser follows, such as running a small program.
A simple comparison: imagine a noticeboard where people can pin notes. If the person in charge copies whatever is written on a note directly onto an official sign, then someone could write "ignore all other signs" on their note and it would appear as an official instruction. The problem is not the note; it is that the note's content was treated as an instruction rather than as text.
Stored XSS is the version where the website saves the input first (in a database, for example) and shows it later to other people. The input is stored and then displayed without being correctly filtered. The report puts it this way:
Stored XSS occurs when a web application gathers input from a user which might be malicious, and then stores that input in a data store for later use. The input that is stored is not correctly filtered.
So there are two ingredients: the site saves what you typed, and the site later puts it into a page without making it safe.
The tip that started it
The researcher read a tip on Twitter about finding XSS. The tip was: always put something before the XSS payload. For example, put the word TEST before the code. Then, when you search for the word TEST on the site, if the site is not filtering properly, the code will run. The idea is that the word TEST acts as a marker, so you can find where your input ends up.
The payload used in the tip is:
TEST"><svg onload=alert(1)//
Let us go through this piece by piece.
TESTis just a word. It is the marker. It has no special meaning to the browser; it is there so the researcher can search for it later."is a double quote. In the HTML code of a web page, double quotes are often used to mark the beginning and end of an attribute value. Putting a double quote in your input can end the attribute early, which lets the rest of your input be treated as new HTML rather than as part of the value.>is a greater-than sign. In HTML, it marks the end of a tag. So">together can close an attribute and close the tag, letting the following text start a new tag.<svgstarts an SVG element. SVG is a way to draw graphics in a web page, and browsers treat it as HTML-like markup. The important part here is that SVG elements can have event handlers.onload=alert(1)is an event handler.onloadmeans "when this element finishes loading".alert(1)is a built-in browser function that shows a small pop-up box with the number 1. So when the SVG element loads, the browser runsalert(1).//is a comment marker in JavaScript. It makes the rest of the line a comment, which can help avoid breaking the surrounding code.
So the whole payload tries to break out of whatever HTML the site put around the display name, create an SVG element, and make it run alert(1) when it loads. The alert(1) is harmless; it is just a way to prove that code ran. A real attacker would use something more harmful.
Setting up the test
The researcher was looking at a private site. In the report it is called redacted.com, which means the real name is hidden. He created two accounts on that site. While creating them, he set the display name to the payload above. The display name is the name other users see for your account, like a nickname.
Why two accounts? Because to test whether one user can attack another, you need at least two users: one to be the attacker and one to be the victim. With two accounts you can play both roles yourself.
Why set the display name to the payload? Because the display name is shown to other people. If the site does not clean it up, the payload will run when someone views it. The TEST marker makes it easy to find later: you can search for TEST and see where your name appears.
Finding the bug
After creating the accounts, the researcher typed the name into the search bar. The search bar is a feature that suggests users as you type. When he typed the name, the XSS fired. That means the browser ran the alert(1) and showed a pop-up. The input was not filtered, and the tip worked.
He reported the issue to the private program. A private program is a bug bounty programme where only invited researchers can participate; the site owner pays for reports of security bugs. He did not think much more about it at that point.
The first rejection
Two days later, the program replied and marked the report as "wont fix". They called it self XSS. Self XSS means the only person who can trigger the bug is the person who entered the malicious input. In this case, they said the victim could not find the attacker's name in the search bar, so the victim could only attack himself.
Why did they think that? Because the search bar only shows you certain users. If the attacker's name does not appear when the victim searches, then the victim never sees the payload, so the payload never runs in the victim's browser. From the program's point of view, the bug was only reachable by the attacker himself, which is much less serious.
Working out how to reach a victim
The researcher then spent a day understanding how the site works. He found two rules for the search bar:
- If user1 is following user2, then user2 will appear in user1's search bar.
- If user1 is not following user2, then user2 will not appear.
"Following" is a common feature on social sites: you subscribe to another person's updates. The key point is that the search bar only shows people you already follow. That explains the first rejection: the victim did not follow the attacker, so the attacker's name did not appear, so the payload did not run.
This turns the self XSS into a stored XSS that reaches another user, provided the victim follows the attacker. The attack scenario he submitted was:
Step 1: set the payload as the display name
The attacker sets his display name to:
Attacker1"><svg onload=alert(1)//
This is the same payload as before, but with Attacker1 instead of TEST. Attacker1 is the name the attacker will use, and it also acts as the marker. The rest is the same: a double quote and greater-than sign to break out of the HTML, an SVG element with an onload handler that runs alert(1), and // to comment out the rest.
Why use Attacker1 instead of TEST? Because the victim will search for the attacker's name. If the name contains the payload, then searching for the name will display the payload. The name itself is the marker.
Step 2: follow the victim
The attacker, Attacker1, follows victim1. The attacker follows the victim as the first move in the scenario; the report does not say this step is required for the name to appear. In other words, following the victim is the first half of the relationship that the search bar requires.
Why does the attacker need to follow the victim? Because the rule is about following. The search bar shows people you follow. For the attacker's name to appear in the victim's search results, the victim must follow the attacker. The report simply has the attacker follow the victim first, without explaining why.
Step 3: get the victim to follow back
Victim1 gets interested and also follows Attacker1. This is the social step: the victim has to follow the attacker's account for the stored name to be shown to him. There is no technical trick here; it relies on normal user behaviour. The attacker follows the victim, the victim notices and follows back.
Why is this step necessary? Because of the rule discovered earlier: the search bar only shows accounts you follow. If the victim does not follow the attacker, the attacker's name will not appear in the victim's search results, and the payload will not run.
Step 4: the victim searches
Victim1 wants to see the attacker's profile, so he starts typing Attacker1 into the search bar. This is a normal action: you type a name to find a person. The victim is not doing anything unusual.
Why does the victim search? Because he is interested in the attacker's profile. The attacker's goal is to get the victim to search for his name, because that is when the stored name is displayed.
Step 5: the payload fires
The XSS pops out in the victim's browser, because the stored display name is rendered in the search results without being filtered. When the victim types Attacker1, the search results show the attacker's display name. The stored display name is rendered in the search results without being filtered, so the payload runs. The pop-up appears in the victim's browser, not the attacker's.
The researcher submitted the report with this scenario. Some days later an analyst triaged it, thanking him for a detailed POC. "Triaged" means the report was reviewed and accepted as valid. "POC" stands for proof of concept: a demonstration that the bug is real.
Why it worked
The display name is stored as supplied and later rendered in the search results without correct filtering, so the payload fires. The first report was closed as self XSS because the victim could not find the attacker's name in the search bar, so the victim could only attack himself. The missing piece was the follow relationship: once the victim follows the attacker, the attacker's stored name is displayed to the victim, and the payload executes in the victim's session.
A "session" is the period during which you are logged in. When you log in, the site gives your browser a cookie or token, which is a small piece of data that proves who you are. Every request you make includes that cookie, so the site knows it is you. If an attacker can run code in your browser while you are logged in, that code can act as you: it can read your data, send messages, or change your settings. That is why XSS is serious.
The report does not describe the server-side filtering or output encoding in any more detail, so the exact defence that failed is not stated.
What an attacker could do
The payload runs in the browser of a user who follows the attacker and then searches for the attacker's name, as in the submitted scenario. Under those conditions an attacker can execute script in the victim's session on redacted.com. The report does not go beyond alert(1) and does not describe what an attacker could do with the execution, such as reading data or acting as the victim.
In general, if an attacker can run script in your session, they can do anything you can do on the site: read your private messages, post on your behalf, change your password, or steal your session cookie. The report only demonstrates the pop-up, which is enough to prove the bug exists.
How to fix it
The general fix is to filter or encode the display name on output, so that it is rendered as text in the search results rather than as markup, and to apply the same treatment wherever a user-supplied name is displayed.
"Encoding" means converting special characters into a safe form. For example, < becomes < and > becomes >. Then the browser shows them as text instead of treating them as HTML. "Filtering" means removing or rejecting dangerous characters. Either way, the goal is that user input is always treated as text, never as code.
Other things to test
- Put a unique marker before the payload, as in
TEST"><svg onload=alert(1)//, then search for the marker. A positive result is the payload firing when the marker is displayed. - Test every place a display name is rendered, not only the search bar: profile pages, follower lists, mentions and notifications. A positive result is the payload firing in any of those views.
- Test the follow relationship rule on other targets: create two accounts, follow from one to the other, and check whether the followed account's stored fields appear in the follower's search results. A positive result is the stored payload being shown to the second account.
- Try the same payload in other profile fields that are shown to other users, such as a bio or a username. A positive result is the payload firing when another account views the field.
Timeline
The report gives these dates:
- Issue found: 15 November
- Submitted: 15 November
- Marked wont fix: 21 November
- Triaged: 25 November
- Bounty awarded: 26 November
Glossary
- XSS (cross-site scripting): a bug where a website shows user input as code instead of text, so the browser runs it.
- Stored XSS: XSS where the malicious input is saved by the site and shown later to other users.
- Self XSS: XSS where only the person who entered the input can trigger it, so it is much less serious.
- Payload: the piece of code or input an attacker uses to exploit a bug.
- Display name: the name other users see for your account, like a nickname.
- Search bar: a feature that suggests users as you type.
- Following: subscribing to another user's updates; the search bar only shows people you follow.
- HTML: the language web pages are written in; it uses tags and attributes.
- Attribute: a property of an HTML tag, often written with quotes, like
name="value". - Tag: an HTML element, like
<svg>or<div>. - SVG: a way to draw graphics in a web page; SVG elements can have event handlers.
- Event handler: code that runs when something happens, like
onloadwhen an element loads. alert(1): a browser function that shows a pop-up with the number 1; used to prove code ran.- Session: the period you are logged in; the site gives your browser a cookie or token to prove who you are.
- Cookie: a small piece of data stored by your browser that the site uses to recognise you.
- Token: similar to a cookie; a piece of data that proves who you are.
- Encoding: converting special characters into a safe form, like
<to<, so they are shown as text. - Filtering: removing or rejecting dangerous characters from user input.
- POC (proof of concept): a demonstration that a bug is real.
- Triaged: reviewed and accepted as valid by the programme.
- Wont fix: the programme decided not to fix the bug.
- Bug bounty programme: a scheme where researchers are rewarded for reporting security bugs.
- Private programme: a bug bounty programme open only to invited researchers.
- Bounty: the reward paid for a valid report.