[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Make a perl client fake a GET containing a cookie

Let's say I execute the following code and it doesn't work because the server
doesn't believe I have cookies enabled.  The server returns "Cannot Identify
User. You must allow cookies in  your browser so that PINVAL can identify you after
Login".

My code follows.  It works agains other http's that don't need a cookie.  I'm basically grabbing .HTML and dumping it to the disk.

     use LWP::UserAgent;
     $user_agent = new LWP::UserAgent;
     $request = new HTTP::Request ('GET','http://www.this.com/pinVAL/XS.ISA?Action=GETINSTSTD&ID=951692');
     $response = $user_agent->request($request);

     open FILEHANDLE, ">file.txt";
     print FILEHANDLE $response->content;
     close FILEHANDLE;

What can I do before hand to force the server to recognize my cookie?

I don't know much about the server, but when I sniff the transmission (during a IE 4.0 browser transmission) I see the following during a SEND.

GET /finVAL/Images/developed.jpg HTTP/1.1
Accepted: */*
Referer: http://www.this.com/finVAL/XS.ISA?Action=STCON
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt; version.dll)
Host: www.this.com
Connection: Keep-Allive
Cookie: PINXZZZ=MYACCT; PINDDDID=MYACCT-46158x19377x781178x50891218
0
cwoodwar
Asked:
cwoodwar
  • 3
  • 3
1 Solution
 
tgoetzeCommented:
You can add the cookie in the GET request by sending an optional third parameter to HTTP::Request.

For example:


use LWP::UserAgent;
use HTTP::Headers;

$h = new HTTP::Headers();
$h->header('Cookie' => 'PINXZZZ=MYACCT; PINDDDID=MYACCT-46158x19377x781178x50891218');

     $user_agent = new LWP::UserAgent;
     $request = new HTTP::Request ('GET','http://www.this.com/pinVAL/XS.ISA?Action=GETINSTSTD&ID=951692',$h);
     $response = $user_agent->request($request);

     open FILEHANDLE, ">file.txt";
     print FILEHANDLE $response->content;
     close FILEHANDLE;



You can also check out the HTTP::Cookies module if you want to store new cookies as you go.

0
 
cwoodwarAuthor Commented:
You mentioned the following.

"You can also check out the HTTP::Cookies module if you want to store new cookies as you go"

How do I do this.  I need to check the cookie so I can plug in the values.

How do I do this?

0
 
tgoetzeCommented:
You already have a way of transmitting the request with the approrpiate header (once you know the cookie content). If you can find where IE stores the cookie, you can just grab it and put it in the program. Often times the cookie value doesn't change very often. Look in: c:\windows\Profiles\user_name\Cookies for your cookies.

Alternatively, you can try to have Perl do everything for you. Two ways you can do this. (1) Write a script that does the original registration through the web page and consequently grabs the cookie when it is originally created. This requies a decent sized effort and good understanding of the site you are going to and LWP.

Here's an alternative:
(2) Use Netscape to establish your account originally. Then you can have HTTP::Cookies::Netscape share Netscape's cookies. Here's an example:

use LWP::UserAgent;
use HTTP::Cookies::Netscape;

     $user_agent = new LWP::UserAgent;
     $request = new HTTP::Request ('GET','http://www.this.com/pinVAL/XS.ISA?Action=GETINSTSTD&ID=951692',$h);
     $cookie_jar = HTTP::Cookies::Netscape->new(
                     File => 'C:\netscape\cookies', #CHANGE THIS TO POINT TO YOUR LOCATION
                     Autosave => 1
                   );
     $cookie_jar->add_cookie_header($request); # adds the header to the request, this is
                                               #  what I showed you how to do previously
     $response = $user_agent->request($request);

     #If you think the cookie might have changed, you can:
     $cookie_jar->extract($response);
     $cookie_jar->save();

     open FILEHANDLE, ">file.txt";
     print FILEHANDLE $response->content;
     close FILEHANDLE;



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tgoetzeCommented:
I didn't clean up properly in the last code pasting. Try this as the code sample from in the previous post:

use LWP::UserAgent;
use HTTP::Cookies::Netscape;

     $user_agent = new LWP::UserAgent;
     $request = new HTTP::Request ('GET','http://www.this.com/pinVAL/XS.ISA?Action=GETINSTSTD&ID=951692');
     $cookie_jar = HTTP::Cookies::Netscape->new(
                     File => 'C:\netscape\cookies', #CHANGE THIS TO POINT TO YOUR LOCATION
                     Autosave => 1
                   );
     $cookie_jar->add_cookie_header($request); # adds the header to the request, this is
                                               #  what I showed you how to do previously
     $response = $user_agent->request($request);

     #If you think the cookie might have changed, you can:
     $cookie_jar->extract($response);
     $cookie_jar->save();

     open FILEHANDLE, ">file.txt";
     print FILEHANDLE $response->content;
     close FILEHANDLE;





0
 
cwoodwarAuthor Commented:
I'll give this a try when I get back to my other lab.

Please stand by.

Charles
0
 
cwoodwarAuthor Commented:
We nolonger have access to the target machine.  Therefore I can't 100% validate the answer.

Looking at the code I believe this definitely the answer.

Therefore I give tgoetze 95% out of a 100%.

Thank you very much.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now