1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
package Data::Password;
# Ariel Brosh (RIP), January 2002, for Raz Information Systems
# Oded S. Resnik, 3 April 2004, for Raz Information Systems
use strict;
require Exporter;
use vars qw($DICTIONARY $FOLLOWING $GROUPS $MINLEN $MAXLEN
$FOLLOWING_KEYBOARD @DICTIONARIES
$VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
@EXPORT_OK = qw($DICTIONARY $FOLLOWING $GROUPS $FOLLOWING_KEYBOARD
@DICTIONARIES $MINLEN $MAXLEN IsBadPassword IsBadPasswordForUNIX);
%EXPORT_TAGS = ('all' => [@EXPORT_OK]);
@ISA = qw(Exporter);
$VERSION = '1.07';
$DICTIONARY = 5;
$FOLLOWING = 3;
$FOLLOWING_KEYBOARD = 1;
$GROUPS = 2;
$MINLEN = 6;
$MAXLEN = 8;
# Modified for Debian FHS from /usr/dict/* to /usr/share/dict/*
#@DICTIONARIES = qw(/usr/dict/web2 /usr/dict/words /usr/share/dict/words /usr/share/dict/linux.words);
@DICTIONARIES = qw(/usr/share/dict/web2 /usr/share/dict/words /usr/share/dict/linux.words);
sub OpenDictionary {
foreach my $sym (@DICTIONARIES) {
return $sym if -r $sym;
}
return;
}
sub CheckDict {
return unless $DICTIONARY;
my $pass = shift;
my $dict = OpenDictionary();
return unless $dict;
open (DICT,"$dict") || return;
$pass = lc($pass);
while (my $dict_line = <DICT>) {
chomp ($dict_line);
next if length($dict_line) < $DICTIONARY;
$dict_line = lc($dict_line);
if (index($pass,$dict_line)>-1) {
close(DICT);
return $dict_line;
}
}
close(DICT);
return;
}
sub CheckSort {
return unless $FOLLOWING;
my $pass = shift;
foreach (1 .. 2) {
my @letters = split(//, $pass);
my $diffs;
my $last = shift @letters;
foreach (@letters) {
$diffs .= chr((ord($_) - ord($last) + 256 + 65) % 256);
$last = $_;
}
my $len = $FOLLOWING - 1;
return 1 if $diffs =~ /([\@AB])\1{$len}/;
return unless $FOLLOWING_KEYBOARD;
my $mask = $pass;
$pass =~ tr/A-Z/a-z/;
$mask ^= $pass;
$pass =~ tr/qwertyuiopasdfghjklzxcvbnm/abcdefghijKLMNOPQRStuvwxyz/;
$pass ^= $mask;
}
return;
}
sub CheckTypes {
return undef unless $GROUPS;
my $pass = shift;
my @groups = qw(a-z A-Z 0-9 ^A-Za-z0-9);
my $count;
foreach (@groups) {
$count++ if $pass =~ /[$_]/;
}
$count < $GROUPS;
}
sub CheckCharset {
my $pass = shift;
$pass =~ /[\0-\x1F \x7F]/;
}
sub CheckLength {
my $pass = shift;
my $len = length($pass);
return 1 if ($MINLEN && $len < $MINLEN);
return 1 if ($MAXLEN && $len > $MAXLEN);
return;
}
sub IsBadPassword {
my $pass = shift;
if (CheckLength($pass)) {
if ($MAXLEN && $MINLEN) {
return "Not between $MINLEN and $MAXLEN characters";
}
elsif (!$MAXLEN) { return "Not $MINLEN characters or greater"; }
else { return "Not less than or equal to $MAXLEN characters"; }
}
return "contains bad characters" if CheckCharset($pass);
return "contains less than $GROUPS character groups"
if CheckTypes($pass);
return "contains over $FOLLOWING leading characters in sequence"
if CheckSort($pass);
my $dict = CheckDict($pass);
return "contains the dictionary word '$dict'" if $dict;
return;
}
sub IsBadPasswordForUNIX {
my ($user, $pass) = @_;
my $reason = IsBadPassword($pass);
return $reason if $reason;
my $tuser = $user;
$tuser =~ s/[^a-zA-Z]//g;
return "is based on the username" if ($pass =~ /$tuser/i);
my ($name,$passwd,$uid,$gid,
$quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam($user);
return unless $comment;
foreach ($comment =~ /([A-Z]+)/ig) {
return "is based on the finger information" if ($pass =~ /$_/i);
}
return;
}
1;
__END__
=head1 NAME
Data::Password - Perl extension for assesing password quality.
=head1 SYNOPSIS
use Data::Password qw(IsBadPassword);
print IsBadPassword("clearant");
# Bad password - contains the word 'clear', only lowercase
use Data::Password qw(:all);
$DICTIONARY = 0;
$GROUPS = 0;
print IsBadPassword("clearant");
=head1 DESCRIPTION
This modules checks potential passwords for crackability.
It checks that the password is in the appropriate length,
that it has enough character groups, that it does not contain the same
chars repeatedly or ascending or descending characters, or charcters
close to each other in the keyboard.
It will also attempt to search the ispell word file for existance
of whole words.
The module's policies can be modified by changing its variables. (Check L<"VARIABLES">).
For doing it, it is recommended to import the ':all' shortcut
when requiring it:
I<use Data::Password qw(:all);>
=head1 FUNCTIONS
=over 4
=item 1
IsBadPassword(password)
Returns undef if the password is ok, or a textual description of the fault if any.
=item 2
IsBadPasswordForUNIX(user, password)
Performs two additional checks: compares the password against the
login name and the "comment" (ie, real name) found on the user file.
=back
=head1 VARIABLES
=over 4
=item 1
$DICTIONARY
Minimal length for dictionary words that are not allowed to appear in the password. Set to false to disable dictionary check.
=item 2
$FOLLOWING
Maximal length of characters in a row to allow if the same or following.
If $FOLLOWING_KEYBOARD is true (default), the module will also check
for alphabetical keys following, according to the English keyboard
layout.
Set $FOLLOWING to false to bypass this check.
=item 3
$GROUPS
Groups of characters are lowercase letters, uppercase letters, digits
and the rest of the allowed characters. Set $GROUPS to the number
of minimal character groups a password is required to have.
Setting to false or to 1 will bypass the check.
=item 4
$MINLEN
$MAXLEN
Minimum and maximum length of a password. Both can be set to false.
=item 5
@DICTIONARIES
Location where we are looking for dictionary files. You may want to
set this variable if you are using not *NIX like operating system.
=back
=head1 FILES
=over 4
=item *
/usr/share/dict/web2
=item *
/usr/share/dict/words
=item *
/etc/passwd
=back
=head1 SEE ALSO
See L<Data::Password::BasicCheck> if you need only basic password checking.
=head1 AUTHOR
Raz Information Systems, B<razinf@cpan.org>, B<raz@raz.co.il>.
=head1 COPYRIGHT
Copyright (c) 2001 - 2006 Raz Information Systems Ltd.
http://www.raz.co.il/
This package is distributed under the same terms as Perl itself, see the
Artistic License on Perl's home page.
=cut
|