SHA1 + Salt 加密在 PHP 中失败


sha1 + salt encryption fails in php

我正在尝试使用 sha1 哈希到 saltMe() 函数来保护密码,但无论我在密码字段中输入什么,密码最终都是一样的。我认为这可能会导致安全漏洞。

这是我的盐函数

**************************************************
* sha1 salt string to make a more secure hash 
***************************************************/
function SaltMe() {
   //Secret keyword encrypted with sha1
   return "4cefe49883b6dd1a00565e2a80fb035f348da3aa";
}

这是我的登录检查

$select_user_sql = $this->db->selectSQL("*", "tdic_users", "email = '". $email ."' AND password = '". sha1($this->main->SaltMe($password)) ."'");

无论我在密码字段中输入什么,我最终都会得到:

1c2c2961d35148e8dfc83c7b31cf144f0987de9d

这也是我的加密密码。但是我可以键入任何我想匹配密码的内容并不好。

登录

表单的操作是验证登录.php其中包含:

$user = new UserHandling();
$user->UserLogMeIn($_POST["login_email"], $_POST["login_password"]);

和登录功能:

 /**********************************************************
     * User login function
     * 
     * @param string    | User's email
     * @param string    | User's password
     **********************************************************/
    function UserLogMeIn($email, $password) {
        $select_user_sql = $this->db->selectSQL("*", "tdic_users", "email = '". $email ."' AND password = '". sha1($this->main->SaltMe($password)) ."'");
        $select_user_result = $this->db->SQLquery($select_user_sql);
        if(mysql_num_rows($select_user_result) < 1) {
            $this->main->txtOutput("Wrong email or password", "TXT_ERR"); //The user typed something wrong.
        } else { 
            while($row = $this->db->SQLfetch($select_user_result)) {
                /*** We will check if user have activated the profile ***/
                if($row["activated"] == 0) {
                    $this->main->txtOutput("Your profile haven't been activated! You need to click on the activation link in the email you recieved upon registration.", "TXT_ERR"); //The user haven't activated the new profile. This is necessary for security / spamming reasons
                    $this->main->JSredirector("http://localhost/test/login.php", 5); //Redirect the user back from where he/she came from
                } else {
                    /*** Everything is in order and we will let the user in ***/
                    $_SESSION["usr_logged_in"] = 1;
                    $_SESSION["user_email"] = $row["email"];
                    $_SESSION["user_id"] = $row["user_id"];
                    $_SESSION["user_name"] = $row["name"];
                    /*** This will just update the last login field in the user table ***/
                    $fields = array("user_last_logged_in" => time());
                    $update_user_sql = $this->db->updateSQL('tdic_users',  'email = "'. $email .'"', $fields);
                    $this->db->SQLquery($update_user_sql);
                }
            }
        }
    }

我无法弄清楚字符串设置在哪里,所以它总是匹配!

您的SaltMe函数没有参数,它总是返回相同的字符串。所以

SaltMe($password)

不会按照您的意图行事。

不过,严肃地说:停止尝试实现自己的密码哈希方案。你在这里询问你的实现中的错误这一事实应该足以证明你没有足够的理解来这样做。使用库,例如 bcrypt(可移植的 PHP 密码哈希框架有一个实现),并且远离自己实现任何加密代码。

U 在加盐传递时返回一个常量字符串。

尝试
function SaltMe($pass) {
   // Pass salted with Secret keyword encrypted with sha1 
   return "4cefe49883b6dd1a00565e2a80fb035f348da3aa" . $pass;
}

正如SLaks所说,你有SQLinj。最好使用 PDO 或 mysqli 数据库函数。

您只是对盐字符串进行哈希处理,尚未将其前缀为密码。

"4cefe49883b6dd1a00565e2a80fb035f348da3aa" -> [SHA-1] -> "1c2c2961d35148e8dfc83c7b31cf144f0987de9d"

在散列密码之前,您需要在密码前面加上盐。