Passphrase/LANManagerHalf.pm

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