Home » Php » php – Hashing a session fingerprint really necessary?

php – Hashing a session fingerprint really necessary?

Posted by: admin April 23, 2020 Leave a comment


Please read this THOUROUGHLY before voting…

So I have seen a lot of session management classes that create a fingerprint via concatenation of user agent and a couple of ip blocks or whatever. They seem to also add a salt and then hash this fingerprint before storing it in a session variable.

This fingerprint generation typically happens every request in order to verify that the current user of the session is in deed the original session user. This is why I am wondering, is the salt and hash really necessary on something like this?

If a hacker can get onto your filesystem to see your session file contents, aren’t you already hosed at that point?

Any info greatly appreciated.

How to&Answers:

Most of it makes sense, but the hashing and salting makes no sense.

If you tie the session to an IP address, then it becomes a lot harder to hijack into a session. This is something I recommend doing, but you don’t need to be utterly strict about it. You can just tie to the first three parts of the IPv4 or so. The choice is yours. The more strict IP check the more secure it is, but the less convenient it is for users.

And as for tying the session based on the user agent, that may also help. It must be realized that if you work on an unencrypted channel (HTTP for example), then the user agent check is less useful as it can be reproduced by the intruder as well.

When it comes to salting and hashing, that is useless. They add no strength to your identity checks. The only thing they do is complicate your design. For this matter, I believe they lower your level of security.

As always, a few rules to keep in mind:

  • Use strong session identifiers. This means use good random sources and make sure there are enough bits.
  • Tie the session to an IP, at least to some extent.
  • Tie the session to a user agent, if possible.
  • Use SSL/TLS. Without it, theoretically all session systems are insecure.
  • Secure your session storage. Whether it’s filesystem based or database based.


I can think of two cases where it would be useful:

  1. When the session data is stored client-side. (Like in a cookie.) So, I’d be prevented from taking my cookie to another computer, and I’d be prevented from making up my own cookie contents. (Ok, so this is not a very likely scenario…)
  2. When the session data is stored in some shared server-side resource (i.e., /tmp) and is vulnerable to snooping. In this case, if the snooper is able to see the contents of the session, they’ll still be unable to fake a connection to that session because they don’t know what data went into the fingerprint.


In complement to the response of @Kai Sellgren (+1) which contains some good hints on how to secure your session storage I would add some ideas than can explain the hash & salt on some specific applications.

I’m not talking of application that are using the cookie as a session storage, we still see this for example on Prestashop eCommerce solution, with encryption of the cookie content (why the hell did they decide to store the session on the cookie?). I understand we talk about server side session storage.

The key point is layered Security and in-depth defense:

Compromissions are never boolean things, your are not ‘completly compromised’ or ‘completly secure’. One of the the real history I like about that is the mySpace worm explanation, it shows a real attack and how defensive steps must be break. There’s always a new wall. Just one example, I share the same IP as my boss when i’m in the office, and maybe the same browser, this could break a security based only on IP+user-agent.

So in the hash & salt of session stamping we are clearly acting after a few walls have fallen. And kai shows us some of these walls. When he talks about securing the session storage I would add 2 things:

  • it’s a really good idea to alter the session.save_path and the open_basedir of each PHP application (and get a separate Virtualhost for each). Rarely done.
  • if your application is installed on a path (like /myapp), add a prefix_path on the session cookie (and fix it for any other app on the same server)

Now Let’s imagine a realistic compromission. You’ve several ways to compromise the session on the server side:

  • The application is running on a website with some other applications running in other paths (or in other domains in the same server). And at least on of theses applications is quite unsecure. At worst server side code could be injected in this app, but some of the security walls (like open_basedir or other chrooting techniques) may prevent this injected code from affecting your separate application (or data).
  • Some of the javascript libraries comes with some test subdirectories containing highly insecure scripts, with not only nice session disclosure but maybe some session fixation or prediction available.
  • The application is shared, and talking about wordpress-like softs you can imagine some platforms sharing a lot of different installations, with different modules and maybe some custom code. On such platforms you’ll find settings to allow altering the salt for each consumer, there’s a reason for that. One of the website could impact the security of others and clean separation can be harder to do if the applications wants to manage the websites all-in-one.

