Created
September 16, 2011 23:44
-
-
Save cmlh/1223414 to your computer and use it in GitHub Desktop.
Final (Third) Example from http://www.symantec.com/connect/articles/using-libwhisker with http://perltidy.sourceforge.net/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Define the modules that we intend to use. | |
use strict; | |
use LW2; | |
use Getopt::Std; | |
#Define hashes for our command line options, request #information, response information and cookies. | |
my ( %opts, %request, %response, %jar, $headers_array, $header ); | |
##note the addition of 'U' and 'D' as options | |
getopts( 'dh:m:u:U:D:', \%opts ); | |
#Initialize all the request variables. Some of these we will overwrite. | |
LW2::http_init_request( \%request ); | |
if ( !( defined( $opts{h} ) ) ) { | |
die "You must specify a host to scan.\n"; | |
} | |
if ( defined( $opts{m} ) ) { | |
##defaults to GET if we need to POST | |
if ( $opts{m} =~ /OPTIONS|HEAD|GET/ ) { | |
$request{'whisker'}->{'method'} = $opts{m}; | |
} | |
} | |
if ( defined( $opts{u} ) ) { | |
##don't set URI if method is POST | |
$request{'whisker'}->{'uri'} = $opts{u} unless ( $opts{m} eq "POST" ); | |
} | |
##now set user-agent based on 'U' option ##'F', 'I' or 'N' for Firefox/IE/Netscape | |
##as explained in the text | |
if ( defined( $opts{U} ) ) { | |
if ( $opts{U} eq "F" ) { | |
$request{'User-Agent'} = | |
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040614 Firefox/0.9"; | |
} | |
elsif ( $opts{U} eq "I" ) { | |
$request{'User-Agent'} = | |
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; | |
} | |
elsif ( $opts{U} eq "N" ) { | |
$request{'User-Agent'} = | |
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)"; | |
} | |
else { | |
die "You did not specify a supported \'User-Agent\'.\n"; | |
} | |
} | |
#Set the host that we want to scan | |
$request{'whisker'}->{'host'} = $opts{h}; | |
#Make RFC compliant | |
LW2::http_fixup_request( \%request ); | |
#Do the actual scan. | |
H_REQUEST: | |
if ( LW2::http_do_request( \%request, \%response ) ) { | |
print 'ERROR: ', $response{'whisker'}->{'error'}, "\n"; | |
print $response{'whisker'}->{'data'}, "\n"; | |
} | |
else { | |
##else preserve cookie info for our next request | |
LW2::cookie_read( \%jar, \%response ); | |
#Get the information out of the anonymous array. | |
#'$headers_array' is a reference. | |
$headers_array = $response{'whisker'}->{'header_order'}; | |
print "\n\n"; | |
print "HTTP ", $response{'whisker'}->{'version'}, "\t"; | |
print $response{'whisker'}->{'code'}, "\n"; | |
foreach $header (@$headers_array) { | |
print "$header"; | |
print "\t$response{$header}\n"; | |
} | |
if ( defined( $opts{d} ) ) { | |
"\n\n---------------------------------- ------------------------\n\n"; | |
print $response{'whisker'}->{'data'}, "\n"; | |
} | |
if ( $opts{m} eq "POST" ) { | |
LW2::cookie_write( \%jar, \%request ); | |
$request{'whisker'}->{'method'} = "POST"; | |
$request{'whisker'}->{'uri'} = $opts{u}; | |
$request{'whisker'}->{'data'} = $opts{d}; | |
LW2::http_fixup_request( \%request ); | |
$opts{d} = undef; | |
$opts{m} = undef; | |
goto H_REQUEST; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment