SQL Server加密存储过程的破解过程

发布于:
分类: Script

  1. Exec sp_decrypt Objectname --这是方法    
  2. CREATE PROCEDURE sp_decrypt(@objectName varchar(50))    
  3. AS    
  4. begin    
  5. set nocount on    
  6. --CSDN:j9988 copyright:2004.04.15    
  7. --V3.1    
  8. --破解字节不受限制,适用于SQLSERVER2000存储过程,函数,视图,触发器    
  9. --修正上一版视图触发器不能正确解密错误    
  10. --发现有错,请E_MAIL:CSDNj9988@tom.com    
  11. begin tran    
  12. declare @objectname1 varchar(100),@orgvarbin varbinary(8000)    
  13. declare @sql1 nvarchar(4000),@sql2 varchar(8000),@sql3 nvarchar(4000),@sql4 nvarchar(4000)    
  14. DECLARE @OrigSpText1 nvarchar(4000), @OrigSpText2 nvarchar(4000) , @OrigSpText3 nvarchar(4000), @resultsp nvarchar(4000)    
  15. declare @i int,@status int,@type varchar(10),@parentid int    
  16. declare @colid int,@n int,@q int,@j int,@k int,@encrypted int,@number int    
  17. select @type=xtype,@parentid=parent_obj from sysobjects where id=object_id(@ObjectName)    
  18. create table #temp(number int,colid int,ctext varbinary(8000),encrypted int,status int)    
  19. insert #temp SELECT number,colid,ctext,encrypted,status FROM syscomments WHERE id = object_id(@objectName)    
  20. select @number=max(number) from #temp    
  21. set @k=0    
  22. while @k<=@number    
  23. begin    
  24. if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k)    
  25. begin    
  26. if @type='P'    
  27. set @sql1=(case when @number>1 then 'ALTER PROCEDURE '+ @objectName +';'+rtrim(@k)+' WITH ENCRYPTION AS '    
  28. else 'ALTER PROCEDURE '+ @objectName+' WITH ENCRYPTION AS '    
  29. end)    
  30. if @type='TR'    
  31. begin    
  32. declare @parent_obj varchar(255),@tr_parent_xtype varchar(10)    
  33. select @parent_obj=parent_obj from sysobjects where id=object_id(@objectName)    
  34. select @tr_parent_xtype=xtype from sysobjects where id=@parent_obj    
  35. if @tr_parent_xtype='V'    
  36. begin    
  37. set @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTERD OF INSERT AS PRINT 1 '    
  38. end    
  39. else    
  40. begin    
  41. set @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '    
  42. end    
  43. end    
  44. if @type='FN' or @type='TF' or @type='IF'    
  45. set @sql1=(case @type when 'TF' then    
  46. 'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '    
  47. when 'FN' then    
  48. 'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns char(1) with encryption as begin return @a end'    
  49. when 'IF' then    
  50. 'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns table with encryption as return select @a as a'    
  51. end)    
  52. if @type='V'    
  53. set @sql1='ALTER VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f'    
  54. set @q=len(@sql1)    
  55. set @sql1=@sql1+REPLICATE('-',4000-@q)    
  56. select @sql2=REPLICATE('-',8000)    
  57. set @sql3='exec(@sql1'    
  58. select @colid=max(colid) from #temp where number=@k    
  59. set @n=1    
  60. while @n<=CEILING(1.0*(@colid-1)/2) and len(@sQL3)<=3996    
  61. begin    
  62. set @sql3=@sql3+'+@'    
  63. set @n=@n+1    
  64. end    
  65. set @sql3=@sql3+')'    
  66. exec sp_executesql @sql3,N'@Sql1 nvarchar(4000),@ varchar(8000)',@sql1=@sql1,@=@sql2    
  67. end    
  68. set @k=@k+1    
  69. end    
  70. set @k=0    
  71. while @k<=@number    
  72. begin    
  73. if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k)    
  74. begin    
  75. select @colid=max(colid) from #temp where number=@k    
  76. set @n=1    
  77. while @n<=@colid    
  78. begin    
  79. select @OrigSpText1=ctext,@encrypted=encrypted,@status=status FROM #temp WHERE colid=@n and number=@k    
  80. SET @OrigSpText3=(SELECT ctext FROM syscomments WHERE id=object_id(@objectName) and colid=@n and number=@k)    
  81. if @n=1    
  82. begin    
  83. if @type='P'    
  84. SET @OrigSpText2=(case when @number>1 then 'CREATE PROCEDURE '+ @objectName +';'+rtrim(@k)+' WITH ENCRYPTION AS '    
  85. else 'CREATE PROCEDURE '+ @objectName +' WITH ENCRYPTION AS '    
  86. end)    
  87. if @type='FN' or @type='TF' or @type='IF'    
  88. SET @OrigSpText2=(case @type when 'TF' then    
  89. 'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '    
  90. when 'FN' then    
  91. 'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns char(1) with encryption as begin return @a end'    
  92. when 'IF' then    
  93. 'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns table with encryption as return select @a as a'    
  94. end)    
  95. if @type='TR'    
  96. begin    
  97. if @tr_parent_xtype='V'    
  98. begin    
  99. set @OrigSpText2='CREATE TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTEAD OF INSERT AS PRINT 1 '    
  100. end    
  101. else    
  102. begin    
  103. set @OrigSpText2='CREATE TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '    
  104. end    
  105. end    
  106. if @type='V'    
  107. set @OrigSpText2='CREATE VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f'    
  108. set @q=4000-len(@OrigSpText2)    
  109. set @OrigSpText2=@OrigSpText2+REPLICATE('-',@q)    
  110. end    
  111. else    
  112. begin    
  113. SET @OrigSpText2=REPLICATE('-', 4000)    
  114. end    
  115. SET @i=1    
  116. SET @resultsp = replicate(N'A', (datalength(@OrigSpText1) / 2))    
  117. WHILE @i<=datalength(@OrigSpText1)/2    
  118. BEGIN    
  119. SET @resultsp = stuff(@resultsp, @i, 1, NCHAR(UNICODE(substring(@OrigSpText1, @i, 1)) ^    
  120. (UNICODE(substring(@OrigSpText2, @i, 1)) ^    
  121. UNICODE(substring(@OrigSpText3, @i, 1)))))    
  122. SET @i=@i+1    
  123. END    
  124. set @orgvarbin=cast(@OrigSpText1 as varbinary(8000))    
  125. set @resultsp=(case when @encrypted=1    
  126. then @resultsp    
  127. else convert(nvarchar(4000),case when @status&2=2 then uncompress(@orgvarbin) else @orgvarbin end)    
  128. end)    
  129. print @resultsp    
  130. set @n=@n+1    
  131. end    
  132. end    
  133. set @k=@k+1    
  134. end    
  135. drop table #temp    
  136. rollback tran    
  137. end   

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注