UnixDroid

Lesson 2 of 5 · 7 min

Cookies and sessions

How websites remember that you logged in, and why stealing one cookie can be as good as stealing a password.

HTTP has no memory. Each request arrives on its own, with no idea that you logged in a second ago. Websites fix this with sessions, and sessions are carried by cookies.

How a login session is created and checked using a cookie
How a login session is created and checked using a cookie

See your own cookies

In Chrome, press F12, open the Application tab, then Cookies in the left panel. In Firefox it's the Storage tab. Log in to any site and you'll see a cookie with a long random value. That value is your session ID.

The security flags

FlagWhat it does
HttpOnlyJavaScript can't read the cookie, so XSS can't steal it easily
SecureOnly sent over HTTPS, never plain HTTP
SameSite=Lax or StrictNot sent on most requests from other websites, which helps against CSRF
Expires / Max-AgeWhen the cookie dies

A session cookie without HttpOnly and Secure is one of the most common findings in web tests.

How sessions get attacked

  • Session hijacking: the attacker steals the cookie, often through XSS or an unencrypted connection, and pastes it into their own browser. No password needed.
  • Session fixation: the attacker makes the victim use a session ID the attacker already knows, then waits for them to log in.
  • Never-ending sessions: sessions that don't expire give stolen cookies a long life.

What good apps do

  • Create a new session ID right after login, which kills fixation.
  • Destroy the session on the server when you log out, not just in the browser.
  • Expire idle sessions after a reasonable time.
  • Set HttpOnly, Secure and SameSite on every session cookie.

When a site has a "log out of all devices" button, it is deleting every session it has stored for you on the server.

Try it yourself
  1. Log in to any website you use, open DevTools and find the session cookie. Which flags does it have?
  2. In your lab, log in to DVWA at http://192.168.56.102/dvwa/ (admin / password) and check its PHPSESSID cookie. Which flags are missing?
No Linux machine? Practise in the free online terminal

Quick check

Which cookie flag stops JavaScript from reading a cookie?

Need a hint?

Only HTTP may touch it.

Finished this lesson?

Done lessons keep your daily streak going 🔥

0 day streak

0 lessons done

best 0 days

Log in to keep your streak on every device →