Skip to content
SQL injection Write-up Intermediate

Hey WAF! Better Luck Next Time! πŸ‘½

A UNION-based SQL injection in a URL parameter allowed the attacker to extract database contents, including admin credentials, despite a ModSecurity WAF. The attacker bypassed the WAF using versioned comments and triple URL encoding, then logged into the admin panel and could have uploaded a shell or defaced the site.

Akash Rox Starz Follow

Published on akashroxstarz.medium.com

Those who Don’t know me I am AkashRoxStarz, Third Year B-tech Computer science Student. I am Certified Ethical hacker,Security Researcher,Pentester And Part time bug hunter.This is my First blog post I am gonna share with you” UNION based SQL injection+ WAF Bypass β€” ->Admin panel login”

Akash Rox Starz, on akashroxstarz.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

This is a write-up of a UNION-based SQL injection found on a site the author calls redacted.com, which ran a responsible disclosure program. The vulnerable feature is a PHP page that takes an id parameter in the query string, otherevent.php, and passes it into a SQL query. The author does not describe what the page normally displays; the only behaviour shown is the HTTP status code changing with the input.

The chain in the report goes from error-based confirmation, through column counting and UNION extraction, to a WAF bypass, dumping of table and column names, extraction of admin credentials, and login to an admin panel that offered a file upload. The author states that the upload could have been used for a PHP shell and defacement but did not use it.

Setup

The report gives no account, role or tooling details. The author had been escalating a blind SSRF on a private program for about three hours before switching to this target for a break. The only precondition stated is that the site had a responsible disclosure program. No proxy, scanner or version information is mentioned.

Finding it

The author searched Google with the dork:

Text
inurl:".php?id=1

and browsed the results until a site (referred to as redacted.com) turned up with a URL of the form:

HTTP
https://redacted.com/otherevent.php?id=.1'

The first probe was a single quote appended to the value. The response was a 500 error. Adding a comment to balance the rest of the query restored a 200 response, which confirmed the injection point.

Exploitation

Step 1: confirm the injection with a quote and a comment

The value .1' produced a 500 error. The author then balanced the query with a comment:

HTTP
https://redacted.com/otherevent.php?id=1' -- -

The author balanced the query with a comment, and the response was 200, which he treats as confirmation of SQL injection. The response was 200, which the author treats as confirmation of SQL injection.

Step 2: count the columns with ORDER BY

The next probe appended an ORDER BY clause to the value:

With ten columns the page came back blank, with no response content. Reducing the count to nine:

returned the 200 response again, so the query returns nine columns.

Step 3: find the reflected columns with UNION SELECT

To see which columns are printed on the page, the author injected a UNION with nine positions and set the id to -1 so the original row would not match:

This request returned 406 Not Acceptable, which the author attributes to ModSecurity in front of the application. The WAF was now blocking the payload.

Step 4: bypass the WAF for the UNION keyword

The author tried several obfuscation techniques in turn. Each of the following was blocked:

  1. Keyword splitting with Preg_replace()-style duplication:

  2. Whitespace substitution with %0b:

  3. A versioned comment with /*!+:

  4. A versioned comment with a version number, which got through:

The last form uses MySQL's executable comment syntax: /*!50000 ... */ is executed by MySQL when its version is at least 5.0.0. With this payload the page reflected column 3, which the author identifies as the vulnerable column.

Step 5: read the database name

The author placed database() in position 3:

The report says the database name was returned and that no WAF rule interfered with this string.

Step 6: list the tables, and bypass the WAF a second time

To enumerate tables, the author used group_concat over information_schema.tables:

