tags:

views:

38

answers:

2

hoe we can stop to accept externally created session identifiers..

thanks

+3  A: 

If people pass in their own session identifier it will only work if it's a valid one anyways. You are already protected against this.

Daniel Egeberg
+1  A: 
  • Make sure sessions are only started from a cookie.
  • Use session_regenerate_id() when adding/updating auth info
  • Add an internal check that compares a hash made of the session_id, remote_addr and user_agent to the currently stored hash (inside your session data), eg:

    session_start();
    if (empty($_SESSION['auth_hash'])) {
    // new visitor
    $_SESSION['auth_hash'] = sha1(session_id() . 'SECRET_STRING' . $_SERVER['REMOTE_ADDR']);
    }
    if ($_SESSION['auth_hash'] != sha1(session_id() . 'SECRET_STRING' . $_SERVER['REMOTE_ADDR'])) {
    // invalid user / mitm-attack
    session_destroy();
    // display login or so
    }

  • after adding/updating auth info, eg:

    session_regenerate_id();
    $_SESSION['auth_hash'] = sha1(session_id() . 'SECRET_STRING' . $_SERVER['REMOTE_ADDR']);

@Daniel: This is NOT true, assigning your own value to a session cookie WILL create that session, and when that session already exists, WILL use that session. If measures like I suggested above are nog used, session takeovers and mitm attacks are possible...

Sjon
Possible inaccuracies in my answer can more than likely be attributed to the fact that the question is horribly phrased. The only thing I ever claimed is that PHP will only assign existing data to your session if you can provide an existing, valid session id. That is correct.
Daniel Egeberg
+1 for session_regenerate_id though I would add to use it when changing 'levels' of access. So if a user logs in, regenerate. If they log out, regenerate.
Narcissus
@Daniel True, I guess I wasn't clear on what your awnser was saying.
Sjon
@Narcissus In case of logout, it's better to destroy the session data, after wich the session_id doesn't have the significance it had when the user was logged in, and a regenerate isn't really necessary
Sjon