1 of 29

1

Web Authentication and CSRF

Lecture 24

2 of 29

Web Authentication

Web Authentication

Session Authentication

  • Session Tokens
  • Cookies
  • CSRF
  • Defending CSRF

​

3 of 29

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).

4 of 29

Session Tokens

Web Authentication

Session Authentication

  • Session Tokens
  • Cookies
  • CSRF
  • Defending CSRF

5 of 29

HTTP

HTTP (Hypertext Transfer Protocol) is a client-server protocol that specifies the format of messages used on the web.

  • The request must indicate its type (e.g. "GET," "POST," etc.).
  • The response must indicate a status code (200 OK, 404 Page Not Found, etc.).

HTTP is stateless: Each request is processed independently.

  • If HTTP is stateless, how does the server remember that I am logged in?

Client

Server

GET /index.html HTTP/1.1

200 OK <html>...

6 of 29

Session Tokens

Session: A sequence of requests and responses associated with the same authenticated user.

  • Example: When checking emails, the Gmail server needs a way to know that all the requests are from you.
  • When the session is over (you log out, or the session expires), future requests are not associated with you.

​

Naive solution: Type your username and password with every request.�Very inconvenient and never used.

7 of 29

Session Tokens

Analogy: Imagine attending a concert.

The first time you enter the venue:

  • Present your ticket and ID
  • The doorperson checks your ticket and ID.
  • If they’re valid, you receive a wristband.

If you leave and want to re-enter:

  • Show your wristband to the doorperson.
  • No need to present your ticket and ID again.

The real thing: Session tokens.

When you log in:

  • Present your username and password.
  • The server checks your username and password.
  • If it's valid, you receive a session token.

When you make future requests:

  • Attach the session token in your request.
  • No need to re-enter your password again.

8 of 29

How Session Tokens Work

  1. The user logs in with their username and password.
  2. The server creates a long, random token and associates the token with the user.
  3. The server sends the token to the user.
  4. The user remembers the token for this website.
  5. The user attaches the token to every subsequent HTTP request.
  6. The server uses the database to figure out which user the request is from.

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

9 of 29

Securing Session Tokens

If an attacker steals your session token, they can log in as you!

  • The server thinks any requests with your session token attached come from you.

​

To minimize the risk of this:

  • Session tokens need to be long and random, so an attacker cannot guess them.
  • Browsers need to ensure that tokens do not get sent to the wrong website

​

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.

10 of 29

Cookies

Web Authentication

Session Authentication

  • Session Tokens
  • Cookies
  • CSRF
  • Defending CSRF

11 of 29

Cookies and Session Tokens

HTTP Cookies: a piece of data used to maintain state across multiple requests.

  • Used for many things: dark mode, language, tracking, session authentication, etc.

​

Cookies hold session tokens, but cookies aren't session tokens:�Cookies can hold other things too.

  • Analogy: Cups hold coffee, but cups aren’t coffee:�Cups can hold other things too.

12 of 29

Creating and Sending Cookies

Setting (writing) cookies:

  • If the server includes a Set-Cookie header in a HTTP response, the browser remembers that cookie and associates it to that domain.

Sending (reading) cookies:

  • On every request, the client's browser automatically attaches to each HTTP request all cookies for that domain.
  • JavaScript running on a page in the browser can read all cookies for the same domain.

13 of 29

Session Tokens with Cookies

Session tokens can be implemented directly with cookies.

  • On login: Server sets a cookie holding the session token.
  • On subsequent requests: Client's browser automatically attaches the cookie.

14 of 29

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?

  • Yes, because I'll still be on the same IP address, and Amazon remembers my IP address
  • Yes, because switching to cellular data will switch to a new IP address but keep the same HTTPS (TLS) connection, which is how Amazon remembers me
  • Yes, because I'll get a new IP address and new HTTPS (TLS) connection, but the session cookie will stay the same
  • Yes, because I'll get a new IP address and new HTTPS (TLS) connection and new session cookie, but my browser will automatically resend my saved password

pollev.com/cs161d

15 of 29

CSRF Attacks

Web Authentication

