How to ignore 'Certificate Verify Failed' error in perl? - perl

I want to access a website where the certificate cannot be verified. I'm using WWW::Mechanize get request. So how would go about ignoring this and continues to connect to the website?

use IO::Socket::SSL qw();
use WWW::Mechanize qw();
my $mech = WWW::Mechanize->new(ssl_opts => {
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0, # this key is likely going to be removed in future LWP >6.04
});
With IO::Socket::SSL earlier than 1.79, see PERL_LWP_SSL_VERIFY_HOSTNAME.

my $mech = WWW::Mechanize->new( 'ssl_opts' => { 'verify_hostname' => 0 } );

Related

Perl Mechanize, making a script to login on a webpage

I'm making a script to automatically login on a webpage, it starts with this:
use HTTP::Cookies;
use WWW::Mechanize;
my $cookie_jar = HTTP::Cookies->new;
my $agent = WWW::Mechanize->new( cookie_jar => $cookie_jar );
my $server_endpoint = "http://10.11.5.2/index.php";
$agent->post($server_endpoint,[tg => 'login', referer => 'index.php',login => 'login',sAuthType=>'LOL',nickname=>'admin',password=>'012345678',submit=>'Login']);
print "Set Cookie Jar?\n", $agent->cookie_jar->as_string, "\n";
print $agent->content;
And I get a page saying "you are not logged in"...but when I use the same credentials in the browser everything works.
So I retrieved the value of the cookie sent by the server (located in the set-cookie header of the response) with $agent->cookie_jar->as_string, here it's OV3176019645=3inkmpee0r5gpfm41c3iltvda1.
THen I put it in the POST request before sending it, like the following:
use HTTP::Cookies;
use WWW::Mechanize;
my $cookie_jar = HTTP::Cookies->new;
my $agent = WWW::Mechanize->new( cookie_jar => $cookie_jar );
my $server_endpoint = "http://10.11.5.2/index.php";
$agent->add_header( Cookie => 'OV3176019645=osovm5u0vfc2dmkuo6bqn6hah1' );
$agent->post($server_endpoint,[tg => 'login', referer => 'index.php',login => 'login',sAuthType=>'LOL',nickname=>'admin',password=>'012345678',submit=>'Login']);
print $agent->content;
This time everything works...
So, my question is: how can I automatically get the value of the cookie given by the server before I send my request ?
Another problem also appears, it's that the server sends back a cookie with the following shape (in the set-cookie header):
Set-Cookie3: OV3176019645=3inkmpee0r5gpfm41c3iltvda1; path="/"; domain=10.11.5.2; path_spec; discard; version=0
And I just need the 1st item of this cookie (OV3176019....).
I hope I was clear in my explanations.
Thanks

Attempting to ignore local SSL certificate [duplicate]

I want to access a website where the certificate cannot be verified. I'm using WWW::Mechanize get request. So how would go about ignoring this and continues to connect to the website?
use IO::Socket::SSL qw();
use WWW::Mechanize qw();
my $mech = WWW::Mechanize->new(ssl_opts => {
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0, # this key is likely going to be removed in future LWP >6.04
});
With IO::Socket::SSL earlier than 1.79, see PERL_LWP_SSL_VERIFY_HOSTNAME.
my $mech = WWW::Mechanize->new( 'ssl_opts' => { 'verify_hostname' => 0 } );

Perl and LWP not authenticating