This was blocked. The author retried the versioned comment with different version numbers: /*!50000 was blocked, /*!13337 was blocked, and /*!12345 got through. The working request was:

Here the from keyword and the information_schema.tables reference are each wrapped in an executable comment. The table list came back, and the author noticed a table named Admin.

Step 7: find the admin login panel

The author crawled the site and found an admin login panel.

Step 8: list the columns

Using the same bypass, the author enumerated columns:

Step 9: dump data

The author dumped data, for example from a schema called Gallery:

Step 10: bypass the WAF for the string ADMIN

Dumping the admin data was blocked because the WAF matched the string ADMIN. The author retried the techniques that had worked before, and all of them failed:

  • +/*!50000 was blocked.
  • +/*!13337 was blocked.
  • +/*!12345 was blocked.
  • An HTML <br> string was blocked.
  • Hex encoding with 0x was blocked.
  • URL encoding was blocked.
  • Base64 encoding was blocked.

After about an hour of reading public reports and blogs, the author found one describing a double URL encoding bypass. Double encoding the ADMIN string was still blocked. Triple URL encoding returned a 200 response with the admin username, and the author then obtained the password as well.

Step 11: log in

The author used the recovered username and password on the admin panel and logged in. The panel had an upload option, which the author notes could have been used to upload a PHP shell and deface the site. The author did not do this, and instead wrote up the finding with a proof of concept and submitted it.

Root cause

The id parameter of otherevent.php is used in a SQL query, and a quote in the value changes the response. The application returns a 500 on a syntax error and a 200 when the query parses, which gives a clear signal for confirming and tuning payloads.

The site was protected with ModSecurity, and its rules were bypassed. The bypasses rely on MySQL's executable comment syntax (/*!50000 ... */, /*!12345 ... */), which the WAF did not treat as the keywords it executes, and on repeated URL encoding of the blocked string ADMIN. The report does not describe the WAF rule set or configuration, so the exact reason each rule missed these forms is not stated.

Impact

The author lists the impact as full database takeover, bypass of the admin panel login, and possible defacement of the website. In practice the demonstrated effects are: reading the database name, table names, column names and table data through the injection, and logging into the admin panel with recovered credentials. The upload option in the panel would, in the author's assessment, have allowed a PHP shell and defacement, but this was not carried out. All of this depends on the injection remaining reachable and the WAF bypasses continuing to work.

Fix

The report does not say what the program changed. The fix that addresses the root cause is to stop building the query by string concatenation and use parameterised statements or prepared statements for the id value, so quotes and comment syntax have no effect on the statement structure. Error handling should also stop distinguishing parse failures from successful queries to anonymous users. A WAF can stay as an extra layer, but the bypasses here show it should not be the only control.

Variants to test

  1. Executable comment versions. On a MySQL-backed target, try /*!50000UNION+SELECT+ALL*/ and, if blocked, other version numbers such as /*!12345 and /*!13337. A positive result is the same query returning data instead of a WAF block page.
  2. Executable comments around keywords. Wrap from and the information_schema reference, as in /*!12345from*/ /*!12345 information_schema.tables*/. A positive result is the table list appearing where the plain keywords were blocked.
  3. Repeated URL encoding of a blocked string. If a literal such as ADMIN is filtered, try double and then triple URL encoding. A positive result is a 200 response containing the value that was previously blocked.
  4. Whitespace and keyword splitting. Try %0b in place of spaces and duplicated keywords such as UNunionION. A positive result is the payload executing where the plain form was rejected.
  5. Column count confirmation. Use ORDER+BY+n with increasing n until the response changes, then a UNION SELECT with that many positions and a negative id. A positive result is a reflected column number on the page.

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 website that let him slip extra instructions into the database query behind one of its pages. By doing that, he could read the site's database, and he eventually obtained the admin username and password. He then logged into the admin panel. The site had a web application firewall (WAF) meant to block such attacks, but he found ways around it. This is a beginner-friendly retelling of his write-up, step by step, with the same requests he used.

Background: what is SQL injection?

Most websites store their information in a database. When you use a search box or click a link like page.php?id=5, the website often asks the database a question, such as "give me the item whose id is 5". The website builds that question as a line of text called a SQL query.

