Docsity

Preventing SQL Injection Attacks: An Architecture Approach, Lecture notes of Architecture

Partial preview of the text

Download Preventing SQL Injection Attacks: An Architecture Approach and more Lecture notes Architecture in PDF only on Docsity!

SECURING WEB APPLICATIONS FROM APPLICATION-LEVEL ATTACK

A thesis submitted to Kent State University in partial fulfillment of the requirements for the degree of Master of Science

by Amit Kumar Pandey August, 2007

ii

Thesis written by Amit Kumar Pandey B.E., Pune University, India, 2002 M.S, Kent State University, 2007

Approved by Dr. Michael Rothestein , Advisor Dr. Robert A. Walker , Chair, Department of Computer Science Dr. Jerry Feezel , Dean, College of Arts and Sciences

iv

v

7.4 Performance Evaluation............................................................. ...................... 54 CHAPTER 8 CONCLUSION................................................................................ 57 REFERENCES............................................................................... .................. .......... 59

vii

17 DOM representation for XML given in figure 7.2............................................... 18 XML DOM representation of malicious SQL statement...................................... 19 Response time of plain SQL versus Secured SQL................................................

viii

DEDICATION

This thesis is dedicated to my parents.

CHAPTER 1

Introduction

The rapid growth of internet has created many services, which have become an integral part of our day to today life. Web applications are used for making reservations, paying bills, and shopping on-line. With advent of Business-to-Business (B2B) and Business-to-Consumer (B2C) interaction, it is has become a necessity that information be exchanged in a secure and accurate way. Most of the web applications contain security vulnerabilities which enable attackers to exploit them and launch attacks. As a result of the attacks confidentiality, integrity and availability of information are lost. Information that can be read or copied by unauthorized users is called loss of confidentiality. Confidential information should be stored properly so that they cannot be disclosed. Credit card numbers, bank records, medical records, social security etc are example of this kind of information. Loss of integrity takes place when data is modified in an unexpected way. Attackers would intentionally tamper information resulting in loss of integrity of information. The loss of availability takes place when information is erased so that the legitimate user or authorized user would not be able to read it or use it. Loss of availability affects service oriented business

which depends upon data. These attacks which are at application level, cannot be prevented using packet inspection firewalls which analyze individual IP packets for signatures or allow specific ports. What is needed is a mechanism which analyses the whole message stream. Attacks at application level differ from network layer attacks. Application level attack exploit vulnerabilities present in web application code and limitations of protocol like HTTP. Attacks at the application level cannot be stopped by most network firewalls and antivirus software programs. A network firewall normally leaves port 80 open for web server. It is through this port that the web application communicates to the user. If the attacker is able to access applications he may launch attack which cannot be prevented by the firewall. For example, consider a user who has a legitimate account at a banking system. He connects to his account by authenticating and establishing a valid session. If the user is injecting code to access unauthorized information of other users, then the network firewall or Intrusion Detection Systems (IDSs) will not be able to stop him. SQL injection is a very serious application level attack on web applications.

1.3 Web application and SQL injection Most of the current web applications use RDBMS (Relational Database Management Systems). Sensitive information like credit card, social security and financial records are stored in these databases. Web application allows users to view, edit or store information in RDBMS through programs written by web application programmers. Usually programmers who write these programs are unaware of technique for writing secure code. They would focus on implementing desired functionalities and would focus less on security aspects. This results in vulnerabilities in web applications. Vulnerabilities allow attacker to target these web application and obtain valuable information. Attackers would send SQL (Structured Query language) to interact with RDBMS servers or modify existing SQL to retrieve unauthorized information without any authentication. The risk is higher if the application is open source or if the attacker is able to gain source code through other means. In this case he can analyze the code to find out the vulnerabilities

CHAPTER 2

Overview of Problem

Web applications often use data submitted by a client to construct SQL queries. The communication between the programming language and the database commonly takes place through a call level interface such as JDBC or ODBC. Call level interfaces allows full power of SQL by accepting dynamically generated SQL statements. User input is concatenated to a predefined SQL statement and is passed by CLI to the database for query. However, constructing queries by concatenating user input with SQL statements leads to vulnerability where the user can execute arbitrary SQL against the database. The work described here tries to prevent SQL injection by using an SQL parser written in java. In addition to that we have developed a data type checking system which prevents data type mismatch in dynamically generated SQL queries. An implementation of this system is described and is shown how it handles most common SQL injection and data type mismatches.

2.1 Motivation Cross site scripting, buffer overflow, content spoofing and session fixation SQL injection are some of the application layer attacks targeting most of the current web application. Among all this attacks cross site scripting and SQL injection are very common. SQL injection is considered a severe of attack affecting

statements are precompiled SQL statements with one or more parameters. Prepared statement is generally immune to SQL injection as the database will use the value of the parameter exclusively and would not interpret the contents of the variable in any other way. However they can be used only when the same query is to be executed many times. When the queries are dynamically generated (like if they are constructed based upon what user selects from several options), a prepared statement cannot be used. Other alternatives like SQLJ do perform static syntax and type checking but they work only with static SQL statements. Also SQLJ is not very popular and hardly used.

2.2 Research Contributions An Architecture which prevents SQL injection by static and dynamic analysis of SQL string is presented. To implement analysis of the SQL string we wrote a top down parser using javaCC. We present a simulation of the algorithm on two different cases. The applications are written in java technology with MS SQL server as a database. The algorithm was able to detect the SQL injection in both cases. The approach use an XML document to store the static model of the SQL statement. The use of XML allows flexibility in deploying our web security system. It makes it possible for the web developer to specify acceptable SQL string by writing their own XML document to represent the static model. Though we tested our system

for web applications written in java technology, the approach can easily be adapted for web applications written in other languages. In addition to preventing SQL injection the architecture prevents data type mismatch in dynamically generated SQL query strings. Compared to other approaches like prepared statement, our approach works well with legacy code, does not require changes in application code and helps achieve utilize full power of CLI without sacrificing the security aspect.

2.3 Organization of the thesis This work is organized as follows. CHAPTER 3 provides description of the different cases of SQL injection attack. It also describes how attackers are able to use the data type error returned by the database server for formulating their query for retrieving unauthorized information. CHAPTER 4 gives a survey on the related work done in the area of application layer attack, specifically SQL injection attack. CHAPTER 5 gives introduction to the parser generator tool: javaCC. CHAPTER 6 describes the various components and algorithm of our security system which we had designed and implemented to prevent SQL injection attack. CHAPTER 7 explains working of our security system for the two different cases of SQL injection attack; it also gives performance evaluation of our system. CHAPTER 8 presents our conclusions and future work.

E.g.: consider a query select * from tablename where username='a' and password = 'b', where 'a' and 'b' are the username and the password respectively entered by the user to view his account information. The attacker enters ' OR 1=1 -- in username field and leaves the password field blank. The query would become:

Select * from tablename where username=' ' OR 1 =1 -- and password = ''

The single quote entered by the user disables the opening quote and OR 1=1, which is a tautology, satisfies Where clause for all records. Hence, when this query is executed it will return information about all the users in the table.

3.1.2 Code injection This type of attack is possible only if the database supports multiple SQL statements per database request. MS SQL server and postgrSQL support it but Oracle does not. Consider a Student directory Website which prompts a user to enter the surname of a student to search for by means of a form-box called searchName.

On the server-side this search string (stored in the variable LastName) is used to build an SQL query. This may involve code such as: query = "SELECT forenname,tel,fax,email FROM personal WHERE surname=`LastName'; ";

However, if the user enters the following text into the surname form box: '; SELECT password,tel,fax,email FROM personal WHERE surname=Jones then the value of variable, query will become: SELECT forenames,tel,fax,email FROM personal WHERE surname='; SELECT password,tel,fax,email FROM personal WHERE surname=`Jones';

When executed on some SQL databases, this will result in Jones's password being returned instead of his forename.

3.1.3 Exploiting INSERT Normally an insert statement looks like this: Insert into TableName Values (‘ValueOne’, ‘valueTwo’, ‘valueThree’); Consider a webpage which allows user to edit his information or webpage where user fills in their information. The fields are user name: Name, user email : Email and user Phone number: Phone