SQL Injections Seminar Report
SQL Injections Seminar Report
A report on
‘SQL INJECTION’
Submitted In
Partial Fulfillment of
Bachelor of Technology Degree
In Computer Science & Engineering
Supervisor Submitted By
Mr. S. R. Dogiwal Prateek Chuahan
Reader Roll No: 10ESKCS738
Department of Computer Science & Engineering VIII Sem CS-II
1
Candidate’s Declaration
I have not submitted the matter presented in this report elsewhere for the
award of any other Degree.
……………….
(Prateek Chauhan)
B.Tech (Computer Science)
Roll No.: 10ESKCS738
Swami Keshvanand Institute of Technology, Management and Gramothan, Jaipur
Counter Signed by
…………………
(Mr. S.R. DOGIWAL)
Reader, Department of information Technology
Swami Keshvanand Institute of Technology, Management and Gramothan, Jaipur
2
ACKNOWLEDGEMENT
I take this opportunity to express my gratitude to the people who have been
instrumental in the successful completion of this dissertation.
Prateek Chauhan
B. Tech. (Computer Science & Engineering)
Roll No: 10ESKCS738
Swami Keshvanand Institute of Technology, Management & Gramothan, Jaipur
3
CONTENTS
(III) ACKNOWLEDGEMENT 3
(IV) CONTENTS 4
(VIII) ABSTRACT 7
1. SQL INJECTION 8
1.1 INTRODUCTION 8
2. WORKING 11
3. TYPES OF ATTACKS 16
4
3.1.1 First Order Attacks 16
4 METHODS OF INJECTION 18
5 CATEGORIES 20
6. DETECTION OF VULNERABILITY 25
9. CONCLUSION 32
10. REFRENCES 33
5
ABBREVIATIONS AND ACRONYMS
LIST OF TABLES
S_NO. TABLE_NO. TABLE_NAME PAGE
1. Table 1 Vulnerable 29
characters
LIST OF IMAGES
S_NO. IMAGE_NO. IMAGE_NAME PAGE
1. Fig 1 Vulnerability 10
2. Fig 2 Injecting SQL 11
3. Fig 3 Login Page 30
4. Fig 4 User’s home page 30
5. Fig 5 Injecting SQL 31
6. Fig 6 Unauthorized 31
access of data
6
ABSTRACT
This report contains information about an extremely popular database attack.
Most of today’s web applications require dynamic content and input from users
to achieve the same appeal as traditional applications within the desktop
operating system.
This is achieved by using languages such as SQL, the most common being
MySQL. The attacker can gain unauthorized access to restricted data such as
usernames/ passwords/ email addresses etc.
Using SQL injections, attackers can add new data to the database. With some
more advanced queries and tricky techniques the attacker can:
Potentially bypass the authentication
Gain complete control over the web application and potentially the web
server.
Perform an INSERT in the injected SQL.
Modify data currently in the database.
Perform an UPDATE in the injected SQL.
Other can gain access to other user’s system capabilities by obtaining
their password.
Not only is it a threat easily instigated, it is also a threat that, with a little
common-sense and forethought can be almost totally prevented. This report will
look at a selection of the methods available to a SQL injection attacker and how
they are best defended against.
7
SQL INJECTION
1.1 Introduction
The World Wide Web has experienced remarkable growth in recent years.
Businesses, individuals, and governments have found that web applications can
offer effective, efficient and reliable solutions to the challenges of
communicating and conducting commerce in the Twenty-first century.
· Shell injection.
· File inclusion.
· XML injection.
· SQL Injection
· XPath injection.
· LDAP injection.
· SMTP injection.
8
1.2 What is SQL Injection?
SQL Injection is a technique to hack the database. It is a type of security exploit
in which the attacker adds a SQL code to a Web form input box to gain access
to resources or make changes to data.
Most of the time existing queries are edited to achieve the same results.
Transact SQL is easily changed by the placement of a single character in a
chosen spot causing the query to behave in malicious ways. The most
commonly used characters are back tick (`), double dash (--) and the semicolon
all of which have specified meaning in SQL.
A user of our form enters a piece of SQL code into it, and wraps it in special
characters in such a way that the data entered doesn't get used for the purpose
we had intended, instead it gets used to corrupt or destroy our database.
When attacker enters the data into the form, that data is directly used to build a
dynamic SQL query to retrieve the data from database. Such malicious code
injection is called as an SQL Injection attack.
The attacker is then free to extract, modify, add or delete content from the
database. In some circumstances, the attacker may even penetrate past the
database server and into the underlying operating system..
9
1.3 Who’s vulnerable?
A web application is vulnerable to SQL injection for only one reason – end user
string input is not properly validated and is passed to a dynamic SQL statement.
The string input is usually passed directly to the SQL statement. However, the
user input may be stored in the database and later passed to a dynamic SQL
statement. Because of the stateless nature of many web applications, it is
common to write data to the database between web pages. This indirect type of
attack is much more complex and requires in depth knowledge of the
application.
VULNERABILITY
What’s Vulnerable? What’s not Vulnerable?
• MySQL
• SQL Server
• PostgreSQL
•
•
HBase
IBM DB2
• ORACLE
• Apache Derby
• RBase
• ANY DBMS using SQL
Figure 1. VULNERABILITY
10
WORKING
2.1 Working of SQL Injection
The principles behind a SQL injection are simple and these types of attacks are
easy to execute and master. To exploit a SQL injection flaw, the attacker must
find a parameter that the web application passes through to a database. By
carefully embedding malicious SQL commands into the content of the
parameter, the attacker can trick the web application into forwarding a
malicious query to the database.
For example, consider the login form which accepts the username and password
from the user.
The values supplied in the field “Username” and “Password” are directly used
to build the SQL Query like:
Now, Suppose the user supplied the Username =”Admin” and Password =
‘Magic’
11
This will work with no problem. But suppose the user supplied some poorly
devised string of code then that will allow the attacker to by-pass the
authentication and access the information from the database. i.e., if user
supplied username=’ OR 1=1— then the query will be passed as:
It Works as follows:
‘: Closes the user input field.
OR: Continues the SQL query so that the process should equal to what comes
before OR what comes after.
1=1: A statement which is always true.
--: Comments outs the rest of the lines so that it won’t be processed.
The data we're filling is used by the WHERE clause. And because the
application is not really thinking about the query - merely constructing a string -
our use of OR has turned a single-component WHERE clause into a two-
component one and the 1=1 clause are guaranteed to be true no matter what the
first clause is. The query means that:
“Select everything from the table customers if the name equals “nothing” Or
1=1. Ignore anything that follows on this line.
Seeing as 1 will always equal 1, the server has received a true statement and is
fooled into allowing an attacker more access than they should have. The code
that refers to the password input field is never run by the server and therefore
does not apply.
When the user clicks on the submit button to start the login process, the SQL
query causes ASP to spit the following error to the browser:
12
“Microsoft OLE DB Provider for SQL Server (0x80040E14)
Column 'users.userName' is invalid in the select list because it is not
contained in an aggregate function and there is no GROUP BY clause.
/login.asp, line 16”
This error message now tells the unauthorized users the name of one field from
the database that application is trying to validate the login credentials against:
users.username. Using the name of this field, attacker can now use SQL Server's
LIKE clause to login with the following credentials:
SELECT userName
FROM users
WHERE userName='' OR users.userName LIKE 'a%' --' and userPass=''
The query grabs the userName field of the first row whose userName field starts
with ‘a’.
To create a new user record, the attacker must have the information about the
table name and column names it that table. For that the user might use the
following technique. First the user supplies an input at username field like:
So the attacker now knows that the query is referencing only the 'users' table,
and is using the columns 'id, username, password, privates', in that order.
It would be useful if he could determine the types of each column. This can be
achieved using a 'type conversion' error message, like this:
This takes advantage of the fact that SQL server attempts to apply the 'sum'
clause before determining whether the number of fields in the two row sets is
equal. Attempting to calculate the 'sum' of a textual field results in this message:
We can use this technique to approximately determine the type of any column
of any table in the database.
This allows the attacker to create a well - formed 'insert' query, like this:
Username: '; insert into users values( 666, 'attacker', 'foobar', 0xffff )--
Several databases use semicolon (;) to delimit a query. The use of a semi-colon
allows multiple queries to be submitted as one batch and executed sequentially.
The attacker might use this to inject the database. For example,
Then the query would execute in two parts. Firstly, it would select the
userName field for all rows in the users table. Secondly, it would delete the
users table, so that when we went to login next time, we would see the
following error:
15
TYPES OF ATTACKS
3.1 Types of SQL Injection attacks
The SQL Injection attacks are basically divided into 2 types:
First order attacks are those attacks when the attacker receives the desired result
immediately, either by direct response from the application they are interacting
with or through some other response mechanism, such as e-mail.
For example, suppose a form ask the email id of the user. If the user provided
the correct email id with no extra code then the query will run properly. But
suppose if the user enter a “LIKE” clause with the email id then the database
will return the matching criteria to the user immediately.
Here, the database will return information of any user where the name starts
with “Bob”.
As, the attacker is getting the result immediately, this type of attacks are called
first order attacks.
A Second order attack can be classified as the process in which the malicious
code is injected into a web-based application and not immediately executed, but
is stored by application (e.g. temporarily cached, logged, stored in database) and
then later retrieved, rendered or executed by the victim.
This type of attacks often occurs because once data is in the database; it is often
thought of as being clean and is not checked again. However, the data is
frequently used in queries where it can still cause harm.
16
Consider an application that permits the users to set up some favourite search
criteria.
When the user defines the search parameters, the application escapes out all the
apostrophes so that a first-order attack cannot occur when the data for the
favourite is inserted into the database. However, when the user comes to
perform the search, the data is taken from the database and used to form a
second query which then performs the actual search. It is this second query
which is the victim of the attack.
For example, if the user types the following as the search criteria:
The application takes this input and escapes out apostrophe so that the final
SQL statement might look like this:
which is entered into the database without problems. However, when the user
selects their favourite search, the data is retrieved to the application, which
forms a new SQL command and executes that.
The second query to the database, when fully expanded, now looks like this:
It will return no results for the expected query, but the company has just lost all
of their orders. These types of attacks are known as second order attacks.
17
METHODS OF INJECTION
In this type of attacks, when an attacker tries to execute SQL Query, sometimes
the server returns an error page to the user which describes the type and cause
of the error in detail.
Thus, the attacker can try to match his query with the developers query by using
the information contained in the error messages returned in response by the
database server.
By appending a union select statement to the parameter, the attacker can test to
see if he can gain access to the database: For example,
http://example/article.asp?ID=2+union+all+select+name+from+sysobjects
The SQL server then may return the database error similar to this:
This tells the attacker that he must now guess the correct number of columns for
his SQL statement to work.
Blind SQL injection is identical to normal SQL Injection except that when an
attacker attempts to exploit an application, rather than getting a useful error
message, they get a generic page specified by the developer instead. This makes
exploiting a potential SQL Injection attack more difficult but not impossible. An
18
attacker can still steal data by asking a series of True and False questions
through SQL statements.
http://example/article.asp?ID=2+and+1=1
http://example/article.asp?ID=2
http://example/article.asp?ID=2+and+1=0
would then cause the web site to return a friendly error or no page at all. This is
because the SQL statement "and 1=0" is always false.
Once the attacker discovers that a site is susceptible to Blind SQL Injection, he
can exploit this vulnerability more easily, in some cases, than by using normal
SQL Injection.
19
CATEGORIES
5.1 Categories of SQL Injection Attacks
There are four main categories of SQL Injection attacks against databases. They
are:
1. SQL Manipulation
2. Code Injection
3. Function Call Injection
4. Buffer Overflows
Based on operator precedence, the WHERE clause is true for every row and the
attacker has gained access to the application.
The set operator UNION is frequently used in SQL injection attacks. The goal is
to manipulate a SQL statement into returning rows from another table. A web
form may execute the following query to return a list of available products:
20
SELECT product_name
FROM all_products
WHERE product_name like '%Chairs%'
The list returned to the web form will include all the selected products, but also
all the database users in the application.
In PL/SQL and Java, Oracle does not support multiple SQL statements per
database request.
Thus, the following common injection attack will not work against an Oracle
database via a PL/SQL or Java application. This statement will result in an
error:
SELECT *
FROM users
WHERE username = 'bob'
AND Password = 'mypassword';
DELETE FROM users
WHERE username = 'admin';
However, some programming languages or APIs may allow for multiple SQL
statements to be executed.
21
PL/SQL and Java applications can dynamically execute anonymous PL/SQL
blocks, which are vulnerable to code injection. The following is an example of a
PL/SQL block executed in a web application –
The above example PL/SQL block executes an application stored procedure that
encrypts and saves the user’s password. An attacker will attempt to manipulate
the PL/SQL block to execute as –
The following example demonstrates even the most simple of SQL statements
can be vulnerable. Application developers will sometimes use database
functions instead of native code (e.g., Java) to perform common tasks. There is
no direct equivalent of the TRANSLATE database function in Java, so the
programmer decided to use a SQL statement.
This SQL statement is not vulnerable to other types of injection attacks, but is
easily manipulated through a function injection attack. The attacker attempts to
manipulate the SQL statement to execute as:
The changed SQL statement will request a page from a web server. The attacker
could manipulate the string and URL to include other functions in order to
retrieve useful information from the database server and send it to the web
server in the URL. Since the Oracle database server is most likely behind a
firewall, it could also be used to attack other servers on the internal network.
In addition, most application and web servers do not gracefully handle the loss
of a database connection due to a buffer overflow. Usually, the web process will
hang until the connection to the client is terminated, thus making this a very
effective denial of service attack.
24
DETECTION OF VULNERABILITY
When trying to test a site for SQL injection vulnerabilities, look for these files
specifically. And if you don’t see any URLs in the status bar, then just click on
links, and watch the address bar until you find a URL that has parameters.
Step 3: Once a URL with parameters has been found, click the link, and go to
that page. In the address bar, you should now see the URL that was seen in the
status bar, one having parameter values.
Step 4: Here is where the actual testing for hacker protection takes place. There
are 2 methods for testing script for SQL injection. Be sure to test each
parameter value one at a time with both methods.
Method 1: Go to the address bar, click your cursor, and highlight a parameter
value (Ex. Highlight the word value in “name=value”), and replace it with a
single quote (‘). It should now look like “name=’ ”.
Method 2: Go to the address bar, click your cursor, and put a single quote (‘) in
the middle of the value. It should now look like “name=val’ue”
Step 5: Click the ‘GO’ button to send your request to the Web Server.
Step 6: Analyse the response from the Web server for any error messages. Most
database error messages will look similar like:
25
Example Error 1:
Example Error 2:
Step 7: Sometimes the error message is not obvious and is hidden in the source
of the page. To look for it, you must view the HTML source of the page and
search for the error. To do this in Internet Explorer, click the ‘View’ menu, and
select the ‘Source’option. This will cause Notepad to open with the HTML
source of the page. In Notepad, click the ‘Edit’ menu, and select ‘Find’. A
dialog box will appear that will ask you to 'Find What’. Type the phrase
‘Microsoft OLEDB’ (ODBC), in the text box and then click ‘Find Next’.
26
PREVENTING SQL INJECTION ATTACKS
SQL Injection attacks normally occur due to unfiltered user input or some over-
privileged database logins. Some of the common mistakes which make your
application susceptible to SQL Injection attacks are:
SQL Injection attacks can be easily defeated with simple programming changes,
however, developers must be disciplined enough to apply the following
methods to every web accessible procedure and function. Every dynamic SQL
statement must be protected. A single unprotected SQL statement can result in
comprising of the application, data or database server.
Following are some techniques that can be used to prevent SQL Injection
attacks:
The most powerful protection against SQL injection attacks is the use of bind
variables. Using bind variables will also improve application performance.
Application coding standards should require the use of bind variables in all SQL
statements. No SQL statement should be created by concatenating together
strings and passed parameters. Bind variables should be used for every SQL
statement regardless of when or where the SQL statement is executed. This is
Oracle’s internal coding standard and should also be your organization’s
standard. A very complex SQL injection attack could possibly exploit an
application by storing an attack string in the database, which would be later
executed by a dynamic SQL statement.
27
Many web applications use hidden fields and other techniques, which also must
be validated. If a bind variable is not being used, special database characters
must be removed or escaped.
For Oracle databases, the only character at issue is a single quote. The simplest
method is to escape all single quotes – Oracle interprets consecutive single
quotes as a literal single quote.
The use of bind variables and escaping of single quotes should not be done for
the same string. A bind variable will store the exact input string in the database
and escaping any single quotes will result in double quotes being stored in the
database.
Try to limit the open-ended input wherever possible, by using select boxes
instead of Text boxes. Application must apply client side validation for all
inputs. For validation only the option in the select box should be taken up and
any other option should be rejected.
Verify the data type using ISNUMERIC or equivalent function before passing it
into a SQL statement. For string data replace single quotes with two single
quotes using the replace function or equivalent.
28
7.6 Use Stored Procedures
Use stored procedures avoid direct access to the table. Stored procedures that
are not being used may be deleted such as: master_xp_cmdshell, xp_sendmail,
xp_startmail, sp_makewebtask.
1. Never build dynamic SQL statement directly from the user input and never
concatenate user input, with SQL statements, which is not validated.
2. Filter out characters like slash, backslash, extended characters like NULL,
carry return, new line in all strings from user input and parameters from
URL.
3. Privileges of the user account used in the application for executing SQL
statements on the database must be defined.
4. Length of the user input should be limited.
5. Whenever possible reject input that contains following potentially
dangerous characters.
29
SCREENSHOTS OF SAMPLE APPLICATION
30
Figure 5. Injecting SQL
31
CONCLUSION
Remember always patch and update holes because exploits are found commonly
and the attacker is not going to wait.
32
REFRENCES
https://www.owasp.org/index.php/Preventing_SQL_Injection_in_Java
http://www.hdm-stuttgart.de/~ms096/SQLInjectionWhitePaper.pdf
http://www.nextgenss.com/papers/advanced_sql_injection.pdf
http://www.appsecinc.com/presentations/Manipulating_SQL_Server_Using_
SQL_Injection.pdf
http://www.w3schools.com/sql/sql_injection.asp
Microsoft. "SQL Injection". Retrieved 2013-08-04. "SQL injection is an
attack in which malicious code is inserted into strings that are later passed to
an instance of SQL Server for parsing and execution. Any procedure that
constructs SQL statements should be reviewed for injection vulnerabilities
because SQL Server will execute all syntactically valid queries that it
receives. Even parameterized data can be manipulated by a skilled and
determined attacker.
We Are Anonymous: Inside the Hacker World of LulzSec". Little, Brown
and Company.
33