In-Band SQLi
In-band SQLi sends the injection and reads the results through the same channel. This note covers the two main kinds, error-based and union-based, then walks through level one of the SQL Injection Example lab to pull Martin's password out of the database.
In-Band SQL Injection
In-band SQL injection is the easiest type to detect and exploit. "In-band" means the same channel is used to exploit the vulnerability and to receive the results. For example, an SQL injection found on a website page is used to extract data from the database onto that same page.
Error-Based SQL Injection
Error-based injection is the most useful for quickly learning the database structure, because the database's error messages are printed straight to the browser. This can often be used to enumerate a whole database.
Union-Based SQL Injection
Union-based injection uses the SQL UNION operator alongside a SELECT statement to return extra results to the page. It is the most common way to extract large amounts of data through an SQL injection vulnerability.
Practical
Start the machine with the green "Start Machine" button to get the SQL Injection Example practice lab. Each level has a mock browser plus SQL Query and Error boxes that help get the queries and payloads right.
Level one has a mock browser and a blog website with different articles. Each article is reached by changing the id number in the query string.
Confirm the Vulnerability
The key to finding error-based SQL injection is breaking the code's SQL query by trying certain characters until an error message appears. The most common ones are a single apostrophe ( ' ) or a quotation mark ( " ).
Typing an apostrophe ( ' ) after id=1 and pressing enter returns an SQL error about the syntax. Getting that error confirms an SQL injection vulnerability exists. From here, the error messages can be used to learn more about the database structure.
Find the Number of Columns
The first goal is to return data to the browser without an error message. The UNION operator adds an extra result of our choosing. Set the mock browser's id parameter to:
1 UNION SELECT 1
This produces an error saying the UNION SELECT statement has a different number of columns than the original SELECT query. Try again with another column:
1 UNION SELECT 1,2
Same error again, so add one more column:
1 UNION SELECT 1,2,3
This works: the error is gone and the article is displayed. The goal now is to show our data instead of the article. The article appears because the site's code takes the first returned result and shows it. To get around that, the first query has to return nothing, which is done by changing the article id from 1 to 0:
0 UNION SELECT 1,2,3
The article is now made up only of the UNION SELECT result, showing the column values 1, 2 and 3. These display spots can be used to pull out more useful information.
Get the Database Name
First, get the name of the database we have access to:
0 UNION SELECT 1,2,database()
Where the number 3 was shown, the page now shows the database name: sqli_one.
List the Tables
The next query gathers a list of the tables in this database:
0 UNION SELECT 1,2,group_concat(table_name) FROM information_schema.tables WHERE table_schema = 'sqli_one'
Two new things in this query:
group_concat()takes the given column (heretable_name) from multiple returned rows and joins it into one comma-separated string.- The
information_schemadatabase is accessible to every database user and holds information about all the databases and tables that user can access.
Here the query lists all tables in the sqli_one database, which are article and staff_users.
List the Columns of staff_users
The goal of level one is Martin's password, so staff_users is the table of interest. information_schema gives its structure too:
0 UNION SELECT 1,2,group_concat(column_name) FROM information_schema.columns WHERE table_name = 'staff_users'
This is close to the previous query, with three changes:
- The value retrieved changed from
table_nametocolumn_name. - The
information_schematable queried changed fromtablestocolumns. - It searches for rows where the
table_namecolumn isstaff_users.
The result shows three columns in staff_users: id, password and username.
Dump the Usernames and Passwords
The username and password columns are enough for the next query, which pulls the users' information:
0 UNION SELECT 1,2,group_concat(username,':',password SEPARATOR '<br>') FROM staff_users
group_concat again returns all the rows as one string so they are easier to read. The ,':', splits each username from its password. Instead of commas, the rows are separated with the HTML <br> tag, which puts each result on its own line.
That shows Martin's password, which is entered to move on to the next level.