Cookies and CSRF
CS 161 Spring 2025 - Lecture 14
Computer Science 161
Last Time: URLs
2
Computer Science 161
Last Time: HTTP
3
Computer Science 161
Last Time: Parts of a Webpage
4
Computer Science 161
Today: Mysteries
5
Computer Science 161
Today: Cookies and CSRF
6
Computer Science 161
Cookies
7
Computer Science 161
Customizing HTTP Responses
8
Computer Science 161
Cookies: Definition
9
Computer Science 161
Parts of a Cookie: Name and Value
10
Name | Theme |
Value | Dark |
Domain | toon.cs161.org |
Path | /xorcist |
Secure | True |
HttpOnly | False |
Expires | 11 Jul 2024 20:00:00 |
(other fields omitted) | |
Computer Science 161
Parts of a Cookie: Domain and Path
11
Name | Theme |
Value | Dark |
Domain | toon.cs161.org |
Path | /xorcist |
Secure | True |
HttpOnly | False |
Expires | 11 Jul 2024 20:00:00 |
(other fields omitted) | |
Computer Science 161
Parts of a Cookie: Secure and HttpOnly
12
Name | Theme |
Value | Dark |
Domain | toon.cs161.org |
Path | /xorcist |
Secure | True |
HttpOnly | False |
Expires | 11 Jul 2024 20:00:00 |
(other fields omitted) | |
Computer Science 161
Parts of a Cookie: Expires
13
Name | Theme |
Value | Dark |
Domain | toon.cs161.org |
Path | /xorcist |
Secure | True |
HttpOnly | False |
Expires | 11 Jul 2024 20:00:00 |
(other fields omitted) | |
Computer Science 161
Cookie Policy
14
Computer Science 161
Cookies: Issues
15
Computer Science 161
Cookie Policy
16
Computer Science 161
Domain Hierarchy
17
. (root)
.edu
.org
.com
google.com
edstem.org
cs161.org
mit.edu
berkeley.edu
Computer Science 161
Domain Hierarchy
18
. (root)
.edu
berkeley.edu
eecs.berkeley.edu
eecs.berkeley.edu is a subdomain of berkeley.edu.
.edu is a top-level domain (TLD), because it is directly below the root of the tree.
Computer Science 161
Cookie Policy: Setting Cookies
19
Computer Science 161
Cookie Policy: Sending Cookies
20
Computer Science 161
Intuition: Domain attributes
21
Computer Science 161
Cookie Policy: Sending Cookies
22
https://toon.cs161.org/cryptoverse/oneshots/subway.html
cs161.org/cryptoverse
(cookie domain)
(cookie path)
(server URL)
Quick method to check cookie sending:
Computer Science 161
Cookie Policy: Sending Cookies
23
https://toon.cs161.org/cryptoverse/oneshots/subway.html
cs161.org/cryptoverse
(cookie domain)
(cookie path)
(server URL)
Quick method to check cookie sending:
Computer Science 161
Cookie Policy: Sending Cookies
24
https://toon.cs161.org/cryptoverse/oneshots/subway.html
cs161.org/cryptoverse
(cookie domain)
(cookie path)
(server URL)
Quick method to check cookie sending: Concatenate the cookie domain and path. Line it up below the requested URL at the first single slash.
If the domains and paths all match, then the cookie is sent.
Computer Science 161
Cookie Policy: Sending Cookies
25
https://toon.cs161.org/cryptoverse/oneshots/subway.html
cs161.org/xorcist
(server URL)
Quick method to check cookie sending: Concatenate the cookie domain and path. Line it up below the requested URL at the first single slash.
If the domain or path doesn’t match, then the cookie is not sent.
(cookie domain)
(cookie path)
Computer Science 161
Cookie Policy: Sending Cookies
26
https://su24.cs161.org/proj2/story
www.cs161.org/proj2
(server URL)
Will a cookie with this domain and path be sent to this server?
(cookie domain)
(cookie path)
Computer Science 161
Cookie Policy: Sending Cookies
27
https://su24.cs161.org/proj2/story
www.cs161.org/proj2
(server URL)
Will a cookie with this domain and path be sent to this server?
No! The domain does not match!
(cookie domain)
(cookie path)
Computer Science 161
Attacks on Cookies
28
Computer Science 161
Cookie Ambiguity
29
Computer Science 161
Spectre Attack: Vulnerability
30
Computer Science 161
Spectre Attack: Exploiting browser design
31
Computer Science 161
Spectre Attack: Exploiting the processor
32
Computer Science 161
Spectre Attack: Exploiting the processor
33
i = 0
while i <= 1000:
if i <= 1000:
[legal things]
else:
[illegal things]
i += 1
Speculative execution: The else case never runs, but the predictor will try to execute it after the last run of the loop
Computer Science 161
Spectre Attack: Defenses
34
Computer Science 161
Spectre Attack: Takeaways
35
Computer Science 161
Session Authentication
36
Computer Science 161
Session Authentication
37
Computer Science 161
Session Authentication: Intuition
38
Computer Science 161
Session Tokens
39
Computer Science 161
Session Tokens with Cookies
40
Computer Science 161
The Basic Idea: Intuition (A Close Approximation)
41
Computer Science 161
Session Tokens: Security
42
Computer Science 161
Session Token Cookie Attributes
43
Name | token |
Value | {random value} |
Domain | mail.google.com |
Path | / |
Secure | True |
HttpOnly | True |
Expires | {15 minutes later} |
(other fields omitted) | |
Computer Science 161
Cross-Site Request Forgery (CSRF)
44
Computer Science 161
Review: Cookies and Session Tokens
45
Computer Science 161
Cross-Site Request Forgery (CSRF)
46
Computer Science 161
Analogy: Passing Notes to My Banker
47
Computer Science 161
Steps of a CSRF Attack
48
Attacker
User
Server
Computer Science 161
Steps of a CSRF Attack
49
Attacker
User
Server
1. Login
Computer Science 161
Steps of a CSRF Attack
50
Attacker
User
Server
1. Login
2. Make this request
Computer Science 161
Steps of a CSRF Attack
51
Attacker
User
Server
1. Login
2. Make this request
3. Malicious request
Computer Science 161
Steps of a CSRF Attack
52
Computer Science 161
Executing a CSRF Attack
53
Computer Science 161
Executing a CSRF Attack
54
Computer Science 161
Top 25 Most Dangerous Software Weaknesses (2020)
55
Rank | ID | Name | Score |
[1] | Improper Neutralization of Input During Web Page Generation (’Cross-site Scripting’) | 46.82 | |
[2] | Out-of-bounds Write | 46.17 | |
[3] | Improper Input Validation | 33.47 | |
[4] | Out-of-bounds Read | 26.50 | |
[5] | Improper Restriction of Operations within the Bounds of a Memory Buffer | 23.73 | |
[6] | Improper Neutralization of Special Elements used in an SQL Command (’SQL Injection’) | 20.69 | |
[7] | Exposure of Sensitive Information to an Unauthorized Actor | 19.16 | |
[8] | Use After Free | 18.87 | |
[9] | Cross-Site Request Forgery (CSRF) | 17.29 | |
[10] | Improper Neutralization of Special Elements used in an OS Command (’OS Command Injection’) | 16.44 | |
[11] | Integer Overflow or Wraparound | 15.81 | |
[12] | Improper Limitation of a Pathname to a Restricted Directory (’Path Traversal’) | 13.67 | |
[13] | NULL Pointer Dereference | 8.35 | |
[14] | Improper Authentication | 8.17 | |
[15] | Unrestricted Upload of File with Dangerous Type | 7.38 | |
[16] | Incorrect Permission Assignment for Critical Resource | 6.95 | |
[17] | Improper Control of Generation of Code (’Code Injection’) | 6.53 |
Computer Science 161
CSRF Example: Internet of Things (IoT)
56
Computer Science 161
CSRF Example: Malvertising
57
Computer Science 161
CSRF Example: YouTube
58
Computer Science 161
CSRF Example: Facebook
Takeaway: The HTML image tag can be used to execute a CSRF attack
59
| |
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
CSRF Defenses
60
Computer Science 161
CSRF Defenses
61
Computer Science 161
CSRF Tokens
62
Computer Science 161
CSRF Tokens: Usage
63
Computer Science 161
CSRF Tokens: Usage
64
Server
1. Login
Attacker
3. Make this request with attacker token
2. Get token
User
4. Make request
The request in step 4 will fail, because the attacker doesn’t know the token!
token in form
Computer Science 161
Referer Header
65
Computer Science 161
Referer Header
66
Computer Science 161
Referer Header: Issues
67
Computer Science 161
SameSite Cookie Attribute
68
Computer Science 161
Cookies: Summary
69
Computer Science 161
Session Authentication: Summary
70
Computer Science 161
CSRF: Summary
71
Computer Science 161
CSRF Defenses: Summary
72
Computer Science 161