LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 05-28-2017, 11:07 AM   #1
SlowLearner
Member
 
Registered: Dec 2002
Location: Florida
Distribution: Slackware 14.2
Posts: 57
Blog Entries: 1

Rep: Reputation: 20
Question How To Manually Duplicate SHA-512 hash with a known password & hash


I must be misunderstanding some basic concept.

RHEL 7.3

I created a new user called alpha and gave them a password of alpha

Code:
#useradd alpha
#passwd alpha
Changing password for user alpha.
New UNIX password:alpha
Retype new UNIX password:alpha
passwd: all authentication tokens updated successfully.

#cat /etc/shadow | grep alpha

alpha:$6$IkpXV1TK$js2IT6ck4YnAgV9LRLk85/RICfEpDZYxX9QjKnMGqwDJ7Ax0B/kev9.1d0siSr6P6iHoFx3RWeakobMBw1vO0:17311:0:999999:7:::
I am attempting to duplicate the hash created by the passwd command above.

Using the example found here I enter:

Code:
 python -c "import crypt, getpass, pwd; \
             print crypt.crypt('alpha', '\$6\$IkpXV1TK\$')"
Python result is

Code:
$6$IkpXV1$3FdY18hW41KMzV.sm9un1mufhg2VdSrCfeLsAfxfWi1gsKOHufboMhauxoIGwT.U2ba1aBrmHi/KZTZctX6uv/
instead of passwd generated hash of

Code:
$6$IkpXV1$js2IT6ck4YnAgV9LRLk85/RICfEpDZYxX9QjKnMGqwDJ7Ax0B/kev9.1d0siSr6P6iHoFx3RWeakobMBw1vO0
Why don't these two values match?

Last edited by SlowLearner; 05-28-2017 at 12:55 PM. Reason: Edited the output of /etc/shadow :grep alpha command to include the TK - it was simply mis-typed originally
 
Old 05-28-2017, 11:42 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,590

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
Quote:
Originally Posted by SlowLearner View Post
Code:
alpha:$6$IkpXV1$js2IT6ck4YnAgV9LRLk85/RICfEpDZYxX9QjKnMGqwDJ7Ax0B/kev9.1d0siSr6P6iHoFx3RWeakobMBw1vO0:17311:0:999999:7:::
Code:
 python -c "import crypt, getpass, pwd; \
             print crypt.crypt('alpha', '\$6\$IkpXV1TK\$')"
Where did the TK come from?
 
Old 05-28-2017, 11:54 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,522
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
Are you taking the value of the salt into account when calculating the hash?
 
Old 05-28-2017, 12:53 PM   #4
SlowLearner
Member
 
Registered: Dec 2002
Location: Florida
Distribution: Slackware 14.2
Posts: 57

Original Poster
Blog Entries: 1

Rep: Reputation: 20
@ntubski wow YES Very Good Catch! Good eye, ntubski.

I doubled checked my results.. The TK in the python command actually belongs there. I just mis-typed the /etc/shadow output in my original post. I'm working in a very rudimentary Virtualbox setup with no way to share data between the virtual RHEL7.3 machine and the host machine that I have web access on and am typing this post with.

lol that means yes all those hashes were actually hand-typed just to ask this question.
(i love this forum)

I will edit my original post to accurately reflect the output of the
cat /etc/shadow : grep alpha command

@Turbocapitalist
yes i *believe* i am taking into account the salt.
http://www.yourownlinux.com/2015/08/...explained.html
offers a good explanation of what we are ACTUALLY looking at in the /etc/shadow file

My understanding is (i could be wrong) is that the $ in the second field of the /ect/shadow file are field separators where

$
6 = SHA512
$
IkpXV1TK = the random salt that was originally used by passwd to create the hash aka the 86 character string below
$
js2IT6ck4YnAgV9LRLk85/RICfEpDZYxX9QjKnMGqwDJ7Ax0B/kev9.1d0siSr6P6iHoFx3RWeakobMBw1vO0 = the hash used to authenticate a user

Is my understanding correct there ?
 
Old 05-28-2017, 01:53 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,522
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
Quote:
Originally Posted by SlowLearner View Post
$
6 = SHA512
$
IkpXV1TK = the random salt that was originally used by passwd to create the hash aka the 86 character string below
$
js2IT6ck4YnAgV9LRLk85/RICfEpDZYxX9QjKnMGqwDJ7Ax0B/kev9.1d0siSr6P6iHoFx3RWeakobMBw1vO0 = the hash used to authenticate a user

Is my understanding correct there ?
Yes, as far as I know. But using two different methods, I come up with a third hash. :/

Code:
$ mkpasswd --method=sha-512 --salt=IkpXV1TK 'alpha'; 
$6$IkpXV1TK$3FdY18hW41KMzV.sm9un1mufhg2VdSrCfeLsAfxfWi1gsKOHufboMhauxoIGwT.U2balaBrmHi/KZTZctX6uv/

$ perl -e 'print crypt("alpha","\$6\$IkpXV1TK\$") . "\n"'
$6$IkpXV1TK$3FdY18hW41KMzV.sm9un1mufhg2VdSrCfeLsAfxfWi1gsKOHufboMhauxoIGwT.U2balaBrmHi/KZTZctX6uv/
And I see the same hash with python as well:

Code:
$ python -c "import crypt, getpass, pwd; \ 
              print crypt.crypt('alpha', '\$6\$IkpXV1TK\$')";
$6$IkpXV1TK$3FdY18hW41KMzV.sm9un1mufhg2VdSrCfeLsAfxfWi1gsKOHufboMhauxoIGwT.U2balaBrmHi/KZTZctX6uv/
 
1 members found this post helpful.
Old 05-28-2017, 02:22 PM   #6
SlowLearner
Member
 
Registered: Dec 2002
Location: Florida
Distribution: Slackware 14.2
Posts: 57

Original Poster
Blog Entries: 1

Rep: Reputation: 20
Question

@Turbocapitalist VERY intersting !

But at least all three of your hashes are the SAME using all three methods. They all match with each other - as they should I would imagine.

The first difference I see in our approaches is that when i set the password using the passwd command i did not specify the salt. I allowed passwd to use a random generated salt which i assume is the IkpXV1TK value I am seeing in my /etc/shadow. Then i used this value and attempted to match what passwd created.. THEY DON'T MATCH. Which tells me we are missing some basic understand of how the process works.

Is it possible that mkpasswd and the perl script and the python method all come up with the same result because they are using the same 2 pieces of data.. the salt and the password to generate the hash.

Perhaps the passwd command is using the salt, the password AND the username to generate the hash.

Is that what is happening?

(Now i am expecting some snarky RTFM reply to arriving momentarily.)
 
Old 05-28-2017, 11:50 PM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,522
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
Quote:
Originally Posted by SlowLearner View Post
Is it possible that mkpasswd and the perl script and the python method all come up with the same result because they are using the same 2 pieces of data.. the salt and the password to generate the hash.
They would have to be using the same data to come up with the same hash. So I have to wonder if you haven't mistyped the password when you set up the account. That would be a likely explanation. I'm able to get the same results as what I have in /etc/shadow using any of the three methods and known passwords. passwd does always choose a salt for you. As far as I know, there is no way to force it to choose a specific salt.

So try using passwd again and setting the password carefully, just to be sure. Then use one of the three methods to test with what you entered.

About the manual pages, they won't guide much here on the inner workings of the program.

Code:
man 3 crypt
man 5 shadow
If you really do want to dig, the source is the answer for what passwd is really doing and would answer your last question. You can get it from the srpm.

Code:
yum whatprovides /bin/passwd
yumdownloader --source passwd
rpm -ivh passwd-*.src.rpm
cd rpmbuild/SOURCES/
Then uncompress the tarball, apply the patch, enter the directory and browse.
 
Old 05-30-2017, 08:24 AM   #8
SlowLearner
Member
 
Registered: Dec 2002
Location: Florida
Distribution: Slackware 14.2
Posts: 57

Original Poster
Blog Entries: 1

Rep: Reputation: 20
Question

@Turbocapitalist

Thanks for the idea but I just triple checked my result.
I can login as
alpha
with credentials of
alpha
no misspelling there.
I also spent considerable time googling around for an actual explanation of HOW the passwd command creates the hash. Yes I understand random hash, and the password are hashed using an algorithm such as sha512. But I suspect something ELSE is included in the hash. I thought someone on the forum would immediately just know without me having to
Code:
yum whatprovides /bin/passwd
yumdownloader --source passwd
rpm -ivh passwd-*.src.rpm
cd rpmbuild/SOURCES/
Thank you muchly for the explination of how to access source though. I need to get the virtual machine networked first to utilize yum


I will check my theory on a working system as well. Not just a virtualized one that I am currently playing in now.

In the mean time, rather than using mkpasswd as you are.

I would like you (or anyone else) to look at your existing /etc/shadow file and attempt to duplicate a hash for a known user there using the existing known password.

Match ?
Or not?
 
Old 05-30-2017, 08:31 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,522
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
Quote:
Originally Posted by SlowLearner View Post
I would like you (or anyone else) to look at your existing /etc/shadow file and attempt to duplicate a hash for a known user there using the existing known password.
Yep it matches using any of the three methods. It was one of the first things I tried. This is on Devuan Ascii for x86_64
 
1 members found this post helpful.
Old 05-30-2017, 08:42 AM   #10
SlowLearner
Member
 
Registered: Dec 2002
Location: Florida
Distribution: Slackware 14.2
Posts: 57

Original Poster
Blog Entries: 1

Rep: Reputation: 20
Exclamation

Fascinating
Working system Slackware 14.1 the

python -c "import crypt, getpass, pwd; \
print crypt.crypt('enterknownpasswordhere', '\$6\$shadowsalthere\$')";

method works flawlessly - generates a perfect match to what shows in my /etc/shadow

Something unique is happening in Oracle OpenVM Virtualbox or in RHEL 7.0 not sure which is the reason for the aberrant behavior.

I've got a working Centos system I can check.

I am curious what other forum users will discover.
 
Old 05-30-2017, 09:06 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,590

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
Quote:
Originally Posted by SlowLearner View Post
I also spent considerable time googling around for an actual explanation of HOW the passwd command creates the hash. Yes I understand random hash, and the password are hashed using an algorithm such as sha512. But I suspect something ELSE is included in the hash.
It's an iterated hash, so maybe your RHEL system has a different number of iterations set by default. According to this post, that is controlled by /etc/pam.d/common-password.
 
1 members found this post helpful.
Old 05-31-2017, 06:05 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,522
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
If that file is not present, you can also look in /etc/login.defs for the number of rounds. It is sometimes there. If not, the default might be 1000.

Then you can specify the number of rounds explicitly:

Code:
$ mkpasswd --method=sha-512 --rounds=1000 --salt='m1jjn93I' alpha; 
$6$rounds=1000$m1jjn93I$xrF.pbeL9PLhkp3KtWv8Zij1JAbmULK.gme0PX1Oh.SF1hDwvW7kA6kbQe/N976jAc0DJGtWh.cURnO09nvud0

$ perl -e 'print crypt("alpha","\$6\$rounds=1000\$m1jjn93I\$") . "\n"'
$6$rounds=1000$m1jjn93I$xrF.pbeL9PLhkp3KtWv8Zij1JAbmULK.gme0PX1Oh.SF1hDwvW7kA6kbQe/N976jAc0DJGtWh.cURnO09nvud0
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do you encrypt a USB partition with the Twofish cipher and SHA-512 hash? Cinematography Linux - Security 1 05-31-2014 07:41 PM
How can I convert a sha-512 /etc/shadow hash to base64? abefroman Linux - Security 1 09-15-2013 10:47 AM
Dynamically parse BibTeX and create hash of hash wakatana Programming 11 12-13-2012 04:59 PM
Perl Hashes -- Updating a hash ref via hash value 0.o Programming 5 06-05-2012 12:45 PM
NIST's search for the super hash just five candidates left in SHA-3 final win32sux Linux - Security 0 12-10-2010 02:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 07:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration