Lab (4) Session - SQL Injection in Practice
Lab (4) Session - SQL Injection in Practice
Before you get started, load the right branch from the repository. Stash your current
changes to the branch (git stash). Next, checkout the start_chapter4_sqli branch from
the Git repository (git checkout start_chapter4_sqli). Also check for the latest updates
(git pull) command.
It's a good idea to take a backup of the database before you start attacking the
BeerSafe application. Run the following command from a terminal to save the current
database into a file: mysqldump -u root BeerSafe > BeerSafe.sql
A sample payload for this attack is provided at the bottom of this section.
Extracting information
The aim of this SQL injection attack is to extract data from the database. We will abuse
the application's search feature for this. Use the pointers listed below to figure out how
to extract a list of usernames and passwords from the database.
● Inspect the database to get understand how the tables are organized
● The code that creates the SQL query is located in the BeerDAO class in the
eu.beersafe.mooc.data.access source package
● Select data from a different table using the UNION syntax, as explained here:
https://dev.mysql.com/doc/refman/5.7/en/union.html
● Ensure that the number of columns in the second SELECT matches the number
of columns from the first SELECT
A sample payload for this attack is provided at the bottom of this section.
This attack is a bit more complicated than the other ones. Let it sink in for a while, and
see if you can find the solution to the puzzle. Instead of concrete pointers, you will find a
few hints below.
● With the UNION syntax, you can provide custom data in the second query
● The query to the database can only return one record, so forget the first query
A sample payload for this attack is provided at the bottom of this section.
Sample SQL injection payloads
Executing arbitrary SQL code
The following data can be entered in the title of a note while editing it.
Extracting information
The following data can be entered in the search field of the list of beers of a note while
editing it.
duvel' UNION SELECT 0, "", "", 0, email, password, "" from Users
WHERE password like '
To exploit this, enter the payload below in the email field, and the string sqlinjection in
the password field.
In the course, we have covered a couple of defenses against SQL injection. Here in the
lab, we focus on the most important one: parameterized statements with variable
binding. However, feel free to experiment with other defenses as well.
The main idea of using parametrized statements with variable binding is that you
separate the SQL code from the data. In practice, this means that you will prepare a
statement with the code first, and bind the variables to it later.
This page explains everything you need to know about how to do this in Java:
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
A sample solution for the first SQL injection is provided at the bottom of this section.
statement.executeUpdate();
Conclusion
That concludes this lab session. As you have seen, the consequences of these simple
SQL injection payloads were already quite serious. The solution is to ensure that the
proper context information is preserved so that that data can be distinguished from
code.