MSDN Documentation

SQL Data Masking

Data masking is a security technique used to protect sensitive data by obscuring or replacing it with non-sensitive or fictitious data. This is particularly useful in non-production environments like development, testing, and staging, where access to sensitive customer data is not required but replicating the database structure and data volume is beneficial.

What is Data Masking?

Data masking involves modifying data in a database to protect privacy. The original data remains unchanged in the production environment, while masked copies are created for other uses. Common masking techniques include:

Benefits of Data Masking

Implementing Data Masking in SQL Server

SQL Server offers features that can be leveraged for data masking, particularly dynamic data masking, which masks data in real-time for specific users.

Dynamic Data Masking

Dynamic Data Masking restricts sensitive data by transforming it for non-privileged users. It does not change the actual data stored in the database. Instead, it applies masking rules at query time based on the user's permissions.

Syntax for Applying a Mask

You can apply a masking function to a column using the ALTER TABLE statement:

ALTER TABLE Employees
ALTER COLUMN SSN ADD MASKED WITH (FUNCTION = 'xxx-xx-xxxx');

ALTER TABLE Customers
ALTER COLUMN Email ADD MASKED WITH (FUNCTION = 'email()');

ALTER TABLE Orders
ALTER COLUMN Amount ADD MASKED WITH (FUNCTION = 'default()');

Supported masking functions include:

Permissions and Masking

To see the original, unmasked data, a user needs the UNMASK permission.

GRANT UNMASK TO [user_or_role];

By default, users without UNMASK permission will see masked data for columns that have masking applied.

Example Scenario

Consider an Employees table with a Salary column. You want to hide the salary from most employees but allow HR personnel to see it.

  1. Apply masking to the Salary column:
    ALTER TABLE Employees
    ALTER COLUMN Salary ADD MASKED WITH (FUNCTION = 'default()');
  2. Grant UNMASK permission to the HR role:
    GRANT UNMASK TO HR_Role;
  3. Query the data:
    • A regular user queries SELECT Salary FROM Employees WHERE EmployeeID = 101; will see 0.00 or similar default value.
    • A user in the HR_Role queries the same statement and sees the actual salary.

Static Data Masking

For scenarios where you need a permanently masked copy of the database, static data masking tools are available. These tools typically operate offline, creating a masked version of the database that can be used for testing or development.

Dynamic Data Masking is a presentation layer security feature, meaning it does not encrypt data at rest or change the underlying stored values.

Considerations

For more detailed information on implementing and managing data masking in SQL Server, refer to the official Microsoft documentation.