Backend Database Hacking
Backend Database Hacking
Table of Contents
Backend Database Hacking ............................................................................................. 3
Overview......................................................................................................................................... 3
You should know.......................................................................................................................... 3
You will learn ................................................................................................................................ 3
Syllabus ........................................................................................................................................... 4
Module 1: Understanding Database Core Concepts .................................................. 4
Module 2 SQL Statements with Injection Techniques .......................................... 4
Module 3 Walkthrough on Hacking Databases ....................................................... 4
Module 4 What you should know to Advance your Database Hacking Skills
....................................................................................................................................................... 5
Who should take this course? .................................................................................... 5
Key Audience............................................................................................................................ 5
What Students should bring............................................................................................... 5
Instructor .......................................................................................................................... 6
Module 1 Understanding Database Core Concepts (DCC) .................................... 8
Tutorial 1 - Hello World! Lets UDCC .............................................................................. 8
What are Databases? ............................................................................................................. 9
Database Servers .................................................................................................................... 9
Database Language ................................................................................................................ 9
Accessing Database Servers .............................................................................................10
Example 1: ...............................................................................................................................10
Types of Database Servers ................................................................................................10
Database Architecture ........................................................................................................11
Presentation Layer (tier) ...................................................................................................12
Application Layer (tier) .....................................................................................................12
Database Layer (tier) ..........................................................................................................12
Module 2 SQL Statements with Injection Techniques ................................. 13
Tutorial 1 Introduction to SQL Statements.............................................................13
What is SQL Statement? .....................................................................................................13
Common Types of SQL Statements................................................................................14
Syntax of SQL Statements..................................................................................................15
Exercise 1 Executing SQL Statements................................................................ 15
Tutorial 2 SQL Injections ....................................................................................... 18
SQL Injection ..........................................................................................................................19
Detecting SQL Injections ....................................................................................................19
Key in Detecting SQL Injections ......................................................................................19
Types of SQL Injection Attacks ........................................................................................19
Authentication Bypass Attack .........................................................................................21
Exercise 02 Authentication Bypass Attack ...................................................... 22
Performing Attack ................................................................................................................23
Successful Attack ..................................................................................................................23
Explanation .............................................................................................................................24
Union Attack SQL Injection ...............................................................................................25
Syllabus
Module 1: Understanding Database Core Concepts
Network Administrators
Information Security Officers
New Graduates in IT
Newbies, who want to learn hacking
System Administrator
Internet connection
One PC, which can run 2-3 Virtual Machines
Guided Lab development will be covered in the workshop
Instructor
Raheel Ahmad is an information security professional and an experienced
instructor and penetration tester with a computer graduate degree and
holds 10 years of professional experience working for Big4 and boutique
consulting companies. He holds industry recognized certifications including
CISSP, CEH, CEI, MCP, MCT, CobIT, and CRISC.
Raheel is a founder of 26SecureLabs, a management consulting company
based in Auckland, New Zealand. 26SecureLabs provides ethical hacking
and penetration testing services as its core business.
Best way to reach info@26securelabs.com
All the study material, concepts, contents and the ethical hacking tricks or
techniques presented in this course are solely for educational purposes
and must not be used for illegal activities or any computer related crime Raheel Ahmad, CISSP
Now you need this database to be kept somewhere and you need a
service which can help in retrieving this data and can perform processing
of different types when it is required or requested by anyone. To
accomplish this task a computer program that provide these type of
services either to different other services or users is termed as a database
server. And on a broader scale you can have a complete database
management system that is termed as DBMS.
Different companies or vendors designed different database serves and
this why the way these servers works are different and differ in many
features, however, how the data is retrieved and stored is more or less
similar.
Now to talk to the database you need a language in which these database
servers speak and this language is called Structured Query Language
(SQL).
Database Language
SQL is simple to learn and this is the language which is used to query all
databases and this is the most important language for a security
researcher to learn and have enough experience with as this language is
You can access these servers by means of direct access, which we will call
backend and this is were you directly execute SQL statements to access a
database. Developers and programmers mostly use this. However, an end
user may access these servers in an unnoticed fashion when an end user
accesses any application which requires connectivity with this backend
database server and performs certain queries which are developed as part
of this application.
Example 1:
You went to an ecommerce website and created your profile first; the
forms you completed have your information and the web page on which
you complete the form has a backend connectivity with the database
server. So when you completed the form and hit the submit button all of
your information goes into that database.
Types of Database Servers
You can find many different vendors available in the industry providing
database servers. We will list the well known and most commonly used
ones in the industry:
Out of these, the most commonly used are Microsoft SQL Server, Oracle
and MYSQL. In our workshop we will focus on first and last will leave
Oracle behind.
10
Its worthwhile if we put a little light on these servers so that you can grab
basic info about them before moving on from basic topics.
We are not in this workshop to learn about databases only, but we want
to learn how to hack these database servers so we are not going to
explain how these servers work or how you can use them, however, we
will present a quick tutorial on how you can setup your home lab for
practicing the hacking part on these servers. Those of you dont have any
prior experience with database administration will definitely get the flavor
of it.
Database Architecture
Presentation
Application
Database
11
Users also know about this tier or layer as the end users sit on this layer.
End users dont know anything beyond this layer, however, they can have
different types of views or access to this tier.
Application Layer (tier)
This is the middle layer in between the first and last layer. Its main
function is to provide connectivity so that the top and last layer can talk to
each other, but the database tier, basically the application tier, acts like
an end user and the database tier doesnt worry about anything beyond
that.
Database Layer (tier)
This is where all the data lives with all the relationships to the data that is
present; it can have multiple databases running on this layer.
Cool, these are the general concepts that you should understand properly
before we move to the next module. This forms the core of database
hacking tricks or techniques otherwise you would just be using the tools
and not have background knowledge on how these database servers work
or the tools to perform the certain actions.
In the next module we will be focused on understanding the structured
query language (SQL) and then we will start learning the hacking
12
techniques and tricks to hack into databases from the next module. Dont
miss; be connected, newbies are going to turn into hackers at hakin9! See
you in the next module.
13
There are many more examples and it all depends on what action we want
to perform or what communication is required with database servers.
Common Types of SQL Statements
In this section we will study the common and mostly used SQL
Statements that will also cover the above four given examples. As we
have already said, there are many SQL Statements that are used by
programmers or database administrators, but it all depends on action
required to be performed on the database server. So lets explore those
SQL Statements that are required or mandatory to be understood by a
security professional. In fact, a security professional should have a solid
understanding and experience with SQL statements, but at least the
following statements in the graphical diagram presented below should be
well understood, and practically experienced.
14
15
Download:
http://www.microsoft.com/en-nz/download/details.aspx?id=29062
We already have MSSQL running in our lab environment so lets have a
quick walkthrough on SQL Statements. For those who dont have any
experience with databases, we will cover the setup part in the last module
so that you can also have a flavor on how to setup the databases for
practicing SQL statements.
Below is the screenshot of the MSSQL Server Express edition.
You can notice that it has different options available in the tree view; we
will explore the database part and see what is available as pre-installed
for us to play.
16
You can notice in the above snapshot that it has pre-installed databases
as shown in the left pane as well. Now lets run a couple of queries to see
what is available for us in the master databases by running a query
analyzer. Now to directly run query and including database as well you
need to follow the following syntax as shown in the snapshot. We are
running a select query where master is a database and
dbo.spt_monitor is a table name.
You can see the results in the below pane with results tab. In this query
we selected all columns from the table, as we were not aware of existing
columns.
17
18
SQL Injection
Now, how can you detect SQL Injections? There are two different ways of
detecting SQL injections, either you do it yourself manually or used a tool
which helps you in detecting SQL Injections.
Key in Detecting SQL Injections
And above error message shows us that the backend database is SQL
Server and the column name is Log.Log_ID this is the manual method
of detecting SQL Injections, however, you can use the below mentioned
tools which are well known for detecting SQL injection vulnerabilities,
however, they dont exploit SQL injection vulnerabilities. This part of the
exploitation you have to do on your own and this can only happen if you
are good enough with SQL Statements.
Types of SQL Injection Attacks
19
20
Advanced
We will now explain more about these types of SQL Injections so that you
can understand better and practice these in the lab environment.
Authentication Bypass Attack
What happens normally when you browse any web application that
requires you to provide credentials before you can be granted access is
that such applications are connected to backend databases and use SQL
statements to authenticate you as a registered user.
However, by using this attack you can simply bypass the login
requirements, which are a valid ID and Password. However, if the SQL
Statement, which is written in background to check for authentication, is
vulnerable then only this bypass technique will work
In the login or password fields, or both fields, the following SQL Injection
vectors are used to gain illegitimate access by bypassing the
authentication.
21
Now, by using these vectors you can easily bypass the authentication and
gain access to the web application restricted pages.
22
Performing Attack
We were using Firefox browser and a plugin that allows us to see what is
typed in the password field as shown below.
Successful Attack
23
24
Well we have presented the demo for bypassing the authentication with
SQL Injections, here we will explain a bit about Union Attack as
walkthroughs will be covered in the upcoming module so that we can
practice the hacking techniques and gain more focused experience.
Union attack is basically appending another select statement in the input
fields, which runs background SQL statements for any reason. Technically,
this attack is easy to execute by appending another statement with the
UNION operator but it is more difficult as it requires knowledge of
database schema. Dont worry, we will explain in detail in the next module
how to gain this information and practically inject the database with
UNION attack.
Example Attack Vector
http://www.example.com/page.asp?id=1
11,22,33,44--
and
1=2
UNION
SELECT
This is a type of SQL Injection which you simply execute by guessing the
required information for injecting the SQL statement. As you dont see any
error messages in this attack, sometimes it works well, like inserting a
new record in login database. However, the key technique is asking
25
database questions, like true or false, so that you can start gathering
information based on your known knowledge of database server. The
questions you are asking in the form of yes or no and then realising the
information and ending up gathering information without getting any error
messages.
Advanced SQL Injections
This is another type in which you put together complex or customised SQL
Statements which can make use of pre-defined functions and stored
procedures in the backend databases. These attacks are very dangerous
and can lead to disclosure of complete databases, execution of system
level commands, creating system users and much more. Damage is
limitless in such attacks. We will execute such statements in our lab
environment to gain advanced level experience with SQL Injections.
Stay tuned to the workshop, in the next module we will be demonstrating
these types of SQL Injections in our lab environment so that you can gain
hands-on experience with SQL Injections.
Knowledge is power, have as much as you can! Keep hakin9
26
As we studied in the previous module, this type of SQLi in its simple form
can be explained as appending another select query as SQLi. However, its
not that easy to do because to execute another statement as union, you
need some information prior to executing this type of SQLi attack. So first,
lets have a look at how you can detect the SQLi attack. Consider the
below link as our target link to perform a SQLi attack.
SQLi: SomePage.asp?PID=1
27
Okay, this means something can cause an error with SQLi attacks, lets
dig deeper and now send single quote value and see the outcome.
Cool, we now know that the backend database is SQL Server and this link
is vulnerable to SQLi attack, lets see if we can execute union attack on
this vulnerable link.
28
login table, users table for storing user related information, which could
be anything, and so on. Since we are doing it manually, we have to guess
and give a try again. Lets say table name Admin and see what happens.
Aha, you can see that now we have been given more information by this
vulnerable application and we can see that message is changed from
invalid object to the SQL specific message that:
All queries in an SQL statement containing a UNION operator
must have an equal number of expressions in their target lists
This means that the SQLi we are performing as a union query attack
should have equal number of expressions, like column names, which also
means that table Admin does exist in the database but the number of
columns are different than the table from which the first SQL statement
was written by the programmer to get the data from the database onto
this web page.
So now we have a valid table name with us, that is admin but we still
cannot execute the UNION attack, as we dont have the columns
information. So lets work to learn more information about the columns.
How we do this is we keep adding column numbers until we see a
different error which ends up in the following union attack query.
SQLi:
SomePage.asp?PID=-1
union
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 from Admin
select
29
Cool, this means that we were able to execute our SQLi union attack to an
extent that gives us information as shown in above snapshot. Now lets
read this information.
You can see the numbering from 1 to 9 and then an error message by
SQL Server. This shows that you can read what is there in the first 9
columns of table admin on the page you landed on after this SQLi
attack.
But, you need to know column names if you want to read information
from the table admin which you can use instead of numbers from 1 to 9.
So lets start with common guessable column names that any programmer
can use. Think like a programmer. We will start with username as the
first column name and will see what happens.
So our SQLi union attack query would look like this as given below.
SQLi: SomePage.asp?PID=-1 union select
username,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 from Admin
However, when we executed this attack, we saw the following error
message again by the SQL server as shown below in the snapshot.
select
When we executed things could change and we land on the following page
as shown below.
30
Cool, this means that we got the correct column name as we placed it in
the first place as you can see that now we can put some text in the same
location where we displayed number 1 and this text webadmin is
basically a user name and the username itself says its a web
administrator account. So we are hacking into the web application.
However, we need to know the password, so apply the same logic to find
out the password column with different column names.
So long story short, we tried different names and found that pwd is
probably the column that has passwords, however, for this user no
password was set. The SQLi attack query we sent was as given below.
SQLi: SomePage.asp?PID=-1 union select
user,pwd,3,4,5,6,7,8,9,10,11,12,13,14,15,16 from Admin
And, we got the following information as shown below in the snapshot.
As you can see, now there is blank field on location 2. So what did we
learn here?
A successful union attack and information gathering on registered users,
so we can try gathering more information from any other known tables.
31
This you have to do on your own, however, we can confirm that we found
more table names including user.
We will now cover Blind SQLi Attack in the next walkthrough and hope
that you got the grip over union attack. This union attack can be extended
further but that we can not cover in this workshop. If you want to learn
more on extended attack methods, request a separate workshop on
advanced and extended SQL Injection attacks and we will arrange it for
you.
32
This advanced level of SQLi attack we will be performing will require SQL
Server views to be used, lets see how it works. The below query was
executed successfully in a search field as shown in below snapshot on a
different page of the web application.
Advance SQLi Attack: union select 1,2,3,4 from sysobjects;--
This SQLi attack took us to search page and we go to the below screen as
shown in snapshot.
33
When we executed this SQLi the search works well and took us to the
following page as shown below in the snapshot and you can see what we
have discovered.
34
More tables.
35
Now the first column is the ID of this table and second column is the
name of the table so now we can find available columns in any table, we
will select one to demonstrate.
We selected two IDs from the above list, one of the default table and one
from user defined table as shown in below SQLi
Advance SQLi Attack: ' union select id,name,3,4 from syscolumns
where id=1125579048;-Advance SQLi Attack: ' union select id,name,3,4 from syscolumns
where id=10;-And we found the below information for each query, respectively.
Similarly, you can execute more advanced and customized queries to read
through databases and dump all the content.
36
We hope you learned something new today and enjoyed the workshop
and want to hack into databases yourself so lets help you in setting up
your home lab for practicing the gained knowledge.
We hope its beneficial for your career and thank you for attending the
workshop.
37
The answer is simple; you need tools and virtual machine software, we
have explained that in other workshops as well, but as we mentioned, we
need to cover all types of students, its not necessary that all students
have gone through other workshops, too, or they already know how to
build home virtual lab. So students who have this knowledge, dont worry,
sometimes its good to revise or you can jump this section easily.
38
You can use virtual box as your virtual machine software, however, I am
running VMware Fusion on my Mac. Lets setup VM for our server.
Add new virtual machine and select the options as shown in below figure
to add new machine.
39
40
We will select install from disc as we have our Windows Server 2008
image with us, at this stage you should already have the image for you,
smart people know how to do this. Double click the selected method and
locate your image and proceed.
41
42
43
Hit finish and you will be running Windows Server 2008 at installation
level which will finish quickly as shown below.
44
You can complete this installation without any hiccups if you have a
Windows installation key with you.
Setup MS SQL Server 2008
Now, you can either setup MS SQL Server 2008 Express edition or install
the full trial version for 180 days. Its your choice, we have explained the
installation method for the 2005 express server in previous module,
however, installing the MS SQL Server 2008 would be bit different but you
can cover this easily as shown below.
Installing MS SQL Server requires PowerShell to be installed first, so lets
do it quickly. For this open your server as shown below.
45
Under customize this server, now add features and you will see below
screen and select PowerShell as shown below.
46
47
This will finish quickly and if all goes well then you will see the following
screen.
48
Now lets install SQL Server 2008 Express. This is also a free version from
Microsoft, which is available to download from below link.
Download: http://www.microsoft.com/ennz/download/details.aspx?id=30438
Download SQL Express and SQL Management Studio as shown in below
screenshot.
49
50
51
52
53
54
55
If all goes well then you will see the following screen confirming successful
installation.
Now, install the management studio so that you can manage this server.
This should be okay as we did the SQL Server installation.
Now, we will setup IIS Server, run the manager server and add the role
as shown below to install IIS Server.
56
57
58
59
60
Okay, now time to setup the database itself in the SQL Server we have
installed. For this we will provide the script to create database and sample
tables as well, however, you need to do it on your own as shown below.
Now run the management studio as shown below.
61
Login with the credentials you set at the time of installation and you will
be able to see the following screen.
62
We will now run the script we have to create the database and sample
data as well. Follow the same process to do so, you will be provided with
the script with this workshop module.
63
Right click database and select restore files and filegroups as shown
above. Follow the steps as shown below.
64
As shown above locate the database file as we selected for Cars.bak and
continue.
65
66
Thats it and now you have setup the database for the sample website we
will be running on the IIS Server we installed in the module.
Now, go back to the IIS Server and create the virtual directory and setup
the web application as explained below. You can run IIS Manager from
Administrative tools in control panel.
67
On the default website right click and add virtual director as shown below.
68
You will be provided with the sample websites as well. Locate these two
web application folders and add them as virtual director as we have
explained in below steps.
69
70
71
Once converted go to the following link on your browser for this web
application.
Link: http://localhost/cars
You will be able to see the following web application up and running on
your Windows Server 2008 Server running IIS Web Server 7.
72
73
Target:
At least one of these options has to be provided to set the target(s)
-u URL, --url=URL Target URL (e.g.
"www.target.com/vuln.php?id=1")
-g GOOGLEDORK
Process Google dork results as target URLs
Request:
These options can be used to specify how to connect to the target URL
--data=DATA
Data string to be sent through POST
--cookie=COOKIE
HTTP Cookie header
--random-agent
Use randomly selected HTTP User-Agent header
--proxy=PROXY
Use a proxy to connect to the target URL
--tor
Use Tor anonymity network
--check-tor
Check to see if Tor is used properly
Injection:
These options can be used to specify which parameters to test for,
provide custom injection payloads and optional tampering scripts
-p TESTPARAMETER Testable parameter(s)
--dbms=DBMS
Force back-end DBMS to this value
Detection:
These options can be used to customize the detection phase
--level=LEVEL
--risk=RISK
Techniques:
These options can be used to tweak testing of specific SQL injection
techniques
--technique=TECH
"BEUSTQ")
Enumeration:
These options can be used to enumerate the back-end database
74
General:
These options can be used to set some general working parameters
--batch
Never ask for user input, use the default behaviour
--flush-session
Flush session files for current target
Miscellaneous:
--wizard
75
You can notice that the backend database detected is MySQL server, and
you can play this server and dump all the content automatically with the
following switches:
--dump
--dump-all
Moreover, you can take the shell on the victim machine too, however, and
can play with it in your virtual lab. Or if you want a more advanced
workshop which can lead to complete owning of underlying operating
system as well than do post on the forum so that we can bring that for
you separately in upcoming workshops, we can not execute this on live
web applications, however, we will develop the vulnerable lab
environment for dedicated labs with more advanced techniques.
We hope this workshop has been informative for you and we thank you for
completing this workshop.
76