tags:

views:

70

answers:

3

I am producing a script that others will put in their websites. It is designed for people with limited knowledge of PHP so that all they have to do is include() the script and set a few configuration variables. This means headers will probably have been sent already and so using sessions may not work. I recommend they call session_start in their own scripts, but I want a fallback option as well.

I already have a hidden input to deal with forms, but I also need links to have a query string appended to their URIs to identify the session. But if the hash is based only on the password+salt then there is a security risk: the logged-in user could click an external link and the owner of the external site could see the URI with the hash in their referrer logs. All they'd have to do is used that hash and they'd be logged in.

Therefore I want to salt the hash in a time-sensitive manner, limiting a session to 10 minutes. I can't figure out how to do this. Of course I can use time() to salt it, but how do I check how old the session is based only on the hash?

+1  A: 

This sounds like a real bad idea. And complicated too. I would definitely not recommend it. You probably can use something like this:

if (session_id() == "") session_start();

The above will basically check if session has been started or not, and otherwise start session.

Since you planning to distribute to users, the whole approach seems a bit off to me. Am not sure what you are looking to achieve out of this, but you could try using a JS which calls your PHP file per page instead. This will make it easier for you. If you could elaborate on what kind of application you are developing, then I could probably help you out better. I have a lot of experience in mass consumer software apps similar to what you are doing.

Alec Smart
Thanks. In my original post I mentioned that session_start() might not work if the headers had already been sent (i.e. some HTML has already been output).Still, I figured out a way to not need any of this garbage and use PHP's sessions, simply by ensuring the login form submits to the script itself (rather than the includer) and then I just redirect back to the includer.Occam's razor at work again.
Rafael
My bad, I missed that part. Anyways I hope you doing something useful to include it in this fashion. Good luck.
Alec Smart
+2  A: 

Expiring sessions after 10 minutes does not protect your users against session hijacking attacks. It only succeeds in annoying your users by forcing a login every 10 minutes. Your proposed scheme still has all kinds of vulnerabilities. Your salted hashed passwords can still leak to the outside world through many other channels; packet sniffing, intermediate proxies, users' emailing each other links to pages or even the saved html, just to name a few. I advise you not to homegrow you're own security framework without being an expert in the area. Even then, this is a solved problem. Just go with a known trusted solution. There are many subtleties in web security that are easy to mess up.

Asaph
Thanks, you partly persuaded me to ditch this idea which was getting messier by the minute.
Rafael
A: 

This is not necessarily a bad idea, but it is dangerous if not done correctly. In fact, it is a fairly common implementation of multi-domain single sign-on using Hash-based Message Authentication Codes. Some ground rules:

  1. Never ever include the password as part of the hash (even as the salt)
  2. Require a timestamp generation as part of the hash that must be passed ALONG with the hash.
  3. Each site to use this hash should have their own 32 or 64 byte guid to be used as a unique salt.
  4. Pass specific data in the query string such as username, timestamp, anything else, and the HMAC. So it would look something like ?user=steve&timestamp=66343532233&otherdata=otherdata&HMAC=AB3445-1234144-AFBBDEDD (you get the idea)
  5. When site authentication is made cross-site, the HTTP_REFERER should be used (if possible) to get the key to generate the comparing HMAC.
  6. Use a solid hashing algorithm (SHA1 is preferred) for generating the HMAC. Generate the the private site keys as randomly as possible. Do not use a standard derivation method, simply make sure that the end result is large enough/unique enough.
Joel Etherton