Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work...

75
Token Up Token Up Keeping Hands out of the Keeping Hands out of the Cookie Jar Cookie Jar Erin Browning Erin Browning 1

Transcript of Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work...

Page 1: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Token UpToken UpKeeping Hands out of theKeeping Hands out of the

Cookie JarCookie JarErin BrowningErin Browning

1

Page 2: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Slides are available at:

https://www.frowning.wtf/token-up

2

Page 3: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

$whoami$whoami

3 . 1

Page 4: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Erin Browning

Senior Security Engineer

a.k.a., I'm a hacker

3 . 2

Page 6: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

3 . 4

Page 7: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

3 . 5

Page 8: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Slack is hiring!Slack is hiring!Slack is used by millions of people every day – we needengineers who want to make that experience as secure

and enjoyable as possible.

slack.com/jobs

3 . 6

Page 9: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

All of the code-blocked frowning.wtf URLs arefake.

Please don't attack my website.

4

Page 10: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

5

Page 11: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

The ProblemThe Problem

6 . 1

Page 12: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

I'm an attacker.

I want to take over accounts on your website.

6 . 2

Page 13: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Your site probably looks like this:

API: www.frowning.wtf/api

Frontend client: www.frowning.wtf

Mobile clients

6 . 3

Page 14: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Your sessions probably look like one or more of these:

JWTsRandomized session tokenAPI tokens

6 . 4

Page 15: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

As you break your website out from a monolith tomicroservices, how do you store sessions/tokensbetween an API and your browser-based frontend

client?

6 . 5

Page 16: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Auth0's answer:

6 . 6

Page 17: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Is storing your auth in local storage that bad?

Let's find out.

6 . 7

Page 18: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

In this talk:In this talk:Common vulnerabilities that execute in a browserModern application structuresHow to take advantage of browser-basedprotections

7 . 1

Page 19: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

1UP1UP

7 . 2

Page 20: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Two common attacks

8 . 1

Page 21: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

8 . 2

Page 22: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

What is XSS?

Cross site scripting

8 . 3

Page 23: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Attacker created javascript is executed in the user'sbrowser in the context of site the user visited

More at: owasp.org/index.php/Cross-site_Scripting_(XSS)

8 . 4

Page 24: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

How?

Injection!

8 . 5

Page 25: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Cannonical testing example:<script>alert(1);</script>

8 . 6

Page 26: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

What can you do with XSS?

Steal cookiesTake actions as the userLike changing passwordsChange page content

8 . 7

Page 27: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

8 . 8

Page 28: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

What is CSRF?

Cross-Site Request ForgeryMore at: owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)

8 . 9

Page 29: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Forces a user to perform actions they didn't intend ona website to which they're authenticated

8 . 10

Page 30: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

How?

By default, cookies are included in requests sent crossdomain.

8 . 11

Page 31: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta
Page 32: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

8 . 12

Page 33: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

8 . 13

Page 34: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

What do these attacks have in common?

8 . 14

Page 35: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

All of these attacks execute in the user's browser

8 . 15

Page 36: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

How are these attacks different?

8 . 16

Page 37: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

XSS > CSRF

8 . 17

Page 38: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

8 . 18

Page 39: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

How can we reduce the impact of these attacks?

9 . 1

Page 40: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

9 . 2

Page 41: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

First, let's talk about a typical application structure:www.frowning.wtf - contains your frontend +any monolith codewww.frowning.wtf/api - apiwww.frowning.wtf/admin - administrator site

10 . 1

Page 42: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

10 . 2

Page 43: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Where is your authentication stored in the browser?

10 . 3

Page 44: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Probably in a cookie

That cookie is probably scoped to *.frowning.wtf

If not, it'll be in local storage, placed there by yourjavascript

10 . 4

Page 45: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Interactions are going through XHR to /apiFor those of you who don't do frontend work:

XHR is an API called XMLHttpRequest.It lets you transfer data between a web browser running JS

and a server without reloading the page.

10 . 5

Page 46: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Traditional CSRF protection stores a random token in aform in an HTML page.

That token gets stored on the server as well.

When the form is submitted, the token is sent with theform data and validated on the server.

10 . 6

Page 47: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Your API may be using a CSRF token, or it may just berelying on monolith form CSRF protection--aka, your

api may be vulnerable.

10 . 7

Page 48: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Improvement: use subdomains

11 . 1

Page 49: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Now you have:

api.frowning.wtfwww.frowning.wtfadmin.frowning.wtf

11 . 2

Page 50: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

You can scope cookies to www, admin and api insteadof using *.

The API cookie can have the secure and HTTPonly flagsset.

Secure means that cookie will only be sent over HTTPS

HTTPonly means js can't touch it

Yes, the names are confusing, so remember: for HTTPonly, only HTTP requests can accessthe cookie.

11 . 3

Page 51: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

You XHR your requests to api from www.

11 . 4

Page 52: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

How do you even do CSRF protection to your API?

11 . 5

Page 53: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Depends on ~content types~

multipart/form-datatext/plainapplication/x-www-url-form-encodedapplication/jsonapplication/xml

11 . 6

Page 54: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

multipart/form-data, can go cross origintext/plain, can go cross originapplication/x-www-url-form-encoded,can go cross originapplication/json, can't go cross origin withoutCORSapplication/xml, can't go cross origin withoutCORS

11 . 7

Page 55: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

What is CORS?

11 . 8

Page 56: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

We care about CORS because of the protection offeredby the Same Origin Policy (SOP).

11 . 9

Page 57: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

What is the Same Origin Policy?

Lots of requests can't be made from URL1 to URL2 ifthey differ on the following things:

Protocol (e.g., HTTP vs HTTPS)PortHost

11 . 10

Page 58: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

CORS must be set on the assets you are accessing.

11 . 11

Page 59: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

How do you reduce the impact of XSS?

API isn't running js.

www could still be vulnerable, and the site could sendrequests through XHR.

11 . 12

Page 60: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Improvement: use iFrames

12 . 1

Page 61: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Instead of using CORS, create an iFrame on www toapi.

Use the window.postMessage APIdocs at developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

12 . 2

Page 62: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

window.postMessage() enables cross-origincommunication through DOM-based

Windows can send and receive messages from eachother through events.

events.

12 . 3

Page 63: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

12 . 4

Page 64: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

ALWAYS ALWAYS ALWAYS validate your origin

if (event.origin !== www.frowning.wtf)return;

// .. otherwise do some stuff

12 . 5

Page 65: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Improvement: use websockets

13 . 1

Page 66: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Verify your Origin header.

13 . 2

Page 67: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

The attacker would need to fake the origin header inthe victim's browser.

Modern browsers don't let you set your own originheader.

13 . 3

Page 68: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Common problems:

Sites don't check auth before upgrading theconnection. The protocol upgrade request will haveaccess to the browser's cookies. Therefore, checkauth when upgrading.

13 . 4

Page 69: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Similarities

iFrames and websockets both have trustworthy originsin the browser.

14 . 1

Page 70: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Ultimately, how can you avoid CSRF hitting your API?

By dropping all requests to API that aren'tapplication/json

14 . 2

Page 71: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

How can you avoid XSS?

You can't completely.

14 . 3

Page 72: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Should you store auth in local storage?

Not unless you're sure you can prevent XSS.

14 . 4

Page 73: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

How do you know you have a good understanding ofall this?

Tell me why XSS is worse in all these cases.

14 . 5

Page 74: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

Special ThanksSpecial Thankslvh @latacora

Leigh Honeywell @tallpoppy

The latacora team

The Product Security teams @Slack

15 . 1

Page 75: Token Up - USENIX are going through XHR to /api For t h os e of you wh o d on' t d o f rontend work : XHR i s a n A P I ca l l ed XMLHttpRequest. I t l et s you t ra ns fer d a ta

see you on the internet bb

16