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

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

Botnet-Powered SQL Injection Attacks A Deeper Look Within (VB, Sep. 2009) David Maciejak Guillaume Lovet

CIS 433/533 - Computer and Network Security. Web Vulnerabilities, Wrapup

Understanding Web Application Security Issues

Advanced Web Technology 10) XSS, CSRF and SQL Injection 2

SQL Injection January 23, 2013

CSE598i - Web 2.0 Security OWASP Top 10: The Ten Most Critical Web Application Security Vulnerabilities

What is Web Security? Motivation

How I hacked PacketStorm ( )

To Cache a Thief Using database caches to detect SQL injection attacks. Kevvie Fowler, CISSP, GCFA Gold, MCTS, MCDBA, MCSD, MCSE

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

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

Web Application Security

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

Discovering passwords in the memory

Still Aren't Doing. Frank Kim

SQL Injection for newbie

Maintaining Stored Procedures in Database Application

SQL INJECTION ATTACKS By Zelinski Radu, Technical University of Moldova

Webapps Vulnerability Report

SECURING APACHE : THE BASICS - III

Cracking the Perimeter via Web Application Hacking. Zach Grace, CISSP, CEH January 17, Mega Conference

SQL Injection Attack Lab

Criteria for web application security check. Version

Threat Modeling. Categorizing the nature and severity of system vulnerabilities. John B. Dickson, CISSP

Magento Security and Vulnerabilities. Roman Stepanov

Web Applications Security: SQL Injection Attack

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

SQL Injection 2.0: Bigger, Badder, Faster and More Dangerous Than Ever. Dana Tamir, Product Marketing Manager, Imperva

Web application security

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

Database System Security. Paul J. Wagner UMSSIA 2008

Testing Web Applications for SQL Injection Sam Shober

Web Application Security. Vulnerabilities, Weakness and Countermeasures. Massimo Cotelli CISSP. Secure

The Top Web Application Attacks: Are you vulnerable?

Understanding Sql Injection

Security Awareness For Website Administrators. State of Illinois Central Management Services Security and Compliance Solutions

The purpose of this report is to educate our prospective clients about capabilities of Hackers Locked.

PHP Authentication Schemes

Where every interaction matters.

Web Application Security Considerations

External Vulnerability Assessment. -Technical Summary- ABC ORGANIZATION

Is Drupal secure? A high-level perspective on web vulnerabilities, Drupal s solutions, and how to maintain site security

Check list for web developers

Web Application Guidelines

INTRUSION PROTECTION AGAINST SQL INJECTION ATTACKS USING REVERSE PROXY

Thick Client Application Security

ASP.NET MVC Secure Coding 4-Day hands on Course. Course Syllabus

ABC LTD EXTERNAL WEBSITE AND INFRASTRUCTURE IT HEALTH CHECK (ITHC) / PENETRATION TEST

SQL Injection Attack Lab Using Collabtive

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

Role Based Access Control. Using PHP Sessions

SQL Injection Protection by Variable Normalization of SQL Statement

CTF Web Security Training. Engin Kirda

Penetration Test Report

3. Broken Account and Session Management. 4. Cross-Site Scripting (XSS) Flaws. Web browsers execute code sent from websites. Account Management

Data Breaches and Web Servers: The Giant Sucking Sound

Rational AppScan & Ounce Products

Enterprise Application Security Workshop Series

Creating Stronger, Safer, Web Facing Code. JPL IT Security Mary Rivera June 17, 2011

SQL Injection Vulnerabilities in Desktop Applications

External Network & Web Application Assessment. For The XXX Group LLC October 2012

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

Intrusion detection for web applications

FINAL DoIT v.4 PAYMENT CARD INDUSTRY DATA SECURITY STANDARDS APPLICATION DEVELOPMENT AND MAINTENANCE PROCEDURES

JOOMLA SECURITY. ireland website design. by Oliver Hummel. ADDRESS Unit 12D, Six Cross Roads Business Park, Waterford City

Hacking de aplicaciones Web

Project 2: Web Security Pitfalls

CS 558 Internet Systems and Technologies

Web Application Security

WEB SECURITY CONCERNS THAT WEB VULNERABILITY SCANNING CAN IDENTIFY

Advanced Web Security, Lab

Top 10 Database. Misconfigurations.

Web Application Report

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

University of Wisconsin Platteville SE411. Senior Seminar. Web System Attacks. Maxwell Friederichs. April 18, 2013

Application Design and Development

Ruby on Rails Secure Coding Recommendations

EVALUATING COMMERCIAL WEB APPLICATION SECURITY. By Aaron Parke

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

LISTSERV LDAP Documentation

Web Application Security

Cross Site Scripting in Joomla Acajoom Component

How To Fix A Web Application Security Vulnerability

Nuclear Regulatory Commission Computer Security Office Computer Security Standard

Columbia University Web Security Standards and Practices. Objective and Scope

The end. Carl Nettelblad

Oracle Security on Windows

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

Transcription:

SQL Injection Slides thanks to Prof. Shmatikov at UT Austin

Dynamic Web Application GET / HTTP/1.0 Browser HTTP/1.1 200 OK Web server index.php Database server slide 2

PHP: Hypertext Preprocessor Server scripting language with C-like syntax Can intermingle static HTML and code <input value=<?php echo $myvalue;?>> Can embed variables in double-quote strings $user = world ; echo Hello $user! ; or $user = world ; echo Hello. $user.! ; Form data in global arrays $_GET, $_POST, slide 3

SQL Widely used database query language Fetch a set of records SELECT * FROM Person WHERE Username= Vitaly Add data to the table INSERT INTO Key (Username, Key) VALUES ( Vitaly, 3611BBFF) Modify data UPDATE Keys SET Key=FA33452D WHERE PersonID=5 Query syntax (mostly) independent of vendor slide 4

Sample PHP Code Sample PHP $selecteduser = $_GET['user']; $sql = "SELECT Username, Key FROM Key ". "WHERE Username='$selecteduser'"; $rs = $db->executequery($sql); What if user is a malicious string that changes the meaning of the query? slide 5

SQL Injection: Basic Idea Attacker 1 post malicious form Victim server 3 receive valuable data 2 unintended query This is an input validation vulnerability Unsanitized user input in SQL query to back- end database changes the meaning of query Specific case of more general command injection Victim SQL DB slide 6

Typical Login Prompt slide 7

User Input Becomes Part of Query Web browser (Client) Enter Username & Password Web server SELECT passwd FROM USERS WHERE uname IS $user DB slide 8

Normal Login Web browser (Client) Enter Username & Password Web server SELECT passwd FROM USERS WHERE uname IS smith DB slide 9

Malicious User Input slide 10

SQL Injection Attack Web browser (Client) Enter Username & Password Web server SELECT passwd FROM USERS WHERE uname IS ; DROP TABLE USERS; -- DB Eliminates all user accounts slide 11

Exploits of a Mom http://xkcd.com/327/ slide 12

Authentication with Back-End DB set UserFound=execute( SELECT * FROM UserTable WHERE username= & form( user ) & AND password= & form( pwd ) & ); User supplies username and password, this SQL query checks if user/password combination is in the database If not UserFound.EOF Authentication correct else Fail Only true if the result of SQL query is not empty, i.e., user/ pwd is in the database slide 13

Using SQL Injection to Steal Data User gives username OR 1=1 -- Web server executes query set UserFound=execute( SELECT * FROM UserTable WHERE username= OR 1=1 -- ); Always true! Everything after -- is ignored! Now all records match the query This returns the entire database! slide 14

Another SQL Injection Example To authenticate logins, server runs this SQL command against the user database: SELECT * WHERE user= name AND pwd= passwd User enters OR WHERE pwd LIKE % as both name and passwd Server executes [From Kevin Mitnick s The Art of Intrusion ] Wildcard matches any password SELECT * WHERE user= OR WHERE pwd LIKE % AND pwd= OR WHERE pwd LIKE % Logs in with the credentials of the first person in the database (typically, administrator!) slide 15

It Gets Better User gives username exec cmdshell net user badguy badpwd / ADD -- Web server executes query set UserFound=execute( SELECT * FROM UserTable WHERE username= exec -- ); Creates an account for badguy on DB server slide 16

Pull Data From Other Databases User gives username AND 1=0 UNION SELECT cardholder, number, exp_month, exp_year FROM creditcards Results of two queries are combined Empty table from the first query is displayed together with the entire contents of the credit card database slide 17

More SQL Injection Attacks Create new users ; INSERT INTO USERS ( uname, passwd, salt ) VALUES ( hacker, 38a74f, 3234); Reset password ; UPDATE USERS SET email=hcker@root.org WHERE email=victim@yahoo.com slide 18

Uninitialized Inputs /* php-files/lostpassword.php */ for ($i=0; $i<=7; $i++) $new_pass.= chr(rand(97,122)) $result = dbquery( UPDATE.$db_prefix. users SET user_password=md5( $new_pass ) WHERE user_id=.$data[ user_id ]. ); Creates a password with 8 random characters, assuming $new_pass is set to NULL In normal execution, this becomes UPDATE users SET user_password=md5(???????? ) WHERE user_id= userid SQL query setting password in the DB slide 19

Exploit User appends this to the URL: &new_pass=badpwd%27%29%2c user_level=%27103%27%2cuser_aim=%28%27 SQL query becomes UPDATE users SET user_password=md5( badpwd ), user_level= 103, user_aim=(???????? ) WHERE user_id= userid with superuser privileges This sets $new_pass to badpwd ), user_level= 103, user_aim=( User s password is set to badpwd slide 20

Second-Order SQL Injection Second-order SQL injection: data stored in database is later used to conduct SQL injection For example, user manages to set uname to admin -- This vulnerability could exist if string escaping is applied inconsistently (e.g., strings not escaped) UPDATE USERS SET passwd= cracked WHERE uname= admin -- why does this work? Solution: treat all parameters as dangerous slide 21

SQL Injection in the Real World (1) http://www.ireport.com/docs/doc-11831 Oklahoma Department of Corrections divulges thousands of social security numbers (2008) Sexual and Violent Offender Registry for Oklahoma Data repository lists both offenders and employees Anyone with a web browser and the knowledge from Chapter One of SQL For Dummies could have easily accessed and possibly, changed any data within the DOC's databases" slide 22

SQL Injection in the Real World (2) Ohio State University has the largest enrolment of students in the United States; it also seems to be vying to get the largest number of entries, so far eight, in the Privacy Rights Clearinghouse breach database. One of the more recent attacks that took place on the 31st of March 2007 involved a SQL injection attack originating from China against a server in the Office of Research. The hacker was able to access 14,000 records of current and former staff members. slide 23

CardSystems Attack (June 2005) CardSystems was a major credit card processing company Put out of business by a SQL injection attack Credit card numbers stored unencrypted Data on 263,000 accounts stolen 43 million identities exposed slide 24

April 2008 Attacks slide 25

Main Steps in April 2008 Attack Use Google to find sites using a particular ASP style vulnerable to SQL injection Use SQL injection to modify the pages to include a link to a Chinese site nihaorr1.com Do not visit that site it serves JavaScript that exploits vulnerabilities in IE, RealPlayer, QQ Instant Messenger Attack used automatic tool; can be configured to inject whatever you like into vulnerable sites There is some evidence that hackers may get paid for each victim s visit to nihaorr1.com slide 26

Part of the SQL Attack String DECLARE @T varchar(255),@c varchar(255) DECLARE Table_Cursor CURSOR FOR select a.name,b.name from sysobjects a,syscolumns b where a.id=b.id and a.xtype='u' and (b.xtype=99 or b.xtype=35 or b.xtype=231 or b.xtype=167) OPEN Table_Cursor FETCH NEXT FROM Table_Cursor INTO @T,@C WHILE(@@FETCH_STATUS=0) BEGIN exec('update ['+@T+'] set ['+@C+']=rtrim(convert(varchar,['+@C+'])) +' ''') FETCH NEXT FROM Table_Cursor INTO @T,@C END CLOSE Table_Cursor DEALLOCATE Table_Cursor; DECLARE%20@S%20NVARCHAR(4000);SET%20@S=CAST( %20AS%20NVARCHAR(4000));EXEC(@S);-- slide 27

Preventing SQL Injection Input validation Filter Apostrophes, semicolons, percent symbols, hyphens, underscores, Any character that has special meanings Check the data type (e.g., make sure it s an integer) Whitelisting Blacklisting bad characters doesn t work Forget to filter out some characters Could prevent valid input (e.g., last name O Brien) Allow only well-defined set of safe values Set implicitly defined through regular expressions slide 28

Escaping Quotes For valid string inputs use escape characters to prevent the quote becoming part of the query Example: escape(o connor) = o connor Convert into \ Only works for string inputs Different databases have different rules for escaping slide 29

Prepared Statements Metacharacters such as in queries provide distinction between data and control In most injection attacks data are interpreted as control this changes the semantics of a query or a command Bind variables:? placeholders guaranteed to be data (not control) Prepared statements allow creation of static queries with bind variables. This preserves the structure of intended query. slide 30

Prepared Statement: Example http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html PreparedStatement ps = db.preparestatement("select pizza, toppings, quantity, order_day " + "FROM orders WHERE userid=? AND order_month=?"); ps.setint(1, session.getcurrentuserid()); ps.setint(2, Integer.parseInt(request.getParamenter("month"))); ResultSet res = ps.executequery(); Bind variable: data placeholder Query parsed without parameters Bind variables are typed (int, string, ) slide 31

Mitigating Impact of Attack Prevent leakage of database schema and other information Limit privileges (defense in depth) Encrypt sensitive data stored in database Harden DB server and host OS Apply input validation slide 32