A. Neumann
A. Neumann

Reputation: 538

Rails4 security: Session fixation possible at all using encrypted cookies?

After studying the rails guide and some other ressources I'm wondering how a session fixation attack on a user's session can actually happen. At least I'm sceptical it works as simple as depicted here in the guide, where an attacker...

1) ...creates a valid session by logging in

2) ...keeps the session alive

3) ...then forces the user to use his session's id e.g. by using some XSS vulnerability

All fine, but... how would the attacker be able to gather the value of his own session id? By default cookies are encrypted in Rails4+. So what to do as an attacker assuming I do not have access to secret_key_base or whatever is used to generate the encryption and signature keys? From what I understand I cannot tamper with the cookie without invalidating it (signature wrong) so somehow passing a self-created cookie to a possible victim is neither an option.

Is the secu guide kind of not up to date or am I missing a point here? If the latter then...

a) how [as an attacker] can I read encrypted cookie information

b) how does a vulnerability have to look like that allows me [the attacker] to inject that session id into the likewise encrypted cookie on another client? Would that be an XSS attack? The guide states that if an attacker uses code like

<script>document.cookie="_session_id=16d5b78abb28e3d6206b60f22a03c8d9";</script>

he might be able to fix that session. But again, why would rails reveal it's plain session to the client making it accessible via client-side processed javascript? It does not, which is why all my cookie-values are simple gibberish not being accessible by Javascript (can test that via console), right?

Thanks for any advice!

Upvotes: 4

Views: 626

Answers (2)

moger777
moger777

Reputation: 1326

As I understand it, session fixation won't work if the session details are stored in the cookie itself. The old fashioned way of doing sessions, the session data would be saved in some sort of database. The session id that got saved in the cookie would point to the record or file where the session data was saved. This means as the session changed the cookie wouldn't change at all. If a hacker had the same cookie as the user, when the user logged in they could potentially be able to hijack the login since their cookie would point to the session that now had a user_id (and potentially other data) attached to it. Under the new system, any change to the session also changes the cookie so the hackers old cookie would just have an empty session.

That being said, it's still a best practice to add reset_session to login, logout and registration endpoints (if you use something like devise this may be done for you) since if you or some future developer changes the session store to be database backed you could end up shooting yourself in the foot. The security guide also doesn't know what type of session store you are using so they have to include that advice.

Upvotes: 0

Steve
Steve

Reputation: 7108

It's not so much about the cookie encryption (or signing). Without signing and/or encryption, the attacker wouldn't need to resort to session fixation with a site that stores session data in a cookie; they could just craft their own cookie.

Assume the attacker has a way to inject cookie-setting JavaScript onto a page that a victim will visit, i.e., an XSS vulnerability. Here's how the attack plays out:

  1. The attacker visits the site and gets a session cookie.
  2. The attacker crafts malicious JavaScript, exploits the XSS vulnerability, and waits for a user to visit (or lures a user to) the page containing the malicious JavaScript payload.

    <script>document.cookie="_appname_session=(...attacker's session cookie...)";</script>
    
  3. A victim comes along and visits the page with the malicious JavaScript, setting or overwriting their session cookie with the one from the attacker.
  4. If they were logged in, the site will no longer recognize them as an authenticated user, and will probably redirect them to log in.
  5. If they weren't logged in, they will be redirected to the login page.
  6. They authenticate with their username and password.
  7. At this point both the victim and the attacker have a cookie containing a session_id belonging to a user who has authenticated.

CookieStore, the Rails default session store

Normally you will set some value in the session to indicate that the user is authenticated. Even if it is as simple as session[:logged_in] = true, it will change the value of the cookie, and the attacker won't be able to access the site, even though the session_id is the same, because the attacker's cookie does not contain the additional data indicating an authenticated session.

Other session storage strategies

Here's where this attack is more feasible. Other session stores still use a cookie to hold the session_id, but they store session data elsewhere — in the cache, in a database, in memcached, etc. In this case, when the attacker visits the site after the victim has authenticated, the attacker's cookie — with the same session_id — will cause the session to be loaded.

HttpOnly

However, there is another major impediment to this attack — HttpOnly cookies.

Set-Cookie: _appname_session=v%2Ba...; path=/; HttpOnly

When a cookie is designated HttpOnly, as Rack/Rails session cookies are, the browser does not expose it through document.cookies. It can neither be read nor overwritten by JavaScript. I tried setting a known session cookie via document.cookie, but it was not possible unless I cleared the existing cookie(s) first. This means that an attacker would need a user who doesn't have a session cookie yet to visit the page with the XSS exploit (which would need to be on a page without a session cookie) before logging in.

Upvotes: 0

Related Questions