cross site scripting (xss) and php securitynyphp.org › resources ›...

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

Upload: others

Post on 27-Jun-2020

32 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Cross Site Scripting (XSS) and PHP Security

Anthony Ferrara

NYPHP and OWASP Security Series

June 30, 2011

Page 2: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

What Is Cross Site Scripting?

Injecting Scripts

Into

Otherwise Benign and Trusted

Browser Rendered Content

Page 3: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Example XSS

$html = ‘<script>alert(“hi”);</script>’;

echo “<b>$html</b>”;

<b><script>alert(“hi”);</script></b>

Page 4: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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)

Page 5: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Before We Talk About XSS, We Need To Talk About:

Filter In, Escape Out

Page 6: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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…

Page 7: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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

Yes.

Page 8: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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

Page 9: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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

Yes.

Page 10: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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”)

Page 11: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Let’s Talk About Escaping First

Page 12: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Never Allow Unfiltered User Input :

• Node Names – <foo />

• Attribute Names – <a bar=“” />

• HTML Comments – <!– Foo

• CSS Identifiers – .baz{foo}

• JS Code – biz();

Page 13: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

You Cannot Escape Content For Those HTML Components!

Page 14: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Values

• <foo>bar</foo>

• Need To Escape The Following Characters:

– & -> &amp;

– “ -> &quot;

– < -> &lgt;

– > -> &gt;

– ‘ -> &#x27;

• Prevents Injection of New Tags

Page 15: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Attribute Values

• Always quote the attribute value

– <foo bar=“baz” />

• Need To Escape The Following Characters:

– & -> &amp;

– “ -> &quot;

– < -> &lgt;

– > -> &gt;

– ‘ -> &#x27;

Page 16: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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

Page 17: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Tools Available In PHP For Escaping

Page 18: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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”)

Page 19: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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 );

Page 20: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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);

Page 21: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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}

Page 22: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Twig

• Templating Engine for PHP

– Similar to Smarty, but cleaner and more powerful

• Does Intelligent Escaping Automatically

• Can be turned off as needed

{{ var }}

Page 23: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Let’s Talk Filtering

Page 24: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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

Page 25: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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

Page 26: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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)

Page 27: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Tools Available In PHP For Filtering

Page 28: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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>’);

Page 29: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

HTMLPurifier

• Library to sanitize HTML

• Very Smart

– Cleans up document structure

– Allows safe attributes

– Highly configurable

• http://htmlpurifier.org/

$purifier->purify($data);

Page 30: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Don’t Roll Your Own!

HTML Sanitization Is Not A Trivial Problem To Solve

Page 31: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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

Page 32: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Demonstration Time!

Page 33: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

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!

Page 34: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Questions?

Comments?

Snide Remarks?

Page 35: Cross Site Scripting (XSS) and PHP Securitynyphp.org › resources › PHP-XSS-OWASP-Security.pdf · Cross Site Scripting (XSS) and PHP Security Anthony Ferrara NYPHP and OWASP Security

Anthony Ferrara

[email protected]

[email protected]

blog.ircmaxell.com

@ircmaxell