Securing Data in SQL Server

Recently I have been having quite a few discussions around security of data inside of SQL Server and how to prevent the massive data breaches that we have been hearing about on the news.  Like most things some people want “THE ANSWER” or THE SOLUTION to securing the data inside of SQL Server. Unfortunately there isn’t a single solution that solves all of the problems that are potentially out there.  Security of data requires defense in depth, starting with a secure configuration and installation of SQL Server.  Often times, defense in depth also means changes to the database, the application, and how you do business.

Features for Securing Data

SQL Server offers multiple options and features that help with securing data and since SQL Server 2016 Service Pack 1, many of them are available in Standard Edition:

  • SSL/TLS Protocol Encryption

    • SQL Server 2005+
    • Uses a SSL certificate to encrypt the network connection between clients and SQL Server, securing data from watching over the wire
    • Can be forced by SQL Server during the handshake when certificate properly installed on SQL Server certificate store
    • Only requires a SSL certificate from a CA for Server Authentication to implement
    • Requires certificate rotation before expiration
    • Generally does not require application changes but may under specific conditions
  • Database Column Level Encryption

    • SQL Server 2005+
    • Uses a certificate or key to encrypt a column securing data from being queried without knowing how to decrypt the values
    • Data remains encrypted at the column level even while the database is online
    • Encryption keys are maintained inside of the SQL Server encryption hierarchy and must be opened properly to decrypt data
    • May require application changes to support encryption
      • Not required if handled by stored procedure access to data but opens risks of decrypted data in process memory
    • Performance impacts when comparing column data in WHERE clause
      • May be mitigated by encrypting value to compare first and performing binary filtering of encrypted values
  • Transparent Data Encryption

    • SQL Server 2012+
    • Encrypts data at rest using database encryption key stored in the boot record of the database and a certificate stored in master
    • Prevents someone copying files or stealing a database backup from restoring the data without the certificate
    • Does not require application changes for securing data at rest
    • Does not protect data from being queried through SQL Server once the database is open
    • If someone has access to the master database or a backup of master, they can get the certificate that encrypts other backups (https://simonmcauliffe.com/technology/tde/)
    • Without the certificate that encrypts the database you cannot recover from a disaster
    • Certificate expiration is not enforced or checked and does not require certificate rotation once used for encryption
  • Backup Encryption

    • SQL Server 2014+
    • Encrypts the backup file using a certificate or asymmetric key, securing data backups from being restored
    • Prevents someone stealing a database backup from restoring the data without the certificate or key
    • Same potential risks as TDE since certificate or key is stored in the master database
  • Always Encrypted

    • SQL Server 2016+
    • Data remains encrypted at the column level even while the database is online
    • Column encryption keys are stored in the database to encrypt data
    • Column master keys are used to encrypt the column encryption keys in the database and are maintained outside of SQL Server and are not available to the DB, securing data from DBAs
      • Requires installing column master keys for SSMS to query and decrypt data – Windows Certificate Store, Azure Key Vault, or HSM
    • Requires application changes to support the encryption
    • Protects data in use from memory dumps and maintains encryption

How do we apply this for Securing Data?

Generally speaking, the first thing that we need to define is what specifically we are trying to protect against?  If we need to prevent someone from monitoring  or intercepting network packets containing data in clear text then we need to implement protocol encryption for connections.  If we are concerned about someone opening the database on another system or stealing a copy of the database or backups using TDE might be a good solution, but only if we also maintain a defense in depth strategy that separates our backups of master and the certificates used by TDE from the database backup files.  If we don’t separate our backups then TDE is very easy to hack around and is simply checking the box without actually securing our data.  If we want to protect the data at rest and from prying eyes while open, then we need to implement some form of column level encryption of the data, whether that is key based inside of SQL Server or using Always Encrypted. The important thing is that there isn’t a single solution that is going to protect every situation, you have to consider the risks and what a specific feature is designed to protect against and then layer them together to meet the requirements. Even then you might not be protected.

Take as an example, the Microsoft Customer Support Database data exposure that happened December 2019 and Microsoft provided public information about after securing the data.  While we don’t know all the security measures that were in place specifically on the database level, none of the above features would have protected the data from being publicly viewable through an application level security misconfiguration. As a part of any data security discussion, auditing and regular reviews of business practices, and any changes, have to be implemented to ensure that the data remains secured.

Keyset Does Not Exist – Add-ClusterNode

While working on a video recording for Paul this week I ran into an interesting problem with one of my Windows Server 2016 clusters. While attempting to add a new node to the cluster I ran into an exception calling Add-ClusterNode:

The server ‘SQL2K16-AG03.SQLskillsDemos.com’ could not be added to the cluster.
An error occurred while adding node ‘SQL2K16-AG03.SQLskillsDemos.com’ to cluster ‘SQL2K16-WSFC’.

Keyset does not exist

The windows account I was using was the domain administrator account and I had just recently made modifications that involved the certificate store on this specific VM, so I decided to take a backup of the VMDK and then revert to a snapshot to try again, and this time it worked.  So needless to say I was intrigued as to what I could have done that would be causing this error to happen.  It turns out that while installing a SSL certificate for SQL Server to use, I had negatively impacted the permissions for the C:\ProgramData\Microsoft\Crypto\RSA folder, and the keys that are protected inside of that folder, which happen to be the private keys for the certificates on the server.  The normal permissions for this folder can be seen on the screenshot from the working node in the cluster.

UPDATE: 1/24/2020 – A reader provided a link from Microsoft Support for the default permissions as well (https://support.microsoft.com/en-us/help/278381/

 

On the broken copy of the VM, the Owner was not set, and none of the other permissions were even close to matching and inheritance had been propagated (oops!!!).  The really crazy thing is, I don’t even know how exactly I caused this to happen at this point, but none of the keys were accessible to anything on that node. While I was able to go into the folder and manually set the Owner: and all the permissions on each of the Keys in the Machine folder to allow the node to be joined to the cluster, I ultimately went and evicted the node and rebuilt it instead of relying on manually applied permissions to make it work.   Here is why:

The owner of the folders is SYSTEM, which is easy to go back and set, but to set the permissions on the Keys inside the Machine Keys folder, you have to Take Ownership of the key:

Even as an Administrator, you don’t have Read access, so when you click the Advanced button, it can’t show any information and offers to try Administrative Permissions to do it, but again that fails when you click Continue to get the objects permissions you can only take ownership to view the properties, which then means you don’t know who the correct owner actually should have been.

However, in a real production down scenario where you made a mistake setting permissions and the cluster nodes can’t join into the cluster, start the cluster service correctly, or take ownership of the resources, manually taking ownership and setting permissions on the Keys as shown in this screenshot from another environment I intentionally destroyed the permissions on to try and complete this blog post, can get you out of a pinch.

SQL Server PDB Symbols and Callstacks The Easy Way

Lately, I have been doing a lot of work troubleshooting certain behaviors in SQL Server for workloads that are, to put it simply, designed horribly. As a part of this, I have found it necessary to collect callstacks with Extended Events and to materialize them using the debugger symbols for SQL Server to see where exactly certain types of issues are being encountered to better understand some of the internals of newer features in SQL Server 2017 and 2019. Years ago I blogged about how to use the package0.callstack action in Extended Events for this type of thing, and Paul also has a blog post that talks about how to download the PDB symbols for SQL Server as well as a post that also demonstrates using the package0.callstack action to determine what causes a particular wait type. Using the debugging tools to get the symbols is somewhat clunky and tedious, so when I happened on this amazingly simple method of getting symbol files I had to share it.

The SQLCallstackResolver on Github has to be one of the greatest things since sliced bread if you want to materialize callstacks from SQL Server. All you need is the current release of the application, and the appropriate SQL Server binaries, and it will create a Powershell script to download the appropriate symbols files. There is even a Wiki page with scripts for downloading most of the builds for all of the versions of SQL Server but if one happens to not be there, you can run the application, choose the Binn path where that build’s executable and DLL’s are located and it will generate the PowerShell script to download the symbols. What’s even better though is that the application allows you to paste a callstack from any version of SQL Server into the window, point the Symbols path to the appropriate folder with the symbols downloaded, and it uses the Microsoft.Diagnostics.Runtime.dll project to materialize the callstack to the function names and calls. So if you have the output of the callstack and you know what the build of SQL Server is that generated it, you can now materialize the functions with just this application. No need to take a memory dump of SQL Server, no need to use Symchk, no need for trace flag 3656 live in your server, you can copy/paste/resolve in one place.

This is very, very cool!