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



Similar documents
Check list for web developers

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

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

The Past, Present and Future of XSS Defense Jim Manico. HITB 2011 Amsterdam

Webapps Vulnerability Report

Universal XSS via IE8s XSS Filters

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

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

XSS Lightsabre techniques. using Hackvertor

XSS PROTECTION CHEATSHEET FOR DEVELOPERS V1.0. Author of OWASP Xenotix XSS Exploit Framework opensecurity.in

Intrusion detection for web applications

Institutionen för datavetenskap

Cross Site Scripting Prevention

Document Structure Integrity: A Robust Basis for Cross-Site Scripting Defense

Bypassing Web Application Firewalls (WAFs) Ing. Pavol Lupták, CISSP, CEH Lead Security Consultant

Web Application Security

Finding XSS in Real World

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

EVALUATING COMMERCIAL WEB APPLICATION SECURITY. By Aaron Parke

Blackbox Reversing of XSS Filters

Towards Automated Malicious Code Detection and Removal on the Web

Carlos Muñoz Application Security Engineer WhiteHat

Ruby on Rails Security. Jonathan Weiss, Peritor Wissensmanagement GmbH

Security Research Advisory IBM inotes 9 Active Content Filtering Bypass

Project 2: Web Security Pitfalls

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

Ruby on Rails Secure Coding Recommendations

Bypassing Internet Explorer s XSS Filter

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

HTML TIPS FOR DESIGNING

Protection, Usability and Improvements in Reflected XSS Filters

Hack-proof Your Drupal App. Key Habits of Secure Drupal Coding

Reliable Mitigation of DOM-based XSS

Interspire Website Publisher Developer Documentation. Template Customization Guide

A talk by ,

Short notes on webpage programming languages

Hack Proof Your Webapps

Instructions for Embedding a Kudos Display within Your Website

Towards More Security in Data Exchange

Input Validation Vulnerabilities, Encoded Attack Vectors and Mitigations OWASP. The OWASP Foundation. Marco Morana & Scott Nusbaum

Still Aren't Doing. Frank Kim

Top 10 Web Application Security Vulnerabilities - with focus on PHP

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

Web Application Guidelines

Drupal CMS for marketing sites

Introduction to Ingeniux Forms Builder. 90 minute Course CMSFB-V6 P

WebCruiser Web Vulnerability Scanner User Guide

Stopping SQL Injection and. Manoranjan (Mano) Paul. Track: Operating Systems Security - Are we there yet?

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

How To Fix A Web Application Security Vulnerability

Mobile development with Apache OFBiz. Ean Schuessler, Brainfood

Example. Represent this as XML

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

Developing ASP.NET MVC 4 Web Applications MOC 20486

Office Fax

Analysis of Browser Defenses against XSS Attack Vectors

AJAX and JSON Lessons Learned. Jim Riecken, Senior Software Engineer, Blackboard Inc.

SharePoint 2007 & 2010 (and Office 365!) Customization for Site Owners End User / Dev (100/200)

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

Magento Security and Vulnerabilities. Roman Stepanov

SQL Injection and XSS

Understanding Cross Site Scripting

Portals and Hosted Files

Abusing HTML5. DEF CON 19 Ming Chow Lecturer, Department of Computer Science TuCs University Medford, MA

QualysGuard WAS. Getting Started Guide Version 3.3. March 21, 2014

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

SIMGallery. User Documentation

HOW TO CREATE THEME IN MAGENTO 2

BASICS OF WEB DESIGN CHAPTER 2 HTML BASICS KEY CONCEPTS COPYRIGHT 2013 TERRY ANN MORRIS, ED.D

NoSQL, But Even Less Security Bryan Sullivan, Senior Security Researcher, Adobe Secure Software Engineering Team

Attacks on Clients: Dynamic Content & XSS

SECURE APPLICATION DEVELOPMENT CODING POLICY OCIO TABLE OF CONTENTS

Form Processing and Workflows

Criteria for web application security check. Version

Defending against XSS,CSRF, and Clickjacking David Bishop

Application security testing: Protecting your application and data

Developing ASP.NET MVC 4 Web Applications

Cyber Security Challenge Australia 2014

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

Uploaded images filter evasion for carrying out XSS attacks

Bypassing XSS Auditor: Taking Advantage of Badly Written PHP Code

RSS Feeds - Content Syndication Feed2JS: a simple solution to display RSS feeds

Sightly Component Development

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

Network Security Web Security

CS 558 Internet Systems and Technologies

OWASP TOP 10 ILIA

Web-Application Security

SQL INJECTION IN MYSQL

Xtreeme Search Engine Studio Help Xtreeme

Dashboard Skin Tutorial. For ETS2 HTML5 Mobile Dashboard v3.0.2

Team Members: Christopher Copper Philip Eittreim Jeremiah Jekich Andrew Reisdorph. Client: Brian Krzys

How To Protect A Web Application From Attack From A Trusted Environment

Detect and Sanitise Encoded Cross-Site Scripting and SQL Injection Attack Strings Using a Hash Map

How to Create a Mobile Responsive Template in ExactTarget

AMIT KLEIN, FORMER DIRECTOR OF SECURITY AND RESEARCH, SANCTUM

PST_WEBZINE_0X04. How to solve XSS and mix user's HTML/JavaScript code with your content with just one script

REDCap General Security Overview

Secure Web Application Coding Team Introductory Meeting December 1, :00 2:00PM Bits & Pieces Room, Sansom West Room 306 Agenda

Using Sprajax to Test AJAX. OWASP AppSec Seattle Oct The OWASP Foundation

Transcription:

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

What Is Cross Site Scripting? Injecting Scripts Into Otherwise Benign and Trusted Browser Rendered Content

Example XSS $html = <script>alert( hi );</script> ; echo <b>$html</b> ; <b><script>alert( hi );</script></b>

Two Types Of XSS Transient XSS Passes Data Through Request Data Affects Only The Tainted Request(s) Persistent XSS Stores Data On The Server Affects All Requesters (visitors)

Before We Talk About XSS, We Need To Talk About: Filter In, Escape Out

What Is Filter In? Can have 2 meanings based on context Removing or stripping unsafe/invalid content Rejecting unsafe/invalid content All input should be filtered Would you accept 2f as an age? What is input? Anything that is not hard coded into your code This includes your database

Wait, Does That Mean I Need To Filter Everything Twice? Yes.

What Is Escape Out? Escaping means to make content safe for a context All output must be escaped How it should be escaped depends upon context SQL requires different techniques than HTML It should be escaped as close to output as possible What is Output? Anything that leaves the memory of the program SQL, Files, HTML, REST, Headers, XML, JSON, etc

Wait, Does That Mean I Need To Escape Everything Multiple Times? Yes.

What Are The Parts Of HTML? Nodes: <a> (the a ) Values: <b>foo</b> (the foo ) Attribute Names: <c d= e /> (the d ) Attribute Values: <f g= h /> (the h ) CSS Identifiers:.foo {} (the.foo ) CSS Literals:.foo {color: bar } (the bar ) JS Code: alert( g ) (the alert ) JS Literals: alert( h ) (the h ) HTML Comments: <! bar (the bar )

Let s Talk About Escaping First

Never Allow Unfiltered User Input : Node Names <foo /> Attribute Names <a bar= /> HTML Comments <! Foo CSS Identifiers.baz{foo} JS Code biz();

You Cannot Escape Content For Those HTML Components!

Values <foo>bar</foo> Need To Escape The Following Characters: & -> & -> " < -> &lgt; > -> > -> &#x27; Prevents Injection of New Tags

Attribute Values Always quote the attribute value <foo bar= baz /> Need To Escape The Following Characters: & -> & -> " < -> &lgt; > -> > -> &#x27;

JS Literals Always quote string literals Always cast numeric literals Need To Escape The Following Characters: All Non-Alpha Numeric Characters Use \xnn format Be Aware That Not All Literals Can Be Escaped setinterval( foo ) Foo should never be unfiltered

Tools Available In PHP For Escaping

htmlspecialchars() Useful for escaping Values and Attribute Values Should always pass ENT_QUOTES flag Should always set the character set htmlspecialchars($input, ENT_QUOTES, UTF-8 )

preg_replace_callback() Useful for escaping JS literals preg_replace_callback( /*^a-z0-9+/i, function ($match) { $chr = dechex(ord($match[0])); return \\x. str_pad($dechex, 2, 0, STR_PAD_LEFT); }, $data );

OWASP s ESAPI Useful for all HTML escaping needs Has multiple methods for escaping https://www.owasp.org/index.php/esapi $encoder->encodeforhtml($data); $encoder->encodeforhtmlattribute($data); $encoder->encodeforjavascript($data);

Smarty Templating Engine for PHP Does not escape anything by default! Cannot be told to do so Must explicitly use special syntax to escape {$var html}

Twig Templating Engine for PHP Similar to Smarty, but cleaner and more powerful Does Intelligent Escaping Automatically Can be turned off as needed {{ var }}

Let s Talk Filtering

Filtering Guidelines Always Favor White-listing over Black-listing Filtering against valid values is more robust Always do it for all input Including Content From The Database! Allows changes to the filter to propagate automatically Identify Improper Input and Notify The User Gives User a Chance To Fix The Issue Also gives immediate feedback to an attacker

What Can You Safely Filter? All User Supplied Data Any part of HTML, if filtered properly, can be supplied by user input Be Careful When Filtering Sensitive Elements: URLs JavaScript Code HTML Content

Filtering HTML Check For Improper Tag Structure <a><b></a></b> Check For Bad Tags: style, script, comments, etc Check For Special Attributes href, src, js events, style, etc Make sure they are valid, and not JS (or remove them entirely)

Tools Available In PHP For Filtering

strip_tags() Removes all tags except those explicitly allowed Removes all attributes Not effective if you need links, etc Removes everything that is wrapped by < > May break user s intent strip_tags($data, <b><u><i> );

HTMLPurifier Library to sanitize HTML Very Smart Cleans up document structure Allows safe attributes Highly configurable http://htmlpurifier.org/ $purifier->purify($data);

Don t Roll Your Own! HTML Sanitization Is Not A Trivial Problem To Solve

The XssBadWebApp Designed To Be A Real World Application Several Known XSS Vulnerabilties No known non-xss vulnerabilities Designed For Educational Use Only Released under the BSD License Available At GitHub github.com/ircmaxell/xssbadwebapp

Demonstration Time!

Quick Review There Is No Magic Solution Always Filter Input Even When Input Comes From The Database Always Escape Output Escaping Is Context Dependent Several Tools Are Available Use Them!

Questions? Comments? Snide Remarks?

Anthony Ferrara Anthony.Ferrara@nbcuni.com ircmaxell@php.net blog.ircmaxell.com @ircmaxell