Cookies and CSRF
CS 161 - Lecture 13
Computer Science 161
Announcements
2
Computer Science 161
Last Time: URLs
3
Computer Science 161
Last Time: Parts of a Webpage
4
Computer Science 161
Last Time: Same-Origin Policy
5
Computer Science 161
Last Time: HTTP
6
Computer Science 161
Today: Cookies and CSRF
7
Computer Science 161
Cookies
8
Computer Science 161
Customizing HTTP Responses
9
Computer Science 161
Cookies: Definition
10
Computer Science 161
Parts of a Cookie: Name and Value
11
Name | Theme |
Value | Dark |
Domain | toon.cs161.org |
Path | /xorcist |
Secure | True |
HttpOnly | False |
Expires | 12 Aug 2021 20:00:00 |
(other fields omitted) | |
Computer Science 161
Parts of a Cookie: Domain and Path
12
Name | Theme |
Value | Dark |
Domain | toon.cs161.org |
Path | /xorcist |
Secure | True |
HttpOnly | False |
Expires | 12 Aug 2021 20:00:00 |
(other fields omitted) | |
Computer Science 161
Parts of a Cookie: Secure and HttpOnly
13
Name | Theme |
Value | Dark |
Domain | toon.cs161.org |
Path | /xorcist |
Secure | True |
HttpOnly | False |
Expires | 12 Aug 2021 20:00:00 |
(other fields omitted) | |
Computer Science 161
Parts of a Cookie: Expires
14
Name | Theme |
Value | Dark |
Domain | toon.cs161.org |
Path | /xorcist |
Secure | True |
HttpOnly | False |
Expires | 12 Aug 2021 20:00:00 |
(other fields omitted) | |
Computer Science 161
Cookie Policy
15
Computer Science 161
Cookies: Issues
16
Computer Science 161
Cookie Policy
17
Computer Science 161
Domain Hierarchy
18
. (root)
.edu
.org
.com
google.com
piazza.com
cs161.org
mit.edu
berkeley.edu
Computer Science 161
Domain Hierarchy
19
. (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
20
Computer Science 161
Cookie Policy: Sending Cookies
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: 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
23
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
Attacks on Cookies
24
Computer Science 161
Cookie Ambiguity
25
Computer Science 161
Spectre Attack: Vulnerability
26
Computer Science 161
Spectre Attack: Exploiting browser design
27
Computer Science 161
Spectre Attack: Exploiting the processor
28
Computer Science 161
Spectre Attack: Exploiting the processor
29
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
30
Computer Science 161
Spectre Attack: Takeaways
31
Computer Science 161
Session Authentication
32
Computer Science 161
Session Authentication
33
Computer Science 161
Session Authentication: Intuition
34
Computer Science 161
Session Tokens
35
Computer Science 161
Session Tokens with Cookies
36
Computer Science 161
Session Tokens: Security
37
Computer Science 161
Session Token Cookie Attributes
38
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)
39
Break: Back at 4:32 PT
Computer Science 161
Review: Cookies and Session Tokens
40
Computer Science 161
Cross-Site Request Forgery (CSRF)
41
Computer Science 161
Steps of a CSRF Attack
42
Attacker
User
Server
1. Login
2. Make this request
3. Malicious request
Computer Science 161
Steps of a CSRF Attack
43
Computer Science 161
Executing a CSRF Attack
44
Computer Science 161
Executing a CSRF Attack
45
Computer Science 161
Top 25 Most Dangerous Software Weaknesses (2020)
46
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)
47
Computer Science 161
CSRF Example: Malvertising
48
Computer Science 161
CSRF Example: YouTube
49
Computer Science 161
CSRF Example: Facebook
Takeaway: The HTML image tag can be used to execute a CSRF attack
50
| |
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
51
Computer Science 161
CSRF Defenses
52
Computer Science 161
CSRF Tokens
53
Computer Science 161
CSRF Tokens: Usage
54
Computer Science 161
CSRF Tokens: Usage
55
4. Make request
Attacker
Server
1. Login
3. Make this request
2. Get token
User
The request in step 4 will fail, because the attacker doesn’t know the token!
Computer Science 161
Referer Header
56
Computer Science 161
Referer Header
57
Computer Science 161
Referer Header: Issues
58
Computer Science 161
SameSite Cookie Attribute
59
Computer Science 161
Cookies: Summary
60
Computer Science 161
Session Authentication: Summary
61
Computer Science 161
CSRF: Summary
62
Computer Science 161
CSRF Defenses: Summary
63
Computer Science 161