Last active
May 14, 2019 18:08
-
-
Save dbuteau/42929a7b3623c2e9ba8572e42ff31f3a to your computer and use it in GitHub Desktop.
check /etc/shadow and say if you found correct password or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usefull if you don't remember which password you setted | |
# you can try misc password and the script will warn you | |
# when you found the correct one | |
# Requirement: | |
# you need to install "whois" package | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run as root" | |
exit | |
fi | |
if [ -z $1 ];then | |
read -p 'Enter username: ' USERNAME | |
else | |
USERNAME=$1 | |
fi | |
read -s -p 'Enter Password: ' PASSWORD | |
line=$(grep "$USERNAME" /etc/shadow) | |
if [ -z $line ];then | |
echo "User $USERNAME does not exist" | |
exit 1 | |
fi | |
format=$(echo $line | sed "s/^$USERNAME:\$\(.*\)\$\(.*\)\$\([^:]*\).*/\1/") | |
salt=$(echo $line | sed "s/^$USERNAME:\$\(.*\)\$\(.*\)\$\([^:]*\).*/\2/") | |
truepassword=$(echo $line | sed "s/^$USERNAME:\$\(.*\)\$\(.*\)\$\([^:]*\).*/\3/") | |
case $format in | |
1) method="md5";; | |
2a) method="Blowfish";; | |
2y) method="Blowfish";; | |
5) method="sha-256";; | |
6) method="sha-512";; | |
esac | |
if [ "$method" = "Blowfish" ];then | |
echo "method not supported by this tool" | |
exit 1 | |
fi | |
# depending of system the following command syntax can change | |
# here its tested for raspbian, try a "mkpasswd --help" to check syntax | |
encrypted=$(mkpasswd -m$method $PASSWORD $salt | sed "s/.*\$\(.*\)$/\1/") | |
if [ "$encrypted" = "$truepassword" ];then | |
echo -e "\r\nyou found it!" | |
else | |
echo -e "\r\nmissed! try again" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment