设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 876|回复: 1

Decrypt stored procedure in SQL Server 2008

[复制链接]
发表于 2013-6-21 14:55:25 | 显示全部楼层 |阅读模式
Solution 1:
Just download Optillect's SQL Decryptor freeware
A modern tool that decrypts T-SQL procedures, functions, triggers, and views on any SQL Server. It gives you a couple of options. You can see the code of the sp if you just wanted to see it or you can have the sp decrypted on the server. The app just needs to connect to the sql server.

Solution 2:
try this
  1. create  PROCEDURE sp_decrypt_sp (@objectName varchar(50))
  2. AS



  3. DECLARE  @OrigSpText1 nvarchar(4000),  @OrigSpText2 nvarchar(4000) , @OrigSpText3 nvarchar(4000), @resultsp nvarchar(4000)
  4. declare  @i int , @t bigint , @ct nvarchar(max)

  5. --get encrypted data
  6. SET @OrigSpText1= (SELECT top 1 ctext FROM syscomments  WHERE id = object_id(@objectName) order by colid)
  7. SET @OrigSpText2='ALTER PROCEDURE '+ @objectName +' WITH ENCRYPTION AS '+REPLICATE('-', 3938)
  8.   EXECUTE (@OrigSpText2)
  9. print @OrigSpText1

  10. SET @OrigSpText3=(SELECT top 1 ctext FROM syscomments  WHERE id = object_id(@objectName) order by colid)
  11. SET @OrigSpText2='CREATE PROCEDURE '+ @objectName +' WITH ENCRYPTION AS '+REPLICATE('-', 4000-62)

  12. --start counter
  13. SET @i=1
  14. --fill temporary variable
  15. SET @resultsp = replicate(N'A', (datalength(@OrigSpText1) / 2))

  16. --loop
  17. WHILE @i<=datalength(@OrigSpText1)/2
  18. BEGIN
  19.   --reverse encryption (XOR original+bogus+bogus encrypted)
  20. SET @resultsp = stuff(@resultsp, @i, 1, NCHAR(UNICODE(substring(@OrigSpText1, @i, 1)) ^
  21.                                  (UNICODE(substring(@OrigSpText2, @i, 1)) ^
  22.                                  UNICODE(substring(@OrigSpText3, @i, 1)))))
  23. print @resultsp
  24.   SET @i=@i+1
  25. END
  26. --drop original SP
  27. --EXECUTE ('drop PROCEDURE '+ @objectName)
  28. --remove encryption
  29. --preserve case
  30. SET @resultsp=REPLACE((@resultsp),'WITH ENCRYPTION', '')
  31. SET @resultsp=REPLACE((@resultsp),'With Encryption', '')
  32. SET @resultsp=REPLACE((@resultsp),'with encryption', '')
  33. IF CHARINDEX('WITH ENCRYPTION',UPPER(@resultsp) )>0
  34.    SET @resultsp=REPLACE(UPPER(@resultsp),'WITH ENCRYPTION', '')
  35. --replace Stored procedure without enryption
  36. set @ct = (SELECT ctext FROM syscomments WHERE id = object_id(@objectName))
  37. print @ct
  38. execute( @resultsp)
  39. GO
复制代码
after that
bass your object to this proc


Solution 3:
Try this link, it gives a step by step description.
http://sqljunkieshare.com/2012/03/07/decrypting-encrypted-stored-procedures-views-functions-in-sql-server-20052008-r2/

http://www.codeproject.com/Quest ... edure-in-Sql-Server
 楼主| 发表于 2013-6-24 15:06:48 | 显示全部楼层
本帖最后由 demo 于 2013-6-25 07:07 编辑
  1. CREATE PROCEDURE [dbo].[sp__procedure]
  2. (@procedure sysname = NULL)
  3. AS
  4. SET NOCOUNT ON

  5. DECLARE @intProcSpace bigint, @t bigint, @maxColID smallint,@intEncrypted
  6. tinyint,@procNameLength int
  7. select @maxColID = max(subobjid)
  8. --,@intEncrypted = encrypted
  9. FROM sys.sysobjvalues WHERE objid = object_id(@procedure)
  10. --GROUP BY encrypted

  11. --select @maxColID as ‘Rows in sys.sysobjvalues’
  12. select @procNameLength = datalength(@procedure) + 29

  13. DECLARE @real_01 nvarchar(max)

  14. DECLARE @fake_01 nvarchar(max)

  15. DECLARE @fake_encrypt_01 nvarchar(max)

  16. DECLARE @real_decrypt_01 nvarchar(max),@real_decrypt_01a nvarchar(max)

  17. select @real_decrypt_01a = ''

  18. -- extract the encrypted imageval rows from sys.sysobjvalues
  19. SET @real_01=(SELECT imageval FROM sys.sysobjvalues WHERE objid =
  20. object_id(@procedure) and valclass = 1 and subobjid = 1 )

  21. -- create this table for later use
  22. create table #output ( [ident] [int] IDENTITY (1, 1) NOT NULL ,
  23. [real_decrypt] NVARCHAR(MAX) )

  24. -- We’ll begin the transaction and roll it back later
  25. BEGIN TRAN
  26. -- alter the original procedure, replacing with dashes
  27. SET @fake_01='ALTER PROCEDURE '+ @procedure +' WITH ENCRYPTION AS
  28. '+REPLICATE('-', 40003 - @procNameLength)

  29. EXECUTE (@fake_01)

  30. --– extract the encrypted fake imageval rows from sys.sysobjvalues
  31. SET @fake_encrypt_01=(SELECT imageval FROM sys.sysobjvalues WHERE objid =
  32. object_id(@procedure) and valclass = 1 and subobjid = 1)

  33. SET @fake_01='CREATE PROCEDURE '+ @procedure +' WITH ENCRYPTION AS
  34. '+REPLICATE('-', 40003 - @procNameLength)
  35. --–start counter
  36. SET @intProcSpace=1
  37. --–fill temporary variable with with a filler character
  38. SET @real_decrypt_01 = replicate(N'A', (datalength(@real_01) /2 ))

  39. --–loop through each of the variables sets of variables, building the real variable
  40. --–one byte at a time.
  41. SET @intProcSpace=1

  42. --– Go through each @real_xx variable and decrypt it, as necessary
  43. WHILE @intProcSpace<=(datalength(@real_01)/2)
  44. BEGIN
  45. --–xor real & fake & fake encrypted
  46. SET @real_decrypt_01 = stuff(@real_decrypt_01, @intProcSpace, 1,
  47. NCHAR(UNICODE(substring(@real_01, @intProcSpace, 1)) ^
  48. (UNICODE(substring(@fake_01, @intProcSpace, 1)) ^
  49. UNICODE(substring(@fake_encrypt_01, @intProcSpace, 1)))))
  50. SET @intProcSpace=@intProcSpace+1
  51. END

  52. --– Load the variables into #output for handling by sp_helptext logic

  53. insert #output (real_decrypt) select @real_decrypt_01
  54. --– select real_decrypt AS ‘#output chek’ from #output — Testing

  55. --– ————————————-
  56. --– Beginning of extract from sp_helptext
  57. --– ————————————-
  58. declare @dbname sysname
  59. ,@BlankSpaceAdded int
  60. ,@BasePos int
  61. ,@CurrentPos int
  62. ,@TextLength int
  63. ,@LineId int
  64. ,@AddOnLen int
  65. ,@LFCR int --–lengths of line feed carriage return
  66. ,@DefinedLength int
  67. ,@SyscomText nvarchar(4000)
  68. ,@Line nvarchar(255)

  69. Select @DefinedLength = 255
  70. SELECT @BlankSpaceAdded = 0
  71. --–Keeps track of blank spaces at end of lines. Note Len function ignores trailing blank spaces
  72. CREATE TABLE #CommentText
  73. (LineId int
  74. ,Text nvarchar(255) collate database_default)

  75. --– use #output instead of sys.sysobjvalues
  76. DECLARE ms_crs_syscom CURSOR LOCAL
  77. FOR SELECT real_decrypt from #output
  78. ORDER BY ident
  79. FOR READ ONLY

  80. --– Else get the text.

  81. SELECT @LFCR = 2
  82. SELECT @LineId = 1

  83. OPEN ms_crs_syscom

  84. FETCH NEXT FROM ms_crs_syscom into @SyscomText

  85. WHILE @@fetch_status >= 0
  86. BEGIN

  87.         SELECT @BasePos = 1
  88.         SELECT @CurrentPos = 1
  89.         SELECT @TextLength = LEN(@SyscomText)

  90.         WHILE @CurrentPos != 0
  91.         BEGIN
  92.                 --–Looking for end of line followed by carriage return
  93.                 SELECT @CurrentPos = CHARINDEX(char(13)+char(10), @SyscomText,
  94.                 @BasePos)

  95.                 --–If carriage return found
  96.                 IF @CurrentPos != 0
  97.                         BEGIN
  98.                                 --–If new value for @Lines length will be > then the
  99.                                 --–set length then insert current contents of @line
  100.                                 --–and proceed.

  101.                                 While (isnull(LEN(@Line),0) + @BlankSpaceAdded +
  102.                                 @CurrentPos-@BasePos + @LFCR) > @DefinedLength
  103.                                         BEGIN
  104.                                                 SELECT @AddOnLen = @DefinedLength-(isnull(LEN(@Line),0) +
  105.                                                 @BlankSpaceAdded)
  106.                                                 INSERT #CommentText VALUES
  107.                                                 ( @LineId,
  108.                                                 isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText,
  109.                                                 @BasePos, @AddOnLen), N''))
  110.                                                 SELECT @Line = NULL, @LineId = @LineId + 1,
  111.                                                 @BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
  112.                                         END
  113.                                 SELECT @Line = isnull(@Line, N'') +
  114.                                 isnull(SUBSTRING(@SyscomText, @BasePos, @CurrentPos-@BasePos + @LFCR), N'')
  115.                                 SELECT @BasePos = @CurrentPos+2
  116.                                 INSERT #CommentText VALUES( @LineId, @Line )
  117.                                 SELECT @LineId = @LineId + 1
  118.                                 SELECT @Line = NULL
  119.                         END
  120.                 ELSE
  121.                         --–else carriage return not found
  122.                         BEGIN
  123.                                 IF @BasePos <= @TextLength
  124.                                 BEGIN
  125.                                         --–If new value for @Lines length will be > then the
  126.                                         --–defined length
  127.                                         --–
  128.                                         While (isnull(LEN(@Line),0) + @BlankSpaceAdded +
  129.                                         @TextLength-@BasePos+1 ) > @DefinedLength
  130.                                         BEGIN
  131.                                                 SELECT @AddOnLen = @DefinedLength -
  132.                                                 (isnull(LEN(@Line),0) + @BlankSpaceAdded)
  133.                                                 INSERT #CommentText VALUES
  134.                                                 ( @LineId,
  135.                                                 isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText,
  136.                                                 @BasePos, @AddOnLen), N''))
  137.                                                 SELECT @Line = NULL, @LineId = @LineId + 1,
  138.                                                 @BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
  139.                                         END
  140.                                         SELECT @Line = isnull(@Line, N'') +
  141.                                         isnull(SUBSTRING(@SyscomText, @BasePos, @TextLength-@BasePos+1 ), N'')
  142.                                         if LEN(@Line) < @DefinedLength and charindex(' ',
  143.                                         @SyscomText, @TextLength+1 ) > 0
  144.                                         BEGIN
  145.                                                 SELECT @Line = @Line + ' ', @BlankSpaceAdded = 1
  146.                                         END
  147.                                 END
  148.                         END
  149.         END

  150.         FETCH NEXT FROM ms_crs_syscom into @SyscomText
  151. END

  152. IF @Line is NOT NULL
  153. INSERT #CommentText VALUES( @LineId, @Line )

  154. select Text from #CommentText order by LineId

  155. CLOSE ms_crs_syscom
  156. DEALLOCATE ms_crs_syscom

  157. DROP TABLE #CommentText

  158. --– ————————————-
  159. --– End of extract from sp_helptext
  160. --– ————————————-

  161. --– Drop the procedure that was setup with dashes and rebuild it with the good stuff
  162. --– Version 1.1 mod; makes rebuilding hte proc unnecessary
  163. ROLLBACK TRAN

  164. DROP TABLE #output
  165. GO
复制代码
Remember running it under DAC mode.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-10-24 15:15 , Processed in 0.013877 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表