I'm trying to get an LWP request working to an https server. I have been given a user & pass, advised to use basic authentication. I've tried various chunks of code, and all seem to get an authentication error. My current code is...
use warnings;
use strict;
use Data::Dumper;
use LWP;
my $ua = LWP::UserAgent->new( keep_alive => 1 );
##also tried by $ua->credentials('domain','','user','pass');
##not sure if I need 'realm' or how I get it, as no popup on screen.
my $request = HTTP::Request->new( GET => "https://my.url.com/somepath/" );
$request->authorization_basic('myuser','mypass');
$request->header( 'Cache-Control' => 'no-cache' );
print $response->content;
print Dumper $response;
The server gives a security error, but if I look at a dump of $response, I see the following...
'_rc' => '401',
'_headers' => bless( { .... lots of stuff
'title' => 'Security Exception',
'client-warning' => 'Missing Authenticate header',
'client-ssl-socket-class' => 'IO::Socket::SSL',
...
'expires' => '-1'
}, 'HTTP::Headers' ),
'_msg' => 'Unauthorized',
'_request' => bless( {
'_content' => '',
'_uri' => bless( do{\(my $o = 'https:theurlabove')}, 'URI::https' ),
'_method' => 'GET',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.04',
'cache-control' => 'no-cache',
'authorization' => 'Basic dzx..........'
}, 'HTTP::Headers' ),
I'm trying to understand whats happening, it looks like in the original request, it has the headers in there, but in the response, its saying I'm 'Missing Authenticate Header'.
Is there something amiss with the code, or something I'm misunderstanding with the request/respinse ?
Thanks.
The "Missing Authenticate header" message is coming from LWP itself. This means that it couldn't find an authenticate header in the target response. This might mean that your proxy settings are misconfigured, if you have anything like that.
I don't know if this is what you are looking for but I came across the same problem trying to authenticate to a webpage and had to solve it with WWW::Mechanize. I had to go to the first page and login then request the page I wanted.
use WWW::Mechanize;
my $loginPage = "http://my.url.com/login.htm"; # Authentication page
my $mech = WWW::Mechanize->new(); # Create new brower object
$mech->get($loginPage); # Go to login page
$mech->form_name('LogonForm'); # Search form named LogonForm
$mech->field("username", myuser); # Fill out username field
$mech->field("password", mypass); # Fill out password field
$mech->click("loginloginbutton"); # submit form
$mech->get("http://my.url.com/somepath/"); # Get webpage
# Some more code here with $mech->content()

Selecting SSL_VERIFY_NONE for SSL_verify_mode

I am trying to create a client connection to an internal ssl site that does not have a certificate and needs to bypass the proxy.
I am able to bypass the proxy, and I am able to connect to the site and create a client connection, however, i am getting this ugly warning:
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
together with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at C:/strawberry/perl/site/lib/LWP/Protocol/http.pm line 31
My Code:
use RPC::XML::Client;
use XML::Simple;
use LWP::Protocol::https;
$ENV{NO_PROXY} = '10.*';
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $server = RPC::XML::Client->new("$vneUrl/api/index.ice",
ssl_opts => { SSL_verify_mode => 'SSL_VERIFY_NONE',
verify_hostname => 0,
SSL_use_cert => 0x00
},
);
That message is from IO::Socket::SSL, and it refers to the constant SSL_VERIFY_NONE it exports rather than the string 'SSL_VERIFY_NONE'.
Secondly, ssl_opts is an argument of LWP::UserAgent's constructor, not RPC::XML::Client's.
Try:
use IO::Socket::SSL qw( SSL_VERIFY_NONE );
RPC::XML::Client->new($uri,
useragent => [
ssl_opts => {
verify_hostname => 0,
SSL_verify_mode => SSL_VERIFY_NONE,
},
],
);
New version I believe you should set to 0 or 1.
I think this was a bug:
500 SSL_verify_mode must be a number and not a string
From:
$useragent->ssl_opts(SSL_verify_mode=>'SSL_VERIFY_NONE');
To:
$useragent->ssl_opts(SSL_verify_mode=>'0');

Unable to login into a site using www:Mechanize

I am using WWW:Mechanize to try to login to a site.
Code
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->get("https://www.amazon.com/gp/css/homepage.html/");
$mech->submit_form(
form_name => 'yaSignIn',
fields => {
email => 'email',
qpassword=> 'pass'
}
);
print $mech->content();
However it is not being logged into the site. What am i doing wrong. The website redirects and says please enable cookies to continue. How do i do that .
Try putting this block before your get.
$mech->cookie_jar(
HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
ignore_discard => 1,
)
);
SuperEdit2: I just tried this myself and it seemed to work. Give it a try.(changed the form number to 3 and added an agent alias)
use strict;
use warnings;
use WWW::Mechanize;
# Create a new instance of Mechanize
my $bot = WWW::Mechanize->new();
$bot->agent_alias( 'Linux Mozilla' );
# Create a cookie jar for the login credentials
$bot->cookie_jar(
HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
ignore_discard => 1,
)
);
# Connect to the login page
my $response = $bot->get( 'https://www.amazon.com/gp/css/homepage.html/' );
# Get the login form. You might need to change the number.
$bot->form_number(3);
# Enter the login credentials.
$bot->field( email => 'email' );
$bot->field( password => 'pass' );
$response = $bot->click();
print $response->decoded_content;