LWP身份验证 - 协商

时间:2014-08-27 07:52:06

标签: perl authentication

我尝试了很多解决方案,但仍然没有答案。 我试图从使用身份验证的服务器访问xml网页。 它是kerberos还是NTLM,但是我遇到了麻烦哪个(我试过它们两个......) 我使用LWP,但我一直收到401未经授权的错误。

我使用wireshark来查看浏览器标题和我的脚本创建的标题之间的区别,它们不一样。 凭证出现在不同的地方。

我尝试了很多方法,这是我目前的尝试:

use FindBin;
use lib "$FindBin::Bin/lib";
use File::Basename;
use POSIX qw(strftime);
use LWP::UserAgent;
use LWP::Debug qw(+);
use HTTP::Headers;
use HTTP::Request::Common;
use Authen::NTLM;
#use HTML::TableExtract;
use HTML::Form;
use HTML::Template;
#use MIME::Entity;

my $Options = {
    user => "Citrix",
    host => "<ip>",
    password => "Rel0aded1",
    domain => "lyncent.com",
    timeout => 30,
    protocol => "http",
    AuthMethod => "Negotiate",
    BrowserAgent => "MSIE 6.0; Windows NT 5.0",
    RequestMethod => "GET",
    DataDir => "/tmp",
};
my $browser = LWP::UserAgent->new(
    agent=>'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
    keep_alive=>'1'
);
my $url = "http://<ip>/Citrix/Monitor/OData/v1/Methods/";
ntlm_domain($Options->{'domain'});
ntlm_user($Options->{'user'});
ntlm_password($Options->{'password'});

my $Authorization = Authen::NTLM::ntlm();

my $header = HTTP::Headers->new(
    Content_Type => 'text/html',
    'WWW-Authenticate' => $Options->{'AuthMethod'}
);

$header->header('Authorization' => "NTLM $Authorization");
my $request = HTTP::Request->new($Options->{'RequestMethod'} => $url, $header);
my $res = $browser->request( $request );


# Second stage of authentication
print Dumper $res;
print $res->request->as_string();
my $Challenge = $res->header('WWW-Authenticate');
$Challenge =~ s/^NTLM //g;
$browser->credentials('192.168.100.41:80', 'LYNCENT', "lyncent.com\\Citrix", 'Rel0aded1');
ntlm_domain($Options->{'domain'});
ntlm_user($Options->{'user'});
ntlm_password($Options->{'password'});
#$Authorization = Authen::NTLM::ntlm($Challenge);
$Authorization = Authen::NTLM::ntlm(
    host     => $Options->{'host'},
    user     => $Options->{'user'},
    domain   => $Options->{'domain'},
    password => $Options->{'password'},
    version  => 1,
);
$Authorization -> challenge($Challenge);
$header->header('Authorization' => "NTLM $Authorization");
$header->header('User' => $Options->{'user'});
$header->header('Password' => $Options->{'password'});
$header->header('Domain' => $Options->{'domain'});

$request = HTTP::Request->new($Options->{'RequestMethod'} => $url, $header);

$res = $browser->request($request);
print Dumper $res;
print $res->request->as_string();
#
# ntlm needs to be resetted after second stage 
#
ntlm_reset();

if($res->is_success) {
    print "$Options->{'DataDir'}/test_url" .  $res->content;
}
else {
    print  "not working:".  $res->code ." and " . $res->status_line ."\n";
}

}

这是我的版画标题:

获取http:链接 授权:NTLM TlRMTVNTUAABAAAAB7IAAAYABgAgAAAACwALACYAAABDaXRyaXhseW5jZW50LmNvbQ == User-Agent:Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.0) WWW-Authenticate:谈判 内容类型:text / html

获取http:&#39;链接&#39; 授权:NTLM TlRMTVNTUAADAAAAGAAYAEAAAAAYABgAWAAAAAAAAAAwAAAADAAMAHAAAAAMAAwAfAAAAAAAAABIAAAAAAAAAMzMtTg4NEGeGv0QgPZbp9YSENlLFzZSqlvbV1ohNAOQV0uJR + lLaaFCEhvYCPPNyUMAaQB0AHIA EMAaQB0AHIAaQB4AA == User-Agent:Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.0) WWW-Authenticate:谈判 内容类型:text / html 域名:lyncent.com 密码:Rel0aded1 用户:Citrix

不工作:401和401未经授权

0 个答案:

没有答案