• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Decrypt encrypted stored procedures_sqlserver

sqlserver 搞代码 7年前 (2018-06-16) 160次浏览 已收录 0个评论

http://searchdatabase.techtarget.com/tip/1,289483,sid13_gci837799,00.html

This SP will decrypt Stored Procedures, Views or Triggers that were encrypted using “with encryption”. It is adapted from a script by Joseph Gama and ShoeBoy. There are two versions: one for SP’s only and the other one for SP’s, triggers and views. For version 1, the input is object name (stored procedure, view or trigger), and for version 2, the input is object name (stored procedure, view or trigger), object type (‘T’-trigger, ‘P’-stored procedure or ‘V’-view). From PlanetSourceCode.com.

http://www.gaodaima.com/35610.htmlDecrypt encrypted stored procedures_sqlserver

create PROCEDURE sp_decrypt_sp (@objectName varchar(50))ASDECLARE @OrigSpText1 nvarchar(4000), @OrigSpText2 nvarchar(4000) , @OrigSpText3 nvarchar(4000), @resultsp nvarchar(4000)declare @i int , @t bigint–get encrypted dataSET @OrigSpText1=(SELECT ctext FROM syscomments WHERE id = object_id(@objectName))SET @OrigSpText2=’ALTER PROCEDURE ‘+ @objectName +’ WITH ENCRYPTION AS ‘+REPLICATE(‘-‘, 3938)EXECUTE (@OrigSpText2)SET @OrigSpText3=(SELECT ctext FROM syscomments WHERE id = object_id(@objectName))SET @OrigSpText2=’CREATE PROCEDURE ‘+ @objectName +’ WITH ENCRYPTION AS ‘+REPLICATE(‘-‘, 4000-62)–start counterSET @i=1–fill temporary variableSET @resultsp = replicate(N’A’, (datalength(@OrigSpText1) / 2))–loopWHILE @i<=datalength(@OrigSpText1)/2BEGIN–reverse encryption (XOR original+bogus+bogus encrypted)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+1END–drop original SPEXECUTE (‘drop PROCEDURE ‘+ @objectName)–remove encryption–preserve caseSET @resultsp=REPLACE((@resultsp),’WITH ENCRYPTION’, ”)SET @resultsp=REPLACE((@resultsp),’With Encryption’, ”)SET @resultsp=REPLACE((@resultsp),’with encryption’, ”)IF CHARINDEX(‘WITH ENCRYPTION’,UPPER(@resultsp) )>0 SET @resultsp=REPLACE(UPPER(@resultsp),’WITH ENCRYPTION’, ”)–replace Stored procedure without enryptionexecute( @resultsp)GO

Reader Feedback

Joakim M. writes: I tried this script with mixed results. It works for some encrypted procedures, but for others I get error meassages like:

Server: Msg 512, Level 16, State 1, Procedure sp_decrypt_sp, Line 7.Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Karl C writes: I got the same message as Joakim M. but upon further investigation I found that this happens only when stored procedures exceed 4000 characters. When this happens, SQL Server stores the procedure across multiple rows so you get the error ‘subquery returne more than 1 row’. To get around that you can change the statement

SELECT ctext FROM syscomments WHERE id = object_id(@objectNameto SELECT top 1 ctext FROM syscomments WHERE id = object_id(@objectName order by colid

That will get you the first part of the stored procedure, which can’t be created because it is missing the end part and is not a valid syntax but you can print @resultsp out to see it.

For More Information

Feedback: E-mail the editor with your thoughts about this tip. More tips: Hundreds of free SQL Server tips and scripts. Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize — submit your tip today! Best web Links: SQL Server tips, tutorials, scripts, and more. Forums: Ask your technical SQL Server questions–or help out your peers by answering them–in our active forums. Ask the Experts: Our SQL, Database Design, oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.

欢迎大家阅读《Decrypt encrypted stored procedures_sqlserver,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Decrypt encrypted stored procedures_sqlserver

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址