Billy Moon Billy Moon - 3 months ago 14
Linux Question

How can I calculate shadow password for sha-512 using php/shell

I created account sysadmin with password delme. I then want to calculate the sha-512 of the password using php.

I expected this should produce the same hash...

billy@iserve:~$ sudo cat /etc/shadow | grep sysadmin
sysadmin:$6$q5HxMEDr$VUPS0JrRFv5bohFtsscvjQ7t2fUhi0m2z8f0ObKtorwlSHqiGde8N9hprkqmnB9LOtEDorG.3yBSXYSAKcJmz.:15276:0:99999:7:::
billy@iserve:~$ php -r "echo crypt('delme','$6$rounds=5000$q5HxMEDr$').\"\n"\";"
=54Jjswxnfslg


I can not replicate the password in the shadow file... can you tell me how?

Answer

I ended up doing this to solve my problem...

/*    Need to add www-data to group shadow (and restart apache)
        $ sudo adduser www-data shadow
        $ sudo /etc/init.d/apache2 restart
      Needs whois to be installed to run mkpasswd
        $ sudo apt-get install whois
      Assumes that sha-512 is used in shadow file
*/

function authenticate($user, $pass){
  // run shell command to output shadow file, and extract line for $user
  // then split the shadow line by $ or : to get component parts
  // store in $shad as array
  $shad =  preg_split("/[$:]/",`cat /etc/shadow | grep "^$user\:"`);
  // use mkpasswd command to generate shadow line passing $pass and $shad[3] (salt)
  // split the result into component parts and store in array $mkps
  $mkps = preg_split("/[$:]/",trim(`mkpasswd -m sha-512 $pass $shad[3]`));
  // compare the shadow file hashed password with generated hashed password and return
  return ($shad[4] == $mkps[3]);
}

// usage...
if(authenticate('myUsername','myPassword')){
  // logged in   
} else {
  // not valid user
}

I am not entirely confident about the security of this method, so will be asking a question about that.

Comments