Top Banner
Web Security: Session Management CS155 *Original slides were created by Prof. Dan Boneh
38

Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Mar 29, 2018

Download

Documents

dotu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Web Security: Session Management

CS155

*OriginalslideswerecreatedbyProf.DanBoneh

Page 2: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Sameoriginpolicy:reviewReview:SameOriginPolicy(SOP)forDOM:

– OriginAcanaccessoriginB’sDOMifmatchon (scheme,domain,port)

Thislecture:SameOriginalPolicy(SOP)forcookies:

–  Basedon:([scheme],domain,path)

optional

scheme://domain:port/path?params

Page 3: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

scope

Setting/deleting cookies by server

Default scope is domain and path of setting URL

BrowserServer

GET …

HTTP Header: Set-cookie: NAME=VALUE ;

domain = (when to send) ; path = (when to send) secure = (only send over SSL); expires = (when expires) ; HttpOnly

ifexpires=NULL:thissessiononly

ifexpires=pastdate:browserdeletescookie

Page 4: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Scope setting rules (write SOP) domain:anydomain-suffixofURL-hostname,exceptTLD

example:host=“login.site.com”

⇒ login.site.comcansetcookiesforallof.site.combutnotforanothersiteorTLD

ProblemaQcforsiteslike.stanford.edu(andsomehosQngcenters)

path:canbesettoanything

allowed domains login.site.com

.site.com

disallowed domains other.site.com othersite.com

.com

Page 5: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Cookies are identified by (name,domain,path)

Both cookies stored in browser’s cookie jar both are in scope of login.site.com

cookie 1 name = userid value = test domain = login.site.com path = / secure

cookie 2 name = userid value = test123 domain = .site.com path = / secure

disQnctcookies

Page 6: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Reading cookies on server (read SOP)

BrowsersendsallcookiesinURLscope:

•  cookie-domainisdomain-suffixofURL-domain,and

•  cookie-pathisprefixofURL-path,and•  [protocol=HTTPSifcookieis“secure”]

Goal:serveronlyseescookiesinitsscope

BrowserServerGET//URL-domain/URL-path

Cookie:NAME=VALUE

Page 7: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Examples

hZp://checkout.site.com/hZp://login.site.com/hZps://login.site.com/

cookie1name=useridvalue=u1domain=login.site.compath=/secure

cookie2name=useridvalue=u2domain=.site.compath=/non-secure

both set by login.site.com

cookie:userid=u2cookie:userid=u2cookie:userid=u1;userid=u2

Page 8: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Clientsideread/write:document.cookie

Se?ngacookieinJavascript: document.cookie=“name=value;expires=…;”

Readingacookie: alert(document.cookie) printsstringcontainingallcookiesavailablefor

document(basedon[protocol],domain,path)

DeleCngacookie: document.cookie=“name=;expires=Thu,01-Jan-70”

document.cookieobenusedtocustomizepageinJavascript

Page 9: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

javascript: alert(document.cookie)

Javascript URL

Displays all cookies for current document

Page 10: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Viewing/deleting cookies in Browser UI

Page 11: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Cookieprotocolproblems

Page 12: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Cookie protocol problems Serverisblind:

–  DoesnotseecookieaZributes(e.g.secure,HZpOnly)–  Doesnotseewhichdomainsetthecookie

Serveronlysees:Cookie:NAME=VALUE

Page 13: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Example 1: login server problems 1.  Alicelogsinatlogin.site.com login.site.comsetssession-idcookiefor.site.com

2.Alicevisitsevil.site.com overwrites.site.comsession-idcookie

withsession-idofuser“badguy”

3.Alicevisitscourse.site.comtosubmithomework course.site.comthinksitistalkingto“badguy”

Problem:course.site.comexpectssession-idfromlogin.site.com; cannottellthatsession-idcookiewasoverwriZen

Page 14: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Example 2: “secure” cookies are not secure

AlicelogsinathNps://accounts.google.com

AlicevisitshNp://www.google.com(cleartext)•  NetworkaZackercaninjectintoresponse

Set-Cookie:SSID=badguy;secureandoverwritesecurecookie

Problem:networkaZackercanre-writeHTTPScookies! ⇒HTTPScookievaluecannotbetrusted

set-cookie:SSID=A7_ESAgDpKYk5TGnf;Domain=.google.com;Path=/; Expires=Wed,09-Mar-202318:35:11GMT;Secure;HNpOnly

