<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://interactiveasp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Nates Stuff : Security, ASP.NET</title><link>http://interactiveasp.net/blogs/natesstuff/archive/tags/Security/ASP.NET/default.aspx</link><description>Tags: Security, ASP.NET</description><dc:language>en</dc:language><generator>CommunityServer 2008 (Build: 30417.1769)</generator><item><title>Encrypting Configuration Information for ASP.NET</title><link>http://interactiveasp.net/blogs/natesstuff/archive/2008/07/16/encrypting-configuration-information-for-asp-net.aspx</link><pubDate>Wed, 16 Jul 2008 22:11:22 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:54</guid><dc:creator>Nathan Zaugg</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/natesstuff/rsscomments.aspx?PostID=54</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/natesstuff/commentapi.aspx?PostID=54</wfw:comment><comments>http://interactiveasp.net/blogs/natesstuff/archive/2008/07/16/encrypting-configuration-information-for-asp-net.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 10px 5px 0px;border-left:0px;border-bottom:0px;" height="104" alt="Encryption Block Transform Graphic" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Encryption-Block-Transform-Graphic_5F00_3.png" width="104" align="left" border="0" /&gt; Every company I consult with invariably has their own &amp;quot;security&amp;quot; assembly and they all have a hard-coded encryption key with the IV and the method to decrypt is right next to the method to encrypt.&amp;nbsp; This is what I call &lt;strong&gt;&lt;em&gt;marginal&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;protection&lt;/em&gt;&lt;/strong&gt;.&amp;nbsp; Yes, it&amp;#39;s encrypted and will probably get a security auditor off of your back but don&amp;#39;t be fooled into thinking that you are protected!&amp;nbsp; A similar thing is done with information in the database, but I&amp;#39;ll cover how to do this on an upcoming post.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Why aren&amp;#39;t you protected?&amp;nbsp; The answer to this question is actually quite simple.&amp;nbsp; If an attacker has access to download your web.config file (say, they brute forced a password on the FTP server) then there is nothing stopping them from downloading the your Security.dll which is responsible for decrypting the password.&amp;nbsp; Once they have that library it&amp;#39;s seconds, not minuets, before they have got the password.&amp;nbsp; &lt;/p&gt; &lt;p&gt;One possible work around is to encrypt configuration sections of your web.config file using DPAPI as outlined in &lt;a href="http://msdn.microsoft.com/en-us/library/ms998280.aspx" target="_blank"&gt;this MSDN How-to&lt;/a&gt;.&amp;nbsp; This is immune to the download attack because the DPAPI uses encryption that is based on a machine or a user.&amp;nbsp; Even if someone was able to download your web.config they would effectively have no way to decrypt that information.&amp;nbsp; &lt;/p&gt; &lt;p&gt;What happens, though, if the attacker has the ability to upload files?&amp;nbsp; Well, in theory, they may be able to grab that configuration in code which will, of course, be decrypted before it is returned.&amp;nbsp; Ahh, but they don&amp;#39;t even know what the name of the connection string (in the case of databases) is because the entire section was encrypted.&amp;nbsp; However, they could guess it or get it from other code. By the way, you really shouldn&amp;#39;t deploy the .cs files to production anyway; you should use the &amp;quot;publish website&amp;quot; option with the setting to not allow the site to be updated.&amp;nbsp; If you follow all of the standards pretty closely your in good shape.&amp;nbsp; Another great idea is to use Integrated Authentication for database access -- that way there is no password to steal!&lt;/p&gt; &lt;p&gt;The How to outlines 3 basic steps summarized below:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Identify the configuration sections to be encrypted&lt;/li&gt; &lt;ol&gt; &lt;li&gt;You may only encrypt the following:&lt;/li&gt; &lt;div&gt;&lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;border-bottom-style:none;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appSettings&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;. This section contains custom application settings. 
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;connectionStrings&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;. This section contains connection strings. 
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;identity&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;. This section can contain impersonation credentials. 
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;sessionState&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;. The section contains the connection string for the out-of-process session state provider.&lt;/pre&gt;&lt;/div&gt;&lt;/ol&gt;
&lt;li&gt;Choose Machine or User store&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;Use Machine store if this is a dedicated server with no other applications running on it or you want to be able to share this information with other applications running on this machine.&lt;/li&gt;
&lt;li&gt;Use User store if the above does not match your situation and in a scenario in which the user has limited access to the server.&lt;/li&gt;&lt;/ol&gt;
&lt;li&gt;Encrypt your configuration file data&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;To encrypt using Machine Store, run the following command from a .NET command prompt: &lt;br /&gt;&lt;strong&gt;aspnet_regiis.exe -pef &amp;quot;&lt;em&gt;{ConfigSectionName}&lt;/em&gt;&amp;quot; &lt;em&gt;{PhysicalDirectory}&lt;/em&gt; –prov &amp;quot;DataProtectionConfigurationProvider&amp;quot;&lt;br /&gt;&lt;/strong&gt;OR&lt;br /&gt;&lt;strong&gt;aspnet_regiis.exe -pef &amp;quot;&lt;em&gt;{ConfigSectionName}&lt;/em&gt;&amp;quot; -app &amp;quot;&lt;em&gt;/{VirtualDirectory}&amp;quot;&lt;/em&gt; –prov &amp;quot;DataProtectionConfigurationProvider&amp;quot;&lt;br /&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;To encrypt using User Store:&lt;br /&gt;Add the following section to your configuration file:&lt;br /&gt;&lt;/li&gt;
&lt;div&gt;&lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;border-bottom-style:none;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configProtectedData&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt; 
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;providers&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt; 
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;add&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;useMachineProtection&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;keyEntropy&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;&amp;quot;&lt;/span&gt; 
                &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;MyUserDataProtectionConfigurationProvider&amp;quot;&lt;/span&gt; 
                &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;System.Configuration.DpapiProtectedConfigurationProvider, 
                System.Configuration, Version=2.0.0.0, Culture=neutral, 
                PublicKeyToken=b03f5f7f11d50a3a&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt; 
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;providers&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt; 
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configProtectedData&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Open a command prompt using the user you plan to encrypt the file with. To do so, Right click on the Command Prompts shortcut, right click -&amp;gt; Run As.&amp;nbsp; Or use the following command: 
&lt;div&gt;&lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;border-bottom-style:none;"&gt;Runas /profile /user:domain\user cmd&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Run the following command:&lt;br /&gt;&lt;strong&gt;Aspnet_regiis -pe &amp;quot;connectionStrings&amp;quot; -app &amp;quot;/{VirtualDirectory}&amp;quot; -prov &amp;quot;MyUserDataProtectionConfigurationProvider&amp;quot;&lt;/strong&gt;&lt;/div&gt;&lt;/ol&gt;&lt;/ol&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It really is that simple!&amp;nbsp; The great thing is that we don&amp;#39;t have to do anything special in development to benefit from the encryption of the configuration sections.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h4&gt;References:&lt;/h4&gt;
&lt;p&gt;&lt;a title="http://msdn.microsoft.com/en-us/library/ms998280.aspx" href="http://msdn.microsoft.com/en-us/library/ms998280.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms998280.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=54" width="1" height="1"&gt;</description><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/Tips+_2600_amp_3B00_+Tricks/default.aspx">Tips &amp;amp; Tricks</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/Security/default.aspx">Security</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/Standards+_2F00_+Conventions/default.aspx">Standards / Conventions</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/Encryption/default.aspx">Encryption</category></item></channel></rss>