Your targeted application may get hit by the environment, if the session storage can be shared with some scripts from other application, or from a script in your own application that you did’nt even notice (like these f*** examples in javascript libs, why didn’t you suspend php execution on static file directories!)

From this first step of compromission the attacker could potentialy (and in severity increasing):

  • read the session stamps and maybe find which information he should fake to get the same stamp
  • build a new session containing a session stamp valid for his configuration, and then use this new session identifier on your application. Your application will find the session file, and accept him.
  • alter one of your valid session to modify the stamp in the same way

A simple hash of the stamp would make his life harder, but it would just be a wall to break, the salt make this wall really harder to break.

One important point is, from your question, if a guy can alter something in the session storage am I already in a bad mood?. Well, maybe not completly. If it is the only thing the chroot/separation/securization of applications allows him to do this salt will be a nightmare for him.

And the second important point is: should I do this level of in-depth security on every web application?. Answer is no. Overengineering is a bad thing and can reduce the security of your application by the simple fact it became harder to understand and maitin. You do not need to complexify your application if:

  • you’ve got a pretty good level of session storage separation
  • you’re alone on your server, only one application, and not any sort of multisite handling
  • your application security level is so weak that a simple code injection is available on the application, so a session fixation is not needed for an attacker 🙂


I can imagine that the point of hashing that fingerprint information is storage space as the resulting hash has a fixed length.

But to also use a salt doesn’t make much sense to me. Because, as you’ve already said, since that data is stored in the session data storage location, you would already have a bigger problem than session fixation/hijacking if someone would be able to obtain that data.


You can find a plausible solution here:

Fingerprinting combats session hijacking.
The attacker not only needs your session_id, he also needs any sensitive HTTP headers.
It adds another barrier for the attacker, albeit one that can be easily overcome.

The hash is there to make the data uniform. The salt is there to obscure the hashing process – so an attacker can not generate a valid fingerprint for his own combination of HTTP headers.

If a hacker is in your filesystem you have bigger problems 😀


A lot of people who don’t understand very much about security combine bits of advice floating around the internet in the hope that what they end up with will be “good enough”. Tying the session ID to the U-A breaks browser upgrades (which Chrome does fairly often) and tying to the IP address breaks mobility (anyone with a laptop that uses Wi-Fi), and many ISPs don’t have contiguous allocations. Anyone who can sniff cookies can also sniff the U-A, and will probably have access to the same IP address because they got the cookie off insecure Wi-Fi behind a NAT.

What you probably do want to do is change the session ID on a login attempt, which is a reliable way to prevent “session fixation” attacks (where the attacker makes the victim load http://example.com/?SESSIONID=foo e.g. through an <img>, waits for you to log in, and now knows the victim’s session ID). There is little reason to preserve a session across a login, and you can copy the few things that need to be preserved (e.g. a shopping cart) across.


If a hacker can get onto you
filesystem to see your session file
contents, aren’t you already hosed at
that point?

If you are using PHP as CGI (like in the case with nginx), then I think no. If you set permissions right then your session files must have read/write permission for PHP user while your PHP files should have only read permissions. So, if you pass the salt from the web server to PHP, then PHP user can’t get access to it (he can’t create any new/change existing PHP files that can be run by your web server and he can’t access web server as it is run on another user), so he can’t really hack(change) cookies (only delete them) because he can’t get salt. Of course you will have to pass database settings from web server as well.

I never really tried it, so please correct me if I am wrong.


is the salt and hash really necessary on something like this [http client fingerprint]?

The hash might be useful to reduce the number of bytes consumed by the fingerprint inside the session data. So as long as the hashed fingerprint is of a smaller size than the fingerprint itself this can make sense in terms of space reduction. Price is the consumption of system resources to generate the hash.

Does it really make sense? You would need to benchmark this to say so.

How can a salt be helpful then? I must admit, I see no reason for a salt. It would only make sense to make it harder to guess the fingerprint from a hash. But as I do not see any security benefit in hashing the fingerprint (it’s kept on the server-side only and is already considerably secure), salting is not adding anything.

In case the session store itself is not considered secure (if that’s for the argument), the whole session should be encrypted, not only the fingerprint.

So particularly for the fingerprint, I do not see much use in hashing and salting it.