set-cookie:SAPISID=wj1gYKLFy-RmWybP/ANtKMtPIHNambvdI4;Domain=.google.com;Path=/; Expires=Wed,09-Mar-202318:35:11GMT;Secure

Page 15: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Interaction with the DOM SOP CookieSOPpathseparaQon: x.com/Adoesnotseecookiesofx.com/B

Notasecuritymeasure:x.com/AhasaccesstoDOMofx.com/B

<iframesrc=“x.com/B"></iframe>

alert(frames[0].document.cookie);

PathseparaQonisdoneforefficiencynotsecurity: x.com/Aisonlysentthecookiesitneeds

Page 16: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Cookies have no integrity Usercanchangeanddeletecookievalues

•  Editcookiedatabase(FF:cookies.sqlite)•  ModifyCookieheader(FF:TamperDataextension)

Sillyexample:shoppingcartsobware Set-cookie: shopping-cart-total=150($)

Usereditscookiefile(cookiepoisoning): Cookie: shopping-cart-total=15($)

Similarproblemwithhiddenfields<INPUTTYPE=“hidden”NAME=priceVALUE=“150”>

16

Page 17: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

17

Not so silly … (as of 2/2000)

•  D3.COM Pty Ltd: ShopFactory 5.8 •  @Retail Corporation: @Retail •  Adgrafix: Check It Out •  Baron Consulting Group: WebSite Tool •  ComCity Corporation: SalesCart •  Crested Butte Software: EasyCart •  Dansie.net: Dansie Shopping Cart •  Intelligent Vending Systems: Intellivend •  Make-a-Store: Make-a-Store OrderPage •  McMurtrey/Whitaker & Associates: Cart32 3.0 •  [email protected]: CartMan 1.04 •  Rich Media Technologies: JustAddCommerce 5.0 •  SmartCart: SmartCart •  Web Express: Shoptron 1.2

Source:hZp://xforce.iss.net/xforce/xfdb/4621

Page 18: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Solution: cryptographic checksums

Bindingtosession-id(SID)makesithardertoreplayoldcookies

Goal:dataintegrityRequiresserver-sidesecretkeykunknowntobrowser

BrowserServer k Set-Cookie:NAME= value T

Cookie:NAME= value T

Generate tag: T ← MACsign(k, SID ll name ll value )

Verify tag: MACverify(k, SID ll name ll value, T)

Page 19: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

19

Example: ASP.NET

System.Web.ConfiguraQon.MachineKey–  SecretwebserverkeyintendedforcookieprotecQon

CreaQnganencryptedcookiewithintegrity:

HZpCookiecookie=newHZpCookie(name,val);HZpCookieencodedCookie= HNpSecureCookie.Encode(cookie);

DecrypQngandvalidaQnganencryptedcookie:

HNpSecureCookie.Decode(cookie);

Page 20: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

SessionManagement

Page 21: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Sessions Asequenceofrequestsandresponsesfromonebrowsertoone(ormore)sites

–  Sessioncanbelong(e.g.Gmail)orshort–  withoutsessionmgmt:

userswouldhavetoconstantlyre-authenQcate

Sessionmgmt:authorizeuseronce;–  AllsubsequentrequestsareQedtouser

Page 22: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Pre-history: HTTP auth HTTP request: GET /index.html

HTTP response contains: WWW-Authenticate: Basic realm="Password Required“ Browsers sends hashed password on all subsequent HTTP requests: Authorization: Basic ZGFddfibzsdfgkjheczI1NXRleHQ=

Page 23: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

HTTP auth problems Hardlyusedincommercialsites:

•  Usercannotlogoutotherthanbyclosingbrowser– WhatifuserhasmulQpleaccounts?mulQpleusersonsamemachine?

•  Sitecannotcustomizepassworddialog

•  Confusingdialogtousers

•  Easilyspoofed

Page 24: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Session tokens Browser

GET/index.html

setanonymoussessiontoken

GET/books.htmlanonymoussessiontoken

POST/do-loginUsername&password

elevatetoalogged-insessiontoken

POST/checkoutlogged-insessiontoken

checkcredenCals(crypto)

Validatetoken

website

Page 25: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Storing session tokens: Lots of options (but none are perfect)

Browsercookie: Set-Cookie:SessionToken=fduhye63sfdb

EmbedinallURLlinks: hZps://site.com/checkout?SessionToken=kh7y3b

Inahiddenformfield: <inputtype=“hidden” name=“sessionid” value=“kh7y3b”>

