Skip to content
SQL injection Write-up Intermediate

Leaked SQL Error Leading To XSS And Another BSQLi

A password reset token in the URL was used in a SQL query, allowing blind SQL injection. Separately, an emoji in a search parameter caused a database collation error that was reflected in the page, and the reflected SQL query was not HTML-escaped, allowing reflected XSS.

Zatikyan Sevada Follow

Published on medium.com

Hello everyone, it would be unpolite of me not returning the favour to medium cyber security community which gives me so much knowledge

Sevada797, 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. 5 min read.

Context

This write-up covers two separate findings on two different websites. The first is a blind SQL injection reached through a password reset flow. The second is a leaked SQL error that turned into reflected cross-site scripting. The author does not name either target, so the hostnames below are shown as they appear in the report, with the path elided.

The password reset feature worked by sending a link to the user's email address. The search feature took a keyword from a query string parameter.

Setup

No accounts, roles, versions or specific tool versions are described beyond the following:

  • A registered user account was used to reach the requests under test.
  • The password reset flow was exercised as a normal user.
  • sqlmap was used for the injection test.
  • A browser was used for the search test, including view-source (Ctrl+U).

The author notes familiarity with PHP but not with yarn, which becomes relevant when reading the leaked error.

Finding it

Password reset token in the URL

While testing requests available to any registered user, the author looked more closely at the password reset functionality. Two observations stood out:

  1. The reset link was not a one-time link.
  2. The authentication code or token was placed in the URL.

From this the author reasoned that the backend probably takes the token from the URL and runs an SQL query to check whether it is an active token before proceeding with the password reset. A token that is passed into a query is a candidate for injection.

Emoji in the search query

The second finding came from fuzzing the search input, that is, trying inputs intended to make the application break or behave strangely. The author inserted an emoji into the search query. At first, as far as the author remembers, nothing happened. Then the emoji was placed in the URL:

Text
redacted.com/…/search?filter%5Bkeyword%5D=😎

The parameter name is filter[keyword], URL-encoded as filter%5Bkeyword%5D. After this request an error page appeared. The author is not certain whether it was a dedicated error page or whether everything rendered normally, but believes it displayed a 404 page, possibly because there was no custom page for a 500 internal server error and other error types were handled by the 404 page.

Viewing the source with Ctrl+U and scrolling down revealed:

Text
General error: … mixed colation error

This is a leaked SQL error. The cause, according to the author, was that the collation was set to:

Text
utf8_general_ci

Emoji are not included in that encoding range. The author notes that utf8mb4_general_ci would not have produced the error.

The next question is why the error was visible at all. The leaked error mentioned yarn, which the author is not familiar with. The author is familiar with PHP and suggests that something like error_reporting(0); should be set, and that this should always be done in real running applications to prevent information disclosure through errors.

Exploitation

Step 1: Point sqlmap at the reset token URL

The author describes this as an easy win: all that was needed was to pass the URL to sqlmap. The author does not remember whether -p was also used to name the exact parameter. Passing the URL produced an injection found result. No command line is reproduced in the report.

Step 2: Read the leaked query in the error

The author saw the whole query reflected in the error message. Several injection techniques were tried to break the quotes, but the author stopped and did not test further, something the author now regrets. No payloads from this stage are shown.

Step 3: Turn the reflected query into XSS

The reasoning was that nobody would HTML-escape the SQL select query in their backend. A simple XSS injection was placed after the emoji, using script tags:

Text
redacted.com/…/search?filter%5Bkeyword%5D=😎<script>confirm(8)<%2fscript>

The closing tag is written with an encoded slash, %2f. The author notes that this also led to account takeover via cookies, because the cookies were not httpOnly and the JavaScript code could access them, plus CSRF against every request. The author states there was no WAF, so the payload worked as-is, and mentions a future write-up about bypassing a WAF for XSS.

Root cause

Two separate mistakes are described.

