Skip to main content

How to secure connection string of a SQL server in a Winwdows Azure application


As in any application, when developing applications for Windows Azure that use SQL Azure, the security of connection string appear (especially database user password) when the application begin to be deployed on the testing or production environment. In this post I will try to offer a solution about how we can secure the connection string.
Usually when we get to the end of a project and our application begin to be deployed on the production environment also, we need to separate each database (development, testing and production). For safety reason we need to use different connection string for each environment that we used. In this moment we don’t want to allow development team to access the production or testing environment. Because of this we need to secure the connection string.
Why is this so important? For example what happen if we are in maintenance with our application and the developer have access to sensitive information? We don’t want this to happen, especially when we store on the database passwords, salaries and so on.
Normally between our company and our client we have a SLA. Also another SLA exists between our company and each developer, but this is not enough. If we can we need to secure access because we need to reduce the risk of someone accessing our client data. If you think that you will increase the database security by encrypting the database password from connection string, you need to know that this is not truth. When someone will be able to access the machine where the application is hosted that we can decrypt the connection string, is only a matter of time.
We want to encrypt the connection string of production environment only for our team (development or testing team). Maybe we have a source control where we store the configuration file and we don’t want to have this information stored in clear text. The solution that I will explain in the next lines can be used only with the condition that out team doesn’t have access to the production servers. In some conditions, the only think that we want to let them to have access is to make a “fast” deploy of the application but without any remote access rights on the Windows Azure machines.
I propose a solution that is based on certificates. Because a certificate can have a public or a private key, we can share this information in such way that the team can only create and prepare our application package, but without any rights to decrypt or use the connection string of the production environment.
Our team (development for example) will have only the public key. Based on this key they will be able to generate the application package that will be used for deployment. Our SQL engineer for example will have access to the public key of the certificate. Based on the public key, the connection string will be encrypted. Any person that will have the public key will not be able to decrypt the connection string.
On our production environment we need to install the private key. Only the person/environment that has the private key of the certificate can decrypt the connection string.
In the next part of the post I will try to explain each step that need to be done to encrypt the connection string and how to generate the certificate.
The first step is to create a signed certificate. This certificate can be created in different methods. One method is to use the “makecert” command from Visual Studio. A password will be required to enter when you will run this command. This password will be used to secure the generated key that is created when the certificate is generated.
akecert -r -pe -n "CN=mysecureconfig" -sky exchange "mysecureconfig.cer" -sv "mysecureconfig.pvk"
pvk2pfx -pvk "mysecureconfig.pvk" -spc "mysecureconfig.cer" -pfx "mysecureconfig.pfx" -pi mysecretpassword
 "mysecureconfig.cer" represents our certificate that will be created (the public key). This file will be used by our SQL administrator to encrypt the connection string.
"mysecureconfig.pvk" represent our private key that need to be installed only on the production environment. Try not to store this file in the same location where you have the source of our application, because in this case the developers will have access to this key and they will be able to decrypt the connection string.
The next step is to generate a certificate of type .pfx. This will be used to import our certificate and the private key on Windows Azure. Now we have the .pfx file and we can import the certificate to cloud. To do this, we need to access the “Certificate” tab from Windows Azure. On this page we can upload our certificate. At this step, we will need to introduce the certificate password that we entered when we created the certificate.
The output of this import will be a thumbprint. This thumbprint need to be added to our application configuration file. Based on this thumbprint, our application will have access to our private key and will be able to decrypt the connection string.
At this step I want to remind you that the SQL administrator will need only the public key. The private key doesn’t need to be shared with anyone. Also, the database user from the connection string needs to have only the right that is needed by our application.
The third step is the most interesting one. In our configuration file of our application we need to add our connection string with user and password. Besides this information we will need to add a new provider that will be used to protect our data (in our case the connection string).
<configProtectedData>

    <providers>

      <add name="PKCS12ProtectedConfigurationProvider" 
           thumbprint="myThumbprintFromWindowsAzurePortal"
           type="Pkcs12PrLinkotectedConfigurationProvider.
                      Pkcs12ProtectedConfigurationProvider, 
           PKCS12ProtectedConfigurationProvider, 
           Version=1.0.0.0, Culture=neutral, 
           PublicKeyToken=34da007ac91f901d"/>

    </providers>

  </configProtectedData>
If you don’t have this provider (PKCS12ProtectedConfigurationProvider) installed on your system, you can download it from the following address: http://archive.msdn.microsoft.com/pkcs12protectedconfg.
Before the last step, I want to make a recap of what we need to have on the machine that wills encrypted the connection string:
  1. The certificate that we created installed (only the public key)
  2. The PKCS12ProtectedConfigurationProvider provider installed
  3. The provider that is used to encrypt the data added to the configuration file
  4. The connection string added to our configuration file (with user and password in clear – for now only)
Now, we can do the last step – to run from Visual Studio command prompt the following command:
aspnet_regiis -pef "connectionStrings" "." -prov 
"PKCS12ProtectedConfigurationProvider"
What does this command do? This command will encrypt the connection string based on our certificate. The only location from where this connection string can be decrypted is the machine that has the private key installed.
After this command is run, in the configuration file we will have the connection string encrypted. Don’t forget to add the following assembly to your solution: “PKCS12ProtectedConfigurationProvider.dll”, because the web role or the worker role will not have provider preinstalled.
Using this method, we can hide the database credentials from the production or testing environment from the development team. Even if they will see the connection string, they will see encrypted data and without the private key they cannot see the user and password.
What do you think about this solution? What other solution did you used for this case?
I decided to translate one of my blog posts in English: http://vunvulearadu.blogspot.ro/2012/06/how-to-secure-connection-string-of-sql.html

Comments

  1. I think it's the best solution for securing the password in the connection string, since SQL Azure does not support Windows integrated auth (it's also the official method recommended by SQL Azure team).

    Anyway, the main threat are not the developers (a dedicated admin should deploy the app to Azure), but the case when a server hosting the web role is compromised and the attacker has access to the virtual dir contents, including web.config, making the DB password accessible unless it's encrypted.

    ReplyDelete
    Replies
    1. Even if the password is encrypted, when you have access to the machine (web/worker-role) you will have access to the private key or to any other mechanism that is used to encrypt/decrypt the password.

      Delete
    2. Not necessarily - sure, if somebody gains root/admin access, everything is possible, but as long as the web role does not run as admin and is properly secured, using just the user running the web application should not be possible to decrypt the password (unless the user has enough rights to inject code into the running app, which should not be possible for a normal user).

      If something similar to Win DPAPI is used, the encription/decription is done by the OS, not the application, so the secret key is not handled by the application http://en.wikipedia.org/wiki/Data_Protection_API
      - unfortunately DPAPI is not available on Azure.

      Delete

Post a Comment

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provided a too