简体   繁体   English

这对用户登录和验证是否足够安全? (PHP,MySQL,Sessions)

[英]Is this safe enough for user login and verification? (PHP, MySQL, Sessions)

Just want to make sure I have not missed anything obvious. 只是想确保我没有错过任何明显的东西。 Need your guys expertise on this. 需要你的家伙专业知识。

User data in database: - Password is stored as a crypt() that is salted with salt string that is stored in user table. 数据库中的用户数据: - 密码存储为crypt(),其中存储在用户表中的salt字符串。

Sessions: - When user logs on correctly I create a new row in a sessions table with a unique sha256 hash session ID. 会话: - 当用户正确登录时,我在会话表中创建一个具有唯一sha256哈希会话ID的新行。 I store user_id there too to see what session exists to what user. 我也在那里存储user_id,以查看哪个会话存在于哪个用户。 - I create a cookie which I store the session ID in. - Session is removed when user logs out by accessing logout function. - 我创建了一个cookie,我将会话ID存储在其中。 - 当用户通过访问注销功能注销时会删除会话。 - Session is automatically deleted on a defined expiration time (eg 7 days) - 会话在定义的到期时间内自动删除(例如7天)

Authentication: - Check if client has a cookie with session ID in it. 身份验证: - 检查客户端是否有包含会话ID的cookie。 If the cookie session ID matches a session ID in sessions table, user is authenticated. 如果cookie会话ID与会话表中的会话ID匹配,则对用户进行身份验证。

What do you guys think? 你们有什么感想? Do I need to do something else about it? 我还需要做些别的事情吗?

EDIT: Added the method to generate the password: 编辑:添加了生成密码的方法:

public function generate_salt($password) {
    $cost = $this->CONFIG['user__password_encrypt_cost'];
    $salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');

    // Prefix information about the hash so PHP knows how to verify it later.
    // "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
    $salt = sprintf("$2a$%02d$", $cost) . $salt;

    // Hash the password with the salt
    $hash = crypt($password, $salt);


    return array(
        'salted_password' => $hash,
        'salt' => $salt
     );
}

Crypt with CRYPT_SHA512 and salt is secure, however usage of password_hash() is recommended. 使用CRYPT_SHA512加密并且盐是安全的,但建议使用password_hash()。

Crypt with MD5 or similar weak hashing should not be used. 不应使用带有MD5或类似弱哈希的密码。

The session handling is overcomplicated. 会话处理过于复杂。

PHP has session handling where you can store the cookie (it stores it actually automatically). PHP具有会话处理功能,您可以在其中存储cookie(它实际上自动存储)。 In the session you can add a user id 在会话中,您可以添加用户ID

$_SESSION['userid'] = $sUserIdFromDB;

If the client has a cookie it will find the session data and you can check if he is logged in. 如果客户端有cookie,它将找到会话数据,您可以检查他是否已登录。

if (isset($_SESSION['userid']) 
    AND is_int($_SESSION['userid']) AND (0 < $_SESSION['userid'])) {
    $oUserRow = $oDB->getByID($_SESSION['userid']);
} else {
   // user is not logged in (has no valid session)
}

Storing the Session ID in DB is possible but adds another additional request to DB which is not necessary. 可以在DB中存储会话ID,但是向DB添加另一个不需要的额外请求。 It does not increase security. 它不会增加安全性。 Actually you will find issues where users log in from two different browsers at same time (mobile and desktop, mobile API) using the DB approach as the Session ID will be different. 实际上,您会发现用户使用数据库方法同时从两个不同的浏览器(移动和桌面,移动API)登录的问题,因为会话ID将不同。

More important is: - every user should have a different salt - enforce minimum length of passwords (8chars) - enforce large variety of characters used (lowercase, uppercase, numbers, special chars) - allow secure resetting of new password (no password should be sent by email) - .... 更重要的是: - 每个用户都应该有不同的盐 - 强制执行最小密码长度(8个字符串) - 强制使用大量字符(小写,大写,数字,特殊字符) - 允许安全重置新密码(不需要密码通过电子邮件发送) - ....

"Safe enough" is a little difficult to answer but here are some other things to consider: “足够安全”有点难以回答,但还有一些其他需要考虑的事项:

  1. Salt strength -- is it the username or is it a random value or time stamp? 盐强度 - 是用户名还是随机值或时间戳? Prefer the latter two. 更喜欢后两者。
  2. Are you requiring SSL when the password is sent over the wire? 通过线路发送密码时是否需要SSL? Consider doing this. 考虑这样做。
  3. Is the cookie 'Secure' and 'HttpOnly' to mitigate CSRF? cookie'Secure'和'HttpOnly'是否可以缓解CSRF? Do this if you can (depends on client support). 如果可以,请执行此操作(取决于客户支持)。
  4. Are employees restricted from accessing both the salt and the encrypted value? 员工是否被限制访问盐和加密值? Restrict who has access to any sensitive information. 限制谁有权访问任何敏感信息。
  5. Which encryption algorithm and how many bits? 哪种加密算法和多少位? Try DES 512 or stronger. 尝试DES 512或更强。
  6. Do you enforce password strength requirements? 您是否强制执行密码强度要求? You should! 你应该!
  7. Is the DB connection secure? 数据库连接安全吗? Important especially if the DB link is over a WAN. 特别重要的是,如果数据库链接在WAN上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM