NTLM.pm

filter: Statementstmt 93.8% 213 / 227 Branchbran 55.0% 22 / 40 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 84.3% 27 / 32 Podpod 34.7% 8 / 23 totaltotal 83.6% 276 / 330 CC 26 SCAR 11.5
CC
26
Cov
88%
ntlm
8.2
ntlmv2
6.0
ntlm_host
6.0
CRAP 3.2
« NTLM/DES.pm ↑ Summary NTLM/MD4.pm »
1 #!/usr/local/bin/perl
2  
3 package Authen::NTLM;
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Authen::NTLM::DES;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Authen::NTLM::MD4;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Digest::HMAC_MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 require Exporter;
12  
13 =head1 NAME
14  
15 Authen::NTLM - An NTLM authentication module
16  
17 =head1 SYNOPSIS
18  
19 use Mail::IMAPClient;
20 use Authen::NTLM;
21 my $imap = Mail::IMAPClient->new(Server=>'imaphost');
22 ntlm_user($username);
23 ntlm_password($password);
24 $imap->authenticate("NTLM", Authen::NTLM::ntlm);
25 :
26 $imap->logout;
27 ntlm_reset;
28 :
29  
30 or
31  
32 ntlmv2(1);
33 ntlm_user($username);
34 ntlm_host($host);
35 ntlm_password($password);
36 :
37  
38 or
39  
40 my $ntlm = Authen::NTLM-> new(
41 host => $host,
42 user => $username,
43 domain => $domain,
44 password => $password,
45 version => 1,
46 );
47 $ntlm-> challenge;
48 :
49 $ntlm-> challenge($challenge);
50  
51  
52  
53 =head1 DESCRIPTION
54  
55 This module provides methods to use NTLM authentication. It can
56 be used as an authenticate method with the Mail::IMAPClient module
57 to perform the challenge/response mechanism for NTLM connections
58 or it can be used on its own for NTLM authentication with other
59 protocols (eg. HTTP).
60  
61 The implementation is a direct port of the code from F<fetchmail>
62 which, itself, has based its NTLM implementation on F<samba>. As
63 such, this code is not especially efficient, however it will still
64 take a fraction of a second to negotiate a login on a PII which is
65 likely to be good enough for most situations.
66  
67 =head2 FUNCTIONS
68  
69 =over 4
70  
71 =item ntlm_domain()
72  
73 Set the domain to use in the NTLM authentication messages.
74 Returns the new domain. Without an argument, this function
75 returns the current domain entry.
76  
77 =item ntlm_user()
78  
79 Set the username to use in the NTLM authentication messages.
80 Returns the new username. Without an argument, this function
81 returns the current username entry.
82  
83 =item ntlm_password()
84  
85 Set the password to use in the NTLM authentication messages.
86 Returns the new password. Without an argument, this function
87 returns the current password entry.
88  
89 =item ntlm_reset()
90  
91 Resets the NTLM challenge/response state machine so that the next
92 call to C<ntlm()> will produce an initial connect message.
93  
94 =item ntlm()
95  
96 Generate a reply to a challenge. The NTLM protocol involves an
97 initial empty challenge from the server requiring a message
98 response containing the username and domain (which may be empty).
99 The first call to C<ntlm()> generates this first message ignoring
100 any arguments.
101  
102 The second time it is called, it is assumed that the argument is
103 the challenge string sent from the server. This will contain 8
104 bytes of data which are used in the DES functions to generate the
105 response authentication strings. The result of the call is the
106 final authentication string.
107  
108 If C<ntlm_reset()> is called, then the next call to C<ntlm()> will
109 start the process again allowing multiple authentications within
110 an application.
111  
112 =item ntlmv2()
113  
114 Use NTLM v2 authentication.
115  
116 =back
117  
118 =head2 OBJECT API
119  
120 =over
121  
122 =item new %options
123  
124 Creates an object that accepts the following options: C<user>, C<host>,
125 C<domain>, C<password>, C<version>.
126  
127 =item challenge [$challenge]
128  
129 If C<$challenge> is not supplied, first-stage challenge string is generated.
130 Otherwise, the third-stage challenge is generated, where C<$challenge> is
131 assumed to be extracted from the second stage of NTLM exchange. The result of
132 the call is the final authentication string.
133  
134 =back
135  
136 =head1 AUTHOR
137  
138 David (Buzz) Bussenschutt <davidbuzz@gmail.com> - current maintainer
139 Dmitry Karasik <dmitry@karasik.eu.org> - nice ntlmv2 patch, OO extensions.
140 Andrew Hobson <ahobson@infloop.com> - initial ntlmv2 code
141 Mark Bush <Mark.Bush@bushnet.demon.co.uk> - perl port
142 Eric S. Raymond - author of fetchmail
143 Andrew Tridgell and Jeremy Allison for SMB/Netbios code
144  
145 =head1 SEE ALSO
146  
147 L<perl>, L<Mail::IMAPClient>, L<LWP::Authen::Ntlm>
148  
149 =head1 HISTORY
150  
151 1.09 - fix CPAN ticket # 70703
152 1.08 - fix CPAN ticket # 39925
153 1.07 - not publicly released
154 1.06 - relicense as GPL+ or Artistic
155 1.05 - add OO interface by Dmitry Karasik
156 1.04 - implementation of NTLMv2 by Andrew Hobson/Dmitry Karasik
157 1.03 - fixes long-standing 1 line bug L<http://rt.cpan.org/Public/Bug/Display.html?id=9521> - released by David Bussenschutt 9th Aug 2007
158 1.02 - released by Mark Bush 29th Oct 2001
159  
160 =cut
161  
162 4 $VERSION = "1.09";
163 4 @ISA = qw(Exporter);
164 4 @EXPORT = qw(ntlm ntlm_domain ntlm_user ntlm_password ntlm_reset ntlm_host ntlmv2);
165  
166 4 my $domain = "";
167 4 my $user = "";
168 4 my $password = "";
169  
170 4 my $str_hdr = "vvV";
171 4 my $hdr_len = 8;
172 4 my $ident = "NTLMSSP";
173  
174 4 my $msg1_f = 0x0000b207;
175 4 my $msg1 = "Z8VV";
176 4 my $msg1_hlen = 16 + ($hdr_len*2);
177  
178 4 my $msg2 = "Z8Va${hdr_len}Va8a8a${hdr_len}";
179 4 my $msg2_hlen = 12 + $hdr_len + 20 + $hdr_len;
180  
181 4 my $msg3 = "Z8V";
182 4 my $msg3_tl = "V";
183 4 my $msg3_hlen = 12 + ($hdr_len*6) + 4;
184  
185 4 my $state = 0;
186  
187 4 my $host = "";
188 4 my $ntlm_v2 = 0;
189 4 my $ntlm_v2_msg3_flags = 0x88205;
190  
191  
192 # Domain Name supplied on negotiate
193 4 ▶use constant NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED => 0x00001000;
Subroutine BEGIN: called CC=1 SCAR=0.0
194 # Workstation Name supplied on negotiate
195 4 ▶use constant NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED => 0x00002000;
Subroutine BEGIN: called CC=1 SCAR=0.0
196 # Try to use NTLMv2
197 4 ▶use constant NTLMSSP_NEGOTIATE_NTLM2 => 0x00080000;
Subroutine BEGIN: called CC=1 SCAR=0.0
198  
199  
200 # Object API
201  
202 sub new
203 2 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
204 2 my ( $class, %opt) = @_;
205 2 for (qw(domain user password host)) {
206 8 ▶ $opt{$_} = "" unless defined $opt{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{$_} 2 6 8
207 }
208 2 ▶ $opt{version} ||= 1;
Condition100%2/2
$opt{"version"} ||= 1
l!l
11
MC/DC100%1/1
$opt{"version"} ||= 1
$opt{"version"}
Truth table100%2/2
A
A= $opt{"version"}
Aresult
11
00
209 2 return bless { %opt }, $class;
210 }
211  
212 sub challenge
213 4 ▶{
Subroutine challenge: called CC=2 SCAR=6.9
Pod documented
214 4 my ( $self, $challenge) = @_;
215 4 $state = defined $challenge;
216 4 ($user,$domain,$password,$host) = @{$self}{qw(user domain password host)};
217 4 ▶ $ntlm_v2 = ($self-> {version} eq '2') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'version'} eq '2' ? : 2 2 4
218 4 return ntlm($challenge);
219 }
220  
221 eval "sub $_ { \$#_ ? \$_[0]->{$_} = \$_[1] : \$_[0]->{$_} }"
222 0 ▶ for qw(user domain password host version);
Subroutine domain: called CC=2 SCAR=10.0
Subroutine host: not called CC=2 SCAR=10.0
Subroutine password: called CC=2 SCAR=10.0
Subroutine user: called CC=2 SCAR=10.0
Subroutine version: not called CC=2 SCAR=10.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 5 branches
BranchTrueFalseTotal
$#_ ? : 0 1 1
$#_ ? : 0 0 0
$#_ ? : 0 1 1
$#_ ? : 0 1 1
$#_ ? : 0 0 0
223  
224 # Function API
225  
226 sub ntlm_domain
227 1 ▶{
Subroutine ntlm_domain: called CC=2 SCAR=7.1
Pod documented
228 1 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
229 {
230 1 $domain = shift;
231 }
232 1 return $domain;
233 }
234  
235 sub ntlm_user
236 1 ▶{
Subroutine ntlm_user: called CC=2 SCAR=7.1
Pod documented
237 1 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
238 {
239 1 $user = shift;
240 }
241 1 return $user;
242 }
243  
244 sub ntlm_password
245 1 ▶{
Subroutine ntlm_password: called CC=2 SCAR=7.1
Pod documented
246 1 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
247 {
248 1 $password = shift;
249 }
250 1 return $password;
251 }
252  
253 sub ntlm_reset
254 0 ▶{
Subroutine ntlm_reset: not called CC=1 SCAR=6.9
Pod documented
255 0 $state = 0;
256 }
257  
258 sub ntlmv2
259 0 ▶{
Subroutine ntlmv2: not called CC=2 SCAR=17.9
Pod documented
260 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
261 0 $ntlm_v2 = shift;
262 }
263 0 return $ntlm_v2;
264 }
265  
266 0 ▶sub ntlm_host {
Subroutine ntlm_host: not called CC=2 SCAR=17.9
Pod undocumented
267 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
268 0 $host = shift;
269 }
270 0 return $host;
271 }
272  
273 sub ntlm
274 6 ▶{
Subroutine ntlm: called CC=8 SCAR=21.0
Pod documented
275 6 my ($challenge) = @_;
276  
277 6 my ($flags, $user_hdr, $domain_hdr,
278 $u_off, $d_off, $c_info, $lmResp, $ntResp, $lm_hdr,
279 $nt_hdr, $wks_hdr, $session_hdr, $lm_off, $nt_off,
280 $wks_off, $s_off, $u_user, $msg1_host, $host_hdr, $u_host);
281 6 my $response;
282 6 ▶ if ($state)
Branch: 1 branch
BranchTrueFalseTotal
if ($state) { } 3 3 6
283 {
284  
285 3 $challenge =~ s/^\s*//;
286 3 $challenge = decode_base64($challenge);
287 3 $c_info = &decode_challenge($challenge);
288 3 $u_user = &unicode($user);
289 3 ▶ if (!$ntlm_v2) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ntlm_v2) { } 2 1 3
elsif ($ntlm_v2 eq '1') { } 1 0 1
290 2 $domain = substr($challenge, $c_info->{domain}{offset}, $c_info->{domain}{len});
291 2 $lmResp = &lmEncrypt($c_info->{data});
292 2 $ntResp = &ntEncrypt($c_info->{data});
293 2 $flags = pack($msg3_tl, $c_info->{flags});
294 }
295 elsif ($ntlm_v2 eq '1') {
296 1 $lmResp = &lmv2Encrypt($c_info->{data});
297 1 $ntResp = &ntv2Encrypt($c_info->{data}, $c_info->{target_data});
298 1 $flags = pack($msg3_tl, $ntlm_v2_msg3_flags);
299 }
300 else {
301 0 $domain = &unicode($domain);#substr($challenge, $c_info->{domain}{offset}, $c_info->{domain}{len});
302 0 $lmResp = &lmEncrypt($c_info->{data});
303 0 $ntResp = &ntEncrypt($c_info->{data});
304 0 $flags = pack($msg3_tl, $c_info->{flags});
305 }
306 3 ▶ $u_host = &unicode(($host ? $host : $user));
Branch: 1 branch
BranchTrueFalseTotal
$host ? : 0 3 3
307 3 $response = pack($msg3, $ident, 3);
308  
309 3 $lm_off = $msg3_hlen;
310 3 $nt_off = $lm_off + length($lmResp);
311 3 $d_off = $nt_off + length($ntResp);
312 3 $u_off = $d_off + length($domain);
313 3 $wks_off = $u_off + length($u_user);
314 3 $s_off = $wks_off + length($u_host);
315 3 $lm_hdr = &hdr($lmResp, $msg3_hlen, $lm_off);
316 3 $nt_hdr = &hdr($ntResp, $msg3_hlen, $nt_off);
317 3 $domain_hdr = &hdr($domain, $msg3_hlen, $d_off);
318 3 $user_hdr = &hdr($u_user, $msg3_hlen, $u_off);
319 3 $wks_hdr = &hdr($u_host, $msg3_hlen, $wks_off);
320 3 $session_hdr = &hdr("", $msg3_hlen, $s_off);
321 3 $response .= $lm_hdr . $nt_hdr . $domain_hdr . $user_hdr .
322 $wks_hdr . $session_hdr . $flags .
323 $lmResp . $ntResp . $domain . $u_user . $u_host;
324 }
325 else # first response;
326 {
327 3 my $f = $msg1_f;
328 3 ▶ if (!length $domain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $domain) 0 3 3
329 0 $f &= ~NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED;
330 }
331 3 $msg1_host = $user;
332 3 ▶ if ($ntlm_v2 and $ntlm_v2 eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ntlm_v2 and $ntlm_v2 eq 1) 1 2 3
Condition66%2/3
$ntlm_v2 and $ntlm_v2 eq 1
!ll&&!rl&&r
201
MC/DC50%1/2
$ntlm_v2 and $ntlm_v2 eq 1
$ntlm_v2 $ntlm_v2 eq 1
Truth table66%2/3
A and B
A= $ntlm_v2B= $ntlm_v2 eq 1
ABresult
0X0
100
111
333 1 $f &= ~NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED;
334 1 $f |= NTLMSSP_NEGOTIATE_NTLM2;
335 1 $msg1_host = "";
336 }
337  
338 3 $response = pack($msg1, $ident, 1, $f);
339 3 $u_off = $msg1_hlen;
340 3 $d_off = $u_off + length($msg1_host);
341 3 $host_hdr = &hdr($msg1_host, $msg1_hlen, $u_off);
342 3 $domain_hdr = &hdr($domain, $msg1_hlen, $d_off);
343 3 $response .= $host_hdr . $domain_hdr . $msg1_host . $domain;
344 3 $state = 1;
345 }
346 6 return encode_base64($response, "");
347 }
348  
349 sub hdr
350 24 ▶{
Subroutine hdr: called CC=2 SCAR=6.9
Pod undocumented
351 24 my ($string, $h_len, $offset) = @_;
352  
353 24 my ($res, $len);
354 24 $len = length($string);
355 24 ▶ if ($string)
Branch: 1 branch
BranchTrueFalseTotal
if ($string) { } 20 4 24
356 {
357 20 $res = pack($str_hdr, $len, $len, $offset);
358 }
359 else
360 {
361 4 $res = pack($str_hdr, 0, 0, $offset - $h_len);
362 }
363 24 return $res;
364 }
365  
366 sub decode_challenge
367 6 ▶{
Subroutine decode_challenge: called CC=2 SCAR=6.9
Pod undocumented
368 6 my ($challenge) = @_;
369  
370 6 my $res;
371 6 my (@res, @hdr);
372 6 my $original = $challenge;
373  
374 6 ▶ $res->{buffer} = $msg2_hlen < length $challenge
Branch: 1 branch
BranchTrueFalseTotal
$msg2_hlen < length $challenge ? : 6 0 6
375 ? substr($challenge, $msg2_hlen) : '';
376 6 $challenge = substr($challenge, 0, $msg2_hlen);
377 6 @res = unpack($msg2, $challenge);
378 6 $res->{ident} = $res[0];
379 6 $res->{type} = $res[1];
380 6 @hdr = unpack($str_hdr, $res[2]);
381 6 $res->{domain}{len} = $hdr[0];
382 6 $res->{domain}{maxlen} = $hdr[1];
383 6 $res->{domain}{offset} = $hdr[2];
384 6 $res->{flags} = $res[3];
385 6 $res->{data} = $res[4];
386 6 $res->{reserved} = $res[5];
387 6 $res->{empty_hdr} = $res[6];
388 6 @hdr = unpack($str_hdr, $res[6]);
389 6 $res->{target}{len} = $hdr[0];
390 6 $res->{target}{maxlen} = $hdr[1];
391 6 $res->{target}{offset} = $hdr[2];
392 6 $res->{target_data} = substr($original, $hdr[2], $hdr[1]);
393  
394 6 return $res;
395 }
396  
397 sub unicode
398 12 ▶{
Subroutine unicode: called CC=2 SCAR=6.9
Pod undocumented
399 12 my ($string) = @_;
400 12 my ($reply, $c, $z) = ('');
401  
402 12 $z = sprintf "%c", 0;
403 12 foreach $c (split //, $string)
404 {
405 48 $reply .= $c . $z;
406 }
407 12 return $reply;
408 }
409  
410 sub NTunicode
411 2 ▶{
Subroutine NTunicode: called CC=2 SCAR=6.9
Pod undocumented
412 2 my ($string) = @_;
413 2 my ($reply, $c);
414  
415 2 foreach $c (map {ord($_)} split(//, $string))
416 {
417 8 $reply .= pack("v", $c);
418 }
419 2 return $reply;
420 }
421  
422 sub lmEncrypt
423 2 ▶{
Subroutine lmEncrypt: called CC=1 SCAR=0.0
Pod undocumented
424 2 my ($data) = @_;
425  
426 2 my $p14 = substr($password, 0, 14);
427 2 $p14 =~ tr/a-z/A-Z/;
428 2 $p14 .= "\0"x(14-length($p14));
429 2 my $p21 = E_P16($p14);
430 2 $p21 .= "\0"x(21-length($p21));
431 2 my $p24 = E_P24($p21, $data);
432 2 return $p24;
433 }
434  
435 sub ntEncrypt
436 2 ▶{
Subroutine ntEncrypt: called CC=1 SCAR=0.0
Pod undocumented
437 2 my ($data) = @_;
438  
439 2 my $p21 = &E_md4hash;
440 2 $p21 .= "\0"x(21-length($p21));
441 2 my $p24 = E_P24($p21, $data);
442 2 return $p24;
443 }
444  
445 sub E_md4hash
446 2 ▶{
Subroutine E_md4hash: called CC=1 SCAR=0.0
Pod undocumented
447 2 my $wpwd = &NTunicode($password);
448 2 my $p16 = mdfour($wpwd);
449 2 return $p16;
450 }
451  
452 1 ▶sub lmv2Encrypt {
Subroutine lmv2Encrypt: called CC=1 SCAR=0.0
Pod undocumented
453 1 my ($data) = @_;
454  
455 1 my $u_pass = &unicode($password);
456 1 my $ntlm_hash = mdfour($u_pass);
457  
458 1 my $u_user = &unicode("\U$user\E");
459 1 my $u_domain = &unicode("$domain");
460 1 my $concat = $u_user . $u_domain;
461  
462 1 my $hmac = Digest::HMAC_MD5->new($ntlm_hash);
463 1 $hmac->add($concat);
464 1 my $ntlm_v2_hash = $hmac->digest;
465  
466 # Firefox seems to use this as its random challenge
467 1 my $random_challenge = "\0" x 8;
468  
469 1 my $concat2 = $data . $random_challenge;
470  
471 1 $hmac = Digest::HMAC_MD5->new($ntlm_v2_hash);
472 1 $hmac->add(substr($data, 0, 8) . $random_challenge);
473 1 my $r = $hmac->digest . $random_challenge;
474  
475 1 return $r;
476 }
477  
478 1 ▶sub ntv2Encrypt {
Subroutine ntv2Encrypt: called CC=1 SCAR=0.0
Pod undocumented
479 1 my ($data, $target) = @_;
480  
481 1 my $u_pass = &unicode($password);
482 1 my $ntlm_hash = mdfour($u_pass);
483  
484 1 my $u_user = &unicode("\U$user\E");
485 1 my $u_domain = &unicode("$domain");
486 1 my $concat = $u_user . $u_domain;
487  
488 1 my $hmac = Digest::HMAC_MD5->new($ntlm_hash);
489 1 $hmac->add($concat);
490 1 my $ntlm_v2_hash = $hmac->digest;
491  
492 1 my $zero_long = "\000" x 4;
493 1 my $sig = pack("H8", "01010000");
494 1 my $time = pack("VV", (time + 11644473600) + 10000000);
495 1 my $rand = "\0" x 8;
496 1 my $blob = $sig . $zero_long . $time . $rand . $zero_long .
497 $target . $zero_long;
498  
499 1 $concat = $data . $blob;
500  
501 1 $hmac = Digest::HMAC_MD5->new($ntlm_v2_hash);
502 1 $hmac->add($concat);
503  
504 1 my $d = $hmac->digest;
505  
506 1 my $r = $d . $blob;
507  
508 1 return $r;
509 }
510  
511 4 1;
« NTLM/DES.pm ↑ Summary NTLM/MD4.pm »