Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Session ID Generator (Rolled My Own)

by jerrygarciuh (Curate)
on Sep 14, 2002 at 23:15 UTC ( [id://197948]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,

I wanted to present the following subroutine for critique. It is cobbled together from code in Camel (2nd) and a post somewhere on PM from a long ways back. I wanted to know what folks thought of it as a random session/transaction ID generator. I need something sufficiently random that the kiddies won't be able to jack sessions by guessing the ID sequence.

Is the following sub sufficient to my aims?
TIA
jg

#!/usr/bin/perl -w use strict; my ($v,$n,$l); $v = srand( time() ^ ($$ + ($$ << 15)) ); #Camel 2nd pg 223 sub sid { #random character from a PM post, can't remember whose my @Chars = split '','abcdefghijkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUV +WXYZ23456789'; for (1..($ARGV[0] ||= 1)){ for (1..7){ $l .= $Chars[rand @Chars]; } } $n = int( rand 1000000); }
_____________________________________________________
"The man who grasps principles can successfully select his own methods.
The man who tries methods, ignoring principles, is sure to have trouble.
~ Ralph Waldo Emerson

Replies are listed 'Best First'.
Re: Session ID Generator (Rolled My Own)
by Zaxo (Archbishop) on Sep 14, 2002 at 23:38 UTC

    Just a few points.

    It's no longer necessary to seed the RNG with srand, that has been automatic for the last few versions of Perl.

    The @Chars array can be initialized with the list range operator:

    my @Chars = '0'..'9', 'A'..'Z', 'a'..'z';

    I'd rename $l so it doesn't look like the first captured match builtin, $1. It could also be generated by

    $l = join '', map {$Chars[rand @Chars]} 1..7*($ARGV[0] ||= 1);

    After Compline,
    Zaxo

      my @Chars = '0'..'9', 'A'..'Z', 'a'..'z';

      That won't work as you might expect. You'll actually need:

      my @Chars = ('0'..'9', 'A'..'Z', 'a'..'z');

      Those parens aren't optional. Here's a demonstration:

      $ perl -le 'my @one="0".."9","a".."z"; my @two=("0".."9","a".."z"); pr +int @one; print @two' 0123456789 0123456789abcdefghijklmnopqrstuvwxyz
      -sauoq
      "My two cents aren't worth a dime.";
      

        Right you are. I'm trying to understand that in terms of operator precedence. I think my erroneous one parses as: ( my @Chars = '0' .. '9'), 'A' .. 'Z', 'a' .. 'z'; sauoq++ for the catch.

        After Compline,
        Zaxo

        Thanks for pointing that out sauoq!
        I would certainly never figured out the problem from the symptom!
        jg
        _____________________________________________________
        "The man who grasps principles can successfully select his own methods.
        The man who tries methods, ignoring principles, is sure to have trouble.
        ~ Ralph Waldo Emerson

      Hi Zaxo!

      A small glitch in this. The equivalent @Chars range would be:

      v my @Chars = ('a'..'z', 'A'..'Z', '2'..'9'); ^
      Also, I think the $1 (one) you read, is actually a $l (ell)... ;)

      So long,
      Flexx

      Update: Added parens, ++sauoq

Re: Session ID Generator (Rolled My Own)
by Beatnik (Parson) on Sep 14, 2002 at 23:32 UTC
    If you're using this on Apache, you might wanna consider compiling mod_unique_id. It stores a unique value in $ENV{UNIQUE_ID} for each request.

    Greetz
    Beatnik
    ...Perl is like sex: if you're doing it wrong, there's no fun to it.
Re: Session ID Generator (Rolled My Own)
by perrin (Chancellor) on Sep 15, 2002 at 01:45 UTC
    Is it sufficiently random? Maybe, I don't know much about that. (I would just use one of the random number modules from CPAN.) However, it's not guranteed to be unique. It is unlikely but possible for this to generate the same ID for multiple live sessions, causing mayhem for those users. I think it's much safer to use mod_unique_id and then use a digest-based authentication to make sure that the cookie is valid and comes from you. This method is described here.
Re: Session ID Generator (Rolled My Own)
by Aristotle (Chancellor) on Sep 15, 2002 at 08:06 UTC
Re: Session ID Generator (Rolled My Own)
by bart (Canon) on Sep 15, 2002 at 10:55 UTC
    Somehow, I don't see how your "unique ID" is garanteed to be unique. You're just using a sequence of randomly picked word characters, aren't you?

    If part of your session ID was the time() when the session started, perhaps encrypted and/or shuffled in a predetermined order, there's only so many sessions that could get started in that second. So if you now make sure the remainder of what your session ID string consists of, is unique for that starting second, then the session ID will be unique, period. And if that remainder is quite random and long enough to be very hard to guess, then hijacking a session seems very unlikely to me — but not impossible. I would think that recognizing attempts to break into a session, with repeated tries with various candidate session ID's, and taking appropriate action (whatever that may mean), would be the logical next step.

Re: Session ID Generator (Rolled My Own)
by no_slogan (Deacon) on Sep 15, 2002 at 07:26 UTC
    The built-in rand() function is not a secure source of random numbers. A suitably skilled attacker will be able to predict your sequence numbers. perrin has a good suggestion.
Re: Session ID Generator (Rolled My Own)
by Ryszard (Priest) on Sep 15, 2002 at 08:19 UTC
    a one liner?  my $sess_id = md5_hex('s3cr37 s7r1n6'.$$.localtime().rand());

    Of course you'll need md5.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://197948]
Approved by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found