Cookies
CS 161 Fall 2021 - Lecture 22
Some content adapted from materials by David Wagner or Dan Boneh
Announcements
Computer Science 161
Exam Preparation Tips
Before the Exam
Memory Safety Tips
Crypto Tips
Cookies
Cookies
A way of maintaining state in the browser
Browser
GET …
Server
Browser maintains cookie jar with all cookies it receives
http response contains
Setting/deleting cookies by server
GET …
HTTP Header:
Set-cookie: NAME=VALUE ;
Server
View a cookie
In a web console (chrome, view->developer->developer tools), type
document.cookie
to see the cookie for that site
Each name=value is one cookie.
document.cookie lists all cookies in scope for document
Cookie scope
GET …
HTTP Header:
Set-cookie🍪:NAME=VALUE ;
domain = (when to send) ;
path = (when to send)
Server
Cookie scope
HTTP Header:
Set-cookie🍪:NAME=VALUE ;
domain = (when to send) ;
path = (when to send)
secure = (only send over HTTPS);
GET …
Server
Cookie scope
GET …
HTTP Header:
Set-cookie🍪:NAME=VALUE ;
domain = (when to send) ;
path = (when to send)
secure = (only send over SSL);
expires = (when expires) ;
HttpOnly
Server
Cookie policy
The cookie policy has two parts:
Cookie scope
What scope a server may set for a cookie
domain: any domain-suffix of URL-hostname, except TLD
example: host = “login.site.com”
⇒ login.site.com can set cookies for all of .site.com � but not for another site or TLD
Problematic for sites like .berkeley.edu
path: can be set to anything
allowed domains
login.site.com
.site.com
disallowed domains
user.site.com
othersite.com
.com
top-level domains
e.g. ‘.com’
The browser checks if the web server may set the cookie, and if not, it will not accept the cookie.
Examples
Credits: The Tangled Web: A Guide to Securing Modern Web Applications, by Michał Zalewski
Whether it will be set
domain
Web server at foo.example.com wants to set cookie with domain:
yes
yes
When browser sends cookie
Browser sends all cookies in URL scope:
GET //URL-domain/URL-path
Cookie: NAME = VALUE
Server
Goal: server only sees cookies in its scope
When browser sends cookie
A cookie with
domain = example.com, and
path = /some/path/
will be included on a request to
http://foo.example.com/some/path/subdirectory/hello.txt
GET //URL-domain/URL-path
Cookie: NAME = VALUE
Server
Examples: Which cookie will be sent?
cookie 1
name = userid
value = u1
domain = login.site.com
path = /
non-secure
cookie 2
name = userid
value = u2
domain = .site.com
path = /
non-secure
http://checkout.site.com/
http://login.site.com/
http://othersite.com/
cookie: userid=u2
cookie: userid=u1, userid=u2
cookie: none
Examples
Credits: The Tangled Web: A Guide to Securing Modern Web Applications, by Michał Zalewski
?
Whether it will be set, and if so, where it will be sent to
domain
Web server at foo.example.com wants to set cookie with domain:
?
?
Examples
Credits: The Tangled Web: A Guide to Securing Modern Web Applications, by Michał Zalewski
Whether it will be set, and if so, where it will be sent to
domain
Web server at foo.example.com wants to set cookie with domain:
Examples
http://checkout.site.com/
http://login.site.com/
https://login.site.com/
cookie 1
name = userid
value = u1
domain = login.site.com
path = /
secure
cookie 2
name = userid
value = u2
domain = .site.com
path = /
non-secure
cookie: userid=u2
cookie: userid=u2
cookie: userid=u1; userid=u2
(arbitrary order)
Client side read/write: document.cookie
document.cookie = “name=value; expires=…; ”
prints string containing all cookies available for document (based on [protocol], domain, path)
document.cookie = “name=; expires= Thu, 01-Jan-00”
document.cookie often used to customize page in Javascript
Viewing/deleting cookies in Browser UI
Firefox: Tools -> page info -> security -> view cookies
Cookie policy versus �same-origin policy
Cookie policy: when browser sends cookie
A cookie with
domain = example.com, and
path = /some/path/
will be included on a request to
http://foo.example.com/some/path/subdirectory/hello.txt
GET //URL-domain/URL-path
Cookie: NAME = VALUE
Server
Cookie policy versus same-origin policy
unless the cookie has the httpOnly flag set.
So there isn’t exact domain match as in same-origin policy, but the cookie policy is invoked instead.
Examples
cookie 1
name = userid
value = u1
domain = login.site.com
path = /
non-secure
cookie 2
name = userid
value = u2
domain = .site.com
path = /
non-secure
http://checkout.site.com/
http://login.site.com/
http://othersite.com/
cookie: userid=u2
cookie: userid=u1, userid=u2
cookie: none
JS on each of these URLs can access the corresponding cookies even if the domains are not the same
Indirectly bypassing same-origin policy using cookie policy
Example
financial.example.com
web server
blog.example.com
web server
(assume attacker compromised this web server)
Victim user browser
financial.example.com
cookies in jar with domain example.com
The browser will send the cookie for financial.example.com to blog.example.com due to domain
blog.example.com
Cookies from:
Example
financial.example.com
web server
blog.example.com
web server
(assume attacker compromised this web server)
Victim user browser
financial.example.com
cookie jar
Browsers maintain a separate cookie jar per domain group, such as one jar for *.example.com to avoid one domain filling up the jar and affecting another domain. Each browser decides at what granularity to hold group domains.
blog.example.com
Cookies from:
(domain:financial.example.com)
Example
financial.example.com
web server
blog.example.com
web server
(assume attacker compromised this web server)
Victim user browser
financial.example.com
cookie jar for *.example.com
blog.example.com
example.com
example.com
GET
set-cookie:
Attacker sets many cookies with domain example.com which overflows the cookie jar for domain *.example.com and overwrites cookies from financial.example.com
Example
financial.example.com
web server
blog.example.com
web server
(assume attacker compromised this web server)
Victim user browser
example.com
cookie jar for *.example.com
example.com
example.com
example.com
Attacker sets many cookies with domain example.com which overflows the cookie jar for domain *.example.com and overwrites cookies from financial.example.com
Example
financial.example.com
web server
Victim user browser
example.com
cookie jar for *.example.com
example.com
example.com
example.com
GET
When Alice visits financial.example.com, the browser automatically attaches the attacker’s cookies due to cookie policy (the scope of the cookies is a domain suffix of financial.example.com)
Why is this a problem?
Indirectly bypassing same-origin policy using cookie policy
RFC6265
https://tools.ietf.org/html/rfc6265
- Browsers are expected to implement this reference, and any differences are browser specific
Session Tokens
Computer Science 161
Fall 2022
Session Authentication
39
Computer Science 161
Fall 2022
Session Authentication: Intuition
40
Computer Science 161
Fall 2022
Session Tokens
41
Computer Science 161
Fall 2022
Session Tokens with Cookies
42
Computer Science 161
Fall 2022
Session Tokens: Security
43
Computer Science 161
Fall 2022
Session Token Cookie Attributes
44
Name | token |
Value | {random value} |
Domain | mail.google.com |
Path | / |
Secure | True |
HttpOnly | True |
Expires | {15 minutes later} |
(other fields omitted) | |
Computer Science 161
Fall 2022
Cross-Site Request Forgery (CSRF)
45
Computer Science 161
Fall 2022
Review: Cookies and Session Tokens
46
Computer Science 161
Fall 2022
Cross-Site Request Forgery (CSRF)
47
Computer Science 161
Fall 2022
Steps of a CSRF Attack
48
Attacker
User
Server
Computer Science 161
Fall 2022
Steps of a CSRF Attack
49
Attacker
User
Server
1. Login
Computer Science 161
Fall 2022
Steps of a CSRF Attack
50
Attacker
User
Server
1. Login
2. Make this request
Computer Science 161
Fall 2022
Steps of a CSRF Attack
51
Attacker
User
Server
1. Login
2. Make this request
3. Malicious request
Computer Science 161
Fall 2022
Steps of a CSRF Attack
52
Computer Science 161
Fall 2022
Executing a CSRF Attack
53
Computer Science 161
Fall 2022
Executing a CSRF Attack
54
Computer Science 161
Fall 2022
Top 25 Most Dangerous Software Weaknesses (2023)
55
Computer Science 161
Fall 2022
CSRF Example: YouTube
56
Computer Science 161
Fall 2022
CSRF Example: Facebook
Takeaway: The HTML image tag can be used to execute a CSRF attack
57
| |
Facebook Hit by Cross-Site Request Forgery Attack | |
Sean Michael Kerner | August 21, 2009 |
Nevertheless, that Facebook accounts were compromised in the wild is noteworthy because the attack used a legitimate HTML tag to violate users’ privacy. According to Zilberman’s disclosure, the attack simply involved the malicious HTML image tag residing on any site, including any blog or forum that permits the use of image tags even in the comments section. "The attack elegantly ends with a valid image so the page renders normally, and the attacked user does not notice that anything peculiar has happened," Zilberman said. | |
Computer Science 161
Fall 2022
CSRF Defenses
58
Computer Science 161
Fall 2022
CSRF Defenses
59
Computer Science 161
Fall 2022
CSRF Tokens
60
Computer Science 161
Fall 2022
CSRF Tokens: Usage
61
Computer Science 161
Fall 2022
CSRF Tokens: Usage
62
Server
1. Login
Attacker
3. Make this request
2. Get token
User
4. Make request
The request in step 4 will fail, because the attacker doesn’t know the token!
Computer Science 161
Fall 2022
Referer Header
63
Computer Science 161
Fall 2022
Referer Header
64
Computer Science 161
Fall 2022
Referer Header: Issues
65
Computer Science 161
Fall 2022
SameSite Cookie Attribute
66
Computer Science 161
Fall 2022
Cookies: Summary
67
Computer Science 161
Fall 2022
Session Authentication: Summary
68
Computer Science 161
Fall 2022
CSRF: Summary
69
Computer Science 161
Fall 2022
CSRF Defenses: Summary
70
Computer Science 161
Fall 2022