標籤:ssms 必須 評估 important 串連 sql_mod error ceiling ueditor
SQL Server 物件加密方法:在預存程序,函數,視圖的“As”位置前加上“with encryption”;如果是觸發器,就在“for”位置前加“with encryption”。
解密過程:
1.執行如下指令碼,開啟管理員串連(DAC),建立解密預存程序。
USE master GO -- enable DAC sp_configure 'show advanced options', 1 GO sp_configure 'remote admin connections', 1 GO RECONFIGURE WITH OVERRIDE GOSELECT * FROM sys.configurations where name = 'remote admin connections'-- create decrypt sp sp_DecryptObjectif object_ID('[sp_DecryptObject]') is not null Drop Procedure [sp_DecryptObject]Gocreate procedure sp_DecryptObject ( @Object sysname, --要解密的對象名:函數,預存程序,視圖或觸發器 @MaxLength int=4000 --評估內容的長度)asset nocount on/* 1. 解密 */ if not exists(select 1 from sys.objects a where a.object_id=object_id(@Object) And a.type in('P','V','TR','FN','IF','TF'))begin --SQL Server 2008 --raiserror 50001 N'無效的對象!要解密的對象必須是函數,預存程序,視圖或觸發器。' --SQL Server 2012/2014 throw 50001, N'無效的對象!要解密的對象必須是函數,預存程序,視圖或觸發器。',1 returnend if exists(select 1 from sys.sql_modules a where a.object_id=object_id(@Object) and a.definition is not null)begin --SQL Server 2008 --raiserror 50001 N'對象沒有加密!' --SQL Server 2012/2014 throw 50001, N'無效的對象!要解密的對象必須是函數,預存程序,視圖或觸發器。',1 returnend declare @sql nvarchar(max) --解密出來的SQL語句 ,@imageval nvarchar(max) --加密字串 ,@tmpStr nvarchar(max) --臨時SQL語句 ,@tmpStr_imageval nvarchar(max) --臨時SQL語句(加密後) ,@type char(2) --物件類型('P','V','TR','FN','IF','TF') ,@objectID int --對象ID ,@i int --While迴圈使用 ,@Oject1 nvarchar(1000) set @objectID=object_id(@Object)set @type=(select a.type from sys.objects a where [email protected]) declare @Space4000 nchar(4000)set @Space4000=replicate('-',4000) /*@tmpStr 會構造下面的SQL語句-------------------------------------------------------------------------------alter trigger Tr_Name on Table_Name with encryption for update as return /**/alter proc Proc_Name with encryption as select 1 as col /**/alter view View_Name with encryption as select 1 as col /**/alter function Fn_Name() returns int with encryption as begin return(0) end/**/*/set @Oject1=quotename(object_schema_name(@objectID))+'.'+quotename(@Object)set @tmpStr= case when @type ='P ' then N'Alter Procedure '[email protected]+' with encryption as select 1 as column1 ' when @type ='V ' then N'Alter View '[email protected]+' with encryption as select 1 as column1 ' when @type ='FN' then N'Alter Function '[email protected]+'() returns int with encryption as begin return(0) end ' when @type ='IF' then N'Alter Function '[email protected]+'() returns table with encryption as return(Select a.name from sys.types a) ' when @type ='TF' then N'Alter Function '[email protected]+'() returns @t table(name nvarchar(50)) with encryption as begin return end ' else 'Alter Trigger '[email protected]+'on '+quotename(object_schema_name(@objectID))+'.'+(select Top(1) quotename(object_name(parent_id)) from sys.triggers a where [email protected])+' with encryption for update as return ' end set @[email protected]+'/*'[email protected]set @i=0while @i < (ceiling(@MaxLength*1.0/4000)-1)begin set @[email protected]+ @Space4000 Set @[email protected]+1endset @[email protected]+'*/' ------------set @imageval =(select top(1) a.imageval from sys.sysobjvalues a where [email protected] and a.valclass=1)begin tranexec(@tmpStr)set @tmpStr_imageval =(select top(1) a.imageval from sys.sysobjvalues a where [email protected] and a.valclass=1) rollback tran -------------set @tmpStr=stuff(@tmpStr,1,5,'create')set @sql=''set @i=1while @i<= (datalength(@imageval)/2)begin set @[email protected]+isnull(nchar(unicode(substring(@tmpStr,@i,1)) ^ unicode(substring(@tmpStr_imageval,@i,1))^unicode(substring(@imageval,@i,1)) ),'') Set @i+=1end /* 2. 列印 */ declare @patindex int while @sql>''begin set @patindex=patindex('%'+char(13)+char(10)+'%',@sql) if @patindex >0 begin print substring(@sql,1,@patindex-1) set @sql=stuff(@sql,1,@patindex+1,'') end else begin set @patindex=patindex('%'+char(13)+'%',@sql) if @patindex >0 begin print substring(@sql,1,@patindex-1) set @sql=stuff(@sql,1,@patindex,'') end else begin set @patindex=patindex('%'+char(10)+'%',@sql) if @patindex >0 begin print substring(@sql,1,@patindex-1) set @sql=stuff(@sql,1,@patindex,'') end else begin print @sql set @sql='' end end end end Goexec sys.sp_MS_marksystemobject 'sp_DecryptObject' --標識為系統對象go
2.開啟SSMS,建立DAC串連,如,在執行個體名“sqlclust\testal”前加上“admin:”,點串連:
3.運行如下T-SQL語句來解密:
USE xxxxxGOEXEC sp_DecryptObject 'sp_xxxxxxx'GO
SQL Server解密預存程序