A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

Similar documents
SQL Injection January 23, 2013

SECURING APACHE : THE BASICS - III

SQL Injection Attack Lab Using Collabtive

1. What is SQL Injection?

SQL Injection. The ability to inject SQL commands into the database engine through an existing application

Tutorial: How to Use SQL Server Management Studio from Home

Webapps Vulnerability Report

Web Application Security

SQL Injection. Blossom Hands-on exercises for computer forensics and security

A Brief Introduction to MySQL

Concepts Design Basics Command-line MySQL Security Loophole

Understanding Sql Injection

DIPLOMA IN WEBDEVELOPMENT

SQL INJECTION ATTACKS By Zelinski Radu, Technical University of Moldova

Serious Threat. Targets for Attack. Characterization of Attack. SQL Injection 4/9/2010 COMP On August 17, 2009, the United States Justice

INTRODUCTION: SQL SERVER ACCESS / LOGIN ACCOUNT INFO:

A basic create statement for a simple student table would look like the following.

Web Applications Security: SQL Injection Attack

Web Development using PHP (WD_PHP) Duration 1.5 months

PHP Authentication Schemes

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

A table is a collection of related data entries and it consists of columns and rows.

SQL Injection Vulnerabilities in Desktop Applications

CSCI110 Exercise 4: Database - MySQL

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

Advanced Web Security, Lab

White Paper. Blindfolded SQL Injection

IRF2000 IWL3000 SRC1000 Application Note - Apps with OSGi - Condition Monitoring with WWH push

SQL Injection. Slides thanks to Prof. Shmatikov at UT Austin

Application note: Connecting the to a Database

Exposed Database( SQL Server) Error messages Delicious food for Hackers

Facebook Twitter YouTube Google Plus Website

INTRUSION PROTECTION AGAINST SQL INJECTION ATTACKS USING REVERSE PROXY

CPE111 COMPUTER EXPLORATION

SQL Injection Attack Lab

Magento Security and Vulnerabilities. Roman Stepanov

AUTHENTICATION... 2 Step 1:Set up your LDAP server... 2 Step 2: Set up your username... 4 WRITEBACK REPORT... 8 Step 1: Table structures...

Detection of SQL Injection Attacks by Combining Static Analysis and Runtime Validation

By : Ashish Modi. CRUD USING PHP (Create, Read, Update and Delete on Database) Create Database and Table using following Sql Syntax.

Enhanced Model of SQL Injection Detecting and Prevention

SQL Injection. SQL Injection. CSCI 4971 Secure Software Principles. Rensselaer Polytechnic Institute. Spring

Guarding Against SQL Server Attacks: Hacking, cracking, and protection techniques.

Thick Client Application Security

Agenda. SQL Injection Impact in the Real World Attack Scenario (1) CHAPTER 8 SQL Injection

DETECTION AND PREVENTION OF TAUTOLOGY AND UNION QUERY BASED SQL INJECTION ATTACKS

SQL Injection Attack Lab

HowTo. Planning table online

Maintaining Stored Procedures in Database Application

An Introduction to SQL Injection Attacks for Oracle Developers. January 2004 INTEGRIGY. Mission Critical Applications Mission Critical Security

Role Based Access Control. Using PHP Sessions

Juniper Secure Analytics Release Notes

USING MYWEBSQL FIGURE 1: FIRST AUTHENTICATION LAYER (ENTER YOUR REGULAR SIMMONS USERNAME AND PASSWORD)

AUTOMATE CRAWLER TOWARDS VULNERABILITY SCAN REPORT GENERATOR

Zend Framework Database Access

SQL Injection for newbie

WebCruiser Web Vulnerability Scanner User Guide

Customer Bank Account Management System Technical Specification Document

Migrate Topaz databases from One Server to Another

External Vulnerability Assessment. -Technical Summary- ABC ORGANIZATION

Perl In Secure Web Development

Blindfolded SQL Injection. Written By: Ofer Maor Amichai Shulman

Web applications. Web security: web basics. HTTP requests. URLs. GET request. Myrto Arapinis School of Informatics University of Edinburgh

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

The SQL Injection Attacking Prevention Applying the Design Techniques

IP Monitoring and Filtering

FileMaker 12. ODBC and JDBC Guide

SQL Injection. By Artem Kazanstev, ITSO and Alex Beutel, Student

VIDEO intypedia007en LESSON 7: WEB APPLICATION SECURITY - INTRODUCTION TO SQL INJECTION TECHNIQUES. AUTHOR: Chema Alonso

