cnit 129s: 12: attacking users: cross-site scripting (part 2 of 3)

56
CNIT 129S: Securing Web Applications Ch 12: Attacking Users: Cross-Site Scripting (XSS) Part 2

Upload: sam-bowne

Post on 08-Feb-2017

61 views

Category:

Education


0 download

TRANSCRIPT

Page 1: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

CNIT 129S: Securing Web Applications

Ch 12: Attacking Users:Cross-Site Scripting (XSS)Part 2

Page 2: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Finding and Exploiting XSS Vunerabilities

Page 3: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Basic Approach

• Inject this string into every parameter on every page of the application

• If the attack string appears unmodified in the response, that indicates an XSS vulnerability

• This is the fastest way to find an XSS, but it won't find them all

Page 4: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

When the Simple Attack Fails

• Applications with rudimentary blacklist-based filters

• Remove <script>, or < > " /

• Crafted attacks may still work

Page 5: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Response Different from Input

• XSS attacks that don't simply return the attack string

• Sometimes input string is sanitized, decoded, or otherwise modified

• In DOM-based XSS, the input string isn't necessarily returned in the browser's immediate response, but is retained in the DOM and accessed via client-side JavaScript

Page 6: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Finding and Exploiting Reflected XSS Vulnerabilities

Page 7: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Identifying Reflections of User Input

• Choose a unique string that doesn't appear anyhere in the application and includes only alphabetical characters that won't be filtered, like "myxsstestdmqlwp"

• Submit it as every parameter, one at a time, including GET, POST, query string, and headers such as User-Agent

• Monitor responses for any appearance of the string

Page 8: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Testing Reflections to Introduce Script

• Manually test each instance of reflected input to see if it's exploitable

• You'll have to customize the attack for each situation

Page 9: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

1. A Tag Attribute Value

• Here are two ways to exploit it

Page 10: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Demos (Use Firefox)

Page 11: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

2. A JavaScript String

• This attack works

Page 12: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

3. An Attribute Containing a URL

• Use the javascript: handler to make your script into a URL

• Or use the onclick event handler

Page 13: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Probing Defensive Filters• Three common types

Page 14: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Beating Signature-Based Filters

• You may see an error message like this

Page 15: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Remove Parts of the String

• Until the error goes away

• Find the substring that triggered the error, usually something like <script>

• Test bypass methods

Page 16: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Ways to Introduce Script Code

Page 17: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Script Tags

• If <script> is blocked, try these

Page 18: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
Page 19: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Event Handlers• All these run without user interaction

Page 20: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Event Handlers in HTML 5• Autofocus

• In closing tags

• New tags

Page 21: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Script Pseudo-Protocols• Used where a URL is expected

• IE allows the vbs: protocol

• HTML 5 provides these new ways:

Page 22: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Dynamically Evaluated Styles

• IE 7 and earlier allowed this:

• Later IE versions allow this:

Page 23: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Bypassing Filters: HTML• Ways to obfuscate this attack

Page 24: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Inserted NULL Butes

• Causes C code to terminate the string

• Will bypass many filters

• IE allows NULL bytes anywhere

• Web App Firewalls (WAFs) are typically coded in C for performance and this trick fools them

Page 25: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Invalid Tags

• Browser will let it run

• Filter may not see it due to invalid tag "x"

Page 26: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Base Tag Hijacking

• Set <base> and later relative-path URLs will be resolved relative to it

Page 27: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Space Following the Tag Name• Replace the space with other characters

• Add extra characters when there's no space

Page 28: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

NULL Byte in Attribute Name

• Attribute delimiters

• Backtick works in IE

Page 29: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Attribute Delimiters• If filter is unaware that backticks work as attribute

delimiters, it treats this as a single attribute

• Attack with no spaces

Page 30: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Attribute Values• Insert NULL, or HTML-encode characters

Page 31: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

HTML Encoding• Can use decimal and hexadecimal format, add

leading zeroes, omit trailing semicolon

• Some browsers will accept these

Page 32: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Tag Brackets• Some applications perform URL decoding twice,

so this input

• becomes this, which has no < or >

• and it's then decoded to this

Page 33: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

• Some app frameworks translate unusual Unicode characters into their nearest ASCII equivalents, so double-angle quotation marks %u00AB and %u00BB work:

Tag Brackets

Page 34: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

• Browsers tolerate extra brackets

• This strange format is accepted by Firefox, despite not having a valid <script> tag

Tag Brackets

Page 35: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Web Developer Add-on

• View Generated Source shows HTML after Firefox has tried to "fix" the code

Page 36: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Character Sets

Page 37: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Telling Browser the Character Set

• Set it in the HTTP Content-Type header

• Or an HTTP META tag

• Or a CHARSET parameter, if one is used

Page 38: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Shift-JIS• Suppose two pieces of input are used in the

app's response

• input1 blocks quotes, input2 blocks < and >

• This attack works, because %f0 starts a two-byte character, breaking the quotation mark

Page 39: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Bypassing Filters: Script Code

Page 40: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

JavaScript Escaping• Unicode

• Eval

• Superfluous escape characters

Page 41: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Dynamically Constructing Strings

• Third example works in Firefox

• And in other browsers too, according to link Ch 12f

Page 42: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Alternatives• Alternatives to eval

• Alternatives to dots

Page 43: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Combining Multiple Techniques

• The "e" in "alert" uses Unicode escaping: \u0065

• The backslash is URL-encoded: &#x5c;

• With more HTML-encoding

Page 44: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

VBScript

• Skip this section

• Microsoft abandoned VBScript with Edge

• Link Ch 12g

Page 45: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Beating Sanitization• Encoding certain characters

• < becomes &lt;

• > becomes &gt;

• Test to see what characters are sanitized

• Try to make an attack string without those characters

Page 46: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Examples

• Your injection may already be in a script, so you don't need <script> tag

• Sneak in <script> using layers of encoding, null bytes, nonstandard syntax, or obfuscates scrip code

Page 47: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Mistakes in Sanitizing Code

• Not removing all instances

• Not acting recursively

Page 48: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Stages of Encoding• Filter first strips <script> recursively

• Then strips <object> recursively

• This attack succeeds

Page 49: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Injecting into an Event Handler

• You control foo

• This attack string

• Turns into this, and executes in some browsers

Page 50: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

Beating Length Limits 1. Short Attacks

• This sends cookies to server with hostname a

• This tag executes a script from the server with hostname a

Page 51: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

JavaScript Packer

• Link Ch 12h

Page 52: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

• Use multiple injection points

• Inject part of the code in each point

• Consider this URL

Beating Length Limits 2. Span Multiple Locations

Page 53: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

• It returns three hidden fields

• Inject this way

Beating Length Limits 2. Span Multiple Locations

Page 54: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

• Result

Beating Length Limits 2. Span Multiple Locations

Page 55: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

• Inject this JavaScript, which evaluates the fragment string from the URL

• The part after #

Beating Length Limits 3. Convert Reflected XSS to DOM

Page 56: CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)

• First attack works in a straightforward manner

• Second one works because http: is interpreted as a code label, // as a comment, and %0A terminates the comment

Beating Length Limits 3. Convert Reflected XSS to DOM