markjugg has asked for the wisdom of the Perl Monks concerning the following question:

I have a question about the best way to handle httpd sessions. I know the basic framework (basically what Apache::Session does.) However, I'm wondering what's the best solution for generating the sessions ids? I see two main contenders:

MD5 hash: I believe these are guaranteed to be unique if they are based on a unique sequence. However, since they depend on a 'secret phrase' for security, if the secret phrase was discovered, then all the sessions might be able to be comprimised, right?

random strings: Since these are random, it seems like eventually the same string could be generated twice, but with large strings and lots of characters, this should be very, very rare. However, since there is no "secret" behind them, they should otherwise be harder to comprimise.

In either case, it makes sense to also include the IP address of the user as part of the security check, adding security to either system.

I assume that MD5 is "better" for some reason since that is what's in the widely-used Apache::Session module. Is so, what specific advantages does it have over the random string method, and what trade-offs are there, if any? Thanks,

-mark

Replies are listed 'Best First'.
(Ovid) Re: sessions: MD5 versus random strings
by Ovid (Cardinal) on Oct 16, 2000 at 23:03 UTC
    If you're trying to maintain state in httpd sessions, you have a few options. I'm taking this from O'Reilley's CGI Programming with Perl, second edition.
    1. Query strings and extra path information.

      I don't care for this method, as one is forced to try to reliably parse all links in documents.

    2. Cookies.

      This is the most reliable. It's easy to use and doesn't matter if the user leaves your site and returns later. However, if your Web site is dedicated to the premise that "BATF employees are bunch of jack-booted thugs", many of your users are probably concerned about privacy and have cookies disabled.

    3. Hidden fields.

      I like this method, but it only works across a series of form submissions. If the user leaves your site and returns later, state information is probably lost.

    Regardless of the method used, you should probably be employing some form of generating a digest or random key for the session id. I prefer the idea of generating a digest with MD5 or SHA1, since many people who try to generate a random key will do so on their own and not generate a key random enough. Unless you're a cryptography wiz (and I'm not), trying to "roll your own" is bad if you are really concerned about security.

    Re: MD5. According to this node, if you use MD5, you should run it twice to prevent someone from duplicating your key with different information. In fact, the aforementioned book points out that theoretical vulnerabilities have been discovered in the MD5 algorithm. As a result, I recommend Digest::SHA1. It does not have these vulnerabilities. Even if it is slower than MD5, it's probably faster than running MD5 twice, though I have not benchmarked it.

    If you're interesting in using user information to generate a digest, the following algorithm is listed:

    use Digest::MD5; my $md5 = new Digest::MD5; my $remote = $ENV{REMOTE_ADDR} . $ENV{REMOTE_PORT}; my $id = $md5->md5_base64( time, $$, $remote ); $id =~ tr|+/=|-_.|; # Make non-word characters URL-friendly
    Further, here's a quote from the book regarding this method:
    This does a good job of generating a unique key for each request. However, it is not intended to create keys that cannot be cracked. If you are generating sessions identifiers that provide access to sensitive data, then you should use a more sensitive method to generate an identifier.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

(tye)Re: sessions: MD5 versus random strings
by tye (Sage) on Oct 16, 2000 at 22:15 UTC

    The size of the random string doesn't make much difference at all. It is actually the size of the seed that matters. Or, more precisely, the number of random bits in the output.

    If you generate a 32-bit seed using methods that really only give you about 8 bits worth of randomness and then generate a 45KB string, then you still have about a 1/256 chance of generating the same string next time.

    And this problem doesn't go away when you use MD5 -- you still need to generate unique strings.

    Also, the "secret" with a random string is how you generated it. If that gets out, then people can still spoof sessions.

            - tye (but my friends call me "Tye")
Re: sessions: MD5 versus random strings
by cianoz (Friar) on Oct 17, 2000 at 15:26 UTC
    you wrote:
    In either case, it makes sense to also include the IP address of the user as part of the security check, adding security to either system.
    Client IP address can change during a session (i.e. due to a pool of proxy servers) so i would not include remote IP in session check.
    a good (IMHO) way to generate an MD5 session ID could be:
    use MD5; my $MD5 = new MD5(); $MD5->add($$ . rand() . time() . (-A '/tmp')); my $SessionID = $MD5->hexdigest;