The Story of My First Reflected XSS
A search box on a home page reflected user input without proper filtering, allowing a reflected cross-site scripting attack. An attacker could run script in a victim's browser via a crafted link.
Published on medium.com
Hello guys, hope you all are doing well, This is my second writeup on medium, I’m Ahmed Kamal a Security Researcher and bug bounty hunter from Egypt. In this writeup, I am going to talk about a Reflected XSS Vulnerability on a VDP on my lovely platform bugcrowd , But Due to the company’s policy I can’t reveal the name…
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 the story of my first reflected XSS finding, on a vulnerability disclosure programme (VDP) hosted on Bugcrowd. The programme's policy does not allow the target to be named, so throughout the write-up the target is called example.com. The vulnerable feature was the search box on the home page: a visitor types a query, and the application reflects it back on the results page.
The finding was made about a month before the write-up was published. It was reported and marked as a duplicate.
Setup
The search box is on the home page, so no parameter collection across the domain was needed.
Because the reflection was in the search box on the home page, there was no need to collect parameters across the domain, so not all of the usual XSS tooling was used. The tools I list are:
- ParamSpider: https://github.com/devanshbatham/ParamSpider
- Gxss: https://github.com/KathanP19/Gxss
- kxss: https://github.com/Emoe/kxss
- XSStrike: https://github.com/s0md3v/XSStrike
Finding it
I started by entering a random string in the search box to see what would happen:
https://subdomain.example.com/search?q=test
The word was reflected on the page. That is the first signal: the value of q comes back in the response, and the next question is whether it comes back in a context where markup is interpreted.
This check can be automated with Gxss, which checks a batch of URLs that contain reflecting parameters:
The result indicated that the value of the q parameter gets reflected in the page.
Exploitation
Step 1: confirm the reflection
Requesting the search URL with q=test returned a page containing the string test. Gxss confirmed the same thing for the q parameter. At this point the parameter is known to reflect; what is not yet known is which characters survive and what payload fits the surrounding context.
Step 2: find the unfiltered parameter and the right payload
This step has two parts: find the unfiltered parameter, then find the payload that works for it. I usually automate the whole process with kxss and XSStrike.
kxss is used to find the unfiltered parameter.
Then XSStrike:
python3 xsstrike.py -d https://www.example.com/search?q=test
XSStrike was run against the URL to help find the payload.
Step 3: the working payload
The final payload looked like this:
https://www.exapmle.com/search?q=%3Chtml%0aonmouseOver%0a=%0a(prompt)``//
Decoded, the q value is <html followed by a newline, onmouseOver, a newline, =, a newline, (prompt)``//. The %3C is the opening angle bracket, and the %0a sequences are newlines used in place of spaces. The payload opens an element and attaches an onmouseOver handler that calls prompt; the backticks and // close and comment out whatever the application appends after the reflected value.
When I tested it in the browser it worked and a prompt box appeared. I reported it and the report was marked as a duplicate.
Root cause
The q value is reflected in the page, and the payload executed in the browser. Because the characters needed to open an element and add an event handler reach the response intact, the browser parses the reflected value as markup rather than as text. The report does not describe any server-side filtering or output encoding that was in place, and does not say which context in the page the value lands in; the payload's use of an event handler suggests the value is placed where an element can be opened.
Impact
The payload ran in the browser and showed a prompt box. The payload shown uses prompt, which only demonstrates execution; what an attacker could actually do depends on what the site's session allows, and the report does not go into that. In this case the finding was a duplicate, so someone else had reported the same issue first.
Fix
The report does not say what fix, if any, was applied. The appropriate fix is to HTML-encode the reflected q value on output so that characters such as <, >, " and ' are rendered as text, and to apply context-appropriate encoding wherever user input is written into the page.
Variants to test
- Search boxes and other reflected parameters on home pages: submit a random string and check whether it comes back verbatim, then check with kxss which characters survive.
- Parameters that reflect inside an attribute or a script block: try breaking out of the surrounding context rather than opening a new element, and watch for the characters that kxss reports as unencoded.
- Newline-separated payloads such as the one above: if spaces are filtered but
%0ais not, the same structure may still work. - Automated confirmation with Gxss on a list of URLs with known reflecting parameters, followed by XSStrike against the ones that reflect.
What happened, in plain words
A security researcher from Egypt, Ahmed Kamal, was hunting for bugs on Bugcrowd, a platform where companies publish programmes that let people test their websites and report problems. This particular programme was a vulnerability disclosure programme (VDP). The company's rules did not allow him to name the target, so he calls it example.com throughout.
The bug he found was a reflected cross-site scripting (XSS) flaw in the search box on the home page. In plain terms: a website's search box takes whatever you type and shows it back to you on the results page. If the site shows it back as raw HTML instead of as harmless text, an attacker can type something that is not a search term at all but a small piece of code, and the browser will run it. That is what happened here.
He found it about a month before writing the story. He reported it, and the report was marked as a duplicate, meaning someone else had already reported the same problem. He still shared the steps.
The setup
The search box is on the home page, so no parameter collection across the site was needed.
Because the flaw was right there in the home page search box, there was no need to go hunting across the whole site for input fields that get echoed back. That is why not all of the usual XSS tools were used. The tools he lists are:
- ParamSpider: https://github.com/devanshbatham/ParamSpider
- Gxss: https://github.com/KathanP19/Gxss
- kxss: https://github.com/Emoe/kxss
- XSStrike: https://github.com/s0md3v/XSStrike
These are free programs that automate parts of the testing. ParamSpider collects URLs that carry parameters (the ?name=value parts of a web address). Gxss checks a list of such URLs and tells you which ones echo your value back. kxss tells you which characters of your value come back unchanged. XSStrike tries many candidate payloads and reports which one gets through.
Finding it
Step 1: does the search box echo me back?
The first move was simple: type a random word into the search box and see what happens. He used the word test:
https://subdomain.example.com/search?q=test
This is a URL. The part before the ? is the page address; everything after the ? is a list of parameters sent to the server. Here there is one parameter, q, with the value test. Typing in the search box and pressing enter produces exactly this kind of URL, which is why you can also just paste it into the address bar.
The word test appeared on the resulting page. That is the first signal: the site takes what you typed and puts it back into the page it sends you. The next question is whether it comes back as harmless text or as something the browser will treat as instructions.
This check can be done by hand, but it can also be automated with Gxss, which takes a batch of URLs that contain parameters and checks which of them reflect:
Line by line: echo prints the URL to the screen; the | symbol (a pipe) takes that printed text and feeds it as input to the program on the right; Gxss reads the URL, requests it, and reports whether the value of q comes back in the response. The result indicated that the value of the q parameter gets reflected in the page.
Step 2: which characters survive, and which payload fits?
Knowing that a value is echoed is not enough. A site might echo < as <, which the browser displays as a literal less-than sign and never treats as the start of an HTML tag. So the next step has two parts: find out which characters come back unfiltered, and then find a payload that works with those characters. A payload is simply the piece of text you put in the parameter to test whether code runs.
He usually automates this with kxss and XSStrike. First kxss:
Same shape as before: print the URL, pipe it into the tool. kxss is used to find the unfiltered parameter. If angle brackets and quotes come back as themselves, the browser may interpret them as markup; if they come back as < and ", they are being neutralised.
Then XSStrike:
python3 xsstrike.py -d https://www.example.com/search?q=test
Line by line: python3 runs the Python interpreter; xsstrike.py is the tool's script; -d tells XSStrike which URL to test. XSStrike was run against that URL to help find the payload.
Step 3: the payload that worked
The final payload looked like this:
https://www.exapmle.com/search?q=%3Chtml%0aonmouseOver%0a=%0a(prompt)``//
This is the same search URL, with the q value replaced by an encoded payload. The % sequences are how special characters are written inside a URL so they survive the trip: %3C is the less-than sign <, and %0a is a newline character. Decoded, the value is <html followed by a newline, onmouseOver, a newline, =, a newline, (prompt)``//.
What each part does:
<htmlopens an HTML element. The browser sees a tag starting and treats everything after it as markup rather than text.onmouseOveris an event attribute: code that the browser runs when the mouse pointer moves over the element.=assigns the code to that event.(prompt)is the code to run.promptis a built-in browser function that shows a small dialog box asking for input; it is a harmless way to prove that code executed.- The backticks and
//close off and comment out whatever the application appends after the reflected value, so the leftover text does not break the payload. - The newlines (
%0a) stand in for spaces, which are often filtered.
When he tested it in the browser, it worked and a prompt box appeared. He reported it, and the report was marked as a duplicate.
Why it works
The q value is reflected in the page, and the payload executed in the browser. Encoding means converting characters such as <, >, " and ' into safe display forms (<, >, ", ') so the browser shows them as text. Because the characters needed to open an element and attach an event handler reach the response intact, the browser parses the reflected value as markup instead of as text. The report does not describe any server-side filtering or output encoding that was in place, and does not say exactly where in the page the value lands; the payload's use of an event handler suggests it lands where an element can be opened.
What an attacker could do
The payload ran in the browser and showed a prompt box. That matters because the browser treats code running on a site's address as belonging to that site: it can read what the site's pages can read and act as the logged-in user where the site allows it. The payload shown only uses prompt, which proves execution and nothing more; what an attacker could actually do depends on what the site's session allows, and the report does not go into that. In this case the finding was a duplicate, so someone else had reported the same issue first.
The fix
The report does not say what fix, if any, was applied. The appropriate fix is to HTML-encode the reflected q value on output so that characters such as <, >, " and ' are rendered as text, and to apply context-appropriate encoding wherever user input is written into the page.
Variants to test
- Search boxes and other reflected parameters on home pages: submit a random string and check whether it comes back verbatim, then check with kxss which characters survive.
- Parameters that reflect inside an attribute or a script block: try breaking out of the surrounding context rather than opening a new element, and watch for the characters that kxss reports as unencoded.
- Newline-separated payloads such as the one above: if spaces are filtered but
%0ais not, the same structure may still work. - Automated confirmation with Gxss on a list of URLs with known reflecting parameters, followed by XSStrike against the ones that reflect.
Glossary
- Bug bounty / VDP: a programme where a company invites people to test its systems and report flaws; a bug bounty pays for valid reports, a vulnerability disclosure programme (VDP) accepts them without payment.
- Bugcrowd: a platform that hosts such programmes for many companies.
- Reflected XSS: a flaw where input from a URL is written back into the page without encoding, so a crafted link can make the browser run attacker-supplied code.
- Payload: the piece of text placed in a parameter to test whether code executes.
- Parameter: the
name=valuepart of a URL after the?, sent to the server with the request. - Reflection: when a server copies a value from the request into the response page.
- Encoding (HTML): converting characters like
<into safe display forms such as<so the browser shows them as text. - URL encoding: writing special characters in a URL as
%sequences, e.g.%3Cfor<and%0afor a newline. - HTML element / tag: the building blocks of a web page, written with angle brackets, e.g.
<html>. - Event handler: an attribute such as
onmouseOverthat tells the browser to run code when something happens to an element. prompt: a built-in browser function that shows a dialog asking for input; used here only to prove code ran.- Pipe (
|): a shell symbol that passes the output of one command as input to the next. - Gxss: a tool that checks a list of URLs and reports which parameters reflect.
- kxss: a tool that reports which characters of a reflected value come back unencoded.
- XSStrike: a tool that tries many payloads against a URL and reports which one is not filtered.
- ParamSpider: a tool that collects URLs carrying parameters from a site.
- Duplicate: a report for a flaw that someone else already reported.