Today I saw a snippet a code for authentication logic :
function checkPass($pass, $login){
$inputedPass = $pass;
$req = $bdd->prepare('SELECT password FROM agents WHERE password = MD5(?) AND ID = ?;');
$req->bindParam(1, $inputedPass, PDO::PARAM_STR);
$req->bindParam(2, $login, PDO::PARAM_STR);
$req->execute();
$result2 = $req->fetch();
if (empty($result2)) {
$req->closeCursor();
return false;
} else {
$req->closeCursor();
return true;
}
}
Ignoring the MD5 part, which we know is outdated, this code doesn't check if your password is the right one (as in, comparing the hash of the password you entered with the hash in the database), it checks if there's a user with your name and the same password.
I changed this to allowing access on equal hashes instead, but I've been told "it's the same".
How do I explain it's not? What are the specific risks with his approach?
[EDIT]
Following @Marc's answer, here's how i updated this function :
checkPass($pass, $login){
$inputedPass = $pass;
$req = $bdd->prepare('SELECT password FROM agents WHERE Id = ?;');
$req->bindParam(1, $login, PDO::PARAM_STR);
$req->execute();
$result2 = $req->fetch();
$req->closeCursor();
if(isset($result2["password"]){
if (password_verify($inputedPass , $result2["password"])) {
return true;
}
} else {
sleep(rand(40, 100) / 100); //measly attempt to mitigate side channel timing attack
return false;
}
}
Which i realized after reading the comments, doesn't handle the case when the user doesn't exists (i assume it will crash). However, it doesn't sends plain passwords nor relies on MD5.
Thank you
(login, md5(password))
pair. The main distinction between checking for(login, md5(password))
vs fetchingstored_md5_of_password
forlogin
and comparing it withmd5(password)
is that you can't tell the difference between non-existing user vs wrong password (this is usually not something you want to leak anyway). The nice thing about doing it this way is that this is constant time (modulo the DB lookup time) unlike your solution which is faster when the user does not exist. – Marc Jun 4 '20 at 9:18md5(password)
alone marks this as horrible. – Marc Jun 4 '20 at 9:18