【问题标题】:Secure password hashing with salt, but what about storing it in local cookies?使用盐进行安全密码散列,但是将其存储在本地 cookie 中呢?
【发布时间】:2013-01-07 00:29:59
【问题描述】:

我最近阅读了一篇关于使用“salt”安全地散列用户密码的有趣文章。 (这是original article,不幸的是,在本文发布时它似乎已关闭,因此here's 是缓存版本。)

我完全同意这个概念,但我似乎无法找到一种将用户登录信息安全地存储在本地 cookie(或会话)中的方法,因为 salt + PBKDF2 哈希组合每次都是随机完成的。为了更好地理解我的意思,让我从the article复制C#代码:

using System;
using System.Text;
using System.Security.Cryptography;

namespace PasswordHash
{
    /// <summary>
    /// Salted password hashing with PBKDF2-SHA1.
    /// Author: havoc AT defuse.ca
    /// www: http://crackstation.net/hashing-security.htm
    /// Compatibility: .NET 3.0 and later.
    /// </summary>
    class PasswordHash
    {
        // The following constants may be changed without breaking existing hashes.
        public const int SALT_BYTES = 24;
        public const int HASH_BYTES = 24;
        public const int PBKDF2_ITERATIONS = 1000;

        public const int ITERATION_INDEX = 0;
        public const int SALT_INDEX = 1;
        public const int PBKDF2_INDEX = 2;

        /// <summary>
        /// Creates a salted PBKDF2 hash of the password.
        /// </summary>
        /// <param name="password">The password to hash.</param>
        /// <returns>The hash of the password.</returns>
        public static string CreateHash(string password)
        {
            // Generate a random salt
            RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
            byte[] salt = new byte[SALT_BYTES];
            csprng.GetBytes(salt);

            // Hash the password and encode the parameters
            byte[] hash = PBKDF2(password, salt, PBKDF2_ITERATIONS, HASH_BYTES);
            return PBKDF2_ITERATIONS + ":" +
                Convert.ToBase64String(salt) + ":" +
                Convert.ToBase64String(hash);
        }

        /// <summary>
        /// Validates a password given a hash of the correct one.
        /// </summary>
        /// <param name="password">The password to check.</param>
        /// <param name="goodHash">A hash of the correct password.</param>
        /// <returns>True if the password is correct. False otherwise.</returns>
        public static bool ValidatePassword(string password, string goodHash)
        {
            // Extract the parameters from the hash
            char[] delimiter = { ':' };
            string[] split = goodHash.Split(delimiter);
            int iterations = Int32.Parse(split[ITERATION_INDEX]);
            byte[] salt = Convert.FromBase64String(split[SALT_INDEX]);
            byte[] hash = Convert.FromBase64String(split[PBKDF2_INDEX]);

            byte[] testHash = PBKDF2(password, salt, iterations, hash.Length);
            return SlowEquals(hash, testHash);
        }

        /// <summary>
        /// Compares two byte arrays in length-constant time. This comparison
        /// method is used so that password hashes cannot be extracted from
        /// on-line systems using a timing attack and then attacked off-line.
        /// </summary>
        /// <param name="a">The first byte array.</param>
        /// <param name="b">The second byte array.</param>
        /// <returns>True if both byte arrays are equal. False otherwise.</returns>
        private static bool SlowEquals(byte[] a, byte[] b)
        {
            uint diff = (uint)a.Length ^ (uint)b.Length;
            for (int i = 0; i < a.Length && i < b.Length; i++)
                diff |= (uint)(a[i] ^ b[i]);
            return diff == 0;
        }

        /// <summary>
        /// Computes the PBKDF2-SHA1 hash of a password.
        /// </summary>
        /// <param name="password">The password to hash.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="iterations">The PBKDF2 iteration count.</param>
        /// <param name="outputBytes">The length of the hash to generate, in bytes.</param>
        /// <returns>A hash of the password.</returns>
        private static byte[] PBKDF2(string password, byte[] salt, int iterations, int outputBytes)
        {
            Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt);
            pbkdf2.IterationCount = iterations;
            return pbkdf2.GetBytes(outputBytes);
        }
    }
}

如您所见,验证密码的唯一方法是使用纯文本密码调用ValidatePassword。在我之前的普通 SHA1 实现中,为了在本地浏览器中存储用户登录信息,我将该 SHA1 值放入 cookie 中,并将其与存储在服务器上每个页面的数据库中的值进行比较。但是,您如何使用这种“安全”的方法来做同样的事情呢?

有什么想法吗?

【问题讨论】:

  • 您绝对不想将哈希值存储在 cookie 中。
  • (我刚刚意识到这听起来像是一个毒品玩笑,但我是认真的......)
  • 那么您将如何做到让用户不必在每个页面上都登录?
  • 您应该确保 cookie 已加密。我同意@OliCharlesworth 的观点,但您不应该将哈希值存储在 cookie 中。您不能只使用 cookie 本身的存在作为登录用户的标记吗?您可以将登录用户的用户 ID 存储在 cookie 中,以便在每次请求时您都可以知道谁在浏览。
  • 上面的代码与存储 cookie 以标记正在登录的用户的解决方案并不真正相关,因为不需要散列。当您登录用户时,您需要在 http 响应中添加一个 cookie。如果您使用的是 ASP.NET,网上有大量示例。只需搜索“表单身份验证”,您就会找到一堆。

标签: c# asp.net passwords cryptography


【解决方案1】:

您不想将散列密码存储在 cookie 中,因为这与将密码本身存储在 cookie 中是一样的。如果哈希是您登录所需的全部,那么它就是密码。您想用随机盐对用户密码进行哈希处理的原因不是为了保护登录过程,而是为了保护您的密码表。如果攻击者窃取了您的密码表,并且每个密码都没有使用唯一的盐进行哈希处理,那么他/她很容易找出许多密码。始终使用唯一的盐对您的用户密码进行哈希处理。用散列密码存储这个盐很好。如果您想要一种安全的方式来使用哈希来根据 cookie 中的数据对用户进行身份验证,您将需要采用临时凭证或会话的方向。我能想到的最简单的方法如下:

  1. 当您的用户使用他的密码登录时,创建一个“会话”。分配一个值来唯一标识此会话,存储会话创建的时间(以毫秒为单位),并创建一个随机值作为 salt。

  2. 使用 salt 散列会话的 ID。将此哈希和会话 ID 保存在用户的 cookie 中。

  3. 每次请求页面时,都会再次执行散列,并将其与存储在用户 cookie 中的值进行比较。如果值匹配并且自会话创建以来没有经过太多时间,您的用户可以查看该页面。否则让他们使用密码再次登录。

【讨论】:

  • 只是好奇,为什么在客户端浏览器 cookie 中存储与用户 ID 的特殊 SHA1 混合的密码哈希是不好的? (我选择这种方法来减轻服务器和数据库的工作负载。)
  • 如果我理解正确的话,那个哈希值可以用来验证和访问用户的内容吗?任何设法临时访问您用户客户端的攻击者都可以从 cookie 缓存中窃取哈希并永久访问他们的帐户。
  • 好点,但他们也可以用你的方法做同样的事情。我同意时间戳稍后会使其失效,但这可能足以让他们“窃取”他们正在寻找的任何东西......
【解决方案2】:

哈希存储在服务器上,当明文密码发送到服务器时,作为用户身份验证的一部分,计算的盐(每个用户)存储在安全数据库中并添加到密码中,并根据密码计算加密结果+ 哈希。

Cookie 是错误的方法,因为它们会过期,并且网络客户端没有理由需要 salt。

Salting Your Password: Best Practices?

【讨论】:

    【解决方案3】:

    散列函数的盐值通常是根据规则计算出来的。例如,您使用用户标识符作为盐本身。您使用“用户名”值在数据库中查找用户,然后使用用户 ID 作为盐。这样,您不必将盐存储在任何地方,并且每个散列值的盐是不同的。

    【讨论】:

    • 您不想将标识符用作盐。 Salt应该始终是一个唯一值,即您以前从未使用过的值。将盐与哈希一起存储是非常好的。
    猜你喜欢
    • 2010-12-06
    • 2011-12-29
    • 1970-01-01
    • 2016-04-01
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2011-01-20
    相关资源
    最近更新 更多