Register
It is currently Fri Aug 01, 2014 4:28 pm

Simple Password protection script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sun Jul 17, 2011 4:51 pm   

Joined: Sun Jul 17, 2011 4:21 pm
Posts: 6
Hi community,

I am rather new to bash, so i apologize in advance for my ignorance. I've created a simple bit of code that will prompt for a password before continuing in the script. The script works simply by reading a hash from the hash file (for the sake of the example, i've made it on the desktop) and checking it with the hash of the entered password. I want to augment the script to ask for a username and password, and to have the ability to work with multiple usernames (so that what the script will then do can differ with usernames), but i've found that i simply don't have the skills to do it. Any help would be greatly appreciated. Thank you so much! What I have so far is below:

Code:
#!/bin/bash
#
# Password_Protection.sh
#


theHash=`cat ~/Desktop/Hash.txt`
tries=2
num=$tries

read -sp "Type your password " enteredPass;
enteredHash=`echo "$enteredPass\\c" | openssl sha1 | cut -c9-`

if [ $enteredHash != $theHash ]; then
   while [ $enteredHash != $theHash ]
   do
   read -sp "
incorrect, try again (try $tries) " enteredPass;
   enteredHash=`echo "$enteredPass\\c" | openssl sha1 | cut -c9-`
   tries=$(($num+1))
   num=$tries
   done
fi

echo "
__    __   ___ _        __  ___  ___ ___   ___
|  |__|  | /  _] |      /  ]/   \|   |   | /  _]
|  |  |  |/  [_| |     /  /|     | _   _ |/  [_
|  |  |  |    _] |___ /  / |  O  |  \_/  |    _]
|        |   [_|     /   \_|     |   |   |   [_
\      /|     |     \     |     |   |   |     |
  \_/\_/ |_____|_____|\____|\___/|___|___|_____|"



#
# code here...
#


Top
 Profile  
 PostPosted: Mon Jul 18, 2011 11:00 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
You could have a colon separated hash/password file, like /etc/passwd.
Just put multiple username:password combination in there and use something like the code below:
Code:
#!/bin/bash

## format: username:password_hash
hashfile="hashfile.txt"

read -p "Username: " user
hash=$(egrep "^${user}:" "$hashfile" | awk -F: '{ print $2; }')

cnt=3
while [ "$pass" != "$hash" -a $((cnt-=1)) -ge 0 ]; do
  read -sp "Password: " pass && echo
  pass=$(echo -n "$pass" | openssl sha1)
done

if [ -n "$user" -a -n "$pass" -a "$pass" == "$hash" ]; then
  echo "User $user is authenticated"
else
  echo "Access denied"
fi


Top
 Profile  
 PostPosted: Wed Jul 20, 2011 9:00 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 664
Location: Des Moines, Iowa
Patsie,

Haven't ran the code, but that is very cool ;)


Top
 Profile WWW  
 PostPosted: Thu Nov 10, 2011 7:35 pm   

Joined: Thu Nov 10, 2011 7:31 pm
Posts: 21
change
Code:
~/Desktop/Hash.txt
to
Code:
./hash


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP