【问题标题】:Storing service account credentials securely in clickonce application在 clickonce 应用程序中安全地存储服务帐户凭据
【发布时间】:2016-11-14 22:02:12
【问题描述】:

我正在编写一个 ClickOnce 应用程序,该应用程序使用服务帐户凭据运行批处理文件进程。我需要存储服务帐户凭据,以便程序可以在运行进程之前将用户名/密码添加到 process.startinfo 属性。用户不知道此密码,因此不会提示他们输入密码。我相信这意味着我无法以这种方式存储哈希并验证密码,我生成的哈希值必须是可逆的,以便它可以将正确的密码添加到 startinfo 属性。我在这个网站上搜索并想出了一个有效的科学怪人类型的解决方案,但它不是很安全。目前,我使用此方法加密密码,存储加密值,然后在运行时使用解密方法获取密码(加密方法在运行时从未运行,我在调试期间在 Visual Studio 中运行它,复制值,然后在下面的解密方法中使用该值):

// used to generate decrypted acct creds
    private void EncryptText(string plaintext)
    {
        string outsrt = null;
        RijndaelManaged aesAlg = null;

        try
        {
            // generate key from secret and salt
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedsecret, _salt);

            aesAlg = new RijndaelManaged();
            aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            using (MemoryStream mEncrypt = new MemoryStream())
            {
                // prepend the IV
                mEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
                mEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
                using (CryptoStream csEncrypt = new CryptoStream(mEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        // write all data to the stream
                        swEncrypt.Write(plaintext);
                    }
                }

                outsrt = Convert.ToBase64String(mEncrypt.ToArray());
            }
        }
        finally
        {
            if (aesAlg != null)
                aesAlg.Clear();
        }

        Console.WriteLine(outsrt);
    }

解密方法如下:

private string GetServiceAcctPW()
    {

        // Declare the RijndaelManaged object
        // used to decrypt the data.
        RijndaelManaged aesAlg = null;

        // Declare the string used to hold
        // the decrypted text.
        string plaintext = null;

        try
        {
            // generate the key from the shared secret and the salt
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedsecret, _salt);

            // Create the streams used for decryption.                
            byte[] bytes = Convert.FromBase64String("EncryptedValueHere");
            using (MemoryStream msDecrypt = new MemoryStream(bytes))
            {
                // Create a RijndaelManaged object
                // with the specified key and IV.
                aesAlg = new RijndaelManaged();
                aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
                // Get the initialization vector from the encrypted stream
                aesAlg.IV = ReadByteArray(msDecrypt);
                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))

                        // Read the decrypted bytes from the decrypting stream
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Error decrypting password");
            Console.WriteLine(e.StackTrace);
            logger.WriteToLog(Logger.LogCodes.ERROR, "Error decrypting service account password");
            MessageBox.Show("An error occurred while trying to start the installation process\nPlease contact the Service Desk for further assistance");
        }
        finally
        {
            // Clear the RijndaelManaged object.
            if (aesAlg != null)
                aesAlg.Clear();
        }

        return plaintext;
    }

此代码运行良好,但是,我知道它不安全。我的代码审查员说他能够在一个小时内用 dotPeek 破解它,因为它只是增加了一层混淆。在应用程序中存储这些凭据的最佳/正确方法是什么?

【问题讨论】:

  • 您不能完全保存硬编码的密钥。至少在运行时您需要获取密钥,而调试器可以简单地获取它们。忘记安全存储密钥的想法。只有使用专用服务器等才能提高安全性...
  • 不要加密密码,当攻击者得到数据库时,他也会得到加密密钥。如果您必须在另一个服务器或 HSM 上进行加密,则密码在 Internet 连接的服务器上不可用。
  • 我完全同意,如果将它们存储在应用程序中,它们将永远不会 100% 安全。您提到专用服务器可以提高安全性。这是否意味着将密钥存储在这些服务器上,在运行时将其作为 SecureString 获取并将其传递给 startinfo 属性?
  • @zaph ,所以我会生成 salt 和 hash 并存储它,但是,PBKDF2、Bcrype 等单向加密方法不是吗?我能否从 salt 和 hash 中获取密码并将其添加到 startinfo 属性中?我为我的无知道歉,因为这些类型的方法在大学期间从来都不是我的强项。
  • 感谢@zaph 的帮助和指导,非常感谢。

标签: c# security encryption clickonce credentials


【解决方案1】:

加密密钥位于专用服务器上。

密码与要加密的 id 一起发送到服务器,并返回加密后的密码以供 DB 存储。

当需要密码时,会使用 id 向专用服务器发出请求,并返回解密后的密码。

密码永远不会保存到磁盘,并且密钥永远不会在专用服务器上可用。

专用服务器有点像穷人 HSM。

这是加密,而不是散列。加密密钥与一个随机 IV 一起是秘密的,该 IV 与专用服务器上的 id 一起保存。密钥不可用且与密码无关,因此没有比暴力破解更好的攻击加密密钥的方法了。

服务器需要非常安全,只有几个两因素登录并且不能用于 Internet。

【讨论】:

  • 啊,我明白了。所以我应该有一个专用的服务器来处理这些值的加密、解密和存储,这样我的应用程序本身就不会存储这些值?应用程序只需使用 ID 与服务器联系,服务器应使用解密的密码进行响应。你有类似的东西吗?或者你能指出我在哪里可以找到一些可以让我继续前进的例子吗?
  • 是的,没错。抱歉,我无法提供代码,我没有这样的代码。
  • 不用担心,我非常感谢您的指导。这已经是一个巨大的帮助,并将引导我找到正确的解决方案。我将不得不与另一个团队合作才能让服务器正常运行,但我喜欢这个想法。
猜你喜欢
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
  • 2018-04-21
相关资源
最近更新 更多