Session Authentication

  • Session Tokens
  • Cookies
  • CSRF
  • Defending CSRF

16 of 29

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.

  • The victim's browser will automatically attach the relevant cookies.
  • The server will think that the request came from the victim!

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.

17 of 29

Steps of a CSRF Attack

  • The user authenticates with the server and receives a valid session token.
  • The attacker tricks the victim into making a malicious HTTP request to the server.
  • The server accepts the malicious request from the victim (with the cookie attached).

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

18 of 29

CSRF: Tricking the Victim

  • The user authenticates with the server and receives a valid session token.
  • The attacker tricks the victim into making a malicious HTTP request to the server.
  • The server accepts the malicious request from the victim (with the cookie attached).

How does the attacker actually trick the victim?

There are two main types of HTTP requests we need to consider:

HTTP GET requests:

  • Intended for requests that don't change the server state.
  • Example: The user visits a URL in their browser.

HTTP POST requests:

  • Intended for requests that change server state.
  • Example: The user clicks the "Submit" button on a form.

Not an enforced rule, e.g. servers can implement GET requests that change server state.

19 of 29

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)

  • https://bank.com/transfer?amount=100&to=Attacker
  • https://attacker.com. Contains some JavaScript to trigger a GET request.

​

Strategy 2: Add some HTML to a website the victim will visit.

  • Example: Make posts with HTML on a forum that the victim frequents.
  • The browser makes a GET request when loading images:�<img src="https://bank.com/transfer?amount=100&to=Attacker">�Image will probably error, but the GET request is still sent. (with cookies!)

20 of 29

Executing a CSRF Attack

How might we trick the victim into making a POST request?

​

Strategy 1: Trick the victim into clicking a link.

  • Note: Clicking the link itself makes a GET, not a POST.
  • But, the link can go to an attacker’s website, which runs JavaScript that makes the malicious POST request.

This is one reason you should implement POST requests for server-changing operations: it makes CSRF attacks harder.

21 of 29

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.

22 of 29

A perspective: ambient authority

An opinion: CSRF is caused by "ambient authority".

  • The browser automatically attaches to each request, some kind of proof that the request is authorized and should be acted on ("take this action, because it comes from a user who has an account with you; here is the session cookie as proof").

​

Ambient authority tends to cause "confused deputy" vulnerabilities.

  • Confused deputy: Attacker tricks some powerful entity to make a request, and the request is allowed because the powerful entity is allowed to do that, even though the attacker wouldn't be allowed to do it directly.
  • With browsers, there's a difference between "I'm sending this message because the human user requested it" vs "I'm sending this message because some other program asked me to", and ambient authority elides this distinction.

23 of 29

CSRF Defenses

Web Authentication

Session Authentication

  • Session Tokens
  • Cookies
  • CSRF
  • Defending CSRF

24 of 29

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).

25 of 29

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

26 of 29

CSRF Defenses: Steps of Using a CSRF Token

  • The user authenticates with the server and receives a valid session token.
  • New Step: The victim loads the page, and the server generates a CSRF token specifically for the victim's next request.
  • The attacker tricks the victim into making a malicious HTTP request to the server.
  • The server rejects the malicious request, because the attacker token is incorrect.

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

27 of 29

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.

  • The server sends the CSRF token as a part of the web page itself, not in cookies.
  • The client sends the CSRF token back in the body of the 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:

  • The attacker can't construct a fake request for the victim, since they don’t have the CSRF token to put in the request body.
  • The attacker can try to fetch their own CSRF token, but it will only be valid for the attacker, not the victim.
  • Put differently: CSRF tokens basically create one-time capability URLs.

​

​

​

​

28 of 29

CSRF Defenses: SameSite Cookie Attribute

A server can tag a cookie with SameSite=Strict attribute.

  • The browser attaches the cookie only if the request domain matches the domain of the origin that triggered the request.
  • Example: If user clicks https://bank.com/delete-account in an email, the browser will not attach the session cookie.

​

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).

29 of 29

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.

  • Example: An attacker who sees you typing in your iPhone passcode.
  • Example: An attacker who sees the session token being sent across the network.

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.