Securing and protecting SQL Server data, log and backup files with TDE

Dolyetyus

Özel Üye
21 Nis 2020
1,208
3
677
Delft
Welcome Turk Hack Team Members,


Problem

I need to keep some old SQL data and log files in a backup folder for DR purposes. What is the best way to protect these files? In this tip we look at using TDE to safeguard your files and the steps necessary to move a TDE database to another SQL Server instance.


Solution

In this tip I'll show you how to setup Transparent Data Encryption (TDE). TDE is new in SQL Server 2008 and serves as an encryption method that uses a database encryption key (DEK) to protect SQL Server's data and log files. The DEK is a key secured by a certificate stored in the master database.

To setup TDE we'll need to run a few scripts: (My test database is named TDE)

The following script will create the master key with a specified password ElephantRhin0:

Kod:
[COLOR="PaleGreen"]USE master;
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'ElephantRhin0';
GO[/COLOR]

Next, we'll create a certificate named TDECert that will be protected by the master key:

Kod:
[COLOR="palegreen"]USE master;
GO
CREATE CERTIFICATE TDECert WITH SUBJECT = 'TDE Certificate';
GO[/COLOR]

After creating the certificate we'll backup the certificate to a specified source:

Kod:
[COLOR="palegreen"]USE master;
GO
BACKUP CERTIFICATE TDECert TO FILE = 'C:\TDECert_backup' WITH 
PRIVATE KEY ( FILE = 'C:\TDECert_key' ,ENCRYPTION BY PASSWORD = 'ElephantRhin0' )
GO
[/COLOR]
Once the certificate is backed up we will create the DEK using the AES algorithm and protect it by the certificate:

Kod:
USE TDE;
GO
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_128
ENCRYPTION BY SERVER CERTIFICATE TDECert;
GO

The final step is to set our database to use encryption:

Kod:
[COLOR="palegreen"]ALTER DATABASE TDE
SET ENCRYPTION ON;
GO[/COLOR]

If everything completed successfully then we have officially encrypted our database with TDE, but don't take my word for it, run the following query to confirm:

SELECT name, is_encrypted
FROM sys.databases
WHERE name = 'TDE'

2471_TDE_clip_image002.png


Once, we have the database encrypted let's try to move data from one instance to another.


Move Database Using a Database Restore

Backup the encrypted database:

Kod:
BACKUP DATABASE [TDE] TO  DISK = N'C:\TDEBackup' WITH NOFORMAT, NOINIT,  NAME = 
N'TDE-Full Database Backup',  SKIP, NOREWIND, NOUNLOAD,  STATS = 10

Restore the encrypted database on a different instance:

Kod:
RESTORE DATABASE TDE FROM DISK = N'\\Principal\C\TDEBackup' WITH FILE = 1, NOUNLOAD,  
STATS = 10

When we try to restore the database we get the following error indicating that the instance can't find the certificate:

2471_TDE_clip_image004.jpg



Move Database Using Copy Database

Right click on the database, choose Tasks > Copy Database...

2471_TDE_clip_image006.jpg


Complete the wizard and on the last step we get a job failed message.

2471_TDE_clip_image007.png


Move Database Using Detach and Attach Method

Right click on the database and select Tasks > Detach...

2471_TDE_clip_image008.png


Move the database files to new server and attach the database to the new server. When we try to attach the database we get the following error:

2471_TDE_clip_image009.png



Successfully Moving The Database to a New Instance

To move, copy, or restore a database that is encrypted with TDE we will need to restore the certificate to the destination server first. To do this we will need to be connected to the destination server and run this statement to restore the certificate that we backed up while enabling TDE.

Kod:
[COLOR="PaleGreen"]USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'ElephantRhin0'
GO
CREATE CERTIFICATE TDECert    
FROM FILE = '\\Principal\C\TDECert_backup'     
WITH PRIVATE KEY (FILE = '\\Principal\C\TDECert_key',    
DECRYPTION BY PASSWORD = 'ElephantRhin0');
GO[/COLOR]

To confirm the certificate was created, you can drill down in SSMS in the master database to Security > Certificates and find our certificate named TDECert.

2471_TDE_clip_image010.png


Once this certificate is created on our destination server, we can use any of the above methods and in our case we will do a RESTORE to show that this works now.

Kod:
[COLOR="palegreen"]RESTORE DATABASE TDE FROM  DISK = N'\\Principal\C\TDEBackup' WITH  FILE = 1, NOUNLOAD,  
STATS = 10[/COLOR]

2471_TDE_clip_image011.png



Next Steps

  • If you need to protect your physical database files such as data files, log files or database backup files consider using TDE
  • Make sure you safeguard the corticated and master key password as these are the keys to enable successful access to these files.
  • While TDE works well for encrypting your data and log files, this encryption method does not encrypt your data inside SQL Server. So look at other methods such as Symmetric and Asymmetric Keys.

//Quoted. Thanks for reading.
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.