欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

存储过程解密(破解函数,过程,触发器,视图.仅限于SQLSERVER2000)

程序员文章站 2023-08-14 09:31:34
复制代码 代码如下:create procedure sp_decrypt(@objectname varchar(50)) as begin begin tran dec...
复制代码 代码如下:

create procedure sp_decrypt(@objectname varchar(50))
as
begin
begin tran
declare @objectname1 varchar(100),@orgvarbin varbinary(8000)
declare @sql1 nvarchar(4000),@sql2 nvarchar(4000),@sql3 nvarchar(4000),@sql4 nvarchar(4000),@sql5 nvarchar(4000),@sql6 nvarchar(4000),@sql7 nvarchar(4000),@sql8 nvarchar(4000),@sql9 nvarchar(4000),@sql10 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 '
set @q=len(@sql1)
set @sql1=@sql1+replicate('-',4000-@q)
select @sql2=replicate('-',4000),@sql3=replicate('-',4000),@sql4=replicate('-',4000),@sql5=replicate('-',4000),@sql6=replicate('-',4000),@sql7=replicate('-',4000),@sql8=replicate('-',4000),@sql9=replicate('-',4000),@sql10=replicate('-',4000)
exec(@sql1+@sql2+@sql3+@sql4+@sql5+@sql6+@sql7+@sql8+@sql9+@sql10)
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 '
set @q=4000-len(@origsptext2)
set @origsptext2=@origsptext2+replicate('-',@q)
end
else
begin
set @origsptext2=replicate('-', 4000)
end
--start counter
set @i=1
--fill temporary variable
set @resultsp = replicate(n'a', (datalength(@origsptext1) / 2))
--loop
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
--execute( @resultsp)
set @n=@n+1
end
end
set @k=@k+1
end
drop table #temp
rollback tran
end
go