Enkripsi File Konfigurasi

Latar belakang


Saya mendapat tugas mengatur CI. Diputuskan untuk menggunakan transformasi file konfigurasi dan menyimpan data rahasia dalam bentuk terenkripsi. Anda dapat mengenkripsi dan mendekripsi mereka menggunakan Key Container.

Wadah kunci


Setiap OS Windows memiliki set kunci yang dibuat. Kunci dihasilkan baik di akun atau di mesin. Kunci yang dihasilkan oleh mesin dapat dilihat di sepanjang jalur ini C: \ ProgramData \ Microsoft \ Crypto \ RSA \ MachineKeys. Di sinilah kunci yang akan kita buat selanjutnya akan pergi.

Penciptaan kunci


Kami mulai cmd dari administrator dan beralih ke direktori dengan aspnet_regiis, saya punya C: \ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319

Jalankan perintah

aspnet_regiis -pc "TestKey" -exp 

exp - ditambahkan sehingga Anda dapat mengekspor kunci di masa mendatang
TestKey - nama Wadah Kunci kami

Ekspor Utama


Tim

 aspnet_regiis -px "TestKey" :\TestKey.xml -pri 

TestKey - beri nama Key Container
C: \ TestKey.xml - path tempat file akan diekspor
pri - tambahkan kunci pribadi untuk ekspor

Kunci impor


Tim

 aspnet_regiis -pi "TestKey" :\TestKey.xml 

TestKey - beri nama Key Container
C: \ TestKey.xml - jalur dari mana file akan diekspor

Hak Pengaturan


Agar aplikasi atau IIS Anda berfungsi dengan wadah utama, Anda perlu mengonfigurasi hak untuk itu.

Ini dilakukan oleh tim

 aspnet_regiis -pa "TestKey" "NT AUTHORITY\NETWORK SERVICE" 

TestKey - beri nama Key Container
NT AUTHORITY \ LAYANAN JARINGAN - yang akan diberikan akses ke kunci

Secara default, IIS memiliki ApplicationPoolIdentity untuk kumpulan.

Dokumentasi Microsoft (lihat tautan 2) menjelaskan ApplicationPoolIdentity sebagai:

  • ApplicationPoolIdentity : Ketika kumpulan aplikasi baru dibuat, IIS membuat akun virtual yang memiliki nama kumpulan aplikasi baru dan yang menjalankan proses pekerja kumpulan aplikasi di bawah akun ini. Ini juga merupakan akun yang paling tidak istimewa.

Oleh karena itu, agar IIS dapat mendekripsi konfigurasi, Identitas harus dikonfigurasi di kumpulan akun, atau Anda dapat memilih "LAYANAN JARINGAN" dan memberikannya hak.

Menambahkan bagian ke config


 <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> 

Juga kontainer kunci dan RsaProtectedConfigurationProvider didefinisikan secara global dalam file

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.DpapiProtectedConfigurationProvider, System.Configuration, Version = 4.0.0.0, Culture = netral, PublicKeyToken = b03f5f7f11d50a3a" description = "Menggunakan CryptProtectData dan CryptUnprotectData API Windows untuk mengenkripsi dan mendekripsi" useMachineProtection = <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> 

Enkripsi


Enkripsi itu sendiri dapat dilakukan dengan tiga cara.

Enkripsi Baris Perintah


 aspnet_regiis.exe -pef connectionStrings :\Site -prov "CustomRsaProtectedConfigurationProvider" 

C: \ Site - path ke file dengan konfigurasi.

CustomRsaProtectedConfigurationProvider - penyedia kami ditentukan dalam konfigurasi yang disebut wadah kunci.

Enkripsi melalui aplikasi tertulis


 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(); } } 

Sepeda


Ketika ada transformasi file dan Anda perlu mengenkripsi bagian secara terpisah dari seluruh konfigurasi, maka hanya versi yang ditulis sendiri yang cocok. Kami membuat instance dari kelas RsaProtectedConfigurationProvider, mengambil node dari xml dan mengenkripsi secara terpisah, kemudian mengganti node xml asli dengan yang dienkripsi kami dan menyimpan hasilnya.

 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); } } 

Referensi


1.docs.microsoft.com/en-us/previous-versions/53tyfkaw
2.support.microsoft.com/en-za/help/4466942/understanding-identities-in-iis

Source: https://habr.com/ru/post/id462379/


All Articles