programming4us
programming4us
SECURITY

Web Security : Automating with LibWWWPerl - Simulating Form Input with POST, Capturing and Storing Cookies

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

1. Simulating Form Input with POST

1.1. Problem

You want to programmatically issue requests that mimic form inputs by a user.

1.2. Solution

See Example 1.

Example 1. Basic Perl script to submit a form
#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
$URL  = "http://www.example.com/login.php";
$UA   = LWP::UserAgent->new();

$req  = HTTP::Request::Common::POST( "$URL",
   Content_Type => 'form-data',
   Content => [
    USERNAME => 'admin',
    PASSWORD => '12345',
    Submit   => 'Login'
   ]
);
$resp = $UA->request($req);

# check for error. Print page if it's OK
if ( ( $resp->code() >= 200 ) && ( $resp->code() < 400 ) ) {
    print $resp->decoded_content;
} else {
    print "Error: " . $resp->status_line . "\n";
}

1.3. Discussion

Example 1 shows posting to a simple login page (login.php) with 2 fields: USERNAME and PASSWORD. If you had a list of usernames and passwords you wanted to try programmatically, you could iteratively redefine $req and reinvoke the $UA->request() method to reissue new login attempts—perhaps in a foreach or while loop.

The Submit item in the form data is simply there for the sake of being identical to what a real browser would send. Many applications do not care what the value of the Submit button is, but the browser will send that value anyways. You could imagine, however, some circumstances where a form might have multiple Submit buttons, and the value of the Submit button would be significant. For example, a search page might have Basic Search and Advanced Search buttons, and your script must change the value of the Submit button to tell your application which button was clicked.

2. Capturing and Storing Cookies

2.1. Problem

Most web applications will use cookies, possibly in conjunction with other techniques, to manage state or maintain session identity. To login and stay logged in, your Perl script will have to receive these cookies and send them back throughout its session. Doing this programmatically allows you to also test various attributes of session maintenance.

2.2. Solution

See Example 2.

Example 2. Perl script that automatically captures cookies
#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common;

$myCookies = HTTP::Cookies−>new(
	file     => "cookies.txt",
	autosave => 1,
    );

$URL  = "http://www.example.com/login.php";
$UA   = LWP::UserAgent->new();
$UA->cookie_jar( $myCookies );

$req  = HTTP::Request->new( GET => "http://www.example.com/" );
$resp = $UA->request($req);

# check for error. Print page if it's OK
if ( ( $resp->code() >= 200 ) && ( $resp->code() < 400 ) ) {
    print $resp->decoded_content;
} else {
    print "Error: " . $resp->status_line . "\n";
}

2.3. Discussion

The code in Example 2 assumes you want to store your cookies in a file, perhaps because you want to look at them after your tests run or perhaps because you have engineered malicious cookies in advance and want to load them. You can change the invocation of the cookie_jar() method to create an empty cookie jar (and one that will be lost when the script terminates) by writing $UA->cookie_jar( {} ).

Other  
 
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us