1. 程式人生 > 其它 >SQL Server 2016新特性:動態資料遮蔽(DDM)

SQL Server 2016新特性:動態資料遮蔽(DDM)

編輯手記:對於敏感資料的適當遮蔽一直是資料安全中一個重要的部分,在SQL Server 2016上推出了動態資料遮蔽的新特性,使得開發人員或者資料庫管理員能夠控制敏感資料的暴露程度,並且在資料庫層面生成資料,大大簡化了資料庫應用層的安全設計和編碼。

Microsoft has introduced an impressive new feature in SQL Server 2016 called Dynamic Data Masking (DDM). Dynamic Data Masking allows a developer or administrator to decide how much of the sensitive data to reveal with minimal impact on the application layer. This feature also helps to simplify the design and coding of security in your application by making the data at the database level.

在SQL Server 2016上推出了一個很強的新特性叫做Dynamic Data Masking (DDM)-動態資料遮蔽,為了儘可能少的對應用層造成影響,該特性允許開發人員或者資料庫管理員能夠控制敏感資料的暴露程度,並且在資料庫層面生成資料,大大簡化了資料庫應用層的安全設計和編碼。

Dynamic Data Masking does not modify or change the actual data stored in a table; it applies the masking functions on the table’s column at the time of returning a data as the result of a query. Dynamic Data Masking supports four data masking functions, as listed below, using which you can mask the data at the database level:

  1. Default
  2. Random
  3. Custom String
  4. Email

動態資料遮蔽並不會真正改動表中儲存的實際資料,只是在查詢的時候應用該特性控制查詢返回的資料,動態資料遮蔽支援四種資料遮蔽函式,可以通過以下四個函式在資料庫層面進行遮蔽:

1、預設遮蔽

2、隨機遮蔽

3、自定義遮蔽

4、郵件遮蔽

Note: There are two ways using which you can apply the DDM functions. You can apply this at the time you create the table or you can apply this function in the existing table that contains data using an ALTER statement. 注:

應用資料遮蔽函式有兩種方式,在建立表的時候應用或者在現有的表上使用ALTER語句應用。

接下來我們將介紹四種遮蔽函式。

1、Default 預設函式

The default function of Dynamic Data Masking masks data on the basis of the column's data type.

預設遮蔽函式是針對基本型別的資料列進行遮蔽的。

  • If the data type is date and time, then it shows the data in 1900-01-01 00:00:00.000 formats. 如果資料型別包含日期和時間,會以“1900-01-01 00:00:00.000”格式顯示;
  • If the data type is numeric then it shows a 0. 如果資料型別是數字型別的,會顯示0;
  • If data type is string, then it displays data by adding Xs in the string. This function can add maximum 4 X’s in string data, if string contains less than 4 characters, then it will show X for fewer characters only. 如果是字串型別的,將會在字串後面新增X,最多能新增4個,如果字串包含的字元少於4個,則會以實際的X數目顯示。

An example of the Default Dynamic Data Masking function is shown below.

以下是使用預設遮蔽函式的一個案例。

In this whole article, we will use the same table, so let's create this table. The below script will create a table named DDM_Student_Sample. While creating the table, we will apply the default DDM function on the Student_DOB column. The actual data of the Student_DOB column will not be visible to the user who has read permission. Instead of the actual data, SQL Server will return data in the 1900-01-01 00.00.00.000 format.

首先我們來建立一張表,命名為“DDM_Student_Sample”,在建立的時候,我們在Student_DOB列上應用以下預設遮蔽函式,此時Student_DOB列上的真實資料將不能被正常訪問,哪怕使用者具有讀取表的許可權,當資料被訪問到的時候,將會返回1900-01-01 00.00.00.000格式的資料。

After table creation, we need to insert some data into table to check how the Default DDM function works. So we will use below query to insert four rows into the table.

建立完成以後,我們需要插入一些資料來驗證預設遮蔽函式的作用。使用以下語句在表中插入四行資料。

After inserting the data we will use the below script to check an actual data stored in the table- DDM_Student_Sample. ( here we are using the user credentials who is having full access or adequate permission which require to check an actual data of the table and those users only will be able to see the sensitive information like as shown in above figure.)

插入資料以後,我們將採用以下指令碼檢查表中的真實資料。我們使用具有足夠許可權的高階使用者來做查詢,這類使用者能夠檢視真實資料,只是在返回的時候會提示敏感資訊。

-- Check the actual data in the table DDM_Student_Sample using the below querySelect * from [dbo].[DDM_Student_Sample]

Now we will create a user and grant read permission on DDM_Student_Sample table using below script:

現在我們建立一個使用者 ,使用以下語句對其授DDM_Student_Sample表的讀取權。

CREATE USER DDM_Read WITHOUT LOGIN

As we have applied Default DDM function on column Student_DOB, so lets check how the data will appear when user having read permission on a table using below script.

在這張表的Student_DOB列我們已經應用了預設遮蔽函式,接下來我們看在查詢到的時候資料會如何返回。

EXECUTE AS USER = 'DDM_Read'SELECT * FROM [dbo].[DDM_Student_Sample]REVERT

On above output we can see that user DDM_Read is not able to see the actual data for the Student_DOB column because we have applied the Default Dynamic Data Masking function on this column. Hence, data of column Student_DOB showing in the 1900-01-01 00.00.00.000 format.

在上面的結果中我們看到,使用者雖然具有訪問表的許可權,但並不能讀取到真實的資料,因為應用了預設遮蔽函式,所以該列最終返回1900-01-01 00.00.00.000。

If you want to allow a few users who have less privileges, like the user, DDM_Read, then grant the UNMASK permission for this set of users:

如果你想用許可權更低的使用者,比如DDM_Read,然後我們對這類使用者授非遮蔽許可權。

Grant UNMASK to DDM_Read

after granting UNMASK permission to the user, DDM_Read, they will be able to see the actual data, like shown in the below figure.

授權非遮蔽之後,就能看到真實的資料,如下所示:

Use the below script to revoke the UNMASK permission of user, DDM_Read.

回收剛才的DDM_Read使用者的非遮蔽許可權

Revoke UNMASK to DDM_Read

2、隨機遮蔽函式

This DDM function is applied on numeric data types only. It displays a random value for the specified range. In the below example we will apply the Random function on the Student_ID column.

隨機遮蔽函式只對數字型別起作用。它會將某一個範圍內的值隨機顯示。在下面的案例中,我們在Student_ID列上應用了隨機遮蔽函式。

Alter Table[dbo].[DDM_Student_Sample] Alter Column Student_ID Add masked with (function='Random(1,4)')

After applying the Random function, when we try to check the data of table using the DDM_Read user (user with read permission only), the data of the table will look like shown in below figure:

應用完隨機遮蔽函式之後,我們通過DDM_Read使用者訪問表的資料,結果如下所示:

In the above figure, we can see that actual values for Student_ID are replaced with some random numeric values. Again, if you want to allow less privileged user to check the actual data of the table, then grant the UNMASK permission.

在上面的表中,我們看到Student_ID列的真實資料被隨機的數值代替,同樣,如果你想嘗試用許可權低一點的使用者,可以授非遮蔽許可權。

3、Custom String 自定義遮蔽

This DDM function uses the below syntax to mask the data:

自定義遮蔽函式使用以下語法進行遮蔽資料。

Syntax : Partial(prefix,[padding],suffix)

語法:Partial(prefix,[padding],suffix)

  • Prefix – Starting numbers of character to be displayed.(要顯示的字元的起始編號)
  • Suffix – Last number of characters to be displayed from specified column value(從指定列值顯示的最後一個字元數)
  • Padding –Custom padding string for masking.(用於遮蔽的自定義填充字串)