EC-Council CAST CENTER FOR ADVANCED SECURITY TRAINING. CAST 619 Advanced SQLi Attacks and Countermeasures. Make The Difference CAST.

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

Online shopping store

No SQL! no injection? A talk on the state of NoSQL security

Check list for web developers

Nikolay Zaynelov Annual LUG-БГ Meeting nikolay.zaynelov.com

Testing Web Applications for SQL Injection Sam Shober

Implementation of Web Application Firewall

PHP Tutorial From beginner to master

1. Building Testing Environment

Application Security Testing. Generic Test Strategy

SQL injection attacks SQL injection user input SQL injection SQL Command parameters Database account. SQL injection attacks Data Code

Intrusion Detection And Prevention System: SQL-Injection Attacks

Designing for Dynamic Content

Web Application Guidelines

Release Notes LS Retail Data Director August 2011

CodeIgniter for Rapid PHP Application Development

A Tokenization and Encryption based Multi-Layer Architecture to Detect and Prevent SQL Injection Attack

SQL injection: Not only AND 1=1. The OWASP Foundation. Bernardo Damele A. G. Penetration Tester Portcullis Computer Security Ltd

Web Application Disassembly with ODBC Error Messages By David Litchfield Director of Security

Half Bridge mode }These options are all found under Misc Configuration

Transcription:

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks Abhay K. Kolhe Faculty, Dept. Of Computer Engineering MPSTME, NMIMS Mumbai, India Pratik Adhikari Student, Dept. Of Computer Engineering MPSTME, NMIMS Mumbai, India Abstract SQL Injection has been always as the top threat in any web site and web application. In this paper we are making a dummy web site and injecting some SQL queries, detecting the SQL injection using the IP tracking method, preventing SQL injection using different types of defense mechanism. We have made the dummy website to inject, detect and prevent the SQL injection attacks. We are also giving the internal view where it is required to explain these attacks, the detection and defense mechanism through the explanation of the source codes. Keywords: SQL injection, SQL injection vulnerability, web security. 1. Introduction The SQL injection is the code injection technique, in which malicious SQL statements are inserted into an entry field for execution and used to attack the database and performs different types of the database interaction, operations and functions without sanitizing the inputs in the entry field [1]. SQL injection is always a top priority for security of any web applications as injection can be done in any website or web application made in any language like PHP, JSP, ASP with the database like Mysql, Oracle, Microsoft SQL server [2] [3]. 2. SQL injection for Authentication, Insert, drop, Update and delete. We have made the simple website where the user can register, login and get authenticated. We make the web vulnerable as we didn t use any protection mechanism in any form. In the below codes in 2.1 we are explaining the simple mechanism for extraction of login information for the registered user through the MySql database. 2.1. Extracting login information //adding values mysql_select_db("pratik", $con); $name1 = mysql_query("select * from user where username='$username' and password = '$password'"); $row1 = mysql_fetch_array($name1); if($row1['username'] == '') { echo 'Wrong username/password!'; echo '</br><a href="loginform.html">retry</a>'; else { $_SESSION['logedinuser']=$row1['username']; echo 'Login successful'; echo'</br></br>hello, '.$row1['username'].' &nbs p; <a href="logout.php">logout</a>'; echo '</br>'; echo '<h1>welcome to our site!!</h1>'; Steps: 1) Select the database. 2) Select Username and password and store in variable name1. 3) From the variable name1 fetch the values to variable row1. 4) If variable row1 is empty or doesn t match return wrong username and password. 5) Else make login successful when values match with the database. 3290

6) Return user name and welcome to our site message 2.2 SQL injection for authentication We are able to get a login into our website successfully by giving username: anything' OR 'x'='x and password: anything' OR 'x'='x as the following shown in the Figure 2.1.1(a).This type of SQL injections is for incorrectly filtered escape character. Later in the prevention phase we will discuss how it can be prevented. We have made the table name as the user where the values of all registered user are stored. Injection query can be explained as: "SELECT * FROM user WHERE name = '" + username + "';" Figure 2.1.1(b).Results for injection 1 st row user 2.1.2 Injection for particular user such as admin For the case, such as admin here we use username as admin we can login as x' OR username LIKE '%admin%. We supply both query to our login details such as username: x' OR username LIKE '%admin% and Password: x' OR username LIKE '%admin%. If the "username" is replaced by SQL string, like anything' or 'x'='x during authentication, the database responds to the code in same manner as the first code and displays the records. This is because evaluation of 'x'='x' is always true. So we get authenticated without any proper valid user name and password in the database. Here in this case single quote always allows the inside quote to get executed [4]. When user enters a LIKE clause with the username then the database will return the matching criteria to the user immediately. SELECT username, password, email, full_name FROM user WHERE username= x OR full_name LIKE %admin% ; Here, the database will return information of any user where the name starts with admin. Figure 2.1.2(a) shows the injection for the particular user such as admin and Figure 2.1.2(b) shows the login results. % is the wild character used to select particular user from the database. Figure 2.1.1(a).SQL injection 1 st row user We can see in the Figure 2.1.1(b) that we are able to login to the website without any username and password. In this case the row for the first user is always getting selected for the login on the website. This form of the SQL injection is easy to check on any website.most of the coders and developers forget to filter the escape character and this attack is always vulnerable in PHP as well as asp.net for both MySQL and MS SQL server 2005, 2008. Figure 2.1.2(a).SQL injection for particular user Figure 2.1.2(b) Results of injection 3291

