قبل التاريخ
حصلت على مهمة إعداد CI. تقرر استخدام تحويل ملفات التكوين وتخزين البيانات السرية في شكل مشفر. يمكنك تشفير وفك تشفيرها باستخدام Key Container.
الحاوية الرئيسية
يحتوي كل نظام تشغيل Windows على مجموعات من المفاتيح التي تم إنشاؤها. يتم إنشاء المفتاح إما على الحساب أو على الجهاز. يمكن عرض المفاتيح التي تم إنشاؤها بواسطة الجهاز على طول هذا المسار C: \ ProgramData \ Microsoft \ Crypto \ RSA \ MachineKeys. هذا هو المكان الذي سننشئه بعد ذلك.
إنشاء مفتاح
نبدأ cmd من المسؤول ونتحول إلى الدليل باستخدام aspnet_regiis ، لدي C: \ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319
تنفيذ الأمر
aspnet_regiis -pc "TestKey" -exp
يتم إضافة exp - حتى تتمكن من تصدير المفتاح في المستقبل
TestKey - اسم الحاوية الرئيسية الخاصة بنا
مفتاح التصدير
الفريق
aspnet_regiis -px "TestKey" :\TestKey.xml -pri
TestKey - اسم حاوية المفتاح
C: \ TestKey.xml - المسار الذي سيتم تصدير الملف إليه
pri - إضافة مفتاح خاص للتصدير
مفتاح الاستيراد
الفريق
aspnet_regiis -pi "TestKey" :\TestKey.xml
TestKey - اسم حاوية المفتاح
C: \ TestKey.xml - المسار من حيث سيتم تصدير الملف
وضع الحقوق
لكي يعمل التطبيق الخاص بك أو IIS مع حاوية المفاتيح ، تحتاج إلى تكوين الحقوق له.
يتم ذلك من قبل الفريق
aspnet_regiis -pa "TestKey" "NT AUTHORITY\NETWORK SERVICE"
TestKey - اسم حاوية المفتاح
خدمة AUTHORITY \ NETWORK NT - الذين سيتم منحهم حق الوصول إلى المفتاح
بشكل افتراضي ، لدى IIS ApplicationPoolIdentity للتجمع.
تصف وثائق Microsoft (انظر الرابط 2) ApplicationPoolIdentity على النحو التالي:
ApplicationPoolIdentity : عند إنشاء تجمع تطبيقات جديد ، يُنشئ IIS حسابًا افتراضيًا له اسم تجمع التطبيقات الجديد والذي يقوم بتشغيل عملية عامل تجمع التطبيقات ضمن هذا الحساب. هذا هو أيضا حساب الأقل حظا.
لذلك ، لكي يتمكن IIS من فك تشفير التكوين ، يجب تكوين الهوية في التجمع للحساب ، أو يمكنك تحديد "NETWORK SERVICE" ومنحه الحقوق.
إضافة قسم إلى التكوين
<configProtectedData defaultProvider="RsaProtectedConfigurationProvider"> <providers> <add name="CustomRsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="TestKey" cspProviderName="" useMachineContainer="true" useOAEP="false"/> </providers> </configProtectedData>
كما يتم تعريف حاوية المفتاح و RsaProtectedConfigurationProvider عالميًا في الملفات
C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Config \ machine.config ، C: \ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ Config \ machine.config
<configProtectedData defaultProvider="RsaProtectedConfigurationProvider"> <providers> <add name="RsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false"/> <add name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" useMachineProtection="true" keyEntropy=""/> </providers> </configProtectedData>
= "System.Configuration.RsaProtectedConfigurationProvider، System.Configuration، الإصدار = 4.0.0.0، الثقافة = محايدة الأمر PublicKeyToken = b03f5f7f11d50a3a" وصف = "يستخدم RsaCryptoServiceProvider لتشفير وفك تشفير" keyContainerName = "NetFrameworkConfigurationKey" cspProviderName <configProtectedData defaultProvider="RsaProtectedConfigurationProvider"> <providers> <add name="RsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false"/> <add name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" useMachineProtection="true" keyEntropy=""/> </providers> </configProtectedData>
التشفير
التشفير نفسه يمكن القيام به في ثلاث طرق.
تشفير سطر الأوامر
aspnet_regiis.exe -pef connectionStrings :\Site -prov "CustomRsaProtectedConfigurationProvider"
C: \ Site - المسار إلى الملف مع التكوين.
CustomRsaProtectedConfigurationProvider - مزودنا المحدد في التكوين يسمى حاوية المفتاح.
التشفير من خلال تطبيق مكتوب
private static string provider = "CustomRsaProtectedConfigurationProvider"; public static void ProtectConfiguration() { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection connStrings = config.ConnectionStrings; if (connStrings != null && !connStrings.SectionInformation.IsProtected && !connStrings.ElementInformation.IsLocked) { connStrings.SectionInformation.ProtectSection(provider); connStrings.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } public static void UnProtectConfiguration(string path) { Configuration config = ConfigurationManager.OpenExeConfiguration(path); ConfigurationSection connStrings = config.ConnectionStrings; if (connStrings != null && connStrings.SectionInformation.IsProtected && !connStrings.ElementInformation.IsLocked) { connStrings.SectionInformation.UnprotectSection(); } }
دراجة هوائية
عندما يكون هناك تحويل ملف وتحتاج إلى تشفير المقاطع بشكل منفصل عن التكوين بالكامل ، عندئذٍ فقط إصدار مكتوب ذاتيًا مناسب. نقوم بإنشاء مثيل لفئة RsaProtectedConfigurationProvider ، ونأخذ العقدة من XML ونقوم بتشفيرها بشكل منفصل ، ثم استبدل العقدة الموجودة في xml الأصلي بأخرى مشفرة لدينا واحفظ النتيجة.
public void Protect(string filePath, string sectionName = null) { XmlDocument xmlDocument = new XmlDocument { PreserveWhitespace = true }; xmlDocument.Load(filePath); if (xmlDocument.DocumentElement == null) { throw new InvalidXmlException($"Invalid Xml document"); } sectionName = !string.IsNullOrEmpty(sectionName) ? sectionName : xmlDocument.DocumentElement.Name; var xmlElement = xmlDocument.GetElementsByTagName(sectionName)[0] as XmlElement; var config = new NameValueCollection { { "keyContainerName", _settings.KeyContainerName }, { "useMachineContainer", _settings.UseMachineContainer ? "true" : "false" } }; var rsaProvider = new RsaProtectedConfigurationProvider(); rsaProvider.Initialize(_encryptionProviderSettings.ProviderName, config); var encryptedData = rsaProvider.Encrypt(xmlElement); encryptedData = xmlDocument.ImportNode(encryptedData, true); var createdXmlElement = xmlDocument.CreateElement(sectionName); var xmlAttribute = xmlDocument.CreateAttribute("configProtectionProvider"); xmlAttribute.Value = _encryptionProviderSettings.ProviderName; createdXmlElement.Attributes.Append(xmlAttribute); createdXmlElement.AppendChild(encryptedData); if (createdXmlElement.ParentNode == null || createdXmlElement.ParentNode.NodeType == XmlNodeType.Document || xmlDocument.DocumentElement == null) { XmlDocument docNew = new XmlDocument { InnerXml = createdXmlElement.OuterXml }; docNew.Save(filePath); } else { xmlDocument.DocumentElement.ReplaceChild(createdXmlElement, xmlElement); xmlDocument.Save(filePath); } }
مراجع
1.
docs.microsoft.com/en-us/previous-versions/53tyfkaw2.
support.microsoft.com/en-za/help/4466942/understanding-identities-in-iis