簡體   English   中英

憑證存儲最佳實踐

[英]Credential storage best practices

我正在編寫Windows服務,需要進行經過身份驗證的Web請求。 該服務不會在用於發出請求的憑據的所有權下運行; 這意味着我需要以某種方式存儲請求的憑據。

這里的最佳做法是什么? 憑證需要存儲在App.config(或模擬)中; 我寧願沒有用明文掛掉密碼。 由於密碼經常更改,因此無法在密碼中構建或以其他方式烘焙二進制文件。

同樣的問題適用於Powershell。 我需要進行經過身份驗證的請求,但我不希望腳本以純文本格式包含用於請求的憑據。

不能歸功於答案:但這里有一篇名為“在.NET App Config中加密密碼”的博客文章,其中包含完整代碼。

http://weblogs.asp.net/jgalloway/archive/2008/04/13/encrypting-passwords-in-a-net-app-config-file.aspx

對於像這樣的東西,我總是參考Keith Brown的“Windows開發人員Windows安全指南”一書。

完整文本在http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook.HomePage上在線

您想要的特定部分(存儲秘密)位於http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/HowToStoreSecretsOnAMachine.html

選項#1 :使用ProtectSection()方法加密App.config中Web.config文件的部分。 這適用於Web和桌面應用程序以及依賴於使用加密部分復制Web.config文件的Web場。

深入介紹MSDN教程,包括使用RSA提供程序:

例:

static public void ProtectSection()
{

    // Get the current configuration file.
    System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);


    // Get the section.
    UrlsSection section =
        (UrlsSection)config.GetSection("MyUrls");


    // Protect (encrypt)the section.
    section.SectionInformation.ProtectSection(
        "RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    section.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration 
    // section. Note, the system
    // uses the Rsa provider to decrypt
    // the section transparently.
    string sectionXml =
        section.SectionInformation.GetRawXml();

    Console.WriteLine("Decrypted section:");
    Console.WriteLine(sectionXml);

}

使用DPAPI提供程序的舊方法:

要加密:

aspnet_regiis -pe "connectionStrings" -app "/SampleApplication" -prov "RsaProtectedConfigurationProvider"

要解密:

aspnet_regiis -pd "connectionStrings" -app "/SampleApplication"

未加密的部分:

<configuration>
  <connectionStrings>
    <add name="SampleSqlServer" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" />
   </connectionStrings>
</configuration>

加密部分:

<configuration>
  <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
      xmlns="http://www.w3.org/2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
          <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <KeyName>RSA Key</KeyName>
          </KeyInfo>
          <CipherData>
            <CipherValue>RXO/zmmy3sR0iOJoF4ooxkFxwelVYpT0riwP2mYpR3FU+r6BPfvsqb384pohivkyNY7Dm4lPgR2bE9F7k6TblLVJFvnQu7p7d/yjnhzgHwWKMqb0M0t0Y8DOwogkDDXFxs1UxIhtknc+2a7UGtGh6Di3N572qxdfmGfQc7ZbwNE=
            </CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>KMNKBuV9nOid8pUvdNLY5I8R7BaEGncjkwYgshW8ClKjrXSM7zeIRmAy/cTaniu8Rfk92KVkEK83+UlQd+GQ6pycO3eM8DTM5kCyLcEiJa5XUAQv4KITBNBN6fBXsWrGuEyUDWZYm6Eijl8DqRDb11i+StkBLlHPyyhbnCAsXdz5CaqVuG0obEy2xmnGQ6G3Mzr74j4ifxnyvRq7levA2sBR4lhE5M80Cd5yKEJktcPWZYM99TmyO3KYjtmRW/Ws/XO3z9z1b1KohE5Ok/YX1YV0+Uk4/yuZo0Bjk+rErG505YMfRVtxSJ4ee418ZMfp4vOaqzKrSkHPie3zIR7SuVUeYPFZbcV65BKCUlT4EtPLgi8CHu8bMBQkdWxOnQEIBeY+TerAee/SiBCrA8M/n9bpLlRJkUb+URiGLoaj+XHym//fmCclAcveKlba6vKrcbqhEjsnY2F522yaTHcc1+wXUWqif7rSIPhc0+MT1hB1SZjd8dmPgtZUyzcL51DoChy+hZ4vLzE=
        </CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>
</configuration>

選項#1a您可以將此加密移動到您的應用中,而不是使用aspnet_regiis執行加密。 它只是檢測您需要保護的部分是否加密,如果沒有,則加密它們。 在開發過程中您需要一個選項,以避免在生產之前運行此代碼。

選項#2加密配置文件中的字符串,並使用SecureString()臨時存儲解密的字符串。 根據您使用的字符串API是否支持SecureString,這可能會或可能不會為您提供更多的攻擊面里程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM