Search code examples
phpsecuritysessionsession-fixation

PHP Sessions - checking pevious IP on every page load - To do, or alternatives?


Right now I have this implemented in my Dev environment:

  • I store the $SERVER['REMOTE_ADDR'] as a session variable upon login, then recheck the page every page load for the same IP address.

The more I read, I see that many people don't like this idea because of proxy's etc... So, what are some other options that can still be good practce? I was thinking about USER AGENT - but any joe-blow can fake that with a simple firefox plugin. That said, at least it would take a good guess from an attacker to successfully pick the right one on the first try...

What do people think? I'd love to simply just change out the text: 'SERVER_ADDR' with something else - all the other code can remain as is.

Thanks.

EDIT: I guess my main goal here is to prevent hijacking/fixation. An IP check would in theory ensure that the user remains the user at all times (well, unless somebody's spoofing the IP also...) - but the session is basically saved to an IP address in this manor...


Solution

  • Well, from someone who has spent half my time trying to prevent session hijacks, I can tell you that is a wrong way to go about it. Yes, in theory you will have guaranteed the user is the same, but then in practice you will get expected 'unexpected' results. Certain ISP change the IP with every page load. Proxies like tor do that as well. Your best bet is simply to use the user agent. Though that has drawbacks, you can't have a fully secure system. Just ensure that you prevent xss on your site and most likely, the user can't be faked too easily. I had other implementations. The first involved taking the first part of the IP and hashing it with the ua to ensure that the IP is always in the same range, but then I found out that the country ranges vary. Another involved a country lookup to ensure that the IP is from the same country, but then again that involved an extra database lookup. The best you can do is make it harder, but it will never be secure.


    Almost forgot. Remember to regenerate the session I'd whenever you increase a user's priveleges such as in a login. This will help prevent session fixation attacks where the session id is passed via url. Remember your xss.