ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 查看MSSQL2000 MSSQL2005加密存储过程的内容 (转)

查看MSSQL2000 MSSQL2005加密存储过程的内容 (转)

原创 Linux操作系统 作者:dla001 时间:2011-05-11 14:35:21 0 删除 编辑
 

公司有个开发的人走了,还没有交接,结果要改动他写的过程。这哥们很强的,所有的过程全是加密写的,一大片,真是汗啊。

要记得开启SQL Server Browser

先找到2000的。

if exists (select * from sysobjects where objectproperty(object_id('sp_decrypt'), 'isprocedure')=1)
drop procedure sp_decrypt
go
create procedure sp_decrypt(@objectname varchar(50))
as
begin
set nocount on
--csdn:j9988 copyright:2004.01.05
--v3.1
--破解字节不受限制,适用于sqlserver2000存储过程,函数,视图,触发器
--发现有错,请e_mail:csdnj9988@tom.com
begin tran
declare @objectname1 varchar(100),@orgvarbin varbinary(8000)
declare @sql1 nvarchar(4000),@sql2 varchar(8000),@sql3 nvarchar(4000),@sql4 nvarchar(4000)
declare @origsptext1 nvarchar(4000), @origsptext2 nvarchar(4000) , @origsptext3 nvarchar(4000), @resultsp nvarchar(4000)
declare @i int,@status int,@type varchar(10),@parentid int
declare @colid int,@n int,@q int,@j int,@k int,@encrypted int,@number int
select @type=xtype,@parentid=parent_obj from sysobjects where id=object_id(@objectname)
create table #temp(number int,colid int,ctext varbinary(8000),encrypted int,status int)
insert #temp select number,colid,ctext,encrypted,status from syscomments where id = object_id(@objectname)
select @number=max(number) from #temp
set @k=0
while @k<=@number
begin
if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k)
begin
if @type='p'
set @sql1=(case when @number>1 then 'alter procedure '+ @objectname +';'+rtrim(@k)+' with encryption as '
else 'alter procedure '+ @objectname+' with encryption as '
end)
if @type='tr'
set @sql1='alter trigger '+@objectname+' on '+object_name(@parentid)+' with encryption for insert as print 1 '
if @type='fn' or @type='tf' or @type='if'
set @sql1=(case @type when 'tf' then
'alter function '+ @objectname+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '
when 'fn' then
'alter function '+ @objectname+'(@a char(1)) returns char(1) with encryption as begin return @a end'
when 'if' then
'alter function '+ @objectname+'(@a char(1)) returns table with encryption as return select @a as a'
end)
if @type='v'
set @sql1='alter view '+@objectname+' with encryption as select 1 as f'
set @q=len(@sql1)
set @sql1=@sql1+replicate('-',4000-@q)
select @sql2=replicate('-',8000)
set @sql3='exec(@sql1'
select @colid=max(colid) from #temp where number=@k
set @n=1
while @n<=ceiling(1.0*(@colid-1)/2) and len(@sql3)<=3996
begin
set @sql3=@sql3+'+@'
set @n=@n+1
end
set @sql3=@sql3+')'
exec sp_executesql @sql3,N'@sql1 nvarchar(4000),@ varchar(8000)',@sql1=@sql1,@=@sql2
end
set @k=@k+1
end
set @k=0
while @k<=@number
begin
if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k)
begin
select @colid=max(colid) from #temp where number=@k
set @n=1
while @n<=@colid
begin
select @origsptext1=ctext,@encrypted=encrypted,@status=status from #temp where colid=@n and number=@k
set @origsptext3=(select ctext from syscomments where id=object_id(@objectname) and colid=@n and number=@k)
if @n=1
begin
if @type='p'
set @origsptext2=(case when @number>1 then 'create procedure '+ @objectname +';'+rtrim(@k)+' with encryption as '
else 'create procedure '+ @objectname +' with encryption as '
end)
if @type='fn' or @type='tf' or @type='if'--刚才有错改一下
set @origsptext2=(case @type when 'tf' then
'create function '+ @objectname+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '
when 'fn' then
'create function '+ @objectname+'(@a char(1)) returns char(1) with encryption as begin return @a end'
when 'if' then
'create function '+ @objectname+'(@a char(1)) returns table with encryption as return select @a as a'
end)
if @type='tr'
set @origsptext2='create trigger '+@objectname+' on '+object_name(@parentid)+' with encryption for insert as print 1 '
if @type='v'
set @origsptext2='create view '+@objectname+' with encryption as select 1 as f'
set @q=4000-len(@origsptext2)
set @origsptext2=@origsptext2+replicate('-',@q)
end
else
begin
set @origsptext2=replicate('-', 4000)
end
set @i=1
set @resultsp = replicate(N'a', (datalength(@origsptext1) / 2))
while @i<=datalength(@origsptext1)/2
begin
set @resultsp = stuff(@resultsp, @i, 1, nchar(unicode(substring(@origsptext1, @i, 1)) ^
(unicode(substring(@origsptext2, @i, 1)) ^
unicode(substring(@origsptext3, @i, 1)))))
set @i=@i+1
end
set @orgvarbin=cast(@origsptext1 as varbinary(8000))
set @resultsp=(case when @encrypted=1
then @resultsp
else convert(nvarchar(4000),case when @status&2=2 then uncompress(@orgvarbin) else @orgvarbin end)
end)
print @resultsp
set @n=@n+1
end
end
set @k=@k+1
end
drop table #temp
rollback tran
end

 

