2012-07-05 36 views
0

我正在嘗試使用sha1哈希來保護密碼,以避免saltMe()函數,但無論我在密碼字段中輸入什麼密碼,ectry的密碼都會以相同的方式結束。 這可能會導致我認爲的安全漏洞。sha1 + salt encyption在php中失敗

這就是我的鹽功能

************************************************** 
* 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 

這也是我的加密密碼。但是我可以輸入任何我想匹配密碼的信息並不好。

登錄表單的行動是validatelogin.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); 
       } 
      } 
     } 
    } 

其中字符串設定所以它總是匹配我想不通!

+0

您必須爲每個密碼使用不同的鹽。 – SLaks 2012-07-05 11:26:37

+0

您有一個SQL注入漏洞。 – SLaks 2012-07-05 11:26:56

+0

SHA-1不是加密,而是散列。 – Joey 2012-07-05 11:28:02

回答

2

當鹽分通過時,U返回一個常量字符串。嘗試

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

也作爲SLaks說,你有SQLinj。最好使用PDO或mysqli數據庫功能。

+0

完美的作品!謝謝! – 2012-07-05 11:41:53

4

您的SaltMe函數沒有得到參數,它總是返回相同的字符串。所以

SaltMe($password) 

不會做你想要的。

儘管如此:停止嘗試實現自己的密碼散列方案。事實上,你在這裏詢問實施過程中的錯誤應該足以證明你沒有足夠的理解。使用圖書館,例如bcrypt(Portable PHP Password Hashing Framework有一個實現)和stay far away from ever implementing any crypto code yourself

+0

不錯的編輯,喬伊:) – Ziumin 2012-07-05 11:35:03

+0

WHOAH! :-O大滿貫! 但是,謝謝:)看起來不錯! – 2012-07-05 11:41:34

1

您只是對salt字符串進行散列處理,您並沒有將其加到您的密碼前。

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

在散列它之前,您需要用鹽作爲密碼的前綴。