Skip to content

Instantly share code, notes, and snippets.

@antonlindstrom
Created April 19, 2009 18:41
Show Gist options
  • Save antonlindstrom/98167 to your computer and use it in GitHub Desktop.
Save antonlindstrom/98167 to your computer and use it in GitHub Desktop.
School assignment #3
#!/usr/bin/perl -w
#
# Check if original password has changed.
#
# Gets user ssn from passwd comment field and hashing it with Crypt::PasswdMD5.
# Program checks if the hashed reversed ssn is the same as the current password.
# Prints message if a user has not changed his/hers password.
# Include Crypt::PasswdMD5 Module.
use Crypt::PasswdMD5;
# Open file $shadow and assign to @shadow array.
$shadow = "shadow";
open(SHAD, $shadow) or die($!);
@shadow = <SHAD>;
# As the file is loaded into @shadow array, it can be closed.
close(SHAD);
# Open passwd file.
$users = "passwd";
open(USRS, $users) or die($!);
@users = <USRS>;
close(USRS);
# Set all users with passwords in hash.
foreach (@shadow) {
# Split values in shadow.
@shadow_posts = split(":", $_);
# If username match a letter, two numbers and a few letters.
if ($shadow_posts[0] =~ /\w(\d\d)(\w+)/) {
# Add username as key, password as value.
$usrh{$shadow_posts[0]} = $shadow_posts[1];
}
}
# Get usernames and personalnumbers from passwd.
foreach (@users) {
# Split values, if username exists in hash add comment pnr.
@users_posts = split(":", $_);
if (exists($usrh{$users_posts[0]})) {
# Comment field is 4, string is reversed and last 11 chars (pnr) is used.
@pnr = split(' ', $users_posts[4]);
# Set reverse pnr in hash with username as key.
$usrc{$users_posts[0]} = reverse($pnr[2]);
}
}
# Print Users and passwords.
foreach (keys %usrh) {
# If password does not exist in $usrc{username} set string NULL.
if (!exists($usrc{$_})) {$pw = "NULL";}
else {$pw = $usrc{$_};}
# If the password contains $1$ it is unix md5, therefor it can be checked.
@pass = split(/\$/,$usrh{$_});
if ($pass[0] eq "" ) {
# Crypt pnr with salt in passwd.
$cryptedpassword = unix_md5_crypt($pw, $pass[2]);
@cpass = split(/\$/,$cryptedpassword);
print "User $_: $pass[3] - $cpass[3] ($pw)\n";
# See if password is unchanged.
if ($pass[3] eq $cpass[3]) {
print "\tUser $_ has not changed his/her password since add.";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment