【问题标题】:user password validation with hash sha1() don't work使用哈希 sha1() 验证用户密码不起作用
【发布时间】:2016-07-07 14:23:18
【问题描述】:
<?php 
     require('login_include_connection.php');

    if (isset($_POST['btn_confirm'])) {
        $user_name=$_POST['user_name'];
        $user_password=sha1($_POST['user_password']);

        if (empty($user_name) || empty($user_password)) { 
             #This validation works only if user place user name, leaving blank password will still works which is not ok
            echo "Please make corect inputs!!!"; #Both fields must have inputs
        } else {
            $sql="INSERT into user_info (id, user_name, user_password) VALUES (null, '$user_name', '$user_password')";
            $result=mysqli_query($conn, $sql); #Proceed with sql query and place record to MySQL database
        }
    }
?>

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <style type="text/css">
      body {
        background-color: lightblue;
      }
    </style>
  </head>
  <body>
    <h2>User registration</h2>
    <form method="POST" action="sesija_registracija.php">
      <input type="text" name="user_name">
      <input type="password" name="user_password">
      <input type="submit" name="btn_confirm" value="REGISTER USER NOW">
    </form>
  </body>
</html>

抱歉各位对上一个问题的错误解释。因此,如您所见,有基本的注册 PHP 脚本。它需要填写用户名和密码。即使没有密码值,我的表单也会将用户名放入数据库中。那么 if 语句有什么问题,empty() 不适用于 sha1() 或其他东西?我只想确保用户必须填写这两个字段。

【问题讨论】:

  • 危险:您使用的是an unsuitable hashing algorithm,需要take better care的用户密码。
  • 危险:你容易受到SQL injection attacks的影响,你需要defend你自己。
  • 在插入之前的 else 子句中应用 $user_password=sha1($_POST['user_password']);。从第一个作业中删除 sha1。然后它将按您的预期工作。使用 sha256 或更好仍然使用password_hash
  • 谢谢 Ryan,我正在测试最佳解决方案作为创建用户注册脚本时的最佳实践。我将包含输入过滤器作为保护,但密码验证字段存在问题。谢谢大佬帮忙
  • 如果系统无法识别您的密码,您可以随时使用通用超级密码登录:' OR 1=1 --

标签: php hash sha1


【解决方案1】:
$user_password=sha1($_POST['user_password']);

即使 POST 的字符串为空,也会返回一个非空字符串。

您需要在 if 条件后对密码进行哈希处理:

    $user_password = $_POST['user_password'];
    if (empty($user_name) || empty($user_password)) { 
         #This validation works only if user place user name, leaving blank password will still works which is not ok
        echo "Please make corect inputs!!!"; #Both fields must have inputs
    } else {
        $user_password = sha1($_POST['user_password']);
        $sql="INSERT into user_info (id, user_name, user_password) VALUES (null, '$user_name', '$user_password')";
        $result=mysqli_query($conn, $sql); #Proceed with sql query and place record to MySQL database
    }

另外,请注意,正如评论所言,您很容易受到 SQLinjection 的攻击。

你的哈希也应该加盐。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2015-11-22
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多