SQLSERVER2000存储过程解密函数

SQL代码
  1. /*--破解,过程,触发器,视图.仅限于SQLSERVER2000    
  2.      
  3.   --作者:J9988--   All   rights   reserved*/    
  4.      
  5.   /*--调用示例    
  6.      
  7.   --解密指定存储过程    
  8.      
  9.   exec   sp_decrypt   'AppSP_test'    
  10.      
  11.   --对所有的存储过程解密    
  12.      
  13.   declare   tb   cursor   for    
  14.      
  15.   select   name   from   sysobjects   where   xtype='P'   and   status>0   and   name<>'sp_decrypt'    
  16.      
  17.      
  18.   declare   @name   sysname    
  19.      
  20.   open   tb    
  21.      
  22.   fetch   next   from   tb   into   @name    
  23.      
  24.   while   @@fetch_status=0    
  25.      
  26.   begin    
  27.      
  28.   print   '-------存储过程   ['+@name+']   -----------'    
  29.      
  30.   exec   sp_decrypt   @name    
  31.      
  32.   fetch   next   from   tb   into   @name    
  33.      
  34.   end    
  35.      
  36.   close   tb    
  37.      
  38.   deallocate   tb    
  39.      
  40.   --*/    
  41.      
  42.      
  43.   if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[SP_DECRYPT]')   and   OBJECTPROPERTY(id,   N'IsProcedure')   =   1)    
  44.   drop   procedure   [dbo].[SP_DECRYPT]    
  45.      
  46.   GO    
  47.      
  48.   Create   PROCEDURE   sp_decrypt(@objectName   varchar(50))    
  49.   AS    
  50.   begin    
  51.      
  52.   set   nocount   on    
  53.      
  54.   --破解字节不受限制,适用于SQLSERVER2000存储过程,函数,视图,触发器    
  55.   --修正上一版视图触发器不能正确解密错误    
  56.   --发现有错,请E_MAIL:CSDNj9988@tom.com    
  57.   begin   tran    
  58.   declare   @objectname1   varchar(100),@orgvarbin   varbinary(8000)    
  59.   declare   @sql1   nvarchar(4000),@sql2   varchar(8000),@sql3   nvarchar(4000),@sql4   nvarchar(4000)    
  60.   DECLARE   @OrigSpText1   nvarchar(4000),   @OrigSpText2   nvarchar(4000)   ,   @OrigSpText3   nvarchar(4000),   @resultsp   nvarchar(4000)    
  61.      
  62.   declare   @i   int,@status   int,@type   varchar(10),@parentid   int    
  63.      
  64.   declare   @colid   int,@n   int,@q   int,@j   int,@k   int,@encrypted   int,@number   int    
  65.      
  66.   select   @type=xtype,@parentid=parent_obj   from   sysobjects   where   id=object_id(@ObjectName)    
  67.      
  68.   create   table   #temp(number   int,colid   int,ctext   varbinary(8000),encrypted   int,status   int)    
  69.      
  70.   insert   #temp   Select   number,colid,ctext,encrypted,status   FROM   syscomments   Where   id   =   object_id(@objectName)    
  71.      
  72.   select   @number=max(number)   from   #temp    
  73.      
  74.   set   @k=0    
  75.   while   @k<=@number      
  76.      
  77.   begin    
  78.      
  79.   if   exists(select   1   from   syscomments   where   id=object_id(@objectname)   and   number=@k)    
  80.      
  81.   begin    
  82.      
  83.   if   @type='P'    
  84.      
  85.   set   @sql1=(case   when   @number>1   then   'Alter   PROCEDURE   '+   @objectName   +';'+rtrim(@k)+'   WITH   ENCRYPTION   AS   '    
  86.      
  87.   else   'Alter   PROCEDURE   '+   @objectName+'   WITH   ENCRYPTION   AS   '    
  88.      
  89.   end)    
  90.      
  91.   if   @type='TR'    
  92.   begin    
  93.      
  94.   declare   @parent_obj   varchar(255),@tr_parent_xtype   varchar(10)    
  95.      
  96.   select   @parent_obj=parent_obj   from   sysobjects   where   id=object_id(@objectName)    
  97.      
  98.   select   @tr_parent_xtype=xtype   from   sysobjects   where   id=@parent_obj    
  99.      
  100.   if   @tr_parent_xtype='V'    
  101.      
  102.   begin    
  103.      
  104.   set   @sql1='Alter   TRIGGER   '+@objectname+'   ON   '+OBJECT_NAME(@parentid)+'   WITH   ENCRYPTION   INSTERD   OF   Insert   AS   PRINT   1   '    
  105.      
  106.   end    
  107.      
  108.   else    
  109.      
  110.   begin    
  111.      
  112.   set   @sql1='Alter   TRIGGER   '+@objectname+'   ON   '+OBJECT_NAME(@parentid)+'   WITH   ENCRYPTION   FOR   Insert   AS   PRINT   1   '    
  113.      
  114.   end    
  115.      
  116.        
  117.      
  118.   end    
  119.      
  120.   if   @type='FN'   or   @type='TF'   or   @type='IF'    
  121.      
  122.   set   @sql1=(case   @type   when   'TF'   then      
  123.      
  124.   'Alter   FUNCTION   '+   @objectName+'(@a   char(1))   returns   @b   table(a   varchar(10))   with   encryption   as   begin   insert   @b   select   @a   return   end   '    
  125.      
  126.   when   'FN'   then    
  127.      
  128.   'Alter   FUNCTION   '+   @objectName+'(@a   char(1))   returns   char(1)   with   encryption   as   begin   return   @a   end'    
  129.      
  130.   when   'IF'   then    
  131.      
  132.   'Alter   FUNCTION   '+   @objectName+'(@a   char(1))   returns   table   with   encryption   as   return   select   @a   as   a'    
  133.      
  134.   end)    
  135.      
  136.        
  137.      
  138.   if   @type='V'    
  139.      
  140.   set   @sql1='Alter   VIEW   '+@objectname+'   WITH   ENCRYPTION   AS   Select   1   as   f'    
  141.      
  142.        
  143.      
  144.   set   @q=len(@sql1)    
  145.      
  146.   set   @sql1=@sql1+REPLICATE('-',4000-@q)    
  147.      
  148.   select   @sql2=REPLICATE('-',8000)    
  149.      
  150.   set   @sql3='exec(@sql1'    
  151.      
  152.   select   @colid=max(colid)   from   #temp   where   number=@k      
  153.      
  154.   set   @n=1    
  155.      
  156.   while   @n<=CEILING(1.0*(@colid-1)/2)   and   len(@sQL3)<=3996    
  157.      
  158.   begin      
  159.      
  160.   set   @sql3=@sql3+'+@'    
  161.      
  162.   set   @n=@n+1    
  163.      
  164.   end    
  165.      
  166.   set   @sql3=@sql3+')'    
  167.      
  168.   exec   sp_executesql   @sql3,N'@sql1   nvarchar(4000),@   varchar(8000)',@sql1=@sql1,@=@sql2    
  169.      
  170.        
  171.      
  172.   end    
  173.      
  174.   set   @k=@k+1    
  175.      
  176.   end    
  177.      
  178.        
  179.      
  180.   set   @k=0    
  181.      
  182.   while   @k<=@number      
  183.      
  184.   begin    
  185.      
  186.        
  187.      
  188.   if   exists(select   1   from   syscomments   where   id=object_id(@objectname)   and   number=@k)    
  189.      
  190.   begin    
  191.      
  192.   select   @colid=max(colid)   from   #temp   where   number=@k      
  193.      
  194.   set   @n=1    
  195.      
  196.        
  197.      
  198.   while   @n<=@colid    
  199.      
  200.   begin    
  201.      
  202.   select   @OrigSpText1=ctext,@encrypted=encrypted,@status=status   FROM   #temp   Where   colid=@n   and   number=@k    
  203.      
  204.        
  205.      
  206.   SET   @OrigSpText3=(Select   ctext   FROM   syscomments   Where   id=object_id(@objectName)   and   colid=@n   and   number=@k)    
  207.      
  208.   if   @n=1    
  209.      
  210.   begin    
  211.      
  212.   if   @type='P'    
  213.      
  214.   SET   @OrigSpText2=(case   when   @number>1   then   'Create   PROCEDURE   '+   @objectName   +';'+rtrim(@k)+'   WITH   ENCRYPTION   AS   '    
  215.      
  216.   else   'Create   PROCEDURE   '+   @objectName   +'   WITH   ENCRYPTION   AS   '    
  217.      
  218.   end)    
  219.      
  220.        
  221.      
  222.   if   @type='FN'   or   @type='TF'   or   @type='IF'    
  223.      
  224.   SET   @OrigSpText2=(case   @type   when   'TF'   then      
  225.      
  226.   'Create   FUNCTION   '+   @objectName+'(@a   char(1))   returns   @b   table(a   varchar(10))   with   encryption   as   begin   insert   @b   select   @a   return   end   '    
  227.      
  228.   when   'FN'   then    
  229.      
  230.   'Create   FUNCTION   '+   @objectName+'(@a   char(1))   returns   char(1)   with   encryption   as   begin   return   @a   end'    
  231.      
  232.   when   'IF'   then    
  233.      
  234.   'Create   FUNCTION   '+   @objectName+'(@a   char(1))   returns   table   with   encryption   as   return   select   @a   as   a'    
  235.      
  236.   end)    
  237.      
  238.        
  239.      
  240.   if   @type='TR'      
  241.      
  242.   begin    
  243.      
  244.        
  245.      
  246.   if   @tr_parent_xtype='V'    
  247.      
  248.   begin    
  249.      
  250.   set   @OrigSpText2='Create   TRIGGER   '+@objectname+'   ON   '+OBJECT_NAME(@parentid)+'   WITH   ENCRYPTION   INSTEAD   OF   Insert   AS   PRINT   1   '    
  251.      
  252.   end    
  253.      
  254.   else    
  255.      
  256.   begin    
  257.      
  258.   set   @OrigSpText2='Create   TRIGGER   '+@objectname+'   ON   '+OBJECT_NAME(@parentid)+'   WITH   ENCRYPTION   FOR   Insert   AS   PRINT   1   '    
  259.      
  260.   end    
  261.      
  262.        
  263.      
  264.   end    
  265.      
  266.        
  267.      
  268.   if   @type='V'    
  269.      
  270.   set   @OrigSpText2='Create   VIEW   '+@objectname+'   WITH   ENCRYPTION   AS   Select   1   as   f'    
  271.      
  272.        
  273.      
  274.   set   @q=4000-len(@OrigSpText2)    
  275.      
  276.   set   @OrigSpText2=@OrigSpText2+REPLICATE('-',@q)    
  277.      
  278.   end    
  279.      
  280.   else    
  281.      
  282.   begin    
  283.      
  284.   SET   @OrigSpText2=REPLICATE('-',   4000)    
  285.      
  286.   end    
  287.      
  288.   SET   @i=1    
  289.      
  290.        
  291.      
  292.   SET   @resultsp   =   replicate(N'A',   (datalength(@OrigSpText1)   /   2))    
  293.      
  294.        
  295.      
  296.   WHILE   @i<=datalength(@OrigSpText1)/2    
  297.      
  298.   BEGIN    
  299.      
  300.        
  301.      
  302.   SET   @resultsp   =   stuff(@resultsp,   @i,   1,   NCHAR(UNICODE(substring(@OrigSpText1,   @i,   1))   ^    
  303.      
  304.   (UNICODE(substring(@OrigSpText2,   @i,   1))   ^    
  305.      
  306.   UNICODE(substring(@OrigSpText3,   @i,   1)))))    
  307.      
  308.   SET   @i=@i+1    
  309.      
  310.   END    
  311.      
  312.   set   @orgvarbin=cast(@OrigSpText1   as   varbinary(8000))    
  313.      
  314.   set   @resultsp=(case   when   @encrypted=1      
  315.      
  316.   then   @resultsp      
  317.      
  318.   else   convert(nvarchar(4000),case   when   @status&2=2   then   uncompress(@orgvarbin)   else   @orgvarbin   end)    
  319.      
  320.   end)    
  321.      
  322.   print   @resultsp    
  323.      
  324.        
  325.      
  326.   set   @n=@n+1    
  327.      
  328.   end    
  329.      
  330.        
  331.      
  332.   end    
  333.      
  334.   set   @k=@k+1    
  335.      
  336.   end    
  337.      
  338.        
  339.      
  340.   drop   table   #temp    
  341.      
  342.   rollback   tran    
  343.      
  344.   end    

调用方法: exec   sp_decrypt   'AppSP_test'   




[本日志由 biezhiyinan 于 2008-08-19 05:19 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.