sp_decrypt  'baoguan.usp_inputicq_m'

 


===============================================

 

以下适用于MSSQL2005


Create PROCEDURE [dbo].[sp_windbi$decrypt]
(@procedure sysname = NULL, @revfl int = 1)
AS
/**//*
王成辉翻译整理,转贴请注明出自微软BI开拓者www.windbi.com
调用形式为:
exec dbo.sp__windbi$decrypt @procedure,0
如果第二个参数使用1的话,会给出该存储过程的一些提示。
--版本4.0  修正存储过程过长解密出来是空白的问题
*/
SET NOCOUNT ON
IF @revfl = 1
BEGIN
PRINT '警告:该存储过程会删除并重建原始的存储过程。'
PRINT ' 在运行该存储过程之前确保你的数据库有一个备份。'
PRINT ' 该存储过程通常应该运行在产品环境的一个备份的非产品环境下。'
PRINT ' 为了运行这个存储过程,将参数@refl的值更改为0。'
RETURN 0
END
DECLARE @intProcSpace bigint, @t bigint, @maxColID smallint,@procNameLength int
select @maxColID = max(subobjid) FROM
sys.sysobjvalues WHERE bjid = object_id(@procedure)
--select @maxColID as 'Rows in sys.sysobjvalues'
select @procNameLength = datalength(@procedure) + 29
DECLARE @real_01 nvarchar(max)
DECLARE @fake_01 nvarchar(max)
DECLARE @fake_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 [object_id]=object_id(@procedure)
-- 从sys.sysobjvalues里提出加密的imageval记录
SET @real_01=(SELECT top 1 imageval FROM sys.sysobjvalues WHERE bjid =
object_id(@procedure) and valclass = 1 order by subobjid)

--创建一个临时表
create table #output ( [ident] [int] IDENTITY (1, 1) NOT NULL ,
[real_decrypt] NVARCHAR(MAX) )
--开始一个事务,稍后回滚
BEGIN TRAN
--更改原始的存储过程,用短横线替换
if @objtype='P'
  SET @fake_01='ALTER PROCEDURE '+ @procedure +' WITH ENCRYPTION AS select 1
  /**//*'+REPLICATE(cast('*'as nvarchar(max)), datalength(@real_01) /2 - @procNameLength)+'*/'
else if @objtype='FN'
  SET @fake_01='ALTER FUNCTION '+ @procedure +'() RETURNS INT WITH ENCRYPTION AS BEGIN RETURN 1
  /**//*'+REPLICATE(cast('*'as nvarchar(max)), datalength(@real_01) /2 - @procNameLength)+'*/ END'
else if @objtype='V'
  SET @fake_01='ALTER view '+ @procedure +' WITH ENCRYPTION AS select 1 as col
  /**//*'+REPLICATE(cast('*'as nvarchar(max)), 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)
  /**//*'+REPLICATE(cast('*'as nvarchar(max)), datalength(@real_01) /2 - @procNameLength)+'*/'
