lib/SMB/Auth.pm

filter: Statementstmt 87.9% 248 / 282 Branchbran 72.6% 93 / 128 Conditioncond 46.8% 45 / 96 MC/DCmcdc 19.6% 11 / 56 Subroutinesub 85.7% 24 / 28 Podpod 100% 15 / 15 totaltotal 72.0% 436 / 605 SCAR 55.4
CC
106
Cov
76%
load_user_passwords
90.0
process_spnego
39.1
generate_asn1
21.8
CRAP 255.9
« blib/lib/SMB/Crypt.pm ↑ Summary blib/lib/SMB/v2/Command/Close.pm »
1 # SMB-Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 package SMB::Auth;
17  
18 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use parent 'SMB';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use SMB::Crypt qw(des_crypt56 md4 hmac_md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use SMB::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use SMB::Packer;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use SMB::Time qw(to_nttime);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # Abstract Syntax Notation One (small subset)
33  
34 use constant {
35 2 ASN1_BINARY => 0x04,
36 ASN1_OID => 0x06,
37 ASN1_ENUMERATED => 0x0a,
38 ASN1_SEQUENCE => 0x30,
39 ASN1_APPLICATION => 0x60,
40 ASN1_CONTEXT => 0xa0,
41 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # Generic Security Service API / Simple Protected Negotiation
44  
45 use constant {
46 2 OID_SPNEGO => '1.3.6.1.5.5.2',
47 OID_MECH_NTLMSSP => '1.3.6.1.4.1.311.2.2.10',
48  
49 SPNEGO_ACCEPT_COMPLETED => 0,
50 SPNEGO_ACCEPT_INCOMPLETE => 1,
51 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # NTLMSSP mechanism
54  
55 use constant {
56 2 NTLMSSP_ID_STR => "NTLMSSP\0",
57  
58 NTLMSSP_NEGOTIATE => 1,
59 NTLMSSP_CHALLENGE => 2,
60 NTLMSSP_AUTH => 3,
61 NTLMSSP_SIGNATURE => 4,
62  
63 NTLMSSP_ITEM_TERMINATOR => 0,
64 NTLMSSP_ITEM_NETBIOSHOST => 1,
65 NTLMSSP_ITEM_NETBIOSDOMAIN => 2,
66 NTLMSSP_ITEM_DNSHOST => 3,
67 NTLMSSP_ITEM_DNSDOMAIN => 4,
68 NTLMSSP_ITEM_TIMESTAMP => 7,
69  
70 NTLMSSP_FLAGS_CLIENT => 0x60008215,
71 NTLMSSP_FLAGS_SERVER => 0x628a8215,
72 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 sub new ($) {
75 2 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
76  
77 2 return $class->SUPER::new(
78 ntlmssp_supported => undef,
79 client_host => undef,
80 client_domain => undef,
81 server_challenge => undef,
82 server_host => undef,
83 server_netbios_host => undef,
84 server_netbios_domain => undef,
85 server_dns_host => undef,
86 server_dns_domain => undef,
87 server_timestamp => undef,
88 client_challenge => undef,
89 lm_response => undef,
90 ntlm_response => undef,
91 domain => undef,
92 host => undef,
93 username => undef,
94 session_key => undef,
95 auth_completed => undef,
96 user_passwords => {},
97 parser => SMB::Parser->new,
98 packer => SMB::Packer->new,
99 );
100 }
101  
102 sub set_user_passwords ($$) {
103 0 ▶ my $self = shift;
Subroutine set_user_passwords: not called CC=3 SCAR=24.8
Pod documented
104 0 ▶ my $user_passwords = shift || die "No user passwords to set";
Condition0%0/2
shift() || die('No user passwords to set')
l!l
00
MC/DC0%0/1
shift() || die('No user passwords to set')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
105  
106 0 ▶ die "User passwords should be HASH"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $user_passwords eq "HASH" 0 0 0
107 unless ref($user_passwords) eq 'HASH';
108  
109 0 $self->user_passwords($user_passwords);
110 }
111  
112 sub create_lm_hash ($) {
113 1 ▶ my $password = substr(encode('ISO-8859-1', uc(shift // "")), 0, 14);
Subroutine create_lm_hash: called CC=2 SCAR=7.0
Pod documented
Condition50%1/2
shift() // ""
l!l
10
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
114 1 $password .= "\0" x (14 - length($password));
115  
116 return join('', map {
117 1 des_crypt56('KGS!@#$%', $_)
118 } $password =~ /^(.{7})(.{7})$/);
119 }
120  
121 sub create_ntlm_hash ($) {
122 3 ▶ my $password = encode('UTF-16LE', shift // "");
Subroutine create_ntlm_hash: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
shift() // ""
l!l
30
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
123  
124 3 return md4($password);
125 }
126  
127 sub create_lm_response ($$) {
128 0 ▶ my $lm_hash = shift || die;
Subroutine create_lm_response: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
shift() || die()
l!l
00
MC/DC0%0/1
shift() || die()
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
129 0 my $server_challenge = shift;
130  
131 0 $lm_hash .= "\0" x (21 - length($lm_hash));
132  
133 return join('', map {
134 0 des_crypt56([ map { ord($_) } split '', $server_challenge ], $_)
135 } $lm_hash =~ /^(.{7})(.{7})(.{7})$/);
136 }
137  
138 sub create_ntlmv2_hash ($$$) {
139 3 ▶ my $ntlm_hash = shift || die;
Subroutine create_ntlmv2_hash: called CC=4 SCAR=14.9
Pod documented
Condition50%1/2
shift() || die()
l!l
30
MC/DC0%0/1
shift() || die()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
140 3 ▶ my $username = shift // '';
Condition50%1/2
shift() // ""
l!l
30
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
141 3 ▶ my $domain = shift // '';
Condition50%1/2
shift() // ""
l!l
30
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
142  
143 3 return hmac_md5(encode('UTF-16LE', uc($username . $domain)), $ntlm_hash);
144 }
145  
146 sub create_lmv2_response ($$$$) {
147 1 ▶ return create_ntlmv2_response($_[0], $_[1], $_[2], $_[3], 8);
Subroutine create_lmv2_response: called CC=1 SCAR=0.0
Pod documented
148 }
149  
150 sub create_ntlmv2_response ($$$$;$) {
151 1 ▶ my $ntlm_hash = shift;
Subroutine create_ntlmv2_response: called CC=2 SCAR=6.9
Pod documented
152 1 my $username = shift;
153 1 my $domain = shift;
154 1 my $server_challenge = shift;
155 1 ▶ my $client_challenge_len = shift || 24;
Condition50%1/2
shift() || 24
l!l
10
MC/DC0%0/1
shift() || 24
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
156  
157 1 my $client_challenge = join('', map { chr(rand(0x100)) } 1 .. $client_challenge_len);
158 1 my $ntlmv2_hash = create_ntlmv2_hash($ntlm_hash, $username, $domain);
159  
160 1 return hmac_md5($server_challenge . $client_challenge, $ntlmv2_hash) . $client_challenge;
161 }
162  
163 sub get_user_passwd_line ($$) {
164 0 ▶ my $username = shift;
Subroutine get_user_passwd_line: not called CC=1 SCAR=6.9
Pod documented
165 0 my $password = shift;
166  
167 return "$username:" . join('',
168 0 map { map { sprintf "%02x", ord($_) } split '', $_ }
169 create_lm_hash($password), create_ntlm_hash($password)
170 );
171 }
172  
173 sub load_user_passwords ($$) {
174 0 ▶ my $self = shift;
Subroutine load_user_passwords: not called CC=9 SCAR=45.0
Pod documented
175 0 ▶ my $filename = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
176  
177 0 ▶ open PASSWD, "<$filename" or return;
Branch: 1 branch
BranchTrueFalseTotal
open PASSWD, "<$filename" or return 0 0 0
178 0 my @lines = <PASSWD>;
179 0 ▶ close PASSWD or return;
Branch: 1 branch
BranchTrueFalseTotal
close PASSWD or return 0 0 0
180  
181 my %user_passwords = map {
182 0 s/^\s+//;
183 0 s/\s+$//;
184 0 my ($username, $hash_str) = split ':', $_;
185 my @hash_bytes = ($hash_str || '') =~ /^[0-9a-f]{64}$/
186 0 ▶ ? map { chr(hex(substr($hash_str, $_ * 2, 2))) } 0 .. 31
Branch: 1 branch
BranchTrueFalseTotal
($hash_str || '') =~ /^[0-9a-f]{64}$/ ? : 0 0 0
Condition0%0/2
$hash_str || ''
l!l
00
MC/DC0%0/1
$hash_str || ''
$hash_str
Truth table0%0/2
A
A= $hash_str
Aresult
11
00
187 : ();
188 0 ▶ $username && $username =~ /^\w[\w.+-]*$/ && @hash_bytes
Branch: 1 branch
BranchTrueFalseTotal
$username && $username =~ /^\w[\w.+-]*$/ && @hash_bytes ? : 0 0 0
Condition0%0/6
$username and $username =~ /^\w[\w.+-]*$/ and @hash_bytes
!ll&&!rl&&r
000
$username and $username =~ /^\w[\w.+-]*$/
!ll&&!rl&&r
000
MC/DC0%0/3
$username and $username =~ /^\w[\w.+-]*$/ and @hash_bytes
$username $username =~ /^\w[\w.+-]*$/ @hash_bytes
Truth table0%0/4
A and B and C
A= $usernameB= $username =~ /^\w[\w.+-]*$/C= @hash_bytes
ABCresult
0XX0
10X0
1100
1111
189 ? ($username => [ join('', @hash_bytes[0 .. 15]), join('', @hash_bytes[16 .. 31]) ])
190 : ();
191 } grep !/^\s*#/, @lines;
192  
193 0 $self->user_passwords(\%user_passwords);
194  
195 # in scalar context - number of users loaded
196 0 return keys %user_passwords;
197 }
198  
199 sub is_user_authenticated ($) {
200 1 ▶ my $self = shift;
Subroutine is_user_authenticated: called CC=8 SCAR=22.9
Pod documented
201  
202 # my $lm_response = $self->lm_response || return $self->err("No lm_response from client");
203 1 ▶ my $ntlm_response = $self->ntlm_response || return $self->err("No ntlm_response from client");
Condition50%1/2
$self->ntlm_response || (return $self->err('No ntlm_response from client'))
l!l
10
MC/DC0%0/1
$self->ntlm_response || (return $self->err('No ntlm_response from client'))
$self->ntlm_response
Truth table50%1/2
A
A= $self->ntlm_response
Aresult
11
00
204  
205 1 my ($hmac, $client_data) = $ntlm_response =~ /^(.{16})(.+)$/s;
206 1 ▶ return $self->err("Invalid short ntlm_response from client")
Branch: 1 branch
BranchTrueFalseTotal
unless $hmac 0 1 1
207 unless $hmac;
208  
209 1 ▶ my $username = $self->username // return $self->err("No username from client");
Condition50%1/2
$self->username // (return $self->err("No username from client"))
l!l
10
MC/DC0%0/1
$self->username // (return $self->err("No username from client"))
$self->username
Truth table50%1/2
A
A= $self->username
Aresult
11
00
210 1 ▶ my $password = $self->user_passwords->{$username} // return $self->err("No user '$username' on server");
Condition50%1/2
$self->user_passwords->{$username} // (return $self->err("No user '${username}' on server"))
l!l
10
MC/DC0%0/1
$self->user_passwords->{$username} // (return $self->err("No user '${username}' on server"))
$self->user_passwords->{$username}
Truth table50%1/2
A
A= $self->user_passwords->{$username}
Aresult
11
00
211 1 ▶ my ($lm_hash, $ntlm_hash) = ref($password) eq 'ARRAY' ? @$password : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $password eq 'ARRAY' ? : 0 1 1
212  
213 # $lm_hash ||= create_lm_hash($password);
214 1 ▶ $ntlm_hash ||= create_ntlm_hash($password);
Condition33%1/3
$ntlm_hash ||= &create_ntlm_hash($password)
l!l&&r!l&&!r
010
MC/DC0%0/2
$ntlm_hash ||= &create_ntlm_hash($password)
$ntlm_hash &create_ntlm_hash($password)
Truth table33%1/3
A ||= B
A= $ntlm_hashB= &create_ntlm_hash($password)
ABresult
1X1
011
000
215 1 my $ntlmv2_hash = create_ntlmv2_hash($ntlm_hash, $username, $self->client_domain);
216  
217 1 ▶ return $self->err("Failed password check for user '$username', client not authenticated")
Branch: 1 branch
BranchTrueFalseTotal
unless $hmac eq &hmac_md5($self->server_challenge . $client_data, $ntlmv2_hash) 0 1 1
218 unless $hmac eq hmac_md5($self->server_challenge . $client_data, $ntlmv2_hash);
219  
220 1 return 1;
221 }
222  
223 2 my @parsed_context_values;
224  
225 sub parse_asn1 {
226 39 ▶ my $bytes = shift;
Subroutine parse_asn1: called CC=15 SCAR=27.2
Pod documented
227  
228 39 my $tag = ord(shift @$bytes);
229 39 my $len = ord(shift @$bytes);
230 39 ▶ if ($len >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len >= 128) 6 33 39
231 6 my $llen = $len - 0x80;
232 6 my $factor = 1;
233 6 $len = 0;
234 6 for (1 .. $llen) {
235 12 $len = $len * $factor + ord(shift @$bytes);
236 12 $factor *= 256;
237 }
238 }
239  
240 39 my @contents;
241 39 my @bytes = splice(@$bytes, 0, $len);
242 39 ▶ if ($tag == ASN1_BINARY) {
Branch: 5 branches
BranchTrueFalseTotal
if ($tag == 4) { } 3 36 39
elsif ($tag == 6) { } 7 29 36
elsif ($tag == 10) { } 2 27 29
elsif ($tag == 48 or $tag == 96) { } 12 15 27
elsif ($tag >= 160 and $tag <= 163) { } 15 0 15
243 3 @contents = (\@bytes);
244 } elsif ($tag == ASN1_OID) {
245 7 my $idx = 0;
246 7 my $carry = 0;
247 @contents = (join('.', map {
248 58 my @i;
249 58 ▶ if (0 == $idx++) { @i = (int($_ / 40), $_ % 40); }
Branch: 2 branches
BranchTrueFalseTotal
if (0 == $idx++) { } 7 51 58
elsif ($_ >= 128) { } 4 47 51
250 4 elsif ($_ >= 0x80) { $carry = $carry * 0x80 + $_ - 0x80; }
251 47 else { @i = ($carry * 0x80 + $_); $carry = 0; }
252 @i
253 7 } map { ord($_) } @bytes));
254 } elsif ($tag == ASN1_ENUMERATED) {
255 2 ▶ die "Unsupported len=$len" unless $len == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $len == 1 0 2 2
256 2 @contents = map { ord($_) } @bytes;
257 ▶ } elsif ($tag == ASN1_SEQUENCE || $tag == ASN1_APPLICATION) {
Condition100%3/3
$tag == 48 or $tag == 96
l!l&&r!l&&!r
9315
MC/DC100%2/2
$tag == 48 or $tag == 96
$tag == 48 $tag == 96
Truth table100%3/3
A or B
A= $tag == 48B= $tag == 96
ABresult
1X1
011
000
258 12 ▶ push @contents, parse_asn1(\@bytes)
Branch: 1 branch
BranchTrueFalseTotal
@bytes and do { push @contents, parse_asn1(\@bytes) } 18 12 30
259 while @bytes;
260 ▶ } elsif ($tag >= ASN1_CONTEXT && $tag <= ASN1_CONTEXT + 3) {
Condition33%1/3
$tag >= 160 and $tag <= 163
!ll&&!rl&&r
0015
MC/DC0%0/2
$tag >= 160 and $tag <= 163
$tag >= 160 $tag <= 163
Truth table33%1/3
A and B
A= $tag >= 160B= $tag <= 163
ABresult
0X0
100
111
261 15 @contents = @{parse_asn1(\@bytes)};
262 15 ▶ $parsed_context_values[$tag - ASN1_CONTEXT] //= \@contents;
Condition100%2/2
$parsed_context_values[$tag - 160] //= \@contents
l!l
411
MC/DC100%1/1
$parsed_context_values[$tag - 160] //= \@contents
$parsed_context_values[$tag - 160]
Truth table100%2/2
A
A= $parsed_context_values[$tag - 160]
Aresult
11
00
263 } else {
264 0 warn sprintf "Unsupported asn1 tag 0x%x on parse\n", $tag;
265 }
266  
267 39 return [ $tag, @contents ];
268 }
269  
270 sub generate_asn1 {
271 39 ▶ my $tag = shift // die "No asn1 tag";
Subroutine generate_asn1: called CC=20 SCAR=30.8
Pod documented
Condition50%1/2
shift() // die("No asn1 tag")
l!l
390
MC/DC0%0/1
shift() // die("No asn1 tag")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
272 39 ▶ my $content = shift // die "No asn1 tag content";
Condition50%1/2
shift() // die("No asn1 tag content")
l!l
390
MC/DC0%0/1
shift() // die("No asn1 tag content")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
273  
274 39 my @bytes;
275 39 ▶ if ($tag == ASN1_BINARY) {
Branch: 5 branches
BranchTrueFalseTotal
if ($tag == 4) { } 3 36 39
elsif ($tag == 6) { } 7 29 36
elsif ($tag == 10) { } 2 27 29
elsif ($tag == 48 or $tag == 96) { } 12 15 27
elsif ($tag >= 160 and $tag <= 163) { } 15 0 15
276 3 @bytes = split('', $content);
277 } elsif ($tag == ASN1_OID) {
278 7 my $idx = 0;
279 7 my $id0;
280 58 @bytes = map { chr($_) } map {
281 7 ▶ 0 == $idx++ ? ($id0 = $_) && () : 2 == $idx ? ($id0 * 40 + $_) : (
Branch: 6 branches
BranchTrueFalseTotal
0 == $idx++ ? : 7 54 61
2 == $idx ? : 7 47 54
$_ >= 268435456 ? : 0 47 47
$_ >= 2097152 ? : 0 47 47
$_ >= 16384 ? : 0 47 47
$_ >= 128 ? : 4 43 47
Condition33%1/3
$id0 = $_ and ()
!ll&&!rl&&r
007
MC/DC0%0/2
$id0 = $_ and ()
$id0 = $_ ()
Truth table33%1/3
A and B
A= $id0 = $_B= ()
ABresult
0X0
100
111
282 $_ >= 1 << 28 ? (0x80 | (($_ >> 28) & 0x7f)) : (),
283 $_ >= 1 << 21 ? (0x80 | (($_ >> 21) & 0x7f)) : (),
284 $_ >= 1 << 14 ? (0x80 | (($_ >> 14) & 0x7f)) : (),
285 $_ >= 1 << 7 ? (0x80 | (($_ >> 7) & 0x7f)) : (),
286 $_ & 0x7f
287 )
288 } split(/\./, $content);
289 } elsif ($tag == ASN1_ENUMERATED) {
290 2 @bytes = (chr($content));
291 ▶ } elsif ($tag == ASN1_SEQUENCE || $tag == ASN1_APPLICATION) {
Condition100%3/3
$tag == 48 or $tag == 96
l!l&&r!l&&!r
9315
MC/DC100%2/2
$tag == 48 or $tag == 96
$tag == 48 $tag == 96
Truth table100%3/3
A or B
A= $tag == 48B= $tag == 96
ABresult
1X1
011
000
292 12 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$content and do { do { push @bytes, @{generate_asn1(@$content);}; $content = shift() } } 6 12 18
293 18 push @bytes, @{generate_asn1(@$content)};
294 18 $content = shift;
295 } while $content;
296 ▶ } elsif ($tag >= ASN1_CONTEXT && $tag <= ASN1_CONTEXT + 3) {
Condition33%1/3
$tag >= 160 and $tag <= 163
!ll&&!rl&&r
0015
MC/DC0%0/2
$tag >= 160 and $tag <= 163
$tag >= 160 $tag <= 163
Truth table33%1/3
A and B
A= $tag >= 160B= $tag <= 163
ABresult
0X0
100
111
297 15 @bytes = @{generate_asn1($content, @_)};
298 } else {
299 0 warn sprintf "Unsupported asn1 tag 0x%x on generate\n", $tag;
300 }
301  
302 39 my $len = @bytes;
303 39 my @sub_lens;
304 39 ▶ while ($len >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len >= 128) 6 39 45
305 6 push @sub_lens, $len % 256;
306 6 $len /= 256;
307 }
308 39 ▶ my @len_bytes = @sub_lens ? (0x80 + @sub_lens + 1, $len, @sub_lens) : ($len);
Branch: 1 branch
BranchTrueFalseTotal
@sub_lens ? : 6 33 39
309  
310 39 return [ (map { chr($_) } $tag, @len_bytes), @bytes ];
311 }
312  
313 sub process_spnego ($$%) {
314 6 ▶ my $self = shift;
Subroutine process_spnego: called CC=33 SCAR=36.7
Pod documented
315 6 ▶ my $buffer = shift // return;
Condition50%1/2
shift() // (return)
l!l
60
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
316 6 my %options = @_;
317  
318 6 my @bytes = split '', $buffer;
319 6 ▶ return unless @bytes > 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @bytes > 2 0 6 6
320  
321 6 @parsed_context_values = ();
322 6 my $struct = parse_asn1(\@bytes);
323 6 ▶ return unless $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless $struct 0 6 6
324  
325 6 ▶ if (!defined $self->ntlmssp_supported || $options{is_initial}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->ntlmssp_supported or $options{"is_initial"}) 2 4 6
Condition100%3/3
not defined $self->ntlmssp_supported or $options{"is_initial"}
l!l&&r!l&&!r
114
MC/DC100%2/2
not defined $self->ntlmssp_supported or $options{"is_initial"}
not defined $self->ntlmssp_supported $options{"is_initial"}
Truth table100%3/3
not(A) or B
A= not defined $self->ntlmssp_supportedB= $options{"is_initial"}
ABresult
1X1
011
000
326 2 my $value = $parsed_context_values[0];
327 2 ▶ return $self->err("No expected spnego context value")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" and shift @$value == 48 0 2 2
Condition33%1/3
ref $value eq "ARRAY" and shift @$value == 48
!ll&&!rl&&r
002
MC/DC0%0/2
ref $value eq "ARRAY" and shift @$value == 48
ref $value eq "ARRAY" shift @$value == 48
Truth table33%1/3
A and B
A= ref $value eq "ARRAY"B= shift @$value == 48
ABresult
0X0
100
111
328 unless ref($value) eq 'ARRAY' && shift @$value == ASN1_SEQUENCE;
329 2 for (@$value) {
330 2 ▶ return $self->ntlmssp_supported(1)
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] == 6 and $_->[1] eq "1.3.6.1.4.1.311.2.2.10" 2 0 2
Condition33%1/3
$_->[0] == 6 and $_->[1] eq "1.3.6.1.4.1.311.2.2.10"
!ll&&!rl&&r
002
MC/DC0%0/2
$_->[0] == 6 and $_->[1] eq "1.3.6.1.4.1.311.2.2.10"
$_->[0] == 6 $_->[1] eq "1.3.6.1.4.1.311.2.2.10"
Truth table33%1/3
A and B
A= $_->[0] == 6B= $_->[1] eq "1.3.6.1.4.1.311.2.2.10"
ABresult
0X0
100
111
331 if $_->[0] == ASN1_OID && $_->[1] eq OID_MECH_NTLMSSP;
332 }
333 0 return $self->ntlmssp_supported(0);
334 }
335  
336 4 my $value = $parsed_context_values[2];
337 4 ▶ my $ntlmssp_bytes = ref($value) eq 'ARRAY' && shift @$value == ASN1_BINARY
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' && shift @$value == 4 ? : 3 1 4
Condition66%2/3
ref $value eq "ARRAY" and shift @$value == 4
!ll&&!rl&&r
103
MC/DC50%1/2
ref $value eq "ARRAY" and shift @$value == 4
ref $value eq "ARRAY" shift @$value == 4
Truth table66%2/3
A and B
A= ref $value eq "ARRAY"B= shift @$value == 4
ABresult
0X0
100
111
338 ? shift @$value
339 : undef;
340 4 my $parser = $self->parser;
341 4 ▶ unless (defined $self->client_challenge) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->client_challenge) 3 1 4
342 3 ▶ return $self->err("No expected spnego context+2 value (ntlmssp)")
Branch: 1 branch
BranchTrueFalseTotal
unless $ntlmssp_bytes 0 3 3
343 unless $ntlmssp_bytes;
344 3 $parser->set(join('', @$ntlmssp_bytes));
345 3 ▶ return $self->err("No expected NTLMSSP id string")
Branch: 1 branch
BranchTrueFalseTotal
unless $parser->bytes(8) eq "NTLMSSP\0" 0 3 3
346 unless $parser->bytes(length(NTLMSSP_ID_STR)) eq NTLMSSP_ID_STR;
347 }
348  
349 4 ▶ if (!defined $self->client_host) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $self->client_host) { } 1 3 4
elsif (not defined $self->server_challenge) { } 1 2 3
elsif (not defined $self->client_challenge) { } 1 1 2
elsif (not defined $self->auth_completed) { } 1 0 1
350 1 ▶ return $self->err("No expected NTLMSSP_NEGOTIATE")
Branch: 1 branch
BranchTrueFalseTotal
unless $parser->uint32 == 1 0 1 1
351 unless $parser->uint32 == NTLMSSP_NEGOTIATE;
352 1 $parser->skip(4); # skip flags
353 1 my $len1 = $parser->uint16;
354 1 my $off1 = $parser->skip(2)->uint32;
355 1 my $len2 = $parser->uint16;
356 1 my $off2 = $parser->skip(2)->uint32;
357 1 $self->client_domain($parser->reset($off1)->bytes($len1));
358 1 $self->client_host ($parser->reset($off2)->bytes($len2));
359 } elsif (!defined $self->server_challenge) {
360 1 ▶ return $self->err("No expected NTLMSSP_CHALLENGE")
Branch: 1 branch
BranchTrueFalseTotal
unless $parser->uint32 == 2 0 1 1
361 unless $parser->uint32 == NTLMSSP_CHALLENGE;
362 1 my $len1 = $parser->uint16;
363 1 my $off1 = $parser->skip(2)->uint32;
364 1 $self->server_challenge($parser->reset(24)->bytes(8));
365 1 $self->server_host($parser->reset($off1)->str($len1));
366 1 my $itemtype;
367 1 ▶ do {{
Branch: 1 branch
BranchTrueFalseTotal
$itemtype != 0 and do { do { { $itemtype = $parser->uint16; $parser->uint16 == 8 && $self->server_timestamp($parser->uint64), next if $itemtype == 7; my $str = $parser->str($parser->uint16); $self->server_netbios_host($str) if $itemtype == 1; $self->server_netbios_domain($str) if $itemtype == 2; $self->server_dns_host($str) if $itemtype == 3; $self->server_dns_domain($str) if $itemtype == 4; } } } 5 1 6
368 6 $itemtype = $parser->uint16;
369 6 ▶ $parser->uint16 == 8 && $self->server_timestamp($parser->uint64), next
Branch: 1 branch
BranchTrueFalseTotal
if $itemtype == 7 1 5 6
Condition33%1/3
$parser->uint16 == 8 and $self->server_timestamp($parser->uint64)
!ll&&!rl&&r
010
MC/DC0%0/2
$parser->uint16 == 8 and $self->server_timestamp($parser->uint64)
$parser->uint16 == 8 $self->server_timestamp($parser->uint64)
Truth table33%1/3
A and B
A= $parser->uint16 == 8B= $self->server_timestamp($parser->uint64)
ABresult
0X0
100
111
370 if $itemtype == NTLMSSP_ITEM_TIMESTAMP;
371 5 my $str = $parser->str($parser->uint16);
372 5 ▶ $self->server_netbios_host($str)
Branch: 1 branch
BranchTrueFalseTotal
if $itemtype == 1 1 4 5
373 if $itemtype == NTLMSSP_ITEM_NETBIOSHOST;
374 5 ▶ $self->server_netbios_domain($str)
Branch: 1 branch
BranchTrueFalseTotal
if $itemtype == 2 1 4 5
375 if $itemtype == NTLMSSP_ITEM_NETBIOSDOMAIN;
376 5 ▶ $self->server_dns_host($str)
Branch: 1 branch
BranchTrueFalseTotal
if $itemtype == 3 1 4 5
377 if $itemtype == NTLMSSP_ITEM_DNSHOST;
378 5 ▶ $self->server_dns_domain($str)
Branch: 1 branch
BranchTrueFalseTotal
if $itemtype == 4 1 4 5
379 if $itemtype == NTLMSSP_ITEM_DNSDOMAIN;
380 }} while ($itemtype != NTLMSSP_ITEM_TERMINATOR)
381 } elsif (!defined $self->client_challenge) {
382 1 ▶ return $self->err("No expected NTLMSSP_AUTH")
Branch: 1 branch
BranchTrueFalseTotal
unless $parser->uint32 == 3 0 1 1
383 unless $parser->uint32 == NTLMSSP_AUTH;
384 1 my $llen = $parser->uint16;
385 1 my $loff = $parser->skip(2)->uint32;
386 1 my $nlen = $parser->uint16;
387 1 my $noff = $parser->skip(2)->uint32;
388 1 my $len1 = $parser->uint16;
389 1 my $off1 = $parser->skip(2)->uint32;
390 1 my $len2 = $parser->uint16;
391 1 my $off2 = $parser->skip(2)->uint32;
392 1 my $len3 = $parser->uint16;
393 1 my $off3 = $parser->skip(2)->uint32;
394 1 $self->client_challenge($parser->reset($noff + 28)->bytes(8));
395 1 $self->lm_response ($parser->reset($loff)->bytes($llen));
396 1 $self->ntlm_response($parser->reset($noff)->bytes($nlen));
397 1 $self->client_domain($parser->reset($off1)->str($len1));
398 1 $self->username ($parser->reset($off2)->str($len2));
399 1 $self->client_host ($parser->reset($off3)->str($len3));
400 } elsif (!defined $self->auth_completed) {
401 1 my $value = $parsed_context_values[0];
402 1 ▶ return $self->err("No expected spnego context value (ACCEPT_COMPLETED)")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" and shift @$value == 10 0 1 1
Condition33%1/3
ref $value eq "ARRAY" and shift @$value == 10
!ll&&!rl&&r
001
MC/DC0%0/2
ref $value eq "ARRAY" and shift @$value == 10
ref $value eq "ARRAY" shift @$value == 10
Truth table33%1/3
A and B
A= ref $value eq "ARRAY"B= shift @$value == 10
ABresult
0X0
100
111
403 unless ref($value) eq 'ARRAY' && shift @$value == ASN1_ENUMERATED;
404 1 ▶ $self->auth_completed(shift @$value == SPNEGO_ACCEPT_COMPLETED ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
shift @$value == 0 ? : 1 0 1
405 } else {
406 0 $self->err("process_spnego called after auth_completed");
407 }
408  
409 4 return 1;
410 }
411  
412 sub generate_spnego ($%) {
413 6 ▶ my $self = shift;
Subroutine generate_spnego: called CC=17 SCAR=28.9
Pod documented
414 6 my %options = @_;
415  
416 6 my $struct;
417  
418 6 ▶ if (!defined $self->ntlmssp_supported || $options{is_initial}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->ntlmssp_supported or $options{"is_initial"}) 2 4 6
Condition100%3/3
not defined $self->ntlmssp_supported or $options{"is_initial"}
l!l&&r!l&&!r
114
MC/DC100%2/2
not defined $self->ntlmssp_supported or $options{"is_initial"}
not defined $self->ntlmssp_supported $options{"is_initial"}
Truth table100%3/3
not(A) or B
A= not defined $self->ntlmssp_supportedB= $options{"is_initial"}
ABresult
1X1
011
000
419 2 $self->ntlmssp_supported(1);
420 2 $struct = [ ASN1_APPLICATION,
421 [ ASN1_OID, OID_SPNEGO ],
422 [ ASN1_CONTEXT, ASN1_SEQUENCE,
423 [ ASN1_CONTEXT, ASN1_SEQUENCE,
424 [ ASN1_OID, OID_MECH_NTLMSSP ],
425 ],
426 ],
427 ];
428 2 goto RETURN;
429 }
430  
431 4 my @names = hostname =~ /^([^.]*+)\.?+(.*)$/;
432 4 ▶ my $host = $options{host} || $names[0];
Condition66%2/3
$options{'host'} || $names[0]
l!l&&r!l&&!r
220
MC/DC0%0/2
$options{'host'} || $names[0]
$options{'host'} $names[0]
Truth table66%2/3
A || B
A= $options{'host'}B= $names[0]
ABresult
1X1
011
000
433 4 ▶ my $domain = $options{domain} || $names[1];
Condition66%2/3
$options{'domain'} || $names[1]
l!l&&r!l&&!r
103
MC/DC50%1/2
$options{'domain'} || $names[1]
$options{'domain'} $names[1]
Truth table66%2/3
A || B
A= $options{'domain'}B= $names[1]
ABresult
1X1
011
000
434  
435 4 ▶ if (!defined $self->client_host) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $self->client_host) { } 1 3 4
elsif (not defined $self->server_challenge) { } 1 2 3
elsif (not defined $self->client_challenge) { } 1 1 2
elsif (not defined $self->auth_completed) { } 1 0 1
436 1 $self->client_host($host);
437 1 $self->client_domain($domain);
438  
439 1 $self->packer->reset
440 ->bytes(NTLMSSP_ID_STR)
441 ->uint32(NTLMSSP_NEGOTIATE)
442 ->uint32(NTLMSSP_FLAGS_CLIENT)
443 ->uint16(length($domain))
444 ->uint16(length($domain))
445 ->uint32(32)
446 ->uint16(length($host))
447 ->uint16(length($host))
448 ->uint32(32 + length($domain))
449 ->bytes($domain)
450 ->bytes($host)
451 ;
452 1 $struct = [ ASN1_APPLICATION,
453 [ ASN1_OID, OID_SPNEGO ],
454 [ ASN1_CONTEXT, ASN1_SEQUENCE,
455 [ ASN1_CONTEXT, ASN1_SEQUENCE,
456 [ ASN1_OID, OID_MECH_NTLMSSP ],
457 ],
458 [ ASN1_CONTEXT + 2, ASN1_BINARY, $self->packer->data ],
459 ],
460 ];
461 } elsif (!defined $self->server_challenge) {
462 1 $self->server_challenge(join('', map { chr(rand(0x100)) } 1 .. 8));
463 1 $self->server_host($host);
464 1 $self->server_netbios_host($host);
465 1 $self->server_netbios_domain($domain);
466 1 $self->server_dns_host($host);
467 1 $self->server_dns_domain($domain);
468 1 my $tlen = 32 + length(
469 $self->server_netbios_host .
470 $self->server_netbios_domain .
471 $self->server_dns_host .
472 $self->server_dns_domain
473 ) * 2;
474  
475 1 $self->packer->reset
476 ->bytes(NTLMSSP_ID_STR)
477 ->uint32(NTLMSSP_CHALLENGE)
478 ->uint16(length($self->server_host) * 2)
479 ->uint16(length($self->server_host) * 2)
480 ->uint32(56)
481 ->uint32(NTLMSSP_FLAGS_SERVER)
482 ->bytes($self->server_challenge)
483 ->uint64(0) # reserved
484 ->uint16($tlen)
485 ->uint16($tlen)
486 ->uint32(56 + length($self->server_host) * 2)
487 ->bytes("\x06\x01\xb1\x1d\x00\x00\x00\x0f") # version
488 ->str($self->server_host)
489 ->uint16(NTLMSSP_ITEM_NETBIOSDOMAIN)
490 ->uint16(length($self->server_netbios_domain) * 2)
491 ->str($self->server_netbios_domain)
492 ->uint16(NTLMSSP_ITEM_NETBIOSHOST)
493 ->uint16(length($self->server_netbios_host) * 2)
494 ->str($self->server_netbios_host)
495 ->uint16(NTLMSSP_ITEM_DNSDOMAIN)
496 ->uint16(length($self->server_dns_domain) * 2)
497 ->str($self->server_dns_domain)
498 ->uint16(NTLMSSP_ITEM_DNSHOST)
499 ->uint16(length($self->server_dns_host) * 2)
500 ->str($self->server_dns_host)
501 ->uint16(NTLMSSP_ITEM_TIMESTAMP)
502 ->uint16(8)
503 ->bytes("\0" x 8)
504 ->uint16(NTLMSSP_ITEM_TERMINATOR)
505 ->uint16(0)
506 ;
507  
508 1 $struct = [ ASN1_CONTEXT + 1, ASN1_SEQUENCE,
509 [ ASN1_CONTEXT, ASN1_ENUMERATED, SPNEGO_ACCEPT_INCOMPLETE ],
510 [ ASN1_CONTEXT + 1, ASN1_OID, OID_MECH_NTLMSSP ],
511 [ ASN1_CONTEXT + 2, ASN1_BINARY, $self->packer->data ],
512 ];
513 } elsif (!defined $self->client_challenge) {
514 1 ▶ my $username = $options{username} || '';
Condition50%1/2
$options{'username'} || ''
l!l
10
MC/DC0%0/1
$options{'username'} || ''
$options{'username'}
Truth table50%1/2
A
A= $options{'username'}
Aresult
11
00
515 1 ▶ my $password = $options{password} || '';
Condition50%1/2
$options{'password'} || ''
l!l
10
MC/DC0%0/1
$options{'password'} || ''
$options{'password'}
Truth table50%1/2
A
A= $options{'password'}
Aresult
11
00
516 1 ▶ $domain = $options{domain} || 'MYGROUP';
Condition50%1/2
$options{'domain'} || 'MYGROUP'
l!l
10
MC/DC0%0/1
$options{'domain'} || 'MYGROUP'
$options{'domain'}
Truth table50%1/2
A
A= $options{'domain'}
Aresult
11
00
517 1 $self->client_challenge(join('', map { chr(rand(0x100)) } 1 .. 8));
518 1 $self->username($username);
519 1 $self->domain($domain);
520 1 $self->session_key([ map { chr(rand(0x100)) } 1 .. 16 ]);
521  
522 # my $lm_hash = $options{lm_password_hash} || create_lm_hash($password);
523 1 ▶ my $ntlm_hash = $options{ntlm_password_hash} || create_ntlm_hash($password);
Condition33%1/3
$options{'ntlm_password_hash'} || &create_ntlm_hash($password)
l!l&&r!l&&!r
010
MC/DC0%0/2
$options{'ntlm_password_hash'} || &create_ntlm_hash($password)
$options{'ntlm_password_hash'} &create_ntlm_hash($password)
Truth table33%1/3
A || B
A= $options{'ntlm_password_hash'}B= &create_ntlm_hash($password)
ABresult
1X1
011
000
524 1 my $ntlmv2_hash = create_ntlmv2_hash($ntlm_hash, $self->username, $self->domain);
525  
526 1 ▶ $self->packer->reset
Condition50%1/2
$self->server_timestamp || 0
l!l
01
MC/DC0%0/1
$self->server_timestamp || 0
$self->server_timestamp
Truth table50%1/2
A
A= $self->server_timestamp
Aresult
11
00
527 ->uint32(0x0101) # header
528 ->uint32(0) # reserved
529 ->uint64(to_nttime(time))
530 ->bytes($self->client_challenge)
531 ->uint32(0) # unknown
532 ->uint16(NTLMSSP_ITEM_NETBIOSDOMAIN)
533 ->uint16(length($self->server_netbios_domain) * 2)
534 ->str($self->server_netbios_domain)
535 ->uint16(NTLMSSP_ITEM_NETBIOSHOST)
536 ->uint16(length($self->server_netbios_host) * 2)
537 ->str($self->server_netbios_host)
538 ->uint16(NTLMSSP_ITEM_DNSDOMAIN)
539 ->uint16(length($self->server_dns_domain) * 2)
540 ->str($self->server_dns_domain)
541 ->uint16(NTLMSSP_ITEM_DNSHOST)
542 ->uint16(length($self->server_dns_host) * 2)
543 ->str($self->server_dns_host)
544 ->uint16(NTLMSSP_ITEM_TIMESTAMP)
545 ->uint16(8)
546 ->uint64($self->server_timestamp || 0)
547 ->uint16(NTLMSSP_ITEM_TERMINATOR)
548 ->uint16(0)
549 ;
550  
551 1 my $client_data = $self->packer->data;
552 1 my $hmac = hmac_md5($self->server_challenge . $client_data, $ntlmv2_hash);
553 1 my $ntlm_response = "$hmac$client_data";
554 1 my $nlen = 16 + $self->packer->size; # hmac + client data
555  
556 1 my $lm_response = create_lmv2_response($ntlm_hash, $username, $domain, $self->server_challenge);
557  
558 1 $self->lm_response($lm_response);
559 1 $self->ntlm_response($ntlm_response);
560  
561 1 $self->packer->reset
562 ->bytes(NTLMSSP_ID_STR)
563 ->uint32(NTLMSSP_AUTH)
564 ->uint16(24)
565 ->uint16(24)
566 ->uint32(64)
567 ->uint16($nlen)
568 ->uint16($nlen)
569 ->uint32(88)
570 ->uint16(length($domain) * 2)
571 ->uint16(length($domain) * 2)
572 ->uint32(88 + $nlen)
573 ->uint16(length($username) * 2)
574 ->uint16(length($username) * 2)
575 ->uint32(88 + $nlen + length($domain) * 2)
576 ->uint16(length($host) * 2)
577 ->uint16(length($host) * 2)
578 ->uint32(88 + $nlen + length("$domain$username") * 2)
579 ->uint16(16)
580 ->uint16(16)
581 ->uint32(88 + $nlen + length("$domain$username$host") * 2)
582 ->uint32(NTLMSSP_FLAGS_CLIENT)
583 ->bytes($lm_response)
584 ->bytes($ntlm_response)
585 ->str($domain)
586 ->str($username)
587 ->str($host)
588 ->bytes($self->session_key)
589 ;
590  
591 1 $struct = [ ASN1_CONTEXT + 1, ASN1_SEQUENCE,
592 [ ASN1_CONTEXT + 2, ASN1_BINARY, $self->packer->data ],
593 ];
594 } elsif (!defined $self->auth_completed) {
595 1 ▶ $self->auth_completed($self->is_user_authenticated ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->is_user_authenticated ? : 1 0 1
596 1 ▶ $struct = [ ASN1_CONTEXT + 1, ASN1_SEQUENCE,
Branch: 1 branch
BranchTrueFalseTotal
if $self->auth_completed 1 0 1
597 [ ASN1_CONTEXT, ASN1_ENUMERATED, SPNEGO_ACCEPT_COMPLETED ],
598 ] if $self->auth_completed;
599 } else {
600 0 $self->err("generate_spnego called after auth_completed");
601 }
602  
603 6 ▶RETURN:
Branch: 1 branch
BranchTrueFalseTotal
unless $struct 0 6 6
604 return undef unless $struct;
605  
606 6 return join '', @{generate_asn1(@$struct)};
607 }
608  
609 2 1;
610  
611 __END__
« blib/lib/SMB/Crypt.pm ↑ Summary blib/lib/SMB/v2/Command/Close.pm »