Same session ID after login? How to stop session fixation

In session fixation, the attacker plants a session ID in your browser before you log in, then uses it. Per OWASP, your app must renew the session ID after any privilege change, above all at login.

Published 2 min read#App security#Web development

The attacker did not steal your password, and did not steal your cookie. They gave you the cookie before you logged in, and waited. That is session fixation, and by the end of this article you will know the line that closes it and where it has to go.

What is session fixation?

Session fixation is when an attacker sets a session ID in the victim's browser, instead of trying to steal the victim's ID, and once the victim logs in, that same ID opens the account for the attacker.

The session ID is the value in the cookie that tells the server who you are on every request. Think of a hotel wristband: if someone puts a wristband on you and keeps a copy, and the wristband is then linked to your room, they now hold your room key.

Why does it happen?

It happens when your app keeps the same session ID it had before login, and only adds the fact that the user is now logged in.

The mistake is human: the login code checks the password, writes user_id into the existing session, and moves on. Nobody thought the ID itself might have come from someone else, through a link or a cookie set earlier.

The OWASP Session Management Cheat Sheet splits apps into two kinds: a permissive kind that accepts any session ID the user sends, and a strict kind that accepts only IDs it created. And it says it plainly: an app should never accept a session ID it has never generated.

What closes it?

What closes it is your app renewing the session ID after any privilege level change, and the most important moment is login. The ID the attacker holds then becomes an empty ID with no account behind it.

The same cheat sheet names the functions on each platform:

Platform

What to use

PHP

session_start() and session_regenerate_id(true)

Java (J2EE)

HttpSession.invalidate() and request.getSession(true)

ASP.NET

Session.Abandon() and a new cookie with Response.Cookies.Add

session_start();
// after the password check passes
session_regenerate_id(true);
$_SESSION['user_id'] = $user->id;

The cheat sheet also asks for at least 64 bits of entropy in the session ID, which means at least 16 hex characters, so it cannot be guessed.

How do you check your app today?

You check it by comparing the session ID before and after login: if it did not change, your app has this flaw.

  1. Open the login page in a private window, open the developer tools, and copy the session cookie's value.

  2. Log in, and look at the cookie again. You should find a new value.

  3. Try the same after a password change, or after entering a page with higher privileges.

  4. If the value did not change, add the renewal line to your login code, after the password check and before you write anything into the session.

This check goes with checking your session cookie flags and protecting forms from CSRF, because all three are about the same cookie.

Where do you start now?

A session ID from before login has no business surviving after it. Open your login code now, find the line that writes the user into the session, and look at what comes before it.

If you want someone to test the sessions and permissions on your platform the way an attacker would, there is Muhassan. Did your session ID change when you logged in?

FAQ

If the cookie is HttpOnly, is there still a risk?

Yes. HttpOnly stops JavaScript from reading the cookie, but here the attacker does not need to read it: they set it in the first place and already know its value.

Do I renew the ID only at login?

No, after any privilege change: login, a password change, entering the admin panel. Login is the most common one.

Does renewing the ID log the user out?

No. The data in the session stays, and the browser gets a cookie with a new ID. Only the old ID is lost.

Share

More articles