2013-08-26 71 views
-4

我的註冊表和/或登錄頁面上有一個小問題。我可以註冊,但我無法使用新註冊的帳戶登錄。我可以請你的建議傢伙?使用SHA512 encrypyion無法在PHP上創建註冊頁面

execAddEmp.php

<?php 
include ("./db_connect.php"); 
include ("./functions.php"); 

if(isset($_POST['email'], $_POST['username'], $_POST['status'])) { 

$username = $_POST['username']; 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$mi = $_POST['mi']; 
$email = $_POST['email']; 
$status = $_POST['status']; 

if($result = mysqli_query($mysqli, "SELECT * FROM employee WHERE email='$email'")){ 
$row_count = mysqli_num_rows($result); 
//Now display errors 

print ("Email is already in use!<br>"); 

} 
$password = $_POST['password']; 
$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true)); 
$password = hash('sha512', $password.$random_salt); 
print $password; 
if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, firstname, lastname, MI, status, email, password, salt) VALUES (?, ?, ?, ?,?,?,?,?)")) { $insert_stmt->bind_param('ssssssss', $username, $firstname, $lastname, $mi, $status, $email, $password, $random_salt); 
// Execute the prepared query. 
$insert_stmt->execute(); 
} 
if(!$insert_stmt){ 

die("There's little problem: ".mysqli_error($mysqli)); 

} 

include("loading.php"); 
echo '<meta http-equiv="refresh" content="1;url=addemployee.php">'; 
//echo "<a href='adddivisions.php'>back</a>"; 

} 
    else{ 
    echo 'Invalid Request';} 

?> 

的functions.php

<?php 
function sec_session_start() { 
     $session_name = 'sec_session_id'; // Set a custom session name 
     $secure = false; // Set to true if using https. 
     $httponly = true; // This stops javascript being able to access the session id. 

     ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies. 
     $cookieParams = session_get_cookie_params(); // Gets current cookies params. 
     session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly); 
     session_name($session_name); // Sets the session name to the one set above. 
     session_start(); // Start the php session 
     session_regenerate_id(); // regenerated the session, delete the old one. 
} 

function login($email, $password, $mysqli) { 
    // Using prepared Statements means that SQL injection is not possible. 
    if ($stmt = $mysqli->prepare("SELECT id, username, password, salt FROM members 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! 
      echo $password; 


       $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; 
     } 
    } 
} 

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; 
     } 
    } 
} 

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 members WHERE id = ? 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; 
    } 
} 

?> 

Forms.js

function formhash(form, password) { 
    // Create a new element input, this will be out hashed password field. 
    var p = document.createElement("input"); 
    // Add the new element to our form. 
    form.appendChild(p); 
    p.name = "p"; 
    p.type = "hidden" 
    p.value = hex_sha512(password.value); 
    // Make sure the plaintext password doesn't get sent. 
    password.value = ""; 
    // Finally submit the form. 
    form.submit(); 

} 
+0

看起來你已經從js發送一個散列到你的PHP腳本,所以不需要在登錄函數中再次散列它。但它似乎也有一些鹽的問題。 嘗試擺脫JavaScript哈希,並嘗試它是否工作。 – schoash

+0

在您的登錄功能中,從哪裏獲得$ salt值? –

+0

@FaceOfJock:來自查詢。 $ stmt = $ mysqli-> prepare(「SELECT id,username,password,salt FROM members WHERE email =?LIMIT 1」); $ stmt-> bind_result($ user_id,$ username,$ db_password,$ salt); – JimL

回答

1

SHA512是哈希algoritm,不加密。我知道這個答案可能被認爲是脫離主題,但你應該真的使用默認的PHP密碼類。既然你可能不是PHP 5.5還可以使用此: https://gist.github.com/marcoarment/1053158

Usage example: 

// In a registration or password-change form: 
$hash_for_user = Bcrypt::hash($_POST['password']); 

// In a login form: 
$is_correct = Bcrypt::check($_POST['password'], $stored_hash_for_user); 
+0

我會嘗試使用bcrypt。從使用示例來看,我想這很容易使用。謝謝。 – user2717993

+1

如果您使用的是PHP 5> = 5.5,您可以使用password_hash和password_verify,以便您選擇使用您喜歡的哈希算法(甚至是bcrypt)以及其他一些選項,如鹽,成本等。請參閱PHP文檔[這裏](http://php.net/manual/en/function.password-hash.php)和[這裏](http://www.php.net/manual/en/function.password-verify.php) –

+0

@AntonioE。:正如我在答覆中所說的,我假設他尚未使用PHP 5.5,有多少種生產環境? (希望沒有) – JimL

0

有趣的是,你的問題是關於安全,但你的代碼有多個漏洞。

沒有確認什麼那麼對輸入參數(您只需粘貼$ _ POST [「電子郵件」]進入查詢(在選擇一項)。

那你說說加密,但實際上意味着散列SHA512不是用於存儲密碼的食物哈希值,整個sha-family對於存儲密碼是不利的,如果你真的想要一個強大的哈希密碼方式,使用Bcrypt或者pkbdf2,並且內置哈希算法在php中作爲JimL allready建議使用

你正在使用Salt,但是你計算哈希的方式...不要使用mt_rand這個函數是基於時間的使用mcrypt_create_iv並且不對它進行哈希,散列只會減慢它但是不會增加更多的安全性。

客戶端散列不會讓您的應用程序更安全。它只會告訴大家你是如何散列你的密碼的。如果您希望數據在發送時進行加密,請使用ssl。

+0

謝謝你介紹我加密。現在嘗試使用它。 – user2717993

+0

誠然,在某些情況下,散列密碼客戶端也是有道理的。我爲幾個atm項目做了這個工作,在發送密碼之前(通過SSL)發送密碼,然後在存儲到數據庫之前對它進行加密。僅僅因爲用戶故事要求沒有密碼以純文本形式觸摸服務器。 – JimL

+0

當使用不同的散列哈希散列時,實際上會降低安全性... sha512始終返回相同的長度。所以Bcrypt將總是散列一個相同長度的字符串。這使得它更容易破解... – Pinoniq