Prettymad.net

Scripts for manipulating shadow passwords

Ever wanted to work with /etc/shadow? Well here is some usefull stuff I wrote that can help

unix_md5_crypt.pl

Generates a shadow password

#!/usr/bin/perl
# need to apt-get install libcrypt-passwdmd5-perl

#$crypted = unix_md5_crypt($2, $1);
use Crypt::PasswdMD5;

$numArgs = $#ARGV + 1;
if($numArgs == 0 || $numArgs > 2)
{
        print "unix_md5_crypt outputs unix MD5 Crypted shadow password\n";
        print "Usage: $0  \n";
        exit;
}

$password = $ARGV[1];
$salt = $ARGV[0];

print unix_md5_crypt($password, $salt);

get_shadow.sh

Returns a shadow password for a specific user

#!/bin/sh

# returns the shadow password of a specific user
cat /etc/shadow | grep "^$1:" | awk -F ':' '{ print $2 }'

test_passwd.sh

Shell script that uses the above two scripts to test a password, note that the above two scripts need to be in your $PATH

#!/bin/sh

USERNAME=$1
PASSWORD=$2 

CURRENT_SHADOW=`get_shadow.sh "$USERNAME"`
 
# get the salt
SALT=`echo "$CURRENT_SHADOW" | awk -F '$' '{print $3}'`

# generate a new shadow
NEW_SHADOW=`unix_md5_crypt.pl "$SALT" "$2"`

if [ "$NEW_SHADOW" == "$CURRENT_SHADOW" ]
then
        echo "Success"
        exit 0
else
        sleep 1
        echo "Failure"
        exit 1
fi