For the case, such as admin here we use the username as admin we can login as x' OR username LIKE '%admin%. We supply both query to our login details such as username: x' OR username LIKE '%admin% and Password: x' OR username LIKE '%admin%. When a user enters a LIKE clause with the username then the database will return the matching criteria to the user immediately. SELECT username, password, email, full_name FROM user WHERE username= x OR full_name LIKE %admin% ; Here, the database will return information of any user where the name starts with admin. Figure 2.1.2(a) shows the injection for the particular user such as admin and Figure 2.1.2(b) shows the login results. % is the wild character used to select a particular user from the database. Table 2.1.4(b) shows the update results we can see results clearly that the username for the id 38 have been updated from bob to ted. Table 2.1.4(b) update operation Update operation done 2.4 Injection for delete Here we are injecting the delete query to the database we are deleting the particular row in the database based on the username. Figure 2.1.5(a) shows injection for the delete query. 2.1.3 Injection for insert operation. Here we are injecting the insert query to the database as username, password, email, and the name for the particular user. Figure 2.1.3(a) shows the injection for insert query. Figure 2.1.3(a) SQL Injection for Insert Query Figure 2.1.5(a) SQL Injection for Delete Query Table 2.1.5(b) shows the delete results.we can see clearly that the username naresh with id 37 has been deleted successfully. Table 2.1.5(b) Delete operation Table 2.1.3(b) shows the insert operation results. We can see the email, password, username and name is added in the table name user. Table 2.1.3(b) Insert operation Delete operations done for user id 37 naresh 2.5 Injection for Drop To show the Drop operation in our website we are making additional table for the drop operation. Inserted new row 2.1.4 Injection for update Here we are injecting the update query to the database we are updating the username for the particular email address. Figure 2.1.4(a) shows the injection for the update query. Figure 2.1.4(a) SQL Injection for Update Query CREATE TABLE IF NOT EXISTS `student` ( `fname` varchar(100) NOT NULL, `lname` varchar(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; Dumping data for table `student` INSERT INTO `student` (`fname`, `lname`) VALUES ('Pratik', 'Adhikari'), ('Nikhil', 'Kamath'), 3292

('cc', 'dd'), ('Pratik', 'www.hackingweb.com'); To inject the drop query we have made extra table named student for the first name and last name so our main table user doesn t get altered. Table 2.1.6(a) shows the table name student. Table 2.1.6(a) Student Table Here we are injecting the drop query. We are dropping the table name student. Figure 2.1.6(b) show the injection for the drop query. Figure 2.1.6(b) SQL Injection for Drop Query Figure 2.1.6(c) shows the results after dropping the table. Figure 2.1.6(c) Results for dropped table. 3. Detection based on the IP Tracking mechanism We are taking the array which takes the injection by the keywords and we match the same keyword using preg_match function. After that we track the clients/users IP address and insert the injection type as well as IP address in the database table called iptracker[5]. //Logic for finding out injecion $injection['inject']=array("like"," ","insert","update","delete","drop","'='"); $querytofire=$query; $regex=implode(" ",$injection['inject']); if(preg_match("/(\b{$regex\b)/i",$querytofire,$matche s)){ $ip=$_server['remote_addr']; $injection_type=$matches[0]; $ipupdate="insert into iptracker (ip_addr,injection_type) ('$ip','$injection_type')"; $ipinsert=mysql_query($ipupdate); 3.1 Database for IP detection Here we have created the table name iptracker. values CREATE TABLE IF NOT EXISTS `iptracker` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ip_addr` varchar(20) NOT NULL, `injection_type` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ; Dumping data for table `iptracker` INSERT INTO `iptracker` (`id`, `ip_addr`, `injection_type`) VALUES (1, '127.0.0.1', ''),(2, '127.0.0.1', ''),(3, '127.0.0.1', ''),(4, '127.0.0.1', ''),(5, '127.0.0.1', ''),(6, '127.0.0.1', ''),(7, '127.0.0.1', ''),(8, '127.0.0.1', 'INSERT'),(9, '127.0.0.1', 'UPDATE'),(10, '127.0.0.1', 'delete'), (11, '127.0.0.1', 'drop'),(12, '127.0.0.1', 'or'),(13, '127.0.0.1', 'or'),(14, '127.0.0.1', '1=1'),(15, '127.0.0.1', 'or'),(16,'127.0.0.1', 'or'),(17, '127.0.0.1', 'INSERT'),(18, '127.0.0.1', 'drop'); In PHP we extract the IP address using $_SERVER[ REMOTE_ADDRS ] which is the server variable and added to the database. 4. Prevention technique for SQL injection 4.1 mysql_real_escape_string() Escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query()[6]. mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which adds backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.we are filtering out the query before it is 3293

passed to the database we can see the that we are unable to inject the query after using this mechanism[6]. if(isset($_post['submit'])&& $_POST['prevention_param_escape']==1){ $query=mysql_real_escape_string($_post['query']); mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("pratik") or die(mysql_error()); $result=mysql_query($query); if (!$result) { echo 'Access Unauthorised'; $message='access Unauthorised'; header("location:sqlinjection.php?message=$message" ); Here in the code we are using mysql_real_escape_string() for the prevention mechanism, in the above code first we are checking prevention method is set or not by using isset function and prevention_param_escape parameter. Then by using the function mysql_real_escape_string we check is it injected or not. If it s injected the message access unauthorized is generated. An extra API call is used for multiple statements to reduce the likeliness of SQL injection attacks [7]. if(isset($_post['submit'])&&$_post['prevention_par am']==0&& $_POST['prevention_param_escape']==0){ $query=$_post['query']; $mysqli = new mysqli("localhost", "root", "", "pratik"); if (!$mysqli->multi_query($query)) { echo "Query failed: (". $mysqli->errno. ") ". $mysqli- >error; Here in the code we are first checking the prevention method is set or not using isset function and prevention_param parameter. Then by using the function mysqli we check is it injected or not and if it is injected then we will generate message access unauthorized. Figure 4.1.2(a) MYSQLI Results after mysql_real_escape_string() and MYSQLI Table 4.1.1(b) Using MYSQLI and mysql_real_escape_string() Figure 4.1.1(a) mysql_real_escape_string() 4.1.2 MySQLi MySQLi optionally allows having multiple statements in one statement string [7]. Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolon. Then; all result sets returned by the executed statements must be fetched. The MySQL server allows having statements that do return result sets and statements that do not return result sets in one multiple statements [7]. 5. Conclusion and further work: In this paper we have shown different types of injection of SQL attacks like authentication, insert, delete, drop and update operations. We also have show the detection of these attacks by using IP-tracking method, where we store the IP address of the user for the particular type of attacks that they are injecting. 3294

We also have shown the internal coding for the injection, detection as well as prevention for these types of attacks. In future we would like to explore more on SQL injection attacks and analyse these attacks based on the various parameters. 6. References [1] http://technet.microsoft.com/enus/library/ms16195 3(v=SQL.105).aspx 25 th Nov 2013. [2] https://www.owasp.org/index.php/top_10_2013- Top_10 25th Nov 2013. [3] Zeinab Raveshi, Sonali R.Idate "Investigation and Analysis of SQL Injection Attacks on Web Applications: Survey" International Journal of Engineering and Advanced Technology (IJEAT) ISSN: 2249 8958, Volume-2, Issue-3 February 2013. [4] Ramakanth Dorai,Vinod Kannan "SQL Injection- Database Attack Revolution and Prevention" Journal of International Commercial Law and Technology Vol. 6, Issue 4 (2011). [5] Perumalsamy Ramasamy, Dr.SunithaAbburu, "SQL INJECTION ATTACK DETECTION AND PREVENTION International Journal of Science and Technology (IJEST) ISSN: 0975-5462 Vol. 4 No.04 April 2012. [6] http://php.net/manual/en/function.mysql-realescape-string.php 26 Nov 2013. [7] http://www.php.net/manual/en/mysqli.quickstart. multiple-statement.php 26 Nov 2013. [8] Sid Ansari,Edward R. Sykes,"SQL Injection in Oracle: An exploration of vulnerabilities, International Journal on Computer Science and Engineering (IJCSE) ISSN :0975-3397 Vol. 4 No. 04 April 2012 522. 3295