We will apply the Custom String DDM function on Student_Name column with the below values :

在我們的案例中,將會用以下值對錶的資料做自定義遮蔽。

  • Prefix = 3 -- It will displayed first three characters of Student_Name column values.(.它將顯示Student_Name列值的前三個字元。)
  • Suffix= 9 -- It will display last 9 characters of Student_Name column values.(它將顯示Student_Name列值的最後9個字元。)
  • Padding = &&**& -- It will start masking from 4th character and display this Padding string.(&&**& 它將從第4個字元開始遮蔽並顯示此Padding字串。)

Use the below script to apply Custom String function on a Student_Name column of table DDM_Student_Sample.

使用以下指令碼在表DDM_Student_Sample的Student_Name列上應用自定義字串函式

Alter Table[dbo].[DDM_Student_Sample] Alter Column Student_Name Add masked with (function='Partial(3,"&&**&",9)')

And then check the data using DDM_Read user.

使用DDM_Read使用者檢查資料

The data in the column, Student_Name, will look like it does above for the user, DDM_Read, due to the Custom String DDM function.

因為自定義遮蔽函式的使用, Student_Name列上的值將會如上圖顯示。

4、The Email Function 郵件函式

This DDM function will displays the first character of an email address, masking the rest of the characters with XXX@XXXX until the suffix “.com”. For example, if we apply the email DDM function for an email address like [email protected], then this email address will appear as "[email protected]".

此DDM功能將顯示電子郵件地址的第一個字元,用XXX @ XXXX遮蔽其餘字元,直到字尾“.com”。 例如,如果我們對[email protected]這樣的電子郵件地址應用電子郵件DDM功能,則此電子郵件地址將顯示為“[email protected]”。

Using the below script, we will apply the email DDM function on the Student_Email_Id column of the table, DDM_Student_Sample, and check how the data will appear to the user, DDM_Read user.

使用以下語句,我們將在表的Student_Email_Id列DDM_Student_Sample上應用電子郵件DDM函式,並檢查資料對使用者DDM_Read使用者的顯示方式。

Alter Table[dbo].[DDM_Student_Sample] Alter Column Student_Email_Id Add masked with (function='Email()') 

And the values of Student_Email_ID appear in the below format to the user, DDM_Read:

並且Student_Email_ID的值以下面的格式顯示給使用者DDM_Read:

So, in the above image we can see that how data will look after applying the Default, Random, Custom String, and Email Dynamic Data Masking functions to the user who is having less (read only) permission on the table.

因此,在上面的影象中,我們可以看到在對錶具有較少(只讀)許可權的使用者應用預設,隨機,自定義字串和電子郵件動態資料遮蔽功能後,資料的外觀。

We can use below script to remove all the Dynamic Data masking functions on the table

我們可以使用下面的指令碼刪除表上的所有動態資料遮蔽功能

After removal of all Dynamic Data Masking function a sensitive data will be visible to the user DDM_Read as shown in below figure.

刪除所有動態資料遮蔽功能後,敏感資料將對使用者DDM_Read可見,如下圖所示。

Dynamic Data Masking Feature in SQL Server 2016 allows user to mask the data at database level without altering or obfuscating the actual stored data in a table. We can say this feature adds an advantage for the DBA, allowing them to hide the sensitive data from set of user who are having less privileges. This feature saves the extra effort of obfuscating or masking data when a vendor visits your company to fix some issue related to data in a database.

SQL Server 2016中的動態資料遮蔽功能允許使用者在資料庫級別遮蔽資料,而不會更改或混淆表中的實際儲存資料。 我們可以說這個功能為DBA增加了一個優點,允許他們從具有較少許可權的使用者集中隱藏敏感資料。 此功能節省了當供應商訪問您的公司以修復與資料庫中的資料相關的某些問題時,對資料進行模糊處理或遮蔽的額外工作量。