《解密 SQL Server 的加密存储过程》由会员分享,可在线阅读,更多相关《解密 SQL Server 的加密存储过程(14页珍藏版)》请在金锄头文库上搜索。
1、解密SQL Server加密存储过程,函数,触发器,视图本主主题包括了解密SQLServer2005和Sql Server 2000的加密存储过程,函数,触发器,视图。A),解密SQLServer2005加密存储过程,函数,触发器,视图在SQL SERVER 2005中必须用专用管理连接才可以查看过程过程中用到的表,专用管理连接需要在sqlcmd命令后带参数 -A,要不然就会出错,提示 无效的对象名:sys.sysobjvaluesMsg 208, Level 16, State 1, Server MICROSOF-A91382WINCC, Procedure sp_decrypt,Line
2、 8Invalid object name sys.sysobjvalues.步骤:1、创建sp_decrypt储存过程,见create PROCEDURE dbo.sp_decrypt 部分2、使用命令行:sqlcmd -A -S localhostuse 数据库名gosp_decrypt 加密的对象名go就显示加密对象的内容了/-create PROCEDURE dbo.sp_decrypt (procedure sysname = NULL) AS SET NOCOUNT ON BEGINDECLARE intProcSpace bigint, t bigint, maxColID sm
3、allint,intEncrypted tinyint,procNameLength int select maxColID = max(subobjid),intEncrypted = imageval FROM sys.sysobjvalues WHERE objid = object_id(procedure) GROUP BY imageval select procNameLength = datalength(procedure) + 29 DECLARE real_01 nvarchar(max) DECLARE fake_01 nvarchar(max) DECLARE fak
4、e_encrypt_01 nvarchar(max) DECLARE real_decrypt_01 nvarchar(max),real_decrypt_01a nvarchar(max) declare objtype varchar(2),ParentName nvarchar(max) select real_decrypt_01a = -提取对象的类型如是存储过程还是函数,如果是触发器,还要得到其父对象的名称 select objtype=type,parentname=object_name(parent_object_id) from sys.objects where obje
5、ct_id=object_id(procedure) - 从sys.sysobjvalues里提出加密的imageval记录 SET real_01=(SELECT top 1 imageval FROM sys.sysobjvalues WHERE objid = object_id(procedure) and valclass = 1 order by subobjid) -创建一个临时表 create table #output ( ident int IDENTITY (1, 1) NOT NULL , real_decrypt NVARCHAR(MAX) ) -开始一个事务,稍后回
6、滚 BEGIN TRAN -更改原始的存储过程,用短横线替换 if objtype=P SET fake_01=ALTER PROCEDURE + procedure + WITH ENCRYPTION AS +REPLICATE(-, 40003 - procNameLength) else if objtype=FN SET fake_01=ALTER FUNCTION + procedure +() RETURNS INT WITH ENCRYPTION AS BEGIN RETURN 1 /*+REPLICATE(*, datalength(real_01) /2 - procName
7、Length)+*/ END else if objtype=V SET fake_01=ALTER view + procedure + WITH ENCRYPTION AS select 1 as col /*+REPLICATE(*, datalength(real_01) /2 - procNameLength)+*/ else if objtype=TR SET fake_01=ALTER trigger + procedure + ON +parentname+WITH ENCRYPTION AFTER INSERT AS RAISERROR (N,16,10) /*+REPLIC
8、ATE(*, datalength(real_01) /2 - procNameLength)+*/ EXECUTE (fake_01) -从sys.sysobjvalues里提出加密的假的 SET fake_encrypt_01=(SELECT top 1 imageval FROM sys.sysobjvalues WHERE objid = object_id(procedure) and valclass = 1 order by subobjid ) if objtype=P SET fake_01=Create PROCEDURE + procedure + WITH ENCRYP
9、TION AS +REPLICATE(-, 40003 - procNameLength) else if objtype=FN SET fake_01=CREATE FUNCTION + procedure +() RETURNS INT WITH ENCRYPTION AS BEGIN RETURN 1 /*+REPLICATE(*, datalength(real_01) /2 - procNameLength)+*/ END else if objtype=V SET fake_01=Create view + procedure + WITH ENCRYPTION AS select
10、 1 as col /*+REPLICATE(*, datalength(real_01) /2 - procNameLength)+*/ else if objtype=TR SET fake_01=Create trigger + procedure + ON +parentname+WITH ENCRYPTION AFTER INSERT AS RAISERROR (N,16,10) /*+REPLICATE(*, datalength(real_01) /2 - procNameLength)+*/ -开始计数 SET intProcSpace=1 -使用字符填充临时变量 SET real_decrypt_01 = replicate(NA, (datalength(real_01) /2 ) -循环设置每一个变量,创建真正的变量 -每次一个字节 SET intProcSpace=1 -如有必要,遍历每个real_xx变量并解密 WHILE intProcSpace= 0 BEGIN SELECT BasePos = 1 SELECT CurrentPos = 1 SELECT TextLength = LEN(Sys