Page 26: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Storing session tokens: problems

Browsercookie:browsersendscookiewitheveryrequest,evenwhenitshouldnot(CSRF)

EmbedinallURLlinks:tokenleaksviaHTTPRefererheader

Inahiddenformfield:doesnotworkforlong-livedsessions

Bestanswer:acombinaQonofalloftheabove.

(orifuserpostsURLinapublicblog)

Page 27: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

The HTTP referer header

RefererleaksURLsessiontokento3rdparQesReferersupression:•  notsentwhenHTTPSsitereferstoanHTTPsite•  inHTML5:<a rel=”noreferrer” href=www.example.com>

Page 28: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

The Logout Process WebsitesmustprovidealogoutfuncQon:•  FuncQonality:letusertologinasdifferentuser•  Security:preventothersfromabusingaccount

Whathappensduringlogout:1.DeleteSessionTokenfromclient2.Marksessiontokenasexpiredonserver

Problem:manywebsitesdo(1)butnot(2)!!⇒EspeciallyriskyforsiteswhofallbacktoHTTPaberlogin

Page 29: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Sessionhijacking

Page 30: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

SessionhijackingAZackerwaitsforusertologin

thenaZackerstealsuser’sSessionTokenand“hijacks” session

⇒aZackercanissuearbitraryrequestsonbehalfofuser

Example:FireSheep[2010]

FirefoxextensionthathijacksFacebooksessiontokensoverWiFi.SoluQon:HTTPSaberlogin

Page 31: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Beware: Predictable tokens

Example1:counter

⇒userlogsin,getscountervalue,

canviewsessionsofotherusers

Example2:weakMAC.token={userid,MACk(userid)}•  WeakMACexposeskfromfewcookies.

ApacheTomcat:generateSessionId()•  ReturnsrandomsessionID[serverretrievesclientstatebasedonsess-id]

Page 32: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

SessiontokensmustbeunpredictabletoaZacker

Togenerate:useunderlyingframework(e.g.ASP,Tomcat,Rails)

Rails:token=MD5(currentQme,randomnonce)

Page 33: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Beware:SessiontokenthebExample1:loginoverHTTPS,butsubsequentHTTP•  EnablescookiethebatwirelessCafé(e.g.Firesheep)•  OtherwaysnetworkaZackercanstealtoken:

–  SitehasmixedHTTPS/HTTPpages⇒tokensentoverHTTP– Man-in-the-middleaZacksonSSL

Example2:CrossSiteScripQng(XSS)exploits

Amplifiedbypoorlogoutprocedures:–  Logoutmustinvalidatetokenonserver

Page 34: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Mitigating SessionToken theft by binding SessionToken to client’s computer

ClientIPaddr:makesithardertousetokenatanothermachine–  ButhonestclientmaychangeIPaddrduringsession

•  clientwillbeloggedoutfornoreason.

Clientuseragent:weakdefenseagainsttheb,butdoesn’thurt.

SSLsessionid:sameproblemasIPaddress(andevenworse)

Acommonidea:embedmachinespecificdatainSID

Page 35: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

SessionfixaQonaZacksSupposeaZackercansettheuser’ssessiontoken:•  ForURLtokens,trickuserintoclickingonURL•  Forcookietokens,setusingXSSexploitsAZack:(say,usingURLtokens)

1.  AZackergetsanonymoussessiontokenforsite.com

2.  SendsURLtouserwithaZacker’ssessiontoken3.  UserclicksonURLandlogsintosite.com

–  thiselevatesaZacker’stokentologged-intoken

4.  AZackeruseselevatedtokentohijackuser’ssession.

Page 36: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

SessionfixaQon:lesson When elevating user from anonymous to logged-in:

always issue a new session token

After login, token changes to value unknown to attacker

⇒ Attacker’s token is not elevated.

Page 37: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

Summary

•  Alwaysassumecookiedataretrievedfromclientisadversarial

•  SessiontokensaresplitacrossmulQpleclientstatemechanisms:–  Cookies,hiddenformfields,URLparameters–  Cookiesbythemselvesareinsecure(CSRF,cookieoverwrite)–  SessiontokensmustbeunpredictableandresistthebbynetworkaZacker

•  Ensurelogoutinvalidatessessiononserver

Page 38: Web Security: Session Management - cs.columbia.edusuman/6183_slides/sess_mgmt.pdfeven when it should not (CSRF) Embed in all URL links: token leaks via HTTP Referer header In a hidden

THEEND