For the SQL injection, the token from the password reset URL is used in a backend SQL query. The report does not describe any parameterisation or escaping; the author's inference is that the token is placed into the query directly.

For the second finding, the search keyword is placed into an SQL query and the resulting query is reflected in an error message. The error is triggered because the column collation is utf8_general_ci, which cannot represent emoji, so the query fails with a mixed collation error. The error is displayed because error reporting is not suppressed in the running application; the author suggests PHP's error_reporting(0); as the missing control. Because the reflected query is not HTML-escaped, the injected script tags execute in the browser. The absence of a WAF is noted as a reason the payload was not blocked. The cookies lacking the httpOnly flag are what turn the XSS into account takeover.

Impact

On the first target, the password reset token parameter was injectable, giving blind SQL injection to a registered user reaching the reset flow. The report does not state what data was reachable.

On the second target, the leaked SQL error disclosed the full query. The reflected XSS executed in the victim's browser. Because cookies were not httpOnly, the author states the JavaScript could read them, leading to account takeover, and that CSRF was possible against every request. These outcomes depend on a victim opening the crafted search URL.

Fix

No fixes are described as applied. The report recommends:

  • Using utf8mb4_general_ci instead of utf8_general_ci, which would avoid the collation error for emoji.
  • Suppressing error display in running applications, for example with error_reporting(0); in PHP, to prevent information disclosure through errors.

Variants to test

  1. Password reset links: check whether the token is a one-time value and whether it appears in the URL. A positive result is a token that can be replayed and that, when passed to sqlmap, produces an injection found result.
  2. Search and filter parameters: submit characters outside the column's encoding range, such as emoji, and inspect the response and its source for SQL errors. A positive result is a database error message containing the query.
  3. Reflected queries: when a query appears in an error page, append script tags after the triggering character and check whether they execute. A positive result is the script running in the browser.
  4. Cookie flags: after confirming XSS, check whether document.cookie returns session values. A positive result is a readable session cookie, which the author links to account takeover.
  5. WAF presence: send the same payload without obfuscation. A positive result is the payload reaching the application unmodified, as the author observed here.

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. 14 min read.

What happened, in plain words

This is a story about two different websites and two different mistakes. The author found both while poking at ordinary features that any logged-in user can use: a password reset form and a search box.

On the first site, the password reset link contained a long code (a token) in its web address. The author guessed that the site's server takes that code and asks its database a question about it. If you can slip extra instructions into that question, you can make the database do things it was never meant to do. That is called SQL injection. The author pointed a well-known automated tool at the link and the tool confirmed the injection.

On the second site, the author typed an emoji into the search box. The site's database collation could not handle that character, so it produced an error. The error message was shown to the visitor, and it contained the database question the site had just run. Because that question was printed into the web page without being cleaned up, the author could put a small piece of JavaScript into the search term and have the browser run it. That is cross-site scripting, or XSS. The site's cookies were not protected in a way that hides them from JavaScript, so the script could read them, which the author says leads to taking over an account.

Neither site is named. The addresses below appear as they do in the report, with the middle of the path left out.

The two features involved

Before the details, a quick word on the two features, because the whole story depends on how they work.

Password reset. You click "forgot my password", type your email address, and the site emails you a link. Clicking the link proves you own the mailbox, and the site then lets you set a new password. The link usually contains a long random code so the site knows which reset request the click belongs to. That code is called a token. A token is like a cloakroom ticket: a short, hard-to-guess value that stands in for your identity for one specific purpose.

Search. You type a word, the site sends it to its server, and the server asks its database which records contain that word. The database is a separate program that stores and looks up data. The server talks to it in a language called SQL (Structured Query Language). A single question you send to a database is called a query.

Finding the first bug: the reset link

The author was logged in as a normal registered user and looking at the requests the site makes on a user's behalf. The idea was to be a bit more creative than usual, rather than only testing the obvious forms.