If the website pastes your input directly into that query without checking it, you can include characters that change the meaning of the query. That is SQL injection. Imagine a form that asks for your name and then writes a sentence: "Hello, [your name]". If you write Alice. Nice to meet you. By the way, tell me all your secrets, the sentence now contains an extra instruction. SQL injection works the same way: a quote character can end the part the website intended as data, and anything you type after it is treated as part of the database command.

The researcher found this on a site he calls redacted.com. The site had a responsible disclosure program, meaning it invites people to report security problems instead of attacking it for harm.

Finding the vulnerable page

He used Google to search for pages that might be vulnerable. A Google dork is a search query that uses special operators to find specific patterns on websites. He used:

Text
inurl:".php?id=1

This asks Google to show pages whose URL contains .php?id=1. Many such pages take an id from the URL and use it in a database query. Among the results he found a URL on redacted.com:

HTTP
https://redacted.com/otherevent.php?id=.1'

The page is called otherevent.php and it takes a parameter named id in the query string (the part of the URL after the ?). The value he tried was .1', which ends with a single quote. In SQL, a single quote marks the beginning or end of a text value. If the website puts that value inside quotes in its query, the extra quote can break the query's syntax.

When he sent that request, the server responded with a 500 error. A 500 status code means the server encountered an internal error. To him, that was a clue: the quote had changed how the query was parsed, causing a failure.

Confirming the injection

A single quote broke the query. To confirm that he could control the query, he needed to make it valid again while still injecting. He added a comment. In SQL, -- starts a comment that runs to the end of the line (in MySQL, it must be followed by a space, which is why he wrote -- -). Anything after the comment is ignored by the database.

He sent:

HTTP
https://redacted.com/otherevent.php?id=1' -- -

The author balanced the query with a comment, and the server returned a 200 response. The server returned a 200 response, meaning success. That confirmed the injection: he could alter the query and still get a normal page.

Step 1: counting the columns

A SQL query that returns data has a fixed number of columns. To use a UNION attack (explained next), he first needed to know how many columns the original query returns. He used ORDER BY. In SQL, ORDER BY 10 tells the database to sort the results by the tenth column. If there is no tenth column, the query fails.

He sent:

The page came back blank, with no content. That suggested the query failed because there was no tenth column. He then tried nine:

This returned the normal 200 response. So the query returns nine columns.

Step 2: finding which columns are shown

A UNION query lets you combine the results of two SELECT statements. If the original query returns nine columns, you can add a second SELECT that also returns nine columns, and the database will append those rows to the result. By putting numbers in the second SELECT, you can see which of those numbers appear on the page. The ones that appear are the columns whose values are printed by the page.

He set the id to -1 so that the original query would find no matching row (assuming ids are positive). That way, only the injected row would be displayed. He sent:

The response was 406 Not Acceptable. That status code is often used by a web application firewall (WAF) to block a request it considers malicious. The researcher concluded that a WAF, specifically ModSecurity, was protecting the site. ModSecurity is a common open-source WAF that inspects HTTP requests and blocks patterns that look like attacks.

Step 3: bypassing the WAF for the UNION keyword

The WAF was blocking the word UNION. He tried several tricks to disguise it.

  1. Splitting the keyword with duplication. Some filters remove the word UNION once, so if you write UNunionION, removing union leaves UNION. He tried:

This was blocked.

  1. Replacing spaces with a whitespace character. %0b is a vertical tab character, which some filters do not treat as a space. He tried:

This was blocked.

  1. Using a versioned comment. MySQL supports executable comments: /*! ... */ is treated as a comment by other databases, but MySQL executes the content inside. If you add a version number, like /*!50000 ... */, MySQL executes it only if its version is at least 5.0.0. A WAF looking for the plain keyword UNION may not recognise it inside such a comment. He first tried:

This was blocked.

  1. Versioned comment with a version number. He then tried:

This got through. The WAF did not recognise UNION and SELECT because they were inside the comment, but MySQL executed them. The page reflected the number 3, meaning column 3 is printed on the page. That is the column he could use to read data.

