iThunder has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I am trying to use following code (from CPAN website) to do NTLM authentication with perl but its not working.

#!/usr/bin/perl use LWP::UserAgent; use HTTP::Request::Common; use LWP::Debug qw(+); use Authen::NTLM; use strict; use warnings; ntlmv2(1); my $url = 'http://192.168.1.27/index.html'; # Set up the ntlm client and then the base64 encoded ntlm handshake me +ssage my $ua = LWP::UserAgent->new(keep_alive=>1); my $host = '192.168.1.27:80'; my $uname = 'test.com\User1'; my $psswd = 'test1234'; $ua->credentials('192.168.1.27:80', '', "$uname", "$psswd"); my $request = GET $url; print "--Performing request now...-----------\n"; my $response = $ua->request($request); print "--Done with request-------------------\n"; if ($response->is_success) {print "It worked!->" . $response->code . " +\n"} else {print "It didn't work!->" . $response->code . "\n"

Looking at the packet capture, i see that ???? sent as domain rather test.com. Is there something i am doing wrong?

Replies are listed 'Best First'.
Re: wrong domain name when using LWP::Authen::Ntlm
by kcott (Archbishop) on Dec 19, 2016 at 05:16 UTC

    G'day iThunder,

    There's a number of issues with your post that don't make it easy for us to help you:

    • Please don't type your code into your post as it often results in typos. Copy and paste the actual code you're running.
      • You've identified one typo (in "Re^2: wrong domain name when using LWP::Authen::Ntlm"): "ah sorry it was a typo... i have corrected the post.". [Aside: Please add an Update to posts you alter stating what was changed (as described in "How do I change/delete my post?").]
      • You have another typo at the end of your posted code: an unterminated else block. Your code, as shown, won't compile.
      • I'm wondering if there's a third typo, in the $uname assignment. If your code has double-quotes, but you typed single-quotes, you may be suffering from this problem:
        $ perl -E 'my $x = q{test.com\User1}; say "$x"' test.com\User1 $ perl -E 'my $x = qq{test.com\User1}; say "$x"' test.comSER1
        See uc if you're unfamiliar with the \U escape.
    • Your code contains a number of print statements but you haven't shown the output. If that's useful to you, it's probably also useful to us. Please include all output, including any diagnostic messages, as described in "How do I post a question effectively?".

    — Ken

      thanks. I think i found the problem. The issue was i was setting NTLMv2 authentication which would set credentials differently.

Re: wrong domain name when using LWP::Authen::Ntlm
by huck (Prior) on Dec 19, 2016 at 02:18 UTC

    Just a thought, you are going to 192.168.1.27, but your credentials are registered for 192.168.147.247:80

      ah sorry it was a typo... i have corrected the post. Still not working and seeing same ??? in domain name.
Re: wrong domain name when using LWP::Authen::Ntlm
by xytras78 (Initiate) on Feb 28, 2017 at 20:48 UTC

    I'm running into a VERY similar issue, though I am *not* specifying NTLMv2 as the original poster was doing. I'd really appreciate any pointers anyone could offer. I'm really at a loss here.

    Using the original code above as a base, but fixing the syntax errors... In my code, I'm clearly setting a specific domain. 'mydom' -- however after looking at the WireShark capture, I can see the NTLM challenge back from the webserver with a Target Name of 'differentdomain'. The GET then sends back 'differentdomain\User1' instead of the specified 'mydom\User1'. Any thoughts???

    #!/usr/bin/perl -w use LWP::UserAgent; use HTTP::Request::Common; use LWP::Debug qw(+); use Authen::NTLM; use strict; use warnings; my $url = 'http://mywebserver.com/Path/Path2/MyFile.htm'; # Set up the ntlm client and then the base64 encoded ntlm handshake me +ssage my $ua = LWP::UserAgent->new(keep_alive=>1); my $host = 'mywebserver.com:80'; my $uname = 'mydom\User1'; my $psswd = 'test1234'; $ua->credentials("$host", '', "$uname", "$psswd"); my $request = GET $url; print "--Performing request now...-----------\n"; my $response = $ua->request($request); print "--Done with request-------------------\n"; if ($response->is_success) {print "It worked!->" . $response->code . " +\n";} else {print "It didn't work!->" . $response->code . "\n"}