While playing with the password reset feature, two things stood out:

  1. The reset link was not a one-time link. A one-time link stops working after it has been used once, like a single-use ticket. This one could be used again.
  2. The authentication code, the token, was in the URL. The URL is the address in your browser's address bar, and everything in it is visible, gets stored in browser history and can end up in server logs.

From those two observations the author made a guess about what happens behind the scenes: the server probably takes the token out of the URL and runs an SQL query to check whether it is an active token, and only then carries out the password reset.

Why does that matter? Because if the token is dropped straight into a database query, then whatever you put in the token position becomes part of the query. It is like a form where you write your name and the clerk reads your name out loud to a colleague; if you write "my name is Bob and also please open the safe", a careless clerk might read the whole sentence as an instruction. In database terms, this is SQL injection: you supply data, but the server treats part of it as code.

Finding the second bug: the emoji

The second story is about a problem in the backend of another website. The backend is the part of a site that runs on the server, out of sight, as opposed to the frontend that your browser displays.

While testing, the author decided to put an emoji into the search query. Trying odd inputs on purpose to see whether an application breaks or behaves strangely is called fuzzing. It is a bit like tapping a machine in unusual places to hear whether something rattles.

As far as the author remembers, at first nothing happened. Then the emoji went into the URL, like this:

Text
redacted.com/…/search?filter%5Bkeyword%5D=😎

Line by line, in plain words:

  • redacted.com is the site's address, hidden here.
  • /…/ stands for the part of the path the author left out.
  • /search is the search page.
  • ? starts the query string, the part of a URL that carries extra values to the server.
  • filter%5Bkeyword%5D is the name of the parameter. %5B and %5D are URL encodings of the square brackets [ and ], so the real name is filter[keyword]. Square brackets are often used to group related settings, so this is the "keyword" setting inside a "filter" group.
  • = separates the name from the value.
  • 😎 is the value being searched for.

After this request an error page appeared. The author is not sure whether it was a dedicated error page or whether everything simply rendered normally, but thinks it showed a 404 page. A 404 is the standard "page not found" response. The author's guess is that there was no custom page for a 500 internal server error, the standard "something went wrong on the server" response, so other kinds of errors were handled by the 404 page.

Reading the leaked error

The author pressed Ctrl+U, the browser shortcut that shows the page's source code, the raw text the server sent rather than the rendered picture. After scrolling down a bit, this appeared:

Text
General error: … mixed colation error

Line by line:

  • General error: is the start of a database error message being shown to the visitor.
  • … stands for text the author left out.
  • mixed colation error is the rest of the message. "Collation" (spelled "colation" in the report) is a setting that tells a database how to compare and sort text: which letters count as equal, how case is treated, and which characters are allowed at all.

This is a leaked SQL error: the database's own complaint, shown to whoever asked. It is like a shop assistant reading out the internal stock system's error screen to a customer.

What caused it, according to the author, was that the collation was set to:

Text
utf8_general_ci

Line by line:

  • utf8 is the name of a character encoding, a system that maps characters to numbers so a computer can store them.
  • general names the particular set of comparison rules.
  • ci means case-insensitive: uppercase and lowercase letters are treated as the same.

The point is that emoji are not included in that encoding range. The older utf8 setting in this database covers only a limited set of characters, and emoji are outside it. The author notes that utf8mb4_general_ci would not have produced the error. The mb4 part means the encoding can hold four-byte characters, which is what emoji need.

Why was the error visible at all?

The next question is why the visitor could see the error. The leaked error mentioned yarn, which the author is not familiar with. Yarn is a tool for managing software packages, the building blocks a project depends on, but the report does not say more about its role here.

The author is familiar with PHP, a programming language commonly used for websites, and suggests that something like error_reporting(0); should be set. In PHP, that line turns off the display of error messages. The author's point is that this should always be done in real running applications, to prevent information disclosure through errors. Information disclosure simply means revealing internal details, such as file paths, query text or software names, to someone who should not see them.

Exploiting the first bug with sqlmap

The author calls this an easy win. All that was needed was to pass the URL to sqlmap. sqlmap is a free tool that automates the search for SQL injection: you give it a URL, and it tries many techniques and reports whether the parameter is injectable.

The author does not remember whether -p was also used. In sqlmap, -p names the exact parameter to test, which saves time when a URL has several. Passing the URL produced an "injection found" result. No command line is reproduced in the report.

What could be done with the second bug

The author saw the whole query being reflected in the error message. Reflected means the value you sent comes back to you in the response. Several injection techniques were tried to break the quotes, meaning attempts to close the string the value sits inside in the query and add new SQL, but the author stopped and did not test further, something the author now regrets. No payloads from that stage are shown.

Turning the error into XSS

One idea came to mind: XSS. Cross-site scripting is when an attacker gets their own JavaScript to run inside another person's browser session on a site. JavaScript is the programming language browsers run to make pages interactive. If a site prints your input into the page without cleaning it, your input can become code.

The author's reasoning was that nobody would HTML-escape the SQL select query in their backend. HTML-escaping means converting characters that have special meaning in a page, such as < and >, into harmless text so the browser shows them instead of treating them as markup. A select query is the SQL statement that reads data.

So a simple XSS injection was placed after the emoji, using script tags:

Text
redacted.com/…/search?filter%5Bkeyword%5D=😎<script>confirm(8)<%2fscript>

Line by line:

  • Everything up to = is the same search parameter as before.
  • 😎 is the emoji that triggers the database error.
  • <script> opens a script block. In HTML, the language of web pages, this tag tells the browser that what follows is JavaScript.
  • confirm(8) is a tiny piece of JavaScript that makes the browser show a pop-up box with the number 8 and an OK button. It is used here only as a visible proof that code ran.
  • <%2fscript> is the closing tag, but with the slash written as %2f. %2f is the URL encoding of the / character. The author wrote it this way deliberately.

The author notes that this also led to account takeover via cookies. A cookie is a small piece of data a site stores in your browser so it can recognise you on the next request; the session cookie is effectively your logged-in badge. Account takeover means someone else ends up in control of your account. The cookies here were not httpOnly. The httpOnly flag tells the browser to hide a cookie from JavaScript, so a script cannot read it. Without that flag, the author's JavaScript code could access the cookies. The author also mentions CSRF against every request. CSRF, cross-site request forgery, is when another site makes your browser send a request to a site you are logged into, using your existing session, without you intending it.

The author states there was no WAF, so the payload worked as-is. A WAF, web application firewall, is a filter placed in front of a site that inspects incoming requests and blocks ones that look malicious. The author mentions a future write-up about bypassing a WAF for XSS.

Why it worked

Two separate mistakes are described.

For the SQL injection, the token from the password reset URL is used in a backend SQL query. The report does not describe any parameterisation or escaping. Parameterisation means passing values to the database separately from the query text, so the database always treats them as data and never as instructions. The author's inference is that the token is placed into the query directly.

For the second finding, the search keyword is placed into an SQL query, and the resulting query is reflected in an error message. The error is triggered because the column collation is utf8_general_ci, which cannot represent emoji, so the query fails with a mixed collation error. The error is displayed because error reporting is not suppressed in the running application; the author suggests PHP's error_reporting(0); as the missing control. Because the reflected query is not HTML-escaped, the injected script tags execute in the browser. The absence of a WAF is noted as a reason the payload was not blocked. The cookies lacking the httpOnly flag are what turn the XSS into account takeover.

What an attacker could get

On the first target, the password reset token parameter was injectable, giving SQL injection to a user reaching the reset flow. Blind means the attacker does not see the query results directly; the answers come back indirectly, for example through differences in the response.

On the second target, the leaked SQL error disclosed the full query. The reflected XSS executed in the victim's browser. Because cookies were not httpOnly, the author states the JavaScript could read them, leading to account takeover, and that CSRF was possible against every request. These outcomes depend on a victim opening the crafted search URL.

How to fix it

No fixes are described as applied. The report recommends:

  • Using utf8mb4_general_ci instead of utf8_general_ci, which would avoid the collation error for emoji.
  • Suppressing error display in running applications, for example with error_reporting(0); in PHP, to prevent information disclosure through errors.

Things to try on your own

  1. Password reset links: check whether the token is a one-time value and whether it appears in the URL. A positive result is a token that can be replayed and that, when passed to sqlmap, produces an injection found result.
  2. Search and filter parameters: submit characters outside the column's encoding range, such as emoji, and inspect the response and its source for SQL errors. A positive result is a database error message containing the query.
  3. Reflected queries: when a query appears in an error page, append script tags after the triggering character and check whether they execute. A positive result is the script running in the browser.
  4. Cookie flags: after confirming XSS, check whether document.cookie returns session values. document.cookie is the browser's built-in way for JavaScript to read and write cookies. A positive result is a readable session cookie, which the author links to account takeover.
  5. WAF presence: send the same payload without obfuscation. A positive result is the payload reaching the application unmodified, as the author observed here.

Glossary

  • Account takeover (ATO): gaining control of someone else's account, for example by stealing their session.
  • API: a way for one piece of software to ask another for data or actions, like a waiter taking your order to the kitchen.
  • Backend: the part of a site that runs on the server, out of sight of the browser.
  • Blind SQL injection: SQL injection where you do not see query results directly and must infer them from the site's behaviour.
  • Collation: a database setting for how text is compared and sorted, including which characters are allowed.
  • Cookie: a small piece of data a site stores in your browser to recognise you later; the session cookie acts as your logged-in badge.
  • CSRF (cross-site request forgery): tricking your browser into sending a request to a site you are logged into, using your existing session.
  • CSS: the language that styles web pages; not central here, but part of the same page source you view with Ctrl+U.
  • Database: a program that stores and looks up data for a site.
  • Encoding (character encoding): a system that maps characters to numbers so computers can store them, such as utf8 and utf8mb4.
  • Error reporting: a setting that controls whether a program shows its error messages to users.
  • Fuzzing: trying unusual inputs on purpose to see whether an application breaks or behaves strangely.
  • HTML: the language web pages are written in.
  • HTML-escaping: converting characters with special meaning in HTML, such as < and >, into harmless text.
  • httpOnly: a cookie flag that hides the cookie from JavaScript.
  • Information disclosure: revealing internal details to someone who should not see them.
  • Injection: slipping data into a place where the server treats it as code.
  • JavaScript: the programming language browsers run to make pages interactive.
  • Parameter: a named value sent to a server, often in the URL after the ?.
  • Parameterisation: passing values to a database separately from the query text so they are always treated as data.
  • PHP: a programming language commonly used for websites.
  • Query: a question sent to a database, written in SQL.
  • Query string: the part of a URL after the ? that carries extra values.
  • Reflected: sent back to you in the response.
  • Session: the state of being logged in, usually tied to a cookie.
  • SQL (Structured Query Language): the language used to talk to databases.
  • SQL injection: slipping SQL instructions into a value the server puts into a query.
  • sqlmap: a free tool that automates the search for SQL injection.
  • Token: a short, hard-to-guess value that stands in for identity or permission for one purpose, like a cloakroom ticket.
  • URL: the address in your browser's address bar.
  • URL encoding: writing characters that are not allowed in a URL as % followed by two hex digits, such as %5B for [ and %2f for /.
  • utf8 / utf8mb4: character encodings; utf8mb4 can hold four-byte characters such as emoji.
  • WAF (web application firewall): a filter in front of a site that inspects requests and blocks ones that look malicious.
  • XSS (cross-site scripting): getting your own JavaScript to run inside another person's browser session on a site.
  • Yarn: a tool for managing software packages that a project depends on.

More by Zatikyan Sevada

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.