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(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

What to do when you hit the throughput limits of Azure Storage (Blobs)

In this post we will talk about how we can detect when we hit a throughput limit of Azure Storage and what we can do in that moment. Context If we take a look on Scalability Targets of Azure Storage ( https://azure.microsoft.com/en-us/documentation/articles/storage-scalability-targets/ ) we will observe that the limits are prety high. But, based on our business logic we can end up at this limits. If you create a system that is hitted by a high number of device, you can hit easily the total number of requests rate that can be done on a Storage Account. This limits on Azure is 20.000 IOPS (entities or messages per second) where (and this is very important) the size of the request is 1KB. Normally, if you make a load tests where 20.000 clients will hit different blobs storages from the same Azure Storage Account, this limits can be reached. How we can detect this problem? From client, we can detect that this limits was reached based on the HTTP error code that is returned by HTTP