Don’t Disrespect the Web.Config – Encryption
June 19, 2012 6 Comments
As stated in an earlier post…
There are 3 things every public website should be doing with their web.config
- Use web.config transforms to keep track of development versus production settings
- Encrypt important configuration sections for security
- ELMAH – Error Logging Modules and Handlers
In this post, we’ll discuss how to encrypt sensitive sections of the web.config so passwords and other information cannot be easily read by those who gain access to the file.
Why is it important?
Encrypting sensitive sections of the web.config is important because they are just that, sensitive. Think about your production web.config file. It may contain all sorts of data that you would not want to be accessible. There are often passwords for SQL database connections, passwords to an SMTP server, API Keys, or critical information for whatever system is being automated. In addition, web.config files are usually treated as just another source code file. There are probably many versions in your source control system right now. That means, any developer on the team, or more accurately anyone with access to the source code, can see what information is in the web.config file.
In many cases, storing passwords in a web.config is itself unnecessary and should be avoided. However, we know it is all too easy to fall into the trap of placing them in this flexible, convenient file. Therefore, at the very least, certain sections should be encrypted so they cannot be easily read or used for evil.
Encrypting ConnectionStrings
In our example, we will encrypt two typical configuration sections: ConnectionStrings and AppSettings on a Windows 7 development machine.
Follow the below steps:
1. Open a command prompt with elevated, Administrator, privileges:
2. At the command prompt, enter:
cd “C:\Windows\Microsoft.NET\Framework\v4.0.30319”
3. Now enter the following to encrypt the ConnectionStrings section:
aspnet_regiis.exe -pef “connectionStrings” “C:\WebApplication1\WebApplication1”
In this case, C:\WebApplication1\WebApplication1 is the directory where our web.config is located.
4. Enter the following to encrypt the AppSettings section:
aspnet_regiis.exe -pef “appSettings” “C:\WebApplication1\WebApplication1”
For reference on all the command-line options of aspnet_regiis.exe, refer to this MSDN page.
Decrypting ConnectionStrings
Of course, it is possible you might need to be able to read the original, unencrypted, data at a later time. To access that information is easy. Simply perform the previous steps but use the command-line option “-pdf” to decrypt the important sections.
Deployment
Deploying your web application with encrypted web.config sections is simple, but it may not be obvious. This StackOverflow answer explains the steps best. Generally, any server or development machine that uses the same encrypted web.config data must use the same RSA key pair, which can be exported using the aspnet_regiis tool.
Before and After
There you have it. You have successfully encrypted 2 sections of your web.config file. Take a look below to observe the before and after results:
Before
<connectionStrings>
<add name=“ApplicationServices“ connectionString=“data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true”
providerName=“System.Data.SqlClient“ />
</connectionStrings>
After
<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>fK275KFHx9RKip16DTpwxLi4AHpvCpat4S3edgsDwco9PgudsMKc1qAyh9qNt2y+90qV4QIzyZXm8j27UV5J+R5rNruMUOROLWzVt8qkRYRM3ADoiCi5BJh2SsjE0guGXFbufZDgRpPFV5bstgZSBPYNiYXQF/aOLyQjPCE8VDo=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>CSdausUH7yWcY8t1sPUqiCooYreEauzi4t33gVJuWYcfhspsguTchJjwthUTMLqnulYRmCu8ZnhrVBepQo7PHO/4k5mwo3s46TsgFddvvUlyY/EDQf047LG0pocBDxL3MgIGf3b+atoG29Jg0Wnhj+M6urYG55Ko4nGp36JILQptlEn+sqCl2sQ99izykXtRWP7kC4tldO+YvBuZ7x8fyGoANwSKQFo7cH+dbydvCkRvaFQsRATdsQKGmSrXwIlkoNvxFb1CBPx0qDenyCs+vO4QyF2CZ8QB+UIJzA8EL7W/FovH5zDczjXQWTsFSmsI+vSojl9G9jSVLJFbwOpQBLIKxfximl5r</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
In a future blog post, we will discuss the 3rd party component ELMAH, which is vital to being notified when your users encounter exceptions in your web application.
Update 07/24/2012
It is possible to combine encryption with web.config transforms. I know this will work as I have done it before.
In my experience, I’ve done the following. I had to add an RSA section at the top of my web.config. For me, this went into my web.config.release as I did not encrypt my default/development web.config:
<configProtectedData defaultProvider=”MyRsaProtectedConfigurationProvider” xdt:Transform=”Insert”>
<providers>
<add name=”MyRsaProtectedConfigurationProvider”
type=”System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL”
keyContainerName=”NetFrameworkConfigurationKey_viternuscom”
useMachineContainer=”true” />
</providers>
</configProtectedData>
Also, the web.config sections need to be replaced at the section level. You cannot replace the name or connection string attributes. So, you can use something like the below configuration to replace the whole connection strings section in each environment:
<connectionStrings configProtectionProvider=”MyRsaProtectedConfigurationProvider” xdt:Transform=”Replace”>
<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>fV3NsFhZR/l0/5nvioFfjwjhhauNUTR96fQOK3QeRTW05ERDAQrFGj9MBt5Jh7Ca4rIS2JZfOfNTjTxWiEp/tjk+9LXVyPKrJYMiNlYiUmZGfV/amPsLPmRm2pOEyKwJhJLN6NyZdht/xGrf1ClDKO6CG1ViA5pK5R8Db8X9ul4=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData> <CipherValue>eFvSbAzbVUzwa9Sl8V6t43kuwAcvmaPUjboSJ/oi+MMJXyqtqXS8dKSuxBy+E0rC8tUxxIfJppZNm+CCoKf9Rm39vW2flpgcsvm8ZNMekSf4r2GWYAvLw3vYvMBcbnFRqktlaM7cXia38+3KGN8skHzxioqrBgy2QQqqPWIPrmrCS440BRlXEck6XwAO9rZOERgM6+OtlRan4EuGoB0O4acJWbp51zWxkfzqxMb600BHkYzeIYkHH8GNvWo+LSQt6o+NYW+Q7sm/lLFY5hPp3pGTOygXPehT1b/3BWZM+1dJ5sh8sBXO+t5m7/Dzqt4nvMqArmdEUvQdhYAPauC3Uj9HjDFpHkbOjVEzohIvB0kJ1Wc3uP4VvE6CRMbAsrRiSNLDlT6OpXYVrArLk9c1bBA56nFXPMxLEpN1umRcCfaQY0qxKrZi/yJ8dKD/C/5Vo7o50f10jM9eUrt3/uS71bNJk5U9N7kO42tZZGXZMui51o6MWcYxSC7VQ3KdCpy6UacBnD8MYr7EHeZ591ATQds8dzcsXY7w6Lsg1pXLK74HqXMW/xDeLtBoWJxat9y+</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
I hope this helps.
Pingback: Don’t Disrespect the Web.Config – ELMAH « Midwest Developer Insights
Hello Nathan,
I read your article. Gives apt information about web.config transformation and encryption. How do you merge both. Lets say I have 3 environements ST, UAT and PROD. I want to encrpyt the web.configs and the do transformation and build packages. How can I do this? Do you think it is possible?
Thanks
Yes, it is possible. It’s really not that difficult if you think of web.config transformations as a way to merge sections of the web.config from multiple source files.
As in our example with an encrypted Connection String section, you can just include the encrypted version in each of your transform files.
Hi Stu,
Thanks for replying back,. I wish it was that straight forward. I have already tried replacing the connection string with encrypted one and it resulted in the error cannot find “name”. With encryption the entire section gets encrypted such as here(your example) :
Rsa Key
fK275KFHx9RKip16DTpwxLi4AHpvCpat4S3edgsDwco9PgudsMKc1qAyh9qNt2y+90qV4QIzyZXm8j27UV5J+R5rNruMUOROLWzVt8qkRYRM3ADoiCi5BJh2SsjE0guGXFbufZDgRpPFV5bstgZSBPYNiYXQF/aOLyQjPCE8VDo=
CSdausUH7yWcY8t1sPUqiCooYreEauzi4t33gVJuWYcfhspsguTchJjwthUTMLqnulYRmCu8ZnhrVBepQo7PHO/4k5mwo3s46TsgFddvvUlyY/EDQf047LG0pocBDxL3MgIGf3b+atoG29Jg0Wnhj+M6urYG55Ko4nGp36JILQptlEn+sqCl2sQ99izykXtRWP7kC4tldO+YvBuZ7x8fyGoANwSKQFo7cH+dbydvCkRvaFQsRATdsQKGmSrXwIlkoNvxFb1CBPx0qDenyCs+vO4QyF2CZ8QB+UIJzA8EL7W/FovH5zDczjXQWTsFSmsI+vSojl9G9jSVLJFbwOpQBLIKxfximl5r
There is no way to find the name attribute let alone the connection string name. This wont work. Any other ideas. I am sure somebody out there must be in a similar situation. My main point is I want the connection stings to be encrypted and have the web.config transformation to happen when building packages.
Thanks
Hi there. When I commented here it was stripping out my config code so I added an update to the actual post.
Hello Stu,
Thanks. That just worked. xdt:Transform=”Replace” and inserting the RSA section above. For some reason I could not find this missing piece of information anywhere.
Now I have few more hurdles to get through but this was a big step. Thanks for helping me out.
Regards