SitePoint Sponsor

User Tag List

Results 1 to 23 of 23

Thread: CGI un-encoding

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy CGI un-encoding

    Hi, i am running a user login system built in cgi
    I am trying to grab the cookie and display it.
    the program uses this to set the cookie(i belive):

    Code:
    pm_encode("$input{'login'}|$input{'password'}");
    Does anybody know if i can unencode the cookie?

    Any help apreciated

    David

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I managed to peice this together and i was hoping it might set a unencoded cookie:

    Code:
    $lp = $req->cookie("pmpre");
       my ($clogin, $cID) = split(/\|/, pm_decode($lp));
       
       my $cookie_pmpre_login = $req->cookie( 
    					-NAME           => "username",
    					-VALUE          => "$clogin",
    					-EXPIRES        => "+1y",
    					-PATH           => "/"
    	   );
    	   
    	   print $req->header(-COOKIE => [$cookie_pmpre_login]);
    However when i load the page it tells me there has been an internal server error. anybody know how i can get this working?

  3. #3
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if your script is uing "strict" (and it should) then maybe you need to declare $lp with "my":

    my $lp = $req->cookie("pmpre");

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KevinR
    Obviously if that is all the code it will not work.
    It is all the code ive got because i wrote it.

    How can i expand it to make it work?

    Im sorry but i really dont know anything about cgi

  5. #5
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you read the CGI.pm documentation:

    http://perldoc.perl.org/CGI.html

    By unencode what do you mean anyway?

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KevinR

    By unencode what do you mean anyway?
    pm_decode
    as opposed to pm _encode

  7. #7
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea what pm_encode/decode is. Find the pm_encode/decode sub routines in the script and post them if they are not too long, they will be similar to this:

    Code:
    sub pm_encode {
       some code here
    }
    
    sub pm_decode {
       some code here
    }

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh i didnt realise i thought that they were set functions in cgi i will have a look and post shortly

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have just found this in an included page:

    Code:
    sub pm_decode
    {
        @table = (('A' .. 'Z'), ('a' .. 'z'), ('0' .. '9'), '+', '/', '|');
        for ($_ = 0; $_ <= $#table; $_++) {
             $decode_table[ord($table[$_])] = $_;
        }
        local $_ = $_[0];
        s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
        pack('B' . (int(length($_) / 8) * 8), $_);
    }
    Would this work if i put it with everything else that i have got? ie:

    Code:
    #!/usr/bin/perl
    
    ###############################
     # find the root if this is IIS:
     ###############################
     if($ENV{'SERVER_SOFTWARE'} =~ m/IIS/)
     {
    	if($root eq "" && $ENV{'PATH_TRANSLATED'} ne "")
    	{
    		my $pt = $ENV{'PATH_TRANSLATED'};
    		my @pts = split(/\\/, $pt);
    		$pts[-1] = "";
    		$root = join "/", @pts;
    		$OS = "NT";
    	}
     }
    
    sub pm_decode
    {
        @table = (('A' .. 'Z'), ('a' .. 'z'), ('0' .. '9'), '+', '/', '|');
        for ($_ = 0; $_ <= $#table; $_++) {
             $decode_table[ord($table[$_])] = $_;
        }
        local $_ = $_[0];
        s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
        pack('B' . (int(length($_) / 8) * 8), $_);
    }
    
    my $lp = $req->cookie("pmpre");
       my ($clogin, $cID) = split(/\|/, pm_decode($lp));
       
       my $cookie_pmpre_login = $req->cookie( 
    					-NAME           => "username",
    					-VALUE          => "$clogin",
    					-EXPIRES        => "+1y",
    					-PATH           => "/"
    	   );
    	   
    	   print $req->header(-COOKIE => $cookie_pmpre_login);
    	   
    	   exit;

  10. #10
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh and the pm_encode looks like this:

    Code:
    sub pm_encode
    {
        @table = (('A' .. 'Z'), ('a' .. 'z'), ('0' .. '9'), '+', '/', '|');
        local $_ = unpack('B*', $_[0]);
        $_ .= '0' x (6 - (length($_) % 6)) if (length($_) % 6) != 0;
        s/.{6}/$table[ord(pack('B6', $&)) >> 2]/eg; $_;
    }

  11. #11
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would this work if i put it with everything else that i have got? ie:
    Most likely it will not but it might with some work on your code. Go back and read the documentation of the CGI.pm module I posted a link to so you can understand how to use the CGI.pm module.

    I'm sorry but those sub routines are quite confusing, especially they way they are coded. I don't even see how the pm_decode sub is doing anything useful. It could be and I just don't understand how.

  12. #12
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is no problem, i know the decode sub. but i just nedd to make it a working script that will actually load. im looking at the original coding and seeing if i can alter it into my own version.

  13. #13
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I am understanding better now what you are doing. You have a cookie named "pmpre" already stored on the client machine. It's value is an encoded string, something like:

    pmpre=encoded value here

    Once the value is decoded it becomes a pipe delimited string with two data fields: $clogin and $cID. If that is correct then what you are trying to do should work as long as you understand how to use the CGI.pm module and when to and not to print headers. Sorry for not understanding that earlier.

  14. #14
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah that is what im trying to do. Although i am trying to put it back as a new cookie named "username" which has the value of $clogin
    I had a read through the cgi.pm docs but it is quite confusing.

    I dont understand how to make headers
    The code above i took from other parts of the includes on the site so i know they must work, I just need to get my code working now.

  15. #15
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you trying to do this within your existing script or are you writing a seperate script just to rewrite the cookie?

  16. #16
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am writing a seperate script.
    I am just trying to put an addon on that i made in php. I cant seem to get php to pick up on the $username varible made by cgi. so i am trying to put a new cookie up with user name i can take from there.

  17. #17
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Me01273
    I am writing a seperate script.
    I am just trying to put an addon on that i made in php. I cant seem to get php to pick up on the $username varible made by cgi. so i am trying to put a new cookie up with user name i can take from there.
    Give this a try:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp "fatalsToBrowser";
    my $q = new CGI;
    my @table = ('A'..'Z','a'..'z','0'..'9','+','/','|');
    my $lp = $q->cookie('pmpre');
    if ($lp) {
       my ($clogin, $cID) = split(/\|/, pm_decode($lp));
       my $cookie_pmpre_login = $q->cookie( 
          -NAME           => 'username',
          -VALUE          => $clogin,
          -EXPIRES        => '+1y',
          -PATH           => '/'
       );
       print $q->header(-cookie => $cookie_pmpre_login);
       print qq~Cookie was found and was reformatted: $cookie_pmpre_login~;
       exit;
    }
    else {
       print $q->header();
       print qq~Cookie was not found~;
       exit;
    }
    
    sub pm_encode
    {
        local $_ = unpack('B*', $_[0]);
        $_ .= '0' x (6 - (length($_) % 6)) if (length($_) % 6) != 0;
        s/.{6}/$table[ord(pack('B6', $&)) >> 2]/eg; $_;
    }
    
    sub pm_decode
    {
        my @decode_table = ();
        for ($_ = 0; $_ <= $#table; $_++) {
             $decode_table[ord($table[$_])] = $_;
        }
        local $_ = $_[0];
        s/./unpack('B6', chr($decode_table[ord($&)] << 2))/eg;
        pack('B' . (int(length($_) / 8) * 8), $_);
    }

  18. #18
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that but it still gives me internal server error.

  19. #19
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make sure you upload the script in ASCII (text) mode (not binary mode) and that the path to perl is correct for your server. I tested the code and it works fine for me.

  20. #20
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i will need to get onto my technical manager and find out abaout the perl server stuff,yeah i was uploading in ascii. I think my server may be up the creek!?!

  21. #21
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also make sure to set the file permission if necessary. Most perl based cgi scripts will need to be chmod 755.

  22. #22
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks for all your help. It has been greatly apreciated.

  23. #23
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    in this particular script

    pm_decode = base64_decode

    perl to php

    hope that helps


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •