Blind Boolean Based SQLi By Manipulating URL
A resume URL path segment was parsed with a regex and the extracted number was placed into a SQL query inside quotes, so a quote character broke the query and a boolean OR condition returned another user's resume. An attacker could use blind boolean inference to extract database contents.
Published on medium.com
Hello guys, today I will talk about how I’ve found a blind boolean-based SQL-injection, by manipulating url,
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
The target is a web application that serves user resumes from a URL of the form x.com/resume/<anything>-<number>. The feature is a lookup: the backend takes the number out of the path and uses it to fetch the matching resume. There is no query string parameter involved. Instead, the backend runs a regular expression over the URL, extracts the digits, and builds an SQL query from them.
That design is the whole story here. The number in the path is the only input, and it goes straight into the query.
Setup
The researcher worked manually first and then with sqlmap. No accounts, roles or special access are described. The tools that matter are a browser or HTTP client for the manual probes and sqlmap for automation, run with options that tell it what a normal response looks like and what a false SQL statement looks like. The report does not give sqlmap or server versions.
Finding it
The starting observation is that anything before the number in the path appears not to matter. A URL like this returns the same resume regardless of the text in front of the digits:
x.com/resume/doesntmatterwhathere-76541
If the prefix is ignored and only the number is used, the natural question is whether the number is concatenated into a query without proper handling. The simplest way to test that is to break the syntax and see whether the application reacts differently.
Exploitation
Step 1: break the query with a quote
The first probe appends a single quote to the number:
x.com/resume/-76541'
The response was a 404 status code. The body contained a JavaScript redirect:
<script>window.location="/";</script>
which sent the browser to the main page, x.com. A syntax error in the query therefore produces a distinct response: 404 plus that redirect. That is the signal the rest of the work relies on.
Step 2: confirm the injection with a boolean
The next payload closes the string and adds a condition that is always true:
x.com/resume/-76541'%20OR%20'
This returned an actual person's resume. The query evaluated to true, so the injection is confirmed. The researcher notes being surprised that a numeric-looking value is quoted in the query, but the behaviour shows it is treated as a string.
Step 3: check whether arithmetic would have worked
If the value were used as a bare number, a payload that adds and subtracts one would change nothing:
payload: 76541+1-1
payload url-encoded: 76541%2B1%2D1
That should return the same content as:
x.com/resume/76541
The reasoning is that a query like
SELECT * FROM users where userID=76541
would be unaffected by +1-1. In this case, however, the value is a string in the query:
SELECT * FROM users where userID='76541'
so the arithmetic trick does not apply and the quote-based approach is the one that works.
Step 4: find the column count with ORDER BY
Manual testing continued with ORDER BY. The researcher tried increasing numbers, starting with small values such as 2, 7 and 24, all of which returned true. The working payload was:
payload: 76541' ORDER BY 38 #
payload url-encoded: 76541'%20ORDER%20BY%2038%20%23
full payload: x.com/76541'%20ORDER%20BY%2038%20%23
At 38 the query still returned true. A larger value, for example 41, returned the error response, the 404 with the JavaScript redirect shown above. That redirect body is the string used later with sqlmap's --not-string option.
Step 5: prove the column count with UNION
To confirm that the count is exactly 38, the researcher used UNION SELECT. In SQL the number of columns in the unioned select must match the columns returned by the select before it. The payload was:
full payload: x.com/76541'%20UNION%20SELECT%201,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1%20%23
There are exactly 38 1s in that list. One more or one fewer would produce the error response. With this the vulnerability was confirmed, and the report was submitted with the database tables included as proof.
Step 6: automating with sqlmap
Getting sqlmap to return data was difficult because the responses are unusual. The researcher got a 404 status code with the JavaScript redirect when the SQL statement returned false, and a different HTML response otherwise. The options used to handle this were:
--code, --string, --not-string
Only --code and --not-string were needed. --code tells sqlmap which status code to treat as normal, and --not-string tells it which exact response indicates that the SQL statement returned false. The report also mentions --random-agent in case of blocking, and cookies sometimes, though neither was needed here. Running sqlmap verbosely, for example
sqlmap ... -v 4
or -v 5, lets you see the responses and check whether requests are being blocked. A 403 usually means a WAF blocked the request, but not always, so inspecting the response is the reliable check. If a WAF is in the way, the next step is --tamper with custom bypass payloads until the filter is passed.
The researcher stresses that the manual work came first: if the injection had not been found by hand, sqlmap would not have found it.
Root cause
The backend extracts digits from the URL with a regular expression and places them into an SQL query as a quoted string, without parameterisation. Because the value is quoted, a single quote in the path breaks the query, and a crafted quote plus boolean condition changes the result. The prefix before the number is ignored, which makes the number the only input that needs to be controlled. Existing defences did not stop the injection: the report describes no filtering of the extracted value before it reaches the query.
Impact
The researcher demonstrated that the query result can be controlled through the URL, confirmed the column count, and submitted the database tables as proof. That means an attacker who can reach such a URL can read data from the database through boolean inference, one bit at a time, using the difference between a normal response and the error response. The report does not describe extracting credentials or other specific data beyond the tables used as proof.
Fix
The appropriate fix would be to stop building the query from the extracted string and use a parameterised query, so the number is bound as a value and cannot alter the SQL syntax. Validating that the extracted value is numeric before use adds a second layer, but parameterisation is the fix that addresses the cause.
Variants to test
- Path segments that end in digits, such as
/item/name-1234,/profile/user-99or/invoice/ref-2024. Append a single quote and look for a different status code, error page or redirect compared with the clean URL. - The same path with a boolean payload, for example
-1234'%20OR%20'. A positive result is the normal page being returned where the quote alone produced an error. ORDER BYwith increasing numbers to find the column count. A positive result is the normal response up to a certain number and the error response above it.UNION SELECTwith a list of1s matching the suspected column count. A positive result is the normal page returning with the union accepted, and the error response when the count is off by one.- Arithmetic payloads such as
1234+1-1on numeric-looking values. If the response matches the plain number, the value may be unquoted; if it errors, it is likely quoted and the quote-based payloads are the ones to use. - When automating, use
--codeand--not-stringto describe the normal and false responses, and-v 4or-v 5to watch for blocking. A positive result is sqlmap identifying the injection and returning data without the WAF interfering.
What happened, in plain words
A website showed people's resumes at addresses that looked like x.com/resume/anything-76541. The number at the end told the site which resume to show. The person who found the bug noticed that the text before the number did not matter at all, and that the number was being pasted straight into a database question (a query) without any protection. By adding a single quote character to the number, they could break that question, and by adding a little piece of logic they could make the database answer "yes" or "no" on demand. That is called a blind boolean-based SQL injection. They then worked out how many pieces of information the database was returning (38 columns), proved it with a second technique, and finally used a tool called sqlmap to automate the process. They submitted the report with the database's table names as proof.
This article walks through the same steps for someone who has never done security testing before. Every term is explained the first time it appears, and every request is shown exactly as it was used.
The feature being tested
The site has a page for each person's resume. The address is built like this:
x.com/resume/doesntmatterwhathere-76541
Reading that address from left to right: x.com is the site, /resume/ is the section, doesntmatterwhathere is filler text, and 76541 is a number that identifies one particular person's resume.
Normally, when a website needs a value from the visitor, it puts it after a question mark, like x.com/search?name=alice. Everything after the ? is called a query string, and each name=value pair is a parameter. This site does not do that. There is no question mark and no parameter. Instead, the server reads the whole address, runs a pattern-matching rule (a regular expression, often shortened to regex) over it to pull out the digits, and then uses those digits to ask the database for the matching resume.
A database is a program that stores information in tables, a bit like a very large spreadsheet. To get information out of it, you send it a question written in a language called SQL (Structured Query Language). A typical question looks like SELECT * FROM users where userID=76541, which means "give me every column of every row in the users table where the user ID is 76541".
A regular expression is a way of describing a pattern of characters. For example, a rule could say "find the longest run of digits in this text". The server uses such a rule to find 76541 inside the address.
The important design detail is this: the number pulled out of the address is the only thing the visitor controls, and it goes straight into the database question. There is no separate checking step in between. That is the whole story of this bug.
The tools used
The researcher worked in two phases. First they tested by hand, using a browser or a simple tool that sends HTTP requests (the messages a browser and a server exchange). Then they used sqlmap, a free program that automates SQL injection testing: you give it a URL, and it tries many payloads and reports what it finds.
No accounts, passwords or special access are described. The report does not give the versions of sqlmap or of the server software.
Finding it
The starting observation is that anything before the number in the address appears not to matter. This address returns the same resume no matter what text sits in front of the digits:
x.com/resume/doesntmatterwhathere-76541
Line by line: x.com is the site, /resume/ is the section, doesntmatterwhathere is text the server ignores, and 76541 is the number it actually uses.
If the prefix is ignored and only the number is used, the natural next question is whether that number is glued into a database question without proper handling. The simplest way to test that is to break the syntax on purpose and see whether the site reacts differently. If the site behaves exactly the same, the number is probably being handled safely. If it errors, something is going wrong.
Step 1: break the query with a quote
In SQL, text values are wrapped in single quotes. A question like where userID='76541' treats 76541 as text, while where userID=76541 treats it as a number. If you put a single quote inside a quoted text value, the database gets confused about where the text ends, and the question fails with a syntax error.
The first probe appends a single quote to the number:
x.com/resume/-76541'
Line by line: the address is the same as before, but the filler text is now just a hyphen, and a ' (single quote) is added after the digits. The idea is that if the server builds where userID='76541' and we send 76541', the database sees where userID='76541'' and the extra quote breaks the syntax.
The response was a 404 status code. A status code is a three-digit number the server sends with every response to say how things went: 200 means "here is your page", 404 means "not found", 403 means "forbidden", and so on. The body of the response contained a JavaScript redirect:
<script>window.location="/";</script>
Line by line: <script> starts a piece of JavaScript, the language browsers run. window.location="/" tells the browser to go to the site's root address, which is x.com. </script> closes the script. So the page did not show an error message; it quietly sent the visitor back to the home page.
A syntax error in the database question therefore produces a distinct response: a 404 status code plus that redirect. That difference is the signal the rest of the work relies on. Whenever the site behaves this way, the database question failed; whenever it shows a normal resume, the question succeeded.
Step 2: confirm the injection with a boolean
The next payload closes the quoted text and adds a condition that is always true:
x.com/resume/-76541'%20OR%20'
Line by line: -76541' closes the quoted value, exactly as in the previous step. %20 is how a space is written inside a URL (URL encoding, explained below). OR is an SQL keyword meaning "or". The final ' opens a new quoted text value, which the database will treat as an empty string. The resulting question looks roughly like where userID='76541' OR '', and since an empty string counts as false, the whole condition becomes "this user ID, or false", which is still true for the matching user.
URL encoding is a way of writing characters that are not allowed in a URL, or that would be misread, using a percent sign and two hexadecimal digits. A space becomes %20, a plus sign becomes %2B, and so on. Browsers do this automatically; when testing by hand you often have to do it yourself.
This payload returned an actual person's resume. The database question evaluated to true, so the injection is confirmed: the visitor's input really does change the meaning of the SQL.
The researcher notes being surprised that a numeric-looking value is quoted in the query, but the behaviour shows it is treated as text.
Step 3: check whether arithmetic would have worked
If the value were used as a bare number, a payload that adds and then subtracts one would change nothing:
payload: 76541+1-1
payload url-encoded: 76541%2B1%2D1
Line by line: 76541+1-1 is arithmetic that equals 76541. In URL encoding, + becomes %2B and - becomes %2D, so the second line is the same payload written so a URL can carry it safely.
That should return the same content as:
x.com/resume/76541
Line by line: this is the plain address with no payload, the one that shows the normal resume.
The reasoning is that a query like
SELECT * FROM users where userID=76541
Line by line: SELECT * means "give me all columns"; FROM users means "from the users table"; where userID=76541 means "only rows whose user ID equals 76541". Because 76541+1-1 equals 76541, the database would return the same row.
In this case, however, the value is text in the query:
SELECT * FROM users where userID='76541'
Line by line: the only difference is the quotes around 76541. Now the database compares text, not numbers, so 76541+1-1 is not arithmetic at all; it is just a string of characters that does not match any user ID. That is why the arithmetic trick does not apply here, and why the quote-based approach is the one that works.
Step 4: find the column count with ORDER BY
Manual testing continued with ORDER BY. In SQL, ORDER BY sorts the results by a chosen column, and you can refer to a column by its position number: ORDER BY 1 sorts by the first column, ORDER BY 2 by the second, and so on. If you ask to sort by a column that does not exist, the database returns an error. So by trying larger and larger numbers, you can discover how many columns the query returns.
The researcher tried increasing numbers, starting with small values such as 2, 7 and 24, all of which returned true (the normal resume). The working payload was:
payload: 76541' ORDER BY 38 #
payload url-encoded: 76541'%20ORDER%20BY%2038%20%23
full payload: x.com/76541'%20ORDER%20BY%2038%20%23
Line by line: 76541' closes the quoted value. %20 is a space. ORDER BY 38 asks the database to sort by the 38th column. %23 is the URL encoding of #. In SQL, # starts a comment, so everything after it is ignored by the database. That comment is essential: it swallows the closing quote that the server adds after our input, so the query stays syntactically valid. The third line is the whole thing as a URL.
At 38 the query still returned true. A larger value, for example 41, returned the error response, the 404 with the JavaScript redirect shown above. That redirect body is the string used later with sqlmap's --not-string option.
Step 5: prove the column count with UNION
To confirm that the count is exactly 38, the researcher used UNION SELECT. UNION joins the results of two queries into one list. The rule is that both queries must return the same number of columns, otherwise the database errors. So if you guess the count correctly, the query succeeds; if you are off by one, it fails.
The payload was:
full payload: x.com/76541'%20UNION%20SELECT%201,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1%20%23
%20UNION%20SELECT%20 is UNION SELECT with spaces encoded. Then comes a list of 1s separated by commas, one for each column. %20%23 is a space followed by #, which starts the comment that ignores the rest of the server's query.
There are exactly 38 1s in that list. One more or one fewer would produce the error response. With this the vulnerability was confirmed, and the report was submitted with the database tables included as proof.
Step 6: automating with sqlmap
Getting sqlmap to return data was difficult because the responses are unusual. The researcher got a 404 status code with the JavaScript redirect when the SQL statement returned false, and a different HTML response otherwise. Normally sqlmap expects a normal page and an error page; here the "normal" case was a 404, which confuses the tool unless you tell it otherwise.
The options used to handle this were:
--code, --string, --not-string
Line by line: --code tells sqlmap which HTTP status code to treat as the normal, successful response. --string tells it a piece of text that appears only in a true response. --not-string tells it a piece of text that appears only in a false response. Only --code and --not-string were needed here: --code described the 404 as normal, and --not-string was given the JavaScript redirect, so sqlmap knew that seeing that redirect meant the SQL condition was false.
The report also mentions --random-agent in case of blocking, and cookies sometimes, though neither was needed here. A cookie is a small piece of data a site stores in your browser and sends back with every request, often used to keep you logged in. A user agent is a line in each request saying which browser you are; --random-agent makes sqlmap pretend to be different browsers each time, which can help avoid being spotted.
Running sqlmap verbosely, for example
sqlmap ... -v 4
Line by line: sqlmap runs the tool, ... stands for the rest of the options, and -v 4 sets the verbosity level, meaning how much detail it prints. The report says -v 5 also works but the researcher does not remember which one they used. Higher verbosity lets you see the actual responses and check whether requests are being blocked.
A 403 status code usually means a WAF blocked the request. A WAF, or web application firewall, is a filter that sits in front of a site and rejects requests that look malicious. But 403 is not always a WAF, so inspecting the response is the reliable check. If a WAF is in the way, the next step is --tamper with custom bypass payloads until the filter is passed. A tamper script rewrites each payload in a slightly different way to slip past filters.
The researcher stresses that the manual work came first: if the injection had not been found by hand, sqlmap would not have found it.
Root cause
The backend extracts digits from the URL with a regular expression and places them into an SQL query as a quoted text value, without parameterisation. Parameterisation means sending the query and the value separately, so the database treats the value purely as data and never as part of the query's instructions. Because the value is quoted, a single quote in the path breaks the query, and a crafted quote plus boolean condition changes the result. The prefix before the number is ignored, which makes the number the only input that needs to be controlled. Existing defences did not stop the injection: the report describes no filtering of the extracted value before it reaches the query.
Impact
The researcher demonstrated that the query result can be controlled through the URL, confirmed the column count, and submitted the database tables as proof. That means an attacker who can reach such a URL can read data from the database through boolean inference, one bit at a time, using the difference between a normal response and the error response. Boolean inference means asking the database yes/no questions and watching which response comes back, so information is extracted one small piece at a time. The report does not describe extracting credentials or other specific data beyond the tables used as proof.
Fix
The appropriate fix would be to stop building the query from the extracted string and use a parameterised query, so the number is bound as a value and cannot alter the SQL syntax. Validating that the extracted value is numeric before use adds a second layer, but parameterisation is the fix that addresses the cause.
Variants to test
- Path segments that end in digits, such as
/item/name-1234,/profile/user-99or/invoice/ref-2024. Append a single quote and look for a different status code, error page or redirect compared with the clean URL. - The same path with a boolean payload, for example
-1234'%20OR%20'. A positive result is the normal page being returned where the quote alone produced an error. ORDER BYwith increasing numbers to find the column count. A positive result is the normal response up to a certain number and the error response above it.UNION SELECTwith a list of1s matching the suspected column count. A positive result is the normal page returning with the union accepted, and the error response when the count is off by one.- Arithmetic payloads such as
1234+1-1on numeric-looking values. If the response matches the plain number, the value may be unquoted; if it errors, it is likely quoted and the quote-based payloads are the ones to use. - When automating, use
--codeand--not-stringto describe the normal and false responses, and-v 4or-v 5to watch for blocking. A positive result is sqlmap identifying the injection and returning data without the WAF interfering.
Glossary
- API: a way for one program to ask another for data or actions, usually over the web, without a human clicking anything.
- Blind SQL injection: an SQL injection where you cannot see the database output directly, so you ask yes/no questions and infer the answers from how the page behaves.
- Boolean: a value that is either true or false.
- Column: a vertical field in a database table, such as "name" or "email". A query returns a fixed number of columns.
- Cookie: a small piece of data a site stores in your browser and sends back with each request, often used to keep you logged in.
- Database: a program that stores information in tables and answers questions written in SQL.
- GET request: the most common kind of HTTP request, used when a browser asks for a page; its values usually appear in the URL after a
?. - HTTP: the language browsers and servers use to exchange requests and responses.
- JavaScript: the programming language browsers run to make pages interactive.
- ORDER BY: an SQL clause that sorts results by a chosen column, which can be named by its position number.
- Parameter: a named value in a URL after the
?, likename=alice. - Parameterised query: a database query where values are sent separately from the query text, so they can never change the query's meaning.
- Payload: the crafted piece of input an attacker sends to try to trigger a bug.
- Query: a question sent to a database, written in SQL.
- Regular expression (regex): a pattern used to find or extract parts of text, such as a run of digits.
- Resume: a document listing a person's education and work history.
- Root cause: the underlying reason a bug exists, as opposed to its symptoms.
- SELECT: the SQL keyword that asks the database to return data.
- SQL: Structured Query Language, the language used to talk to databases.
- SQL injection (SQLi): a bug where user input is placed into an SQL query without protection, letting the user change what the query does.
- sqlmap: a free tool that automates SQL injection testing.
- Status code: a three-digit number a server sends with each response, such as 200 (OK), 403 (forbidden) or 404 (not found).
- Tamper script: a sqlmap option that rewrites payloads to slip past filters.
- UNION SELECT: an SQL construct that joins the results of two queries, which must have the same number of columns.
- URL: the address of a page or resource on the web.
- URL encoding: writing characters that are not allowed in a URL using a percent sign and two hexadecimal digits, such as
%20for a space. - User agent: a line in each HTTP request saying which browser and system you are using.
- WAF (web application firewall): a filter in front of a site that rejects requests that look malicious.