1
Web Authentication and CSRF
Lecture 24
Web Authentication
Web Authentication
Session Authentication
Authentication on the Web
How authentication works on the web:�1. You browser opens a TLS (HTTPS) connection to the web server.�2. You send your username and password to the web server (or otherwise verify your identity), over the TLS-encrypted connection.�3. The web server remembers you, with a "session" (described later).
Session Tokens
Web Authentication
Session Authentication
HTTP
HTTP (Hypertext Transfer Protocol) is a client-server protocol that specifies the format of messages used on the web.
HTTP is stateless: Each request is processed independently.
Client
Server
GET /index.html HTTP/1.1
200 OK <html>...
Session Tokens
Session: A sequence of requests and responses associated with the same authenticated user.
Naive solution: Type your username and password with every request.�Very inconvenient and never used.
Session Tokens
Analogy: Imagine attending a concert.
The first time you enter the venue:
If you leave and want to re-enter:
The real thing: Session tokens.
When you log in:
When you make future requests:
How Session Tokens Work
Server
(gmail.com)
Database
Username "evanbot"�Password "pancake123"
Username | Password |
evanbot | pancake123 |
codabot | waffle999 |
Your token is “a2w76H”
Username | Token | Expires |
codabot | Th3hni | 3 hours |
| | |
Client
(EvanBot)
GET email 1 token="a2w76H"
"Dear Evanbot…"
1
Client Storage
Token | Website |
dFA2Qo | netflix.com |
| |
3
6
Token | Website |
dFA2Qo | netflix.com |
a2w76H | gmail.com |
Username | Token | Expires |
codabot | Th3hni | 3 hours |
evanbot | a2w76H | 12 hours |
4
GET email 2 token="a2w76H"
"Hi Evanbot…"
6
5
2
Repeat until session ends
5
Securing Session Tokens
If an attacker steals your session token, they can log in as you!
To minimize the risk of this:
How does the browser ensure that these tokens end up in the right place?
To answer, we must talk about how the browser actually stores and sends tokens.
Cookies
Web Authentication
Session Authentication
Cookies and Session Tokens
HTTP Cookies: a piece of data used to maintain state across multiple requests.
Cookies hold session tokens, but cookies aren't session tokens:�Cookies can hold other things too.
Creating and Sending Cookies
Setting (writing) cookies:
Sending (reading) cookies:
Session Tokens with Cookies
Session tokens can be implemented directly with cookies.
Poll
If I log into https://amazon.com/ while on Wifi, and then I switch from Wifi to cellular data, will I still be logged on?
pollev.com/cs161d
CSRF Attacks
Web Authentication
Session Authentication
Cross-Site Request Forgery
Recall: The browser automatically attaches session token cookies on every request.
Cross-site request forgery (CSRF) attack: The attacker tricks the victim into making an unintended request.
Victim
Bank
Attacker
"Hey Victim, can you pass this note to Bank?"
Okay.
Successfully transfered $100 from Victim to Attacker.
✉️
✉️
✉️
✉️
Plot twist: The note says "Pay Attacker $100."
The CSRF problem: The note looks like it came from Victim.
Steps of a CSRF Attack
Victim
Server
Attacker
“Hey Victim, can you make this request?”
Okay.
Password updated!
Plot twist: The request says "Change my password to attacker123."
The CSRF problem: The request looks like it came from Victim, because Victim's cookie is attached.
Login
1
Malicious request
3
The trick!
2
CSRF: Tricking the Victim
How does the attacker actually trick the victim?
There are two main types of HTTP requests we need to consider:
HTTP GET requests:
HTTP POST requests:
Not an enforced rule, e.g. servers can implement GET requests that change server state.
Executing a CSRF Attack
How might we trick the victim into making an HTTP GET request?
Strategy 1: Trick the victim into clicking a link. (e.g. a disguised link in an email)
Strategy 2: Add some HTML to a website the victim will visit.
Executing a CSRF Attack
How might we trick the victim into making a POST request?
Strategy 1: Trick the victim into clicking a link.
This is one reason you should implement POST requests for server-changing operations: it makes CSRF attacks harder.
XSS vs CSRF
XSS: Attacker tricks a server to send a dangerous response to browser (containing JS)�CSRF: Attacker tricks a browser to send a dangerous request to server (containing cookies)
Both involve tricking a machine to send a message, which implicitly carries all that machine's weight and authority.��How to get the machine to send the message differs. XSS needs an injection bug. CSRF doesn't need any bug.
A perspective: ambient authority
An opinion: CSRF is caused by "ambient authority".
Ambient authority tends to cause "confused deputy" vulnerabilities.
CSRF Defenses
Web Authentication
Session Authentication
CSRF Defenses: Capability URLs
Capability URLs: the URL to take an impactful action (transfer money, etc.) includes a secret known only to the authorized user.��Example: Google Docs share link��Attacker can't trick victim into visiting the capability URL, because the attacker doesn't know the URL (doesn't know the secret in the URL).
CSRF Defenses: Analogy for CSRF Tokens
Analogy: The victim explicitly asks for a piece of paper from the bank to write the note.
If the attacker tries CSRF, the attacker's fake note won't be on the paper from the bank.
Victim
Bank
Attacker
Hey Victim, can you pass this note to Bank?
I'm gonna transfer some money.
OK, here’s a piece of paper to write on.
📧
✉️
📧
Hmm… that's not the paper I gave Victim.�I shouldn’t accept this.
Okay.
✉️
📧
📧
TODO: the emoji should be replaced with a drawing of paper that explicitly says "BANK" here and no "BANK" when from attacker
CSRF Defenses: Steps of Using a CSRF Token
Victim
Attacker
“Hey Victim, can you make this request?”
Okay.
Hmm… that's not the token I gave Victim. I shouldn't accept this.
The attacker does not know the right token to include.
Login
1
Malicious request
4
The trick!
3
Load page
2
Token=ABCD
Token=????
Token=????
Server
CSRF Defenses: Why CSRF Tokens Work
CSRF Token: Another secret token, separate from the cookie, which the server generates on page-load specifically for the client's next request.
A CSRF token is like the bank paper: A value generated before each request that the client must include for that request to be valid.
Why CSRF tokens defend against attacks:
CSRF Defenses: SameSite Cookie Attribute
A server can tag a cookie with SameSite=Strict attribute.
Problem: 3% of browsers don't support SameSite=Strict. Usability loss (users who arrive on your site via a link from another site appear logged out).
Why Passwords and Session Tokens Aren't Enough
A fundamental problem with both passwords and session tokens:�If an attacker knows the secret, all security is lost.
As a result, passwords are vulnerable to "phishing".
Most web sites still use passwords and session tokens.��Some sites are starting to embrace passkeys, which are basically a per-site private key and a digital signature instead of a password, and are secure against phishing.