我正在登录并注册表格。由于我正在处理密码,我想做正确的事,所以请原谅长长的代码行。我已经设法做了将密码哈希的注册表单。但我的问题是登录密码时没有读取密码,我只使用一个模拟帐户和一个密码。你认为它是哈希吗?请帮忙
PHP代码(我已经创建了一个functions.php文件,其中包含执行此操作所需的功能)
登录功能
function login($email, $password, $mysqli) {
// Using prepared Statements means that SQL injection is not possible.
if ($stmt = $mysqli->prepare("SELECT accountID, UserName, Password, salt FROM accounts WHERE email = ? LIMIT 1")) {
$stmt->bind_param('s', $email); // Bind "$email" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($user_id, $username, $db_password, $salt); // get variables from result.
$stmt->fetch();
$password = hash('sha512', $password.$salt); // hash the password with the unique salt.
if($stmt->num_rows == 1) { // If the user exists
// We check if the account is locked from too many login attempts
if(checkbrute($user_id, $mysqli) == true) {
// Account is locked
// Send an email to user saying their account is locked
return false;
} else {
if($db_password == $password) { // Check if the password in the database matches the password the user submitted.
// Password is correct!
$user_browser = $_SERVER['HTTP_USER_AGENT']; // Get the user-agent string of the user.
$user_id = preg_replace("/[^0-9]+/", "", $user_id); // XSS protection as we might print this value
$_SESSION['user_id'] = $user_id;
$username = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $username); // XSS protection as we might print this value
$_SESSION['username'] = $username;
$_SESSION['login_string'] = hash('sha512', $password.$user_browser);
// Login successful.
return true;
} else {
// Password is not correct
// We record this attempt in the database
$now = time();
$mysqli->query("INSERT INTO login_attempts (user_id, time) VALUES ('$user_id', '$now')");
return false;
}
}
} else {
// No user exists.
return false;
}
}
}
我有一个处理强制登录的checkbrute函数
function checkbrute($user_id, $mysqli) {
// Get timestamp of current time
$now = time();
// All login attempts are counted from the past 2 hours.
$valid_attempts = $now - (2 * 60 * 60);
if ($stmt = $mysqli->prepare("SELECT time FROM login_attempts WHERE user_id = ? AND time > '$valid_attempts'")) {
$stmt->bind_param('i', $user_id);
// Execute the prepared query.
$stmt->execute();
$stmt->store_result();
// If there has been more than 5 failed logins
if($stmt->num_rows > 5) {
return true;
} else {
return false;
}
}
}
最后我有一个login_check来检查是否已设置所有会话变量
function login_check($mysqli) {
// Check if all session variables are set
if(isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
$user_id = $_SESSION['user_id'];
$login_string = $_SESSION['login_string'];
$username = $_SESSION['username'];
$user_browser = $_SERVER['HTTP_USER_AGENT']; // Get the user-agent string of the user.
if ($stmt = $mysqli->prepare("SELECT Password FROM accounts WHERE accountID = ? LIMIT 1")) {
$stmt->bind_param('i', $user_id); // Bind "$user_id" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
if($stmt->num_rows == 1) { // If the user exists
$stmt->bind_result($password); // get variables from result.
$stmt->fetch();
$login_check = hash('sha512', $password.$user_browser);
if($login_check == $login_string) {
// Logged In!!!!
return true;
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
}
我通过单独的html文件
运行登录表单<body>
<form action="process_login.php" method="post" name="login_form">
Email: <input type="text" name="email" value=""/>
Password: <input type="password" name="password" id="password" value="" />
<input type="button" value="Login" onclick="formhash(this.form, this.form.password);" />
</form>
</body>
</html>
和Process_login.php
include 'db_connect.php';
include 'functions.php';
sec_session_start(); // Our custom secure way of starting a php session.
if(isset($_POST['email'], $_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password']; // The hashed password.
if(login($email, $password, $mysqli) == true) {
// Login success
echo 'Success: You have been logged in!';
} else {
// Login failed
echo 'Fail';
}
} else {
// The correct POST variables were not sent to this page.
echo 'Invalid Request';
}
由于
答案 0 :(得分:1)
Absolute ZERO是对的,您使用的是随机盐,并且您将密码哈希作为一个整体进行比较,它们永远不会匹配。我认为你应该使用bcrypt。因为你可以创建随机和唯一的盐,但是它们的crypt()函数只能检查散列的部分而不是盐。
这是一种更明智的方法,您不必为存储盐创建单独的列。另外bcrypt可能是目前最安全的方法哈希方法,而且速度非常慢,这对攻击者而不是用户来说只是一个问题。
我不知道如何使用bcrypt的教程,但我相信你在Google上搜索时会发现很多。但是,这是一个关于登录和注册系统的非常好的教程,它也使用了bcrypt():http://www.sunnytuts.com/article/login-and-registration-with-object-oriented-php-and-pdo
我认为你会发现它非常有用,但请确保你继续阅读该教程的第2部分。
祝你好运。答案 1 :(得分:0)
你对安全性有了正确的想法,但如果你在尝试与现有记录进行比较时每次尝试都会生成一个新的随机盐,那么它 永远不会匹配(这是随机的)*。您必须将salt
与salted hashed password
一起存储在表格中。对于每个用户,盐应该是随机的(如随机,而不是他们的用户代理),但是您需要使用从创建帐户的salt
到hash
用户的密码输入进行比较
进行修复:
您需要在帐户表格中添加“盐”列(或与帐户表相关的关系表)。
添加完毕后,您需要使用用户信息将您正在执行insert
的任何功能调整到数据库中。您需要将salt='$salt'
添加到SQL。
在检查输入login_check()
和login()
时,您需要执行以下操作:
更新查询的SQL以首先检查用户名:
"select count(*) from accounts where username = '$username'"
如果计数为“1”,则执行以下操作:
"select salt from accounts where username = '$username'"
然后你会这样做:
$password_for_comparison = hash('sha512', $password.$salt_from_database);
然后你会这样做:
"select count(*) from accounts where username='username' and password='$password_for_comparison'"
如果此计数为1
,则表示您拥有有效用户。
可以将这些内容添加到新函数中并在其他函数中调用(因此您不需要维护两个单独的代码集)。
*注意: 有些情况下可能会发生碰撞,但是如果您选择了sha512,这是一种随机的可能性,如果没有人尝试暴力破碎的话在一个装有25个运行opencl的vid卡的盒子上有类似hashcat的东西。