会话中的密码安全性

时间:2015-07-23 21:38:43

标签: session authentication cookies hash passwords

我们使用强大的散列函数,高计算成本和随机盐来阻止彩虹攻击等,而不是使用纯文本密码。

但是当用户在会话中时,通常将他或她的用户名与密码的哈希值一起存储为cookie来验证会话。如果用户的浏览器cookie空间被泄露,攻击者是否不会获得更容易破解用户名+会话哈希的目标,而不是用户名+传递哈希?

例如,在Django中,密码使用PBKDF2或bcrypt进行哈希处理,但会话哈希使用较不复杂的HMAC并且没有随机盐。这是安全问题吗?如果是,处理会话的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

对于每个会话,我建议使用专用的SessionID - 随机长128位值。并且,将会话密钥保持为:

hash = sha1(SessionID|username|client_IP|secret_server_side_password);

,其中

try {
    $mandrill = new Mandrill('API_KEY');
    $attachment = file_get_contents("../template.xls");
    $attachment_encoded = base64_encode($attachment);
    $message = array(
        'html' => '<p>Some html text</p>',
        'text' => 'Some text',
        'subject' => 'Subject',
        'from_email' => 'email@domain.com',
        'from_name' => 'Name',
        'to' => array(
            array(
                'email' => 'myemail@domain.com',
                'name' => 'Name',
                'type' => 'to'
            )
        ),
        'attachments' => array(
            array(
                'type' => 'application/vnd.ms-excel',
                'name' => 'New_Features_Submission.xls',
                'path' => $attachment_encoded
            )
        )
    );
    $async = false;
    $result = $mandrill->messages->send($message, $async);
    print_r($result);
} catch (Mandrill_Error $e) {
    echo 'A Mandrill error occured: ' . get_class($e) . '-' . $e->getMessage();
    throw $e;
}

每次收到cookie时,都需要再次计算哈希值,并与收到的哈希值进行比较。

结果,会话关闭后,此cookie无效(SessionID不匹配)。 此外,如果cookie将从活动会话中被盗,服务器可以 找出来自另一台计算机的被盗cookie的攻击,因为来自真实客户端的client_IP将与实际的client_IP不同。

当然,如果更改了ClientIP,会自动断开会话。

替代方案 - 使用基于客户端的身份验证系统 SSL证书,例如 - emcSSL