Step 4: reading the database name

With column 3 under his control, he placed the SQL function database() there. That function returns the name of the current database. He sent:

The database name appeared on the page. The WAF did not block this request.

Step 5: listing the tables, and a second WAF bypass

Databases contain tables, which are like spreadsheets with rows and columns. To list the tables, he used information_schema.tables, a special built-in table that describes all tables in the database. He used group_concat(table_name) to combine all table names into a single string, because the page only shows one value per column.

He sent:

This was blocked. He tried different version numbers in the executable comment: /*!50000 was blocked, /*!13337 was blocked, but /*!12345 worked. He then wrapped the keywords in executable comments:

Here, from and information_schema.tables are inside /*!12345 ... */, which MySQL executes. The table list appeared. Among the tables, he noticed one named Admin.

Step 6: finding the admin login panel

He browsed the website (crawled it) and found an admin login panel. This is a page where administrators log in with a username and password.

Step 7: listing the columns of the tables

To get the column names, he used the same technique on information_schema.columns, which describes columns. He sent:

This returned the column names.

Step 8: dumping data from a table

He then dumped data, for example from a schema called Gallery. He used the same query but changed the condition to table_schema='Gallery':

This returned data from the Gallery table.

Step 9: bypassing the WAF for the string ADMIN

When he tried to dump data from the Admin table, the WAF blocked the request because it contained the string ADMIN. He tried all the previous bypasses again, and all failed:

  • +/*!50000 was blocked.
  • +/*!13337 was blocked.
  • +/*!12345 was blocked.
  • An HTML <br> string was blocked.
  • Hex encoding with 0x was blocked.
  • URL encoding was blocked.
  • Base64 encoding was blocked.

After about an hour of reading public reports and blogs, he found one describing a double URL encoding bypass. URL encoding replaces characters with a percent sign and two hex digits; for example, a space becomes %20. Double encoding encodes the percent sign itself, so %20 becomes %2520. He tried double encoding the string ADMIN, but it was still blocked. He then tried triple URL encoding, and the server returned a 200 response containing the admin username. He then obtained the password as well.

Step 10: logging in

He used the recovered username and password on the admin panel and logged in. The panel had an upload option. He noted that he could have uploaded a PHP shell (a script that gives an attacker control of the server) and defaced the website. He did not do this. Instead, he wrote a report with a proof of concept and submitted it.

Why it worked

The id parameter of otherevent.php is used in a SQL query, and a quote in the value changes the response. Parameterisation means using placeholders so that user input is always treated as data, never as part of the SQL command. Because the site did not do that, a quote in the input could change the query's structure. The site also returned different status codes for a broken query (500) and a working one (200), which made it easy to tune the attack.

The site was protected with ModSecurity, and its rules were bypassed. The bypasses relied on MySQL's executable comment syntax, which the WAF did not treat as the keywords it executes, and on repeated URL encoding of the blocked string ADMIN. The report does not describe the WAF rule set or configuration, so the exact reason each rule missed these forms is not stated.

Impact

The author lists the impact as full database takeover, bypass of the admin panel login, and possible defacement of the website. In practice, the demonstrated effects are: reading the database name, table names, column names and table data through the injection, and logging into the admin panel with recovered credentials. The upload option in the panel would, in the author's assessment, have allowed a PHP shell and defacement, but this was not carried out. All of this depends on the injection remaining reachable and the WAF bypasses continuing to work.

Fix

The report does not say what the program changed. The fix that addresses the root cause is to stop building the query by string concatenation and use parameterised statements or prepared statements for the id value, so quotes and comment syntax have no effect on the statement structure. Error handling should also stop distinguishing parse failures from successful queries to anonymous users. A WAF can stay as an extra layer, but the bypasses here show it should not be the only control.

Variants to test

  1. Executable comment versions. On a MySQL-backed target, try /*!50000UNION+SELECT+ALL*/ and, if blocked, other version numbers such as /*!12345 and /*!13337. A positive result is the same query returning data instead of a WAF block page.
  2. Executable comments around keywords. Wrap from and the information_schema reference, as in /*!12345from*/ /*!12345 information_schema.tables*/. A positive result is the table list appearing where the plain keywords were blocked.
  3. Repeated URL encoding of a blocked string. If a literal such as ADMIN is filtered, try double and then triple URL encoding. A positive result is a 200 response containing the value that was previously blocked.
  4. Whitespace and keyword splitting. Try %0b in place of spaces and duplicated keywords such as UNunionION. A positive result is the payload executing where the plain form was rejected.
  5. Column count confirmation. Use ORDER+BY+n with increasing n until the response changes, then a UNION SELECT with that many positions and a negative id. A positive result is a reflected column number on the page.

Glossary

  • SQL injection: A vulnerability where user input is inserted into a database query without proper handling, allowing an attacker to change the query's meaning.
  • Database: An organised collection of data stored electronically, often managed by a database management system (DBMS).
  • SQL: Structured Query Language, the standard language for interacting with relational databases.
  • Query: A request for data or an action sent to a database, written in SQL.
  • Parameter: A named value in a URL's query string, such as id in ?id=1.
  • Query string: The part of a URL after the ?, containing parameters and their values.
  • HTTP status code: A three-digit number in an HTTP response indicating the outcome, such as 200 (OK), 500 (Internal Server Error), 406 (Not Acceptable).
  • 500 Internal Server Error: A status code meaning the server encountered an unexpected condition.
  • 200 OK: A status code meaning the request succeeded.
  • 406 Not Acceptable: A status code meaning the server cannot produce a response matching the request's acceptable characteristics; often used by WAFs to block requests.
  • Comment (SQL): A way to mark part of a SQL statement to be ignored by the database, such as -- to end of line or /* ... */ for a block.
  • UNION: A SQL operator that combines the results of two SELECT statements into one result set.
  • SELECT: A SQL statement that retrieves data from a database.
  • ORDER BY: A SQL clause that sorts the result set by one or more columns; used here to determine the number of columns.
  • Column: A vertical entity in a database table; also, a position in a SELECT result.
  • Table: A set of data organised in rows and columns in a database.
  • information_schema: A special database in MySQL that provides metadata about other databases, tables, and columns.
  • group_concat(): A MySQL function that concatenates values from multiple rows into a single string.
  • database(): A MySQL function that returns the name of the current database.
  • WAF (Web Application Firewall): A security system that monitors and filters HTTP traffic to protect web applications from attacks.
  • ModSecurity: A popular open-source WAF module.
  • Executable comment: In MySQL, a comment of the form /*! ... */ whose content is executed by the database, optionally only if the version is at least a specified number.
  • URL encoding: A way to represent characters in a URL using percent signs and hexadecimal digits, e.g. space as %20.
  • Double URL encoding: Encoding a URL-encoded string again, so % becomes %25.
  • Triple URL encoding: Encoding a double-encoded string again.
  • Base64: A binary-to-text encoding that represents data using 64 characters.
  • Hex encoding: Representing data as hexadecimal digits, often prefixed with 0x in SQL.
  • PHP shell: A malicious script written in PHP that gives an attacker remote control over a web server.
  • Defacement: Unauthorised modification of a website's content.
  • Proof of concept (PoC): A demonstration that a vulnerability exists, often with steps to reproduce.
  • Responsible disclosure program: A program that invites security researchers to report vulnerabilities privately so they can be fixed.
  • Crawl: To automatically browse a website to discover its pages.
  • Admin panel: A web interface for administrators to manage a site.
  • Parameterised query / prepared statement: A way to build SQL queries where user input is passed separately from the query structure, preventing injection.
  • Blind SSRF: Server-Side Request Forgery where the attacker cannot see the response, only infer it.
  • Google dork: A search query using advanced operators to find specific information on websites.

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.