I am reviewing previous undocumented php work from a predecessor on these two scripts to manage access to an administrative dashboard, but I'm not sure about vulnerabilities and other things that usually go unnoticed. I tried to improve on his design and this is the result.
Is this login check secure?
<?php
session_start();
$check1 = (empty($_SESSION) || $_SESSION['admin'] != true);
$check2 = ($_SESSION['NO_HIJACK'] == (
($_SERVER['HTTP_USER_AGENT'] ?? '?') . '+' .
($_SERVER['REMOTE_ADDR'] ?? '?')));
if ($check1 || $check2) {
http_response_code(401);
header("Location: http://admin.example.com");
exit(0);
}
?>
This is the verification script that receives the login key from the login form with a jquery post:
<?php
if (!empty($_POST) && isset($_POST['key'])) {
$controlKey = array();
$controlKey[] = 'token1';
$controlKey[] = 'token2';
$controlKey[] = 'token3';
$hKey = hash('sha256', $_POST['key']);
if (in_array($hKey, $controlKey)) {
session_start();
session_regenerate_id(true);
$_SESSION['admin'] = true;
$_SESSION['NO_HIJACK'] = (($_SERVER['HTTP_USER_AGENT'] ?? '?') .
'+' . ($_SERVER['REMOTE_ADDR'] ?? '?'));
echo json_encode(array("status" => "success"));
} else {
echo json_encode(array("status" => "fail"));
}
} else {
echo json_encode(array("status" => "empty"));
}
?>
md5()tohash('sha256', key). Also, I don't understand why he usedbase64_encode(), it adds no additional security... If you go with passwords in DB, then take a look atpassword_hash()andpassword_verify()functions in PHP. \$\endgroup\$password_verify()isn't that fast. But for administration (where optimizing every little thing usually doesn't matter that much), it is an viable option. \$\endgroup\$base64_encode()to store passwords in database... Again, it doesn't really do anything for you. And if somebody got to that password, he could just usebase64_decode(). One thing with this implementation is, anybody can use any password. Are there different users, or just single user and admins themselves have different passwords for that single user? Ps.: With only 10 passwords max, I would use thepassword_hash()andpassword_verify()for added security. \$\endgroup\$