How to stop session Hijacking in PHP

Discussion in 'PHP' started by liam1412, Sep 25, 2008.

  1. #1
    Im pretty new to PHP and have just been told a script I am building is fairly vulnerable to session hijacking.

    Can anyone please give me any ideas how you can prevent this.

    Is there a definitive way that you use??

    Help will be much appreciated
     
    liam1412, Sep 25, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Link the sessionid to the ip using a database.
     
    matthewrobertbell, Sep 25, 2008 IP
  3. liam1412

    liam1412 Active Member

    Messages:
    387
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Yeah but doesn't that become a problem with static IP's and things
     
    liam1412, Sep 25, 2008 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    remind me again, how can this get highjacked? i can be wrong here but I thought that...

    page 1:
    session_start();
    $sid = session_id(); // makes it a new one.

    page 2:
    session_start();
    $sid = session_id(); // same as page 1.

    you don't need to actually pass the session id to page 2 like ?sid=123291873122213123

    even if for whatever reason somebody can obtain a particular session id (say, from their browser's cookies), it's not a clientside setting, trust into the server alone and have register_globals off.
     
    dimitar christoff, Sep 25, 2008 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    hrm google is your friend. _http://phpsec.org/projects/guide/4.html
     
    dimitar christoff, Sep 25, 2008 IP
  6. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You will need to keep a signature of user's session in a session variable:

    
    /* Right when you log the user in */
    if (usernameandpasswordvalid()) {
       session_regenerate_id(); // This will take care of session fixation attacks */
       $_SESSION['valid_user'] = true;
       $_SESSION['user_sig'] = md5('SOME SECRET KEYWORD' .$_SERVER['HTTP_USER_AGENT']); /* For session hijacking; user agent is most likely to stay the same */
    }
    
    PHP:
    Now in other pages, you will have to check and see if the user is valid like this:

    
    if ($_SESSION['valid_user'] && $_SESSION['user_sig'] == md5('SOME SECRET KEYWORD' .$_SERVER['HTTP_USER_AGENT'])) {
       define('VALID_USER', true);
    } else {
       define('VALID_USER', false);
    }
    
    PHP:
    This is a very strong session...
     
    hamidof, Sep 25, 2008 IP
  7. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Without a doubt, they can use it to hijack your session...
     
    hamidof, Sep 25, 2008 IP