Dvwa SQL Injection Lab
Dvwa SQL Injection Lab
Table of Contents
Let’s take a simple scenario where we have a web application with a login form with username and
password fields. If the developer used PHP for development, the code would look like this:
<?php
?>
If a user Karen with the password ‘12345’ wanted to log in, after clicking the Submit or the Log in
button, the query that would be sent to the database would look like this:
If an attacker knew the username and wanted to bypass the login window, they would put
something like Karen;-- in the username field. The resulting SQL query would look like this:
• Input validation: If the website allows user input, this input should be verified whether it’s
allowed or not.
• Parametrized queries: This is a technique where the SQL statements are precompiled and
all you have to do is supply the parameters for the SQL statement to be executed.
• Use Stored procedures
• Use character-escaping functions
• Avoid administrative privileges - Don't connect your application to the database using an
account with root access
• Implement a Web application firewall (WAF)
Any penetration tester who wants to get started or advance their skills in SQL injection will need a
vulnerable platform to practice. There are many vulnerable applications available both for offline
and online use.
Interestingly, when you check the URL, you will see there is an injectable parameter which is the
ID. Currently, my URL looks like this:
http://127.0.0.1/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit#
Let’s change the ID parameter of the URL to a number like 1,2,3,4 etc. That will also return the
First_name and Surname of all users as follows:
ID: 2
First name: Gordon
Surname: Brown
ID: 3
First name: Hack
Surname: Me
ID: 4
First name: Pablo
Surname: Picasso
If you were executing this command directly on the DVWA database, the query for User ID 3 would
look like this:
The percentage % sign does not equal anything and will be false. The '1'='1' query is registered as
True since 1 will always equal 1. If you were executing that on a database, the query would look
like this:
The database version will be listed under surname in the last line as shown in the image below.
Step 5: Display Database User
To display the Database user who executed the PHP code powering the database, enter the text
below in the USER ID field.
The Database user is listed next to the surname field in the last line as in the image below.
Step 6: Display Database Name
To display the database name, we will inject the SQL code below in the User ID field.
The database name is listed next to the surname field in the last line.
%' and 1=0 union select null, table_name from information_schema.tables where table_name like
'user%'#
Step 9: Display all the columns fields in the information_schema
user table
We will print all the columns present in the users’ table. This information will include column
information like User_ID, first_name, last_name, user, and password. Enter the input in the User_ID
field.