轉貼自teched講師: 牛可
基本概念:
第一層 服務主要金鑰
備份服務主要金鑰
backup service master key to file='c:/smk.bak'
encryption by password='P@ssw0rd'
restore service master key from file='c:/smk.bak'
decryption by password='P@ssw0rd'
第二層 資料庫主要金鑰
1)必須先在該資料庫上建立資料庫主要金鑰才能使用
create master key encryption by password='P@ssw0rd'
2)使用資料庫主要金鑰
-如果資料庫主要金鑰使用服務密鑰進行保護,則在使用時會自動開啟
opren master key decryption by password='P@ssw0rd'
3)查看資料庫主要金鑰狀態
sys.symmetric_keys
4)備份資料庫主要金鑰
backup master key to file='c:/smk.bak'
encryption by password='P@ssw0rd'
restore master key from file='c:/smk.bak'
decryption by password='P@ssw0rd'
數位憑證
建立自簽名
create certificate cert_myCert
encryption by password='P@ssw0rd'
with subject='Self Signed Cert',
start_date='1/31/2006'
expiry_date='1/31/2008'
非對稱金鑰
建立新的金鑰組
create asymmetric key asy_Key1
with algorithm=RSA_2048
encryption by password='P@ssw0rd'
對稱金鑰
建立新的金鑰組
create symmetric key SymKeyMarketing3
with algorithm=AES_2048
encryption by certificate asy_Key1
使用對稱金鑰
使用前必須開啟
open symmetric SymKeyMarketing3
decryption by certificate asy_Key1
sys.open_keys
資料列加密
-使用對稱金鑰密碼編譯大量的列資料
-考慮使用認證,非對稱金鑰保護對稱金鑰
防止繞過加密資料列的攻擊-使用驗證器
注:
在加密列上的索引將變得無效
加密資料列的長度增長,建議使用varbinary(max)資料類型
修改已有的dml語句以支援加密的資料列
-----***********樣本1 瞭解資料庫加密體繫結構*****-----
--************(1) 服務主要金鑰
--準備工作
--建立測試資料庫TestDB
--1)備份服務主要金鑰
backup service master key to file='g:/smk.bak'
encryption by password='p@ssw0rd'
--2)產生新的主要金鑰
Alter service master key regenerate
--3)從備份檔案還原服務主要金鑰
Restore service master key from file= file='g:/smk.bak'
encryption by password='p@ssw0rd'
--*************(2) 資料庫主要金鑰
--1)為資料庫建立資料庫主要金鑰
create master key encryption by password='p@ssw0rd'
go
--2)查看資料庫加密狀態
select [name],is_master_key_encrypted_by_server
from sys.databases where name='TestDB'
--3)查看資料庫主要金鑰的資訊
select * from sys.symmetric_keys
--4)備份資料庫主要金鑰
backup master key
to file='g:/testdbkey.bak'
encryption by password='p@ssw0rd'
--5)刪除服務主要金鑰對資料庫主要金鑰的保護
--建立非對稱金鑰成功,自動使用服務主要金鑰解密並使用該資料庫主要金鑰
create asymmetric key asy_Testkey1 with algorithm=RSA_1024
go
--刪除服務主要金鑰對資料庫主要金鑰的保護
alter master key
drop encryption by service master key
go
--查看資料庫加密狀態
select [name],is_master_key_encrypted_by_server
from sys.databases where name='TestDB'
--建立非對稱金鑰失敗,因為資料庫主要金鑰未開啟
create asymmetric key asy_Testkey2 with algorithm=RSA_1024
go
--開啟資料庫主要金鑰
open master key decryption by password='p@ssw0rd'
select * from sys.openkeys
go
--建立非對稱金鑰成功
create asymmetric key asy_Testkey2 with algorithm=RSA_1024
go
--恢複服務主要金鑰對資料庫主要金鑰的保護
alter master key
add encryption by service master key
close master key
go
--*****(3)認證
--1)建立自我簽署憑證
create certificate cert_Testcert
encryption by password='p@ssw0rd'
with subject='TestCert1',
start_date='1/31/2006',
expiry_date='1/31/2008'
go
select * from sys.certificates
--2)從檔案匯入認證
Create certificate cert_TestCert2
From file=’g:/MSCert.cer’
Go
-- 3)備份匯出認證和密鑰
backup certificate cert_Testcert
to file='g:/Testcert.cer'
with private key
(decryption by password='p@ssw0rd',
file='g:/TestCert_pvt',--私密鑰
encryption by password='p@ssw0rd' )
go
--4)使用認證加解密資料
--加密:使用認證的公開金鑰
declare @cleartext varbinary(200)
declare @cipher varbinary(200)
set @cleartext=convert( varbinary(200),'Test text string')
set @cipher=EncryptByCert(Cert_ID('cert_TestCert'),@cleartext)
select @cipher
--解密:使用認證的私密金鑰
select convert(varchar(200),DecryptByCert(Cert_ID('cert_TestCert'),@cipher,N'p@ssw0rd')) as [cleartext]
--5) 刪除認證私密金鑰
alter certificate cert_TestCert
remove private key
go
--加密成功
declare @cleartext varbinary(200)
declare @cipher varbinary(200)
set @cleartext=convert( varbinary(200),'Test text string')
set @cipher=EncryptByCert(Cert_ID('cert_TestCert'),@cleartext)
select @cipher
--解密失敗:因為私密金鑰被刪除
select convert(varchar(200),DecryptByCert(Cert_ID('cert_TestCert'),@cipher,N'p@ssw0rd')) as [cleartext]
--***(4) 非對稱金鑰
--1)使用sn.exe產生非對稱金鑰,安裝vs2005後有sn.exe, 在命令列方式下執行
sn -k g:/asy_Test.key
--2)從檔案建立非對稱金鑰
create asymmetric key asm_Test
from file='g:/asy_Test.key'
encryption by password='p@ssw0rd'
go
select * from sys.asymmetric_keys
--***********樣本2 使用加密保護列資料
-----*****(1) 準備工作
--1) 建立樣本表
create table empsalary
(EmpID int,
Title nvarchar(50),
Salary varbinary(500)
)
go
--2) 建立資料庫主要金鑰
create master key encryption by password='p@ssw0rd'
go
--3) 建立用於加密的對稱金鑰
create symmetric key sym_Salary
with algorithm=AES_192
encryption by password='p@ssw0rd'
go
select * from sys.symmetric_keys where [name]='sym_Salary'
------****(2)加密列資料
--1)開啟對稱金鑰
open symmetric key sym_Salary
decryption by password='p@ssw0rd'
go
select * from sys.openkeys
--2)向表中插入資料,並對salary列加密
insert into empsalary values (1,'CEO',EncryptByKey(KEY_GUID('sym_Salary'),'20000'))
insert into empsalary values (2,'Manager',EncryptByKey(KEY_GUID('sym_Salary'),'10000'))
insert into empsalary values (3,'DB Admin',EncryptByKey(KEY_GUID('sym_Salary'),'5000'))
--3)關閉開啟的對稱金鑰
close symmetric key sym_Salary
go
select * from sys.openkeys
--4) 查看錶中的資料
select * from empsalary
--(3)解密並訪問被加密的資料列
--1)開啟對稱金鑰
open symmetric key sym_Salary decryption by password='p@ssw0rd'
go
--2)使用對稱金鑰解密並訪問被加密的列
select empid,title,cast(DecryptByKey(Salary) as varchar(20)) as salary from empsalary
--3) 關閉對稱金鑰
close symmetric key sym_Salary
go
--(4)繞過加密資料的攻擊
--1)攻擊者使用其他資料行的加密資料替換某一行的資料
update empsalary
set salary=(select salary from empsalary where empid=1)
where empid=3
--2)查看被攻擊後解密的資料
open symmetric key sym_Salary decryption by password='p@ssw0rd'
select empid,title,cast(DecryptByKey(Salary) as varchar(20)) as salary from empsalary
close symmetric key sym_Salary
--(5)使用驗證器防止繞過加密資料的攻擊
--1)刪除前面添加的資料行
delete empsalary
--2)向表插入資料,並對salary列的資料使用驗證器進行加密
open symmetric key sym_Salary decryption by password='p@ssw0rd'
insert into empsalary values (1,'CEO',EncryptByKey(KEY_GUID('sym_Salary'),'20000',1,'1'))
insert into empsalary values (2,'Manager',EncryptByKey(KEY_GUID('sym_Salary'),'10000',1,'2'))
insert into empsalary values (3,'DB Admin',EncryptByKey(KEY_GUID('sym_Salary'),'5000',1,'3'))
--3)解密並訪問被加密的資料
select empid,title,cast(DecryptByKey(Salary,1,cast(empid as varchar(3))) as varchar(20)) as salary from empsalary
--4)用同樣的方法篡改資料
update empsalary
set salary=(select salary from empsalary where empid=1)
where empid=3
--5)被篡改後的加密了的資料列變成無效
select empid,title,cast(DecryptByKey(Salary,1,cast(empid as varchar(3))) as varchar(20)) as salary from empsalary
--***********樣本3 使用數位憑證簽署預存程序
--*****(1)準備
--1)建立資料庫主要金鑰
create master key encryption by password='p@ssw0rd'
--2)建立簽署預存程序所需要的認證
create certificate cert_Product
with subject='Products Sign',
start_date='1/31/2006',
expiry_date='1/31/2008'
go
--3)建立SPDeveloper登入帳戶和使用者,該使用者建立並訪問Products表的預存程序
create login [SPDeveloper] with password='p@ssw0rd',default_database=[TestDB]
go
create user [SPDeveloper] for login SPDeveloper with default_schema=[SPDeveloper]
go
create schema products authorization SPDeveloper
go
exec sp_addrolemember @rolename='db_owner',@membername='SPDeveloper'
--4)以SPDeveloper的身份建立預存程序products.usp_Products
execute as user='SPDeveloper'
go
create procedure products.usp_Products
as
select * from dbo.Products
go
revert
select user
--5)建立普通使用者jerry
create login [jerry] with password='p@ssw0rd',default_database=[TestDB]
go
create user [jerry] for login jerry
go
--*******(2)使用認證簽署預存程序
--1)授予使用者jerry執行預存程序的許可權
grant execute on products.usp_Products to jerry
--2)以jerry的身份執行預存程序失敗,因為擁有權鏈的斷裂的
execute as user='jerry'
select user
go
execute products.usp_Products
go
revert
--3)使用認證在當前資料庫建立使用者ProductsReader,並為該使用者賦予讀取products表的許可權
create user ProductsReader for certificate cert_Products
go
grant select on products To ProductsReader
--4)使用認證簽署當前預存程序
add signature to products.usp_Products by certificate cert_Products
--5)以jerry的身份重新執行預存程序,成功
--因為預存程序將以ProductsReader的許可權上下文執行
execute as user='jerry'
select user
go
execute products.usp_Products
go
revert
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/cuoguo1111/archive/2006/11/29/1419515.aspx