Opened 18 years ago

Closed 18 years ago

#4531 closed (fixed)

SessionId collision - session takeover by accident

Reported by: Frank Tegtmeyer <fte@…> Owned by: Adrian Holovaty
Component: Contrib apps Version: dev
Severity: Keywords: sessionid session patch
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I just had an accidental session takeover at a clients web site.

My session settings:

SESSION_COOKIE_SECURE = True
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_AGE = 70000

Environment:
Python 2.5, Django SVN Revision: 5320, OpenBSD 4.1, lighttpd with FastCGI

After checking the generation of the sessionid I found that there may be
the following reasons (in combination):

  • very low traffic site (at the moment)
  • I had a very short session before logging in again, I used the logout link (admin interface).
  • no deletion of the session cookie when logging out (not sure about that but it would explain the behaviour)
  • five django processes, each having its own seeded random module
  • exclusive use of fixed data or determined data for the sessionid generation

I think I reused my old sessionid by still having the cookie. Between logging
out and logging in again another user got the same sessionid (because it
was not in the database anymore). So I got an authenticated session from the
other user.

A patch is provided, maybe even the remote IP should be included in feeding md5.
The session cookie should also be deleted when using a logout link.

Regards, Frank

Attachments (1)

sessionid_patch (1.1 KB ) - added by Frank Tegtmeyer <fte@…> 18 years ago.
Patch for safer sessionid generation

Download all attachments as: .zip

Change History (8)

by Frank Tegtmeyer <fte@…>, 18 years ago

Attachment: sessionid_patch added

Patch for safer sessionid generation

comment:1 by Frank Tegtmeyer <fte@…>, 18 years ago

Keywords: patch added
Summary: SessionId collision - session takeover by accident[patch] SessionId collision - session takeover by accident

comment:2 by Malcolm Tredinnick, 18 years ago

Patch needs improvement: set
Summary: [patch] SessionId collision - session takeover by accidentSessionId collision - session takeover by accident
Triage Stage: UnreviewedAccepted

The os.getpid() change in the patch reduces randomness (the range of pid values is smaller than the range 0 - maxint, plus pid will be the same for processes in multi-threaded servers on some systems), so that part should go out. Adding time.time() seems like a good idea; it's similar to how we fixed another clash when the pseudo-random seeding was too close together.

in reply to:  2 ; comment:3 by Frank Tegtmeyer <fte@…>, 18 years ago

Replying to mtredinnick:

The os.getpid() change in the patch reduces randomness

No, it doesn't (reasoning further down).

(the range of pid values is smaller than the range 0 - maxint,

Yes, this is true. But the current doubled random() call is in fact so randon like a single call. So getpid() does add randomness in comparison to the current code.

plus pid will be the same for processes in multi-threaded servers on some systems),

For this case it doesn't matter because then there are multiple calls to random() which will give different results.

You have either

  • randomness from getpid() - or if this is inside one process:
  • randomness by random() and always
  • randomness by time()


so that part should go out.

time() could be enough randomness, but adding the pid will increase that additionally.

Regards, Frank

in reply to:  3 comment:4 by Frank Tegtmeyer <fte@…>, 18 years ago

Replying to Frank Tegtmeyer <fte@fte.to>:

the current doubled random() call is in fact so randon like a single call

I think this point is not obvious to everybody, so here some additional explanation:

The pseudo random number generator is a completely deterministic thing. So if
you call it once you get a value. If you call it once again, you get another value
but this value is dependent on the first. So you have no additional randomness here.
A generator like the one used by random() always produces the same sequence of numbers. That's also the main reason for the id duplication.

The random module documentation has a nice sentence in it that says it all:

However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.

Hope, that makes it clearer.

Regards, Frank

comment:5 by Malcolm Tredinnick, 18 years ago

Yeah, I understood what you were saying. You're correct. Oversight on my part the first time.

comment:6 by Malcolm Tredinnick, 18 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: newclosed

(In [5470]) Fixed #4531 -- Added a bit more randomness to session idents. Thanks, Frank
Tegtmeyer.

Note: See TracTickets for help on using tickets.
Back to Top