Passphrase/LANManager.pm

filter: Statementstmt 95.5% 64 / 67 Branchbran 68.7% 22 / 32 Conditioncond 46.6% 7 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 80.2% 118 / 147 CC 22 SCAR 24.4
CC
22
Cov
82%
new
22.8
match
4.1
from_rfc2307
3.2
CRAP 11.5
« Passphrase/MySQL41.pm ↑ Summary Passphrase/LANManagerHalf.pm »
1 =head1 NAME
2  
3 Authen::Passphrase::LANManager - passphrases using the LAN Manager
4 hash algorithm
5  
6 =head1 SYNOPSIS
7  
8 use Authen::Passphrase::LANManager;
9  
10 $ppr = Authen::Passphrase::LANManager->new(
11 hash_hex => "855c3697d9979e78ac404c4ba2c66533");
12  
13 $ppr = Authen::Passphrase::LANManager->new(
14 passphrase => "passphrase");
15  
16 $ppr = Authen::Passphrase::LANManager->from_rfc2307(
17 "{LANMAN}855c3697d9979e78ac404c4ba2c66533");
18  
19 $hash = $ppr->hash;
20 $hash_hex = $ppr->hash_hex;
21  
22 $ppr0 = $ppr->first_half;
23 $ppr1 = $ppr->second_half;
24  
25 if($ppr->match($passphrase)) { ...
26  
27 $userPassword = $ppr->as_rfc2307;
28  
29 =head1 DESCRIPTION
30  
31 An object of this class encapsulates a passphrase hashed using
32 the Microsoft LAN Manager hash function. This is a subclass of
33 L<Authen::Passphrase>, and this document assumes that the reader is
34 familiar with the documentation for that class.
35  
36 The hash algorithm can be used on up to fourteen Latin-1 characters of
37 passphrase. First the passphrase is folded to uppercase, and zero-padded
38 to fourteen bytes. Then it is split into two halves. Each seven-byte
39 half is used as a 56-bit DES key, to encrypt the fixed plaintext block
40 "KGS!@#$%". The eight-byte ciphertexts are concatenated to form the
41 sixteen-byte hash. There is no salt.
42  
43 Because the two halves of the passphrase are hashed separately, it
44 is possible to manipulate (e.g., crack) a half hash in isolation.
45 See L<Authen::Passphrase::LANManagerHalf>.
46  
47 I<Warning:> Don't even think about using this seriously. It's an
48 exceptionally weak design, flawed in pretty much every respect.
49  
50 =cut
51  
52 package Authen::Passphrase::LANManager;
53  
54 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 2 ▶use Authen::Passphrase 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Authen::Passphrase::LANManagerHalf;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 2 our $VERSION = "0.009";
63  
64 2 ▶use parent "Authen::Passphrase";
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 =head1 CONSTRUCTORS
67  
68 =over
69  
70 =item Authen::Passphrase::LANManager->new(ATTR => VALUE, ...)
71  
72 Generates a new passphrase recogniser object using the LAN Manager
73 hash algorithm. The following attributes may be given:
74  
75 =over
76  
77 =item B<hash>
78  
79 The hash, as a string of 16 bytes.
80  
81 =item B<hash_hex>
82  
83 The hash, as a string of 32 hexadecimal digits.
84  
85 =item B<passphrase>
86  
87 A passphrase that will be accepted.
88  
89 =back
90  
91 Either the hash or the passphrase must be given.
92  
93 =cut
94  
95 9 ▶sub new {
Subroutine new: called CC=17 SCAR=31.3
Pod documented
96 9 my $class = shift;
97 9 my $self = bless({}, $class);
98 9 my $hash;
99 my $passphrase;
100 9 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 9 18
101 9 my $attr = shift;
102 9 my $value = shift;
103 9 ▶ if($attr eq "hash") {
Branch: 3 branches
BranchTrueFalseTotal
if ($attr eq 'hash') { } 3 6 9
elsif ($attr eq 'hash_hex') { } 4 2 6
elsif ($attr eq 'passphrase') { } 2 0 2
104 3 ▶ croak "hash specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash or defined $passphrase 0 3 3
Condition33%1/3
defined $hash or defined $passphrase
l!l&&r!l&&!r
003
MC/DC0%0/2
defined $hash or defined $passphrase
defined $hash defined $passphrase
Truth table33%1/3
A or B
A= defined $hashB= defined $passphrase
ABresult
1X1
011
000
105 if defined($hash) || defined($passphrase);
106 3 ▶ $value =~ m#\A[\x00-\xff]{16}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]{16}\z/ or croak("not a valid LAN Manager hash") 3 0 3
107 or croak "not a valid LAN Manager hash";
108 3 $hash = $value;
109 } elsif($attr eq "hash_hex") {
110 4 ▶ croak "hash specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash or defined $passphrase 0 4 4
Condition33%1/3
defined $hash or defined $passphrase
l!l&&r!l&&!r
004
MC/DC0%0/2
defined $hash or defined $passphrase
defined $hash defined $passphrase
Truth table33%1/3
A or B
A= defined $hashB= defined $passphrase
ABresult
1X1
011
000
111 if defined($hash) || defined($passphrase);
112 4 ▶ $value =~ m#\A[0-9A-Fa-f]{32}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[0-9A-Fa-f]{32}\z/ or croak(qq["$value" is not a valid ] . 'hex LAN Manager hash') 4 0 4
113 or croak "\"$value\" is not a valid ".
114 "hex LAN Manager hash";
115 4 $hash = pack("H*", $value);
116 } elsif($attr eq "passphrase") {
117 2 ▶ croak "passphrase specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash or defined $passphrase 0 2 2
Condition33%1/3
defined $hash or defined $passphrase
l!l&&r!l&&!r
002
MC/DC0%0/2
defined $hash or defined $passphrase
defined $hash defined $passphrase
Truth table33%1/3
A or B
A= defined $hashB= defined $passphrase
ABresult
1X1
011
000
118 if defined($hash) || defined($passphrase);
119 2 ▶ $self->_passphrase_acceptable($value)
Branch: 1 branch
BranchTrueFalseTotal
$self->_passphrase_acceptable($value) or croak("can't accept a passphrase exceeding fourteen bytes") 2 0 2
120 or croak "can't accept a passphrase exceeding".
121 " fourteen bytes";
122 2 $passphrase = $value;
123 } else {
124 0 croak "unrecognised attribute `$attr'";
125 }
126 }
127 9 ▶ if(defined $passphrase) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $passphrase) { } 2 7 9
elsif (defined $hash) { } 7 0 7
128 $self->{first_half} =
129 2 Authen::Passphrase::LANManagerHalf
130 ->new(passphrase => substr($passphrase, 0, 7));
131 $self->{second_half} =
132 2 ▶ Authen::Passphrase::LANManagerHalf
Branch: 1 branch
BranchTrueFalseTotal
length $passphrase > 7 ? : 1 1 2
133 ->new(passphrase =>
134 length($passphrase) > 7 ?
135 substr($passphrase, 7, 7) :
136 "");
137 } elsif(defined $hash) {
138 7 $self->{first_half} = Authen::Passphrase::LANManagerHalf
139 ->new(hash => substr($hash, 0, 8));
140 7 $self->{second_half} = Authen::Passphrase::LANManagerHalf
141 ->new(hash => substr($hash, 8, 8));
142 } else {
143 0 croak "hash not specified";
144 }
145 9 return $self;
146 }
147  
148 =item Authen::Passphrase::LANManager->from_rfc2307(USERPASSWORD)
149  
150 Generates a LAN Manager passphrase recogniser from the supplied RFC2307
151 encoding. The string must consist of "B<{LANMAN}>" (or its synonym
152 "B<{LANM}>") followed by the hash in hexadecimal; case is ignored.
153  
154 =cut
155  
156 2 ▶sub from_rfc2307 {
Subroutine from_rfc2307: called CC=3 SCAR=11.8
Pod documented
157 2 my($class, $userpassword) = @_;
158 2 ▶ if($userpassword =~ /\A\{(?i:lanm(?:an)?)\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userpassword =~ /\A\{(?i:lanm(?:an)?)\}/) 2 0 2
159 2 ▶ $userpassword =~ /\A\{.*?\}([0-9a-fA-F]{32})\z/
Branch: 1 branch
BranchTrueFalseTotal
$userpassword =~ /\A\{.*?\}([0-9a-fA-F]{32})\z/ or croak("malformed {LANMAN} data") 2 0 2
160 or croak "malformed {LANMAN} data";
161 2 my $hash = $1;
162 2 return $class->new(hash_hex => $hash);
163 }
164 0 return $class->SUPER::from_rfc2307($userpassword);
165 }
166  
167 =back
168  
169 =head1 METHODS
170  
171 =over
172  
173 =item $ppr->hash
174  
175 Returns the hash value, as a string of 16 bytes.
176  
177 =cut
178  
179 20 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
180 20 my($self) = @_;
181 20 return $self->{first_half}->hash.$self->{second_half}->hash;
182 }
183  
184 =item $ppr->hash_hex
185  
186 Returns the hash value, as a string of 32 hexadecimal digits.
187  
188 =cut
189  
190 14 ▶sub hash_hex {
Subroutine hash_hex: called CC=1 SCAR=0.0
Pod documented
191 14 my($self) = @_;
192 14 return unpack("H*", $self->hash);
193 }
194  
195 =item $ppr->first_half
196  
197 Returns the hash of the first half of the passphrase, as an
198 L<Authen::Passphrase::LANManagerHalf> passphrase recogniser.
199  
200 =cut
201  
202 5 ▶sub first_half {
Subroutine first_half: called CC=1 SCAR=0.0
Pod documented
203 5 my($self) = @_;
204 5 return $self->{first_half};
205 }
206  
207 =item $ppr->second_half
208  
209 Returns the hash of the second half of the passphrase, as an
210 L<Authen::Passphrase::LANManagerHalf> passphrase recogniser.
211  
212 =cut
213  
214 5 ▶sub second_half {
Subroutine second_half: called CC=1 SCAR=0.0
Pod documented
215 5 my($self) = @_;
216 5 return $self->{second_half};
217 }
218  
219 =item $ppr->match(PASSPHRASE)
220  
221 =item $ppr->as_rfc2307
222  
223 These methods are part of the standard L<Authen::Passphrase> interface.
224  
225 =cut
226  
227 27 ▶sub _passphrase_acceptable {
Subroutine _passphrase_acceptable: called CC=1 SCAR=0.0
228 27 my($self, $passphrase) = @_;
229 27 return $passphrase =~ /\A[\x00-\xff]{0,14}\z/;
230 }
231  
232 25 ▶sub match {
Subroutine match: called CC=4 SCAR=14.2
Pod documented
233 25 my($self, $passphrase) = @_;
234 return $self->_passphrase_acceptable($passphrase) &&
235 $self->{first_half}->match(substr($passphrase, 0, 7)) &&
236 $self->{second_half}->match(
237 25 ▶ length($passphrase) > 7 ?
Branch: 1 branch
BranchTrueFalseTotal
length $passphrase > 7 ? : 1 4 5
Condition66%4/6
$self->_passphrase_acceptable($passphrase) && $self->{'first_half'}->match(substr($passphrase, 0, 7)) && $self->{'second_half'}->match(length $passphrase > 7 ? substr($passphrase, 7, 7) : '')
!ll&&!rl&&r
2005
$self->_passphrase_acceptable($passphrase) and $self->{"first_half"}->match(substr($passphrase, 0, 7))
!ll&&!rl&&r
0205
MC/DC33%1/3
$self->_passphrase_acceptable($passphrase) && $self->{'first_half'}->match(substr($passphrase, 0, 7)) && $self->{'second_half'}->match(length $passphrase > 7 ? substr($passphrase, 7, 7) : '')
$self->_passphrase_acceptable($passphrase) $self->{"first_half"}->match(substr($passphrase, 0, 7)) $self->{'second_half'}->match(length $passphrase > 7 ? substr($passphrase, 7, 7) : '')
Truth table50%2/4
A and B && C
A= $self->_passphrase_acceptable($passphrase)B= $self->{"first_half"}->match(substr($passphrase, 0, 7))C= $self->{'second_half'}->match(length $passphrase > 7 ? substr($passphrase, 7, 7) : '')
ABCresult
0XX0
10X0
1100
1111
238 substr($passphrase, 7, 7) :
239 "");
240 }
241  
242 5 ▶sub as_rfc2307 {
Subroutine as_rfc2307: called CC=1 SCAR=0.0
Pod documented
243 5 my($self) = @_;
244 5 return "{LANMAN}".$self->hash_hex;
245 }
246  
247 =back
248  
249 =head1 SEE ALSO
250  
251 L<Authen::Passphrase>,
252 L<Authen::Passphrase::LANManagerHalf>,
253 L<Crypt::DES>
254  
255 =head1 AUTHOR
256  
257 Andrew Main (Zefram) <zefram@fysh.org>
258  
259 =head1 COPYRIGHT
260  
261 Copyright (C) 2006, 2007, 2009, 2010, 2012
262 Andrew Main (Zefram) <zefram@fysh.org>
263  
264 =head1 LICENSE
265  
266 This module is free software; you can redistribute it and/or modify it
267 under the same terms as Perl itself.
268  
269 =cut
270  
271 2 1;
« Passphrase/MySQL41.pm ↑ Summary Passphrase/LANManagerHalf.pm »