19-An Efficient Technique for SQL Injection Detection And
19-An Efficient Technique for SQL Injection Detection And
Safwat
Abstract: With the recent rapid increase of interactive web applications that employ back-end database
services, a SQL injection attack has become one of the most serious security threats. This type of attack can
compromise confidentiality and integrity of information and database. Actually, an attacker intrudes to the web
application database and consequently, access to data. For preventing this type of attack different techniques
have been proposed by researchers but they are not enough because most of implemented techniques cannot stop
all type of attacks. In this paper our proposed technique are detection of SQL injection and prevention based on
first order, second order and blind SQL injection attacks online. The proposed technique implemented in JAVA
and evaluated for seven types of SQL injection attacks. Experimental results have shown that the proposed
technique is efficient related to execution time overhead. Our technique need to be one second overhead to
execution time. Moreover, we have compared the proposed technique with the popular web application
vulnerabilities scanner techniques. The most advantages of proposed technique Its easiness to adopt by software
developer, having the same syntactic structure as current popular record set retrieval methods.
1. Introduction
Web sites are dynamic, static, and most of the time a combination of both. Web sites need
protection in their database to assure security. An SQL injection attacks interactive web
applications that provide database services. These applications take user inputs and use them to
create a SQL query at run time. In an SQL injection attack, an attacker might insert a malicious
SQL query as input to perform an unauthorized database operation. Using SQL injection
attacks, an attacker can retrieve or modify confidential and sensitive information from the
database.
The popular solutions for the prevention of SQL injection attacks include coding best
practices, input filtering, escaping user input, usage of parameterized queries, implementation
of least privilege, white list input validation [2,3,4], These solutions should be employed
usually during the development of an application. This is the major limitation of such solutions
as they do not cover the millions of Web applications already deployed with this vulnerability.
Detection of SQL injection and prevention technique is proposed based first order, second
order and blind SQL injection attacks online. SQL injection detection and prevention (SQLI-
57
IJCI. Vol. 3 – No. 1, March 2014
DP) technique is implemented in JAVA and evaluated for five types of SQL injection attacks.
Experimental results have that proposed technique is efficient related to execution time
overhead, it's need to be approximately 1second overhead to execution time. In addition, it is
easily adopted by software developer, having the same syntactic structure as current popular
record set retrieval methods.
The rest of this paper is organized as follows; section 2 present the problem formulation
.section 3 presents the background about SQL injection attacks and the concept of parse tree
and presents related work. Proposed SQLI-DP technique and experimental results is presented
in section 4. Section 5 conclusion and future work is presented. Section 6 provides the
References.
2. Problem Formulation
SQL injection is a technique maliciously used to obtain unrestricted access to databases
by inserting maliciously crafted strings to SQL queries via a web application. It allows an
attacker to spoof his identity, expose and tamper with existing data in databases, and control
databases server with the privileges of its administrator. There is a variable SQL injection
scanner but it prolongs the connection time if it wants detect or prevent or both. The popular
solutions for the prevention of SQL injection attacks can't solve the problem of legacy
system and the software developer not easy to adapt. In this paper we try to improve the
execution time and try to found way to improve the legacy system to able to prevent
injection attacks. Try to make our technique easy to adapt by software developer and make
it more efficient in the future.
There are different methods of attacks which depend on the goal of attacker are performed
together or sequentially [5, 6, 7]. For a successful SQLIA the attacker should append a
syntactically correct command to the original SQL query. Show the types of SQLIAs attacks
with examples as the following.
58
E. Mohamed. Safwat
59
IJCI. Vol. 3 – No. 1, March 2014
programmer writes code to query the database, she has a formulation of the structure of the
query. The programmer-supplied portion is the hard-coded portion of the parse tree, and the
user-supplied portion is represented as empty leaf nodes in the parse tree. These nodes
represent empty literals. What she intends is for the user to assign values to these leaf nodes. A
leaf node can only represent one node in the resulting query, it must be the value of a literal,
and it must be in the position where the holder was located. By restricting our validation to
user-supplied portions of the parse tree, we do not hinder the programmer from expressing her
intended query. An example of her intended query is given in Figure 1. This parse tree
corresponds to the example we presented in Section 1, SELECT * FROM users WHERE
username=? AND password=?. The question marks are place holders for the leaf nodes she
requires the user to provide.3 While many programs tend to be several hundred or thousand
lines of code, SQL statements are often quite small. This affords the opportunity to parse a
query without adding significant overhead.
Suppose that [8] a database contains name and password fields in the users table, and a web
application contains the following code to authenticate a user‟s log in.
The WHERE clause of this query is always evaluated to be true, and thus an attacker can
bypass the authentication, regardless of the data inputted in the password field.Web
applications commonly use SQL queries with client-supplied input in the WHERE clause to
retrieve data from a database. By adding additional conditions to the SQL statement and
evaluating the web application‟s output, you can determine whether or not the application is
vulnerable to SQL injection. For instance, many companies allow Internet access to archives of
their press releases. A URL for accessing the company‟s fifth press release might look like this:
http://www.thecompany.com/pressRelease.jsp?pressReleaseID=5
The SQL statement the web application would use to retrieve the press release might look like
this (client-supplied input is underlined):
60
E. Mohamed. Safwat
The database server responds by returning the data for the fifth press release. The web
application will then format the press release data into an HTML page and send the response to
the client. To determine if the application is vulnerable to SQL injection, try injecting an extra
true condition into the WHERE clause. For example, if you request this URL . . .
61
IJCI. Vol. 3 – No. 1, March 2014
Figure.1. SQL_Statement_Safe
Case 1: Let,
Arg Normal = {α, β} = {admin, admin pwd}
Now,
SQL Statement Normal = SQL (Arg Normal)
= SELECT ∗ FROM User_ Table WHERE
user name = „ admin‟ AND
password = „admin_ pwd‟
The SQL Statement Normal can be parsed as shown in figure 2. On comparing the semantic
structure of SQL statement safe and SQL statement normal, we can see that both of them have
similar semantics. Both statements are extracting all values from a table after checking for two
logic equalities combined with an AND operator. This implies that there is no possible SQL
injection and hence we can safely execute the query.
Figure.2. SQL_Statement_Normal
Case 2: Let,
Arg Injection = {α, β} = {admin_ OR _1_ =_ 1, hacker pwd}.
Now,
62
E. Mohamed. Safwat
= SELECT ∗ FROM Use_ Table WHERE user_ name = admin OR „1‟ =‟1‟
AND password = „hacker_ pwd‟
In this case, on comparison of SQL Statement Injection which is represented in figure 3 with
SQL Statement Safe, we can see that the semantic structures of The two statements are not
similar. This is because SQL Statement Injection is doing the additional action of ”OR ‟1‟ = ‟1‟
”. This implies that on application of the input, the semantics of the output has been modified.
This detects a possible SQL injection and the execution of the query should be stopped [10]. So
to prevent and detect the SQL injection we use parse tree.
Figure.3. SQL_Statement_Injection
The techniques related to SQL injection are classified and evaluated by [9]. In this section,
we list the work related to ours and discuss their pros and cons. We can classify the related
work to two main parts:
63
IJCI. Vol. 3 – No. 1, March 2014
From this reason, vulnerability scanners are widely used for detecting vulnerabilities in web
applications.
The dynamic analysis scanners are based on penetration test, which evaluates the security of
web applications by simulating an attack from a malicious user. The attack is typically
generated by embedding an attack code into an innocent HTTP request. After sending the
attack to the target web application, the vulnerability scanner captures the web application
output to analyze the existence of vulnerabilities. Existing vulnerability scanners [10, 11, 12,
13, 14] employ dynamic analysis techniques for detecting vulnerabilities. AMNESIA combines
static analysis and runtime monitoring [15]. In static phase, it builds models of the different
types of queries which an application can legally generate at each point of access to the
database. Machine Learning Approach Valeur [16] proposed the use of an intrusion detection
system (IDS) based on a machine learning technique. IDS is trained using a set of typical
application queries, builds models of the typical queries, and then monitors the application at
runtime to identify the queries that do not match the model. The overall IDS quality depends on
the quality of the training set; a poor training set would result in a large number of false
positives and negatives. WAVES [17] is also based on a machine learning technique. WAVES
is a web crawler that identifies vulnerable spots, and then builds attacks that target those spots
based on a list of patterns and attack techniques. WAVES monitors the response from the
application and uses a machine learning technique to improve the attack methodology.
WAVES is better than traditional penetration testing, because it improves the attack
methodology, but it cannot thoroughly check all the vulnerable spots like the traditional
penetration testing. Instruction-Set Randomization SQLrand [18] provides a framework that
allows developers to create SQL queries using randomized keywords instead of the normal
SQL keywords. A proxy between the web application and the database intercepts SQL queries
and de-randomizes the keywords. The SQL keywords injected by an attacker would not have
been constructed by the randomized keywords, and thus the injected commands would result in
a syntactically incorrect query. Since SQLrand uses a secret key to modify keywords, its
security relies on attackers not being able to discover this key. SQLrand requires the
application developer to rewrite code. SANIA [19] for detecting SQL injection vulnerabilities
in web applications during the development and debugging phases. Sania intercepts the SQL
queries between a web application and a database, and automatically generates elaborate
attacks according to the syntax and semantics of the potentially vulnerable spots in the SQL
queries. In addition, Sania compares the parse trees of the intended SQL query and those
resulting after an attack to assess the safety of these spots. Paros is used for web application
security assessment. Paros is written in Java, and people generally used this tool to
evaluate the security of their web sites and the applications that they provide on web site.
It is free of charge, and using Paros‟s you can exploit and modified all HTTP and HTTPS data
among client and server along with form fields and cookies. In brief the functionality of
scanner is as below. According to web site hierarchy server get scan, it checks for server
miscount figuration. They add this feature because some URL paths can‟t be recognized
and found by the crawler.
64
E. Mohamed. Safwat
65
IJCI. Vol. 3 – No. 1, March 2014
4. Problem Solution
4.1 Proposed technique for SQL Injection Detection and Prevention (SQLI-DP)
In this section, we present SQLI-DP technique, which tests for SQL injection vulnerabilities
and prevent SQL injection by the parse of the queries. The general view of SQLI-DP is shown
in figure 4 the discrete line if the software developer doesn't active SQLI-DP technique.
Web application SQL Query
Http request Server
Clien SQL Query
t Data
& Http request Safety Request
base
SQLI-
DP
Our technique has two options (continue with safety, unsafe option) if we select the safety
option activate SQLI-DP. If no (unsafe option) send request immediately to database and
execute query. Illustrates the core work of SQLI-DP technique where the three points.
66
E. Mohamed. Safwat
19. ELSE
20. {
21. SEND Q TO DATABASE
22. }
-------------------------------------------------------------
4.1.3 The explanation steps of SQLI-DP technique is presents in details as follows
STEP 1:
Read the HTTP request from server and check if there is blind SQL injection or not as
illustrated in [26] some example about blind SQL injection attack we us to find this method to
detect blind SQLIA. There are several uses for the Blind SQL Injection:
There are more examples to traditional blind SQL injection and advanced blind SQL
injection [26]. We noticed that from last example there are traditional kind of blind SQL
injection and advanced kind with regular expression .To testing blind sql injection we scan the
http request after capture if it contain the key words [select, top, virsion,user, order, substring ,
ascii, from ,limit, having and etc. ] or regular expression like [*,>,<,$,%,-- ,‟,[a-z],[A-z],”and
etc] . The normal http request not contain like this. Then classify the http request to normal or
abnormal request. If the system found Blind sql injection reject input go to step 3, if no
continue to step 2.
STEP 2:
Perform SQL validation using validation parse tree. Compare the parse tree generated from an
attack request with that generated from an innocent message to verify whether an attack was
successful or not. If the parse generated from an innocent request, SQLI-DP determines the
attack was successful. Suppose that a web application issues the SQL query “SELECT *
FROM users WHERE name=""(vulnerable spot), or ‟1‟=‟1”.we use the technique idea that
used in [27] when using a fresh key to user input. If the web application sanitizes the input
properly, the parse tree will look like the one shown in figure 1. If not properly sanitized, the
parse tree will look like the one shown in figure 3, where the structure of the parse tree is
different from figure 1, if the two tree are different detect there is injection. Then classify the
query as normal or abnormal and account the False positive (A false positive occurs when the
test returns a positive result, but there is actually no fault). Then Prevent abnormal user queries
and send normal user queries immediately to database and execute query and send the data to
server.
67
IJCI. Vol. 3 – No. 1, March 2014
STEP 3:
SQLI-DP generates the report that contains the SQL query with the user input data, if it found
blind sql injection and execution time by millisecond. The SQL query benefit in detection to
know the kind of SQL injection attack.
1- It is efficient, because it only adds about 1second overhead to database query costs.
2- In addition, it is easily adopted by software developer.
3- It is suitable for legacy system because it is a technique implemented on server side. It
doesn‟t need to rewrite old web application because protection from Injection is on
server side where database resides.
4.2 Experimental Result
The experiments methodology is done by designing MSQL database and a patche server web
application in our platform. The SQLI-DP technique is implemented by Java language. We
used Zql to implement an SQL parser. The SQLI-DP technique consists of an SQL proxy, and
a core component of our technique. The SQL proxy captures the SQL queries. The core
component of SQLI-DP performs the tasks described in the previous section. We apply SQLI-
DPs in windows7, 32-bit operating system and core (TM) i5 CPU. Some snapshoot screens
from SQLI-DP work are shown when using different options of unsafe and safe. If we using
unsafe system with SqlIA we noticed that the attack success and the server return all data
records in database table as shown in figure 5. SQLI-DP (unsafe option) response all recodes
because there are (Tautology injection found). If we using Safety system option SQLI-DP
prevents SQLIA in first test if it found blind injection attack it generates report as shown in
figure 6. If blind injection attack not found the SQLI-DP tests other types of sql injection
attacks. Figures 7 and 8 are show the reports generated after hacking in different types of
attack.
This report is benefit in:
1- To know the type of SQL injection attack.
2- Sure that the system prevents this attack to accessed database.
3- To estimate the execution time of detection and prevention.
As shown in figure 7 this report appears when the SQLI-DP found Piggy-Backed Queries
SQLIAs when try to access the web application database.
68
E. Mohamed. Safwat
As shown in figure 8 this report appears when the SQLI-DP find Tautology SqlIAs
when try to access the web application database.
69
IJCI. Vol. 3 – No. 1, March 2014
Table1 shows the SQLI-DP techniques with respect to the number of attacks trails we using
manual hacking on the database and the execution time in millisecond.
70
E. Mohamed. Safwat
4500
4000
3500
3000
2500
2000
1500
Paros
1000
500 SQL IDP
0
Through the study of previous observations and discuss the reasons, we find that SQLI-DP by
several features not found in other techniques. First, SQLI-DP detects and prevents using less
time than Paros for the following reasons:
1- SQLI-DP is server side technique work as a proxy between the server and database but
Paros work in host side is a proxy between client and server so SQLI-DP decreases the
communication time.
2- SQLI-DP detects and prevents the blind SQL injection attacks but Paros technique
detect only.
3- From the observation the SQLI-DP technique add only one second addition to the
system but Paros technique add approximately 4 second to the system.
4- The SQLI-DP technique gives the SQL query statement in the final report it's very
important to the developer:
- To know kind of attack.
- To detect the kind of attacks and use for protect this type of sits from this kind of
attacks.
- To know the advanced schema of SQL injection attacks.
5- SQLI-DP give the final report after prevent the attacks to sure the attacks are prevented.
6- In addition, the SQLI-DP it is easily adopted by software developer because it written by
Java language that suitable for any platform .
7- It is suitable for legacy system because it is a technique that implemented on server side.
It doesn‟t need to rewrite old web application because protection from Injection is on
server side where database resides.
71
IJCI. Vol. 3 – No. 1, March 2014
In future work, we intend to evaluate SQLI-DPs using different web based applications with
real public domain to achieve great accuracy in SQL injection detection and prevention.
6. References
[1] OWASP (2012, September 6). Testing for SQL Injection[Online]. Available:
http://www.owasp.org/index.php/Testing for SQL Injection.
[2] Nithya. A Survey on SQL Injection attacks, their Detection and Prevention Techniques.
International Journal Of Engineering And Computer Science Volume 2 Issue 4
April,2013 Page No.886-905.
[3] Sayyed Mohammad. Study of SQL Injection Attacks and Countermeasures.
International Journal of Computer and Communication Engineering, Vol. 2, No. 5,
September 2013 Page No.539-514.
[4] Gopi Krishnan. Preventing Injection Attack by Whitelisting Inputs . Lecture Notes on
Information Theory Vol. 1, No. 3, September 2013 Page No.132-134.
[5] W. Halfond and A. Orso, "Combining Static Analysis and Runtime Monitoring to
Counter SQL- Injection Attacks," Proceeding of the Third International ICSE Workshop
on Dynamic Analysis (WODA 2005), 2005.
[6] Chunhui Song. Song, “SQL Injection Attacks and Countermeasures”, California State
University, Sacramento, (Spring 2010).
[7] Z. Lashkaripour .A Simple and Fast Technique for Detection and Prevention of SQL
Injection Attacks. International Journal of Security and Its Applications Vol.7, No.5
(2013), Page 53-66.
[8] K.R Venugopal, L.M Patnaik , Patnaik,"Detection and prevention of SQL injection
attacks", Computer Networks and Intelligent Computing. 5th International Conference
on Information Processing, ICIP 2011, Bangalore, India, August 5-7, 2011, pages 104-
107.
72
E. Mohamed. Safwat
73
IJCI. Vol. 3 – No. 1, March 2014
[26] Simone 'R00T_ATI' Quatrini Marco 'white_sheep' Rondini. Blind Sql Injection with
Regular Expressions Attack.
[27] Jagdish Halde. (2008),SQL Injection analysis, Detection and Prevention. Master's
Theses and Graduate ResearchSan Jose State University.
74