EXECUTE (@fake_01)
--从sys.sysobjvalues里提出加密的假的
SET @fake_encrypt_01=(SELECT top 1 imageval FROM sys.sysobjvalues WHERE bjid =
object_id(@procedure) and valclass = 1 order by subobjid )
if @objtype='P'
  SET @fake_01='Create PROCEDURE '+ @procedure +' WITH ENCRYPTION AS select 1
  /**//*'+REPLICATE(cast('*'as nvarchar(max)), datalength(@real_01) /2 - @procNameLength)+'*/'
else if @objtype='FN'
  SET @fake_01='CREATE FUNCTION '+ @procedure +'() RETURNS INT WITH ENCRYPTION AS BEGIN RETURN 1
  /**//*'+REPLICATE(cast('*'as nvarchar(max)), datalength(@real_01) /2 - @procNameLength)+'*/ END'
else if @objtype='V'
  SET @fake_01='Create view '+ @procedure +' WITH ENCRYPTION AS select 1 as col
  /**//*'+REPLICATE(cast('*'as nvarchar(max)), 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(cast('*'as nvarchar(max)), datalength(@real_01) /2 - @procNameLength)+'*/'
--开始计数
SET @intProcSpace=1
--使用字符填充临时变量
SET @real_decrypt_01 = replicate(cast('A'as nvarchar(max)), (datalength(@real_01) /2 ))
--循环设置每一个变量,创建真正的变量
--每次一个字节
SET @intProcSpace=1
--如有必要,遍历每个@real_xx变量并解密
WHILE @intProcSpace<=(datalength(@real_01)/2)
BEGIN
--真的和假的和加密的假的进行异或处理
SET @real_decrypt_01 = stuff(@real_decrypt_01, @intProcSpace, 1,
NCHAR(UNICODE(substring(@real_01, @intProcSpace, 1)) ^
(UNICODE(substring(@fake_01, @intProcSpace, 1)) ^
UNICODE(substring(@fake_encrypt_01, @intProcSpace, 1)))))
SET @intProcSpace=@intProcSpace+1
END


--通过sp_helptext逻辑向表#output里插入变量
insert #output (real_decrypt) select @real_decrypt_01
--select real_decrypt AS '#output chek' from #output --测试
-- -------------------------------------
--开始从sp_helptext提取
-- -------------------------------------
declare @dbname sysname
,@BlankSpaceAdded int
,@BasePos int
,@CurrentPos int
,@TextLength int
,@LineId int
,@AddOnLen int
,@LFCR int --回车换行的长度
,@DefinedLength int
,@SyscomText nvarchar(max)
,@Line nvarchar(255)
Select @DefinedLength = 255
SELECT @BlankSpaceAdded = 0 --跟踪行结束的空格。注意Len函数忽略了多余的空格
CREATE TABLE #CommentText
(LineId int
,Text nvarchar(255) collate database_default)
--使用#output代替sys.sysobjvalues
DECLARE ms_crs_syscom CURSOR LOCAL
FOR SELECT real_decrypt from #output
ORDER BY ident
FOR READ ONLY
--获取文本
SELECT @LFCR = 2
SELECT @LineId = 1
OPEN ms_crs_syscom
FETCH NEXT FROM ms_crs_syscom into @SyscomText
WHILE @@fetch_status >= 0
BEGIN
SELECT @BasePos = 1
SELECT @CurrentPos = 1
SELECT @TextLength = LEN(@SyscomText)
WHILE @CurrentPos != 0
BEGIN
--通过回车查找行的结束
SELECT @CurrentPos = CHARINDEX(char(13)+char(10), @SyscomText,
@BasePos)
--如果找到回车
IF @CurrentPos != 0
BEGIN
--如果@Lines的长度的新值比设置的大就插入@Lines目前的内容并继续
While (isnull(LEN(@Line),0) + @BlankSpaceAdded +
@CurrentPos-@BasePos + @LFCR) > @DefinedLength
BEGIN
SELECT @AddOnLen = @DefinedLength-(isnull(LEN(@Line),0) +
@BlankSpaceAdded)
INSERT #CommentText VALUES
( @LineId,
isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText,
@BasePos, @AddOnLen), N''))
SELECT @Line = NULL, @LineId = @LineId + 1,
@BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
END
SELECT @Line = isnull(@Line, N'') +
isnull(SUBSTRING(@SyscomText, @BasePos, @CurrentPos-@BasePos + @LFCR), N'')
SELECT @BasePos = @CurrentPos+2
INSERT #CommentText VALUES( @LineId, @Line )
SELECT @LineId = @LineId + 1
SELECT @Line = NULL
END
ELSE
--如果回车没找到
BEGIN
IF @BasePos <= @TextLength
BEGIN
--如果@Lines长度的新值大于定义的长度
While (isnull(LEN(@Line),0) + @BlankSpaceAdded +
@TextLength-@BasePos+1 ) > @DefinedLength
BEGIN
SELECT @AddOnLen = @DefinedLength -
(isnull(LEN(@Line),0) + @BlankSpaceAdded)
INSERT #CommentText VALUES
( @LineId,
isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText,
@BasePos, @AddOnLen), N''))
SELECT @Line = NULL, @LineId = @LineId + 1,
@BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded =
0
END
SELECT @Line = isnull(@Line, N'') +
isnull(SUBSTRING(@SyscomText, @BasePos, @TextLength-@BasePos+1 ), N'')
if LEN(@Line) < @DefinedLength and charindex(' ',
@SyscomText, @TextLength+1 ) > 0
BEGIN
SELECT @Line = @Line + ' ', @BlankSpaceAdded = 1
END
END
END
END
FETCH NEXT FROM ms_crs_syscom into @SyscomText
END
IF @Line is NOT NULL
INSERT #CommentText VALUES( @LineId, @Line )
select Text from #CommentText order by LineId
CLOSE ms_crs_syscom
DEALLOCATE ms_crs_syscom
DROP TABLE #CommentText
-- -------------------------------------
--结束从sp_helptext提取
-- -------------------------------------
--删除用短横线创建的存储过程并重建原始的存储过程
ROLLBACK TRAN
DROP TABLE #output
go


 

执行上面的存储过程方法为:
exec sp_windbi$Decrypt '你要解密的存储过程名',0

在执行上面的语句后出现错误:
对象名'sys.sysobjvalues'无效

主要原因是我们没有用DAC模式登陆,下面是解决办法:
    第一步:允许DAC模式
             (1)如果是在本地服务器做DAC的话用:sqlcmd -A (A一定要大写,要不然不好用哦) 用这个命令就可以很轻松的连接上,连接上以后在里面写语句就可以,语句写玩了要运行GO这个命令。
             (2)如果要做远程连接DAC的话需要用: sqlcmd -S 服务器名 -U sa(一个是sysadmin角色的登陆帐户就可以了)-P(密码)-A就可以了

    启用远程DAC登录选项:
             操作步骤:'程序'->'Sql Server2005'->  '配置工具'-> 'Sql Server  外围应用配置器'-> '功能的外围应用配置器'-> 'DataBase Engine'->  'DAC' -> '启用远程DAC'
    这样DAC模式就启用成功了,接下来是使用DAC模式登陆。
  
    第二步:使用DAC模式登陆(关键就在这个地方)
             1,DAC只支持一个窗口
             2,DAC只支持查询窗口, 不支持Object Explorer. 如果你想在SQL Server Management Studio中用DAC连, 你应该用个正常的连接, 然后用DAC打开查询窗口.
          
    下面是操作步骤:

更正: 使用DAC连接而且还要使用SQL Server Management Studio的话,执行以下方法。
1.打开一个SQL Server Management Studio工具,不连接到任何数据库,选择新建查询,在你的数据库名(IP)前加上admin:
2.输入sysadmin权限的账号,密码。
3.这时就打开了一个查询窗口,这个窗口是唯一的,不能开两个。!!!!!
4.最后执行上面的存储过程方法;

以上是解决办法,希望对大家有用,上面转载的有些不知道出处,如果侵犯了您的权益请及时联系,我将在第一时间更新...

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12807983/viewspace-695001/,如需转载,请注明出处,否则将追究法律责任。

下一篇: TNSPING 非常慢
请登录后发表评论 登录
全部评论

注册时间:2011-04-28

  • 博文量
    40
  • 访问量
    50197