100% found this document useful (1 vote)
174 views2 pages

Decrypt SQL Server 2000 Objects

This document describes a stored procedure that can decrypt SQL Server 2000 stored procedures, views, and triggers that were previously encrypted using "WITH ENCRYPTION". The procedure takes the name of the encrypted object and its type (stored procedure, view, or trigger) as input. It decrypts the encrypted code by XORing the encrypted code with an encrypted bogus object of the same length. It then drops the original encrypted object and executes the decrypted code without encryption.

Uploaded by

api-26392589
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
174 views2 pages

Decrypt SQL Server 2000 Objects

This document describes a stored procedure that can decrypt SQL Server 2000 stored procedures, views, and triggers that were previously encrypted using "WITH ENCRYPTION". The procedure takes the name of the encrypted object and its type (stored procedure, view, or trigger) as input. It decrypts the encrypted code by XORing the encrypted code with an encrypted bogus object of the same length. It then drops the original encrypted object and executes the decrypted code without encryption.

Uploaded by

api-26392589
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

title: decrypt sql server 2000 stored procedures, views and triggers (with

examples)
description: this sp will decrypt stored procedures, views or triggers that were
encrypted using "with encryption"
there are 2 versions: one for sp's only and the other one for sp's, triggers and
views
version 1:
input: object name (stored procedure, view or trigger)
version 2:
input: object name (stored procedure, view or trigger), object type('t'-trigger,
'p'-stored procedure or 'v'-view)
=========================================================
set quoted_identifier off
go
set ansi_nulls off
go

create procedure decrypt2k (@objname varchar(50), @type char(1) )


--input: object name (stored procedure,
-- view or trigger), object type ('s'-store
-- d procedure, 'v'view or 't'-trigger)
--original idea: shoeboy <shoeboy@ade
-- [Link]>
--copyright � 1999-2002 securityfocus
--adapted by joseph gama
--planet source code, my employer and my
-- self are not responsible for the use of
-- this code
--this code is provided as is and for ed
-- ucational purposes only
--please test it and share your results
as
declare @a nvarchar(4000), @b nvarchar(4000), @c nvarchar(4000), @d
nvarchar(4000), @i int, @t bigint, @tablename varchar(255), @trigtype varchar(6)
set @type=upper(@type)
if @type='t'
begin
set @tablename=(select sysobjects_1.name
from [Link] inner join
[Link] sysobjects_1 on [Link].parent_obj = sysobjects_1.id
where ([Link] = 'tr') and ([Link] = @objname))
set @trigtype=(select case when [Link] > 0 then 'delete'
when [Link] > 0 then 'insert'
when [Link] > 0 then 'update' end
from [Link] inner join
[Link] sysobjects_1 on [Link].parent_obj =
sysobjects_1.id
where ([Link] = 'tr') and ([Link] =
@objname))
end
--get encrypted data
set @a=(select ctext from syscomments where id = object_id(@objname))
set @b=case @type
when 's' then 'alter procedure '+ @objname +' with encryption as
'+replicate('-', 4000-62)
when 'v' then 'alter view '+ @objname +' with encryption as select
[Link].* from [Link]'+replicate('-', 4000-150)
when 't' then 'alter trigger '+@objname+' on '+ @tablename+' with
encryption for '+@trigtype+' as print ''a'''+replicate('-', 4000-150)
end
execute (@b)
--get encrypted bogus sp
set @c=(select ctext from syscomments where id = object_id(@objname))
set @b=case @type
when 's' then 'create procedure '+ @objname +' with encryption as
'+replicate('-', 4000-62)
when 'v' then 'create view '+ @objname +' with encryption as select
[Link].* from [Link]'+replicate('-', 4000-150)
when 't' then 'create trigger '+@objname+' on '+ @tablename+' with
encryption for '+@trigtype+' as print ''a'''+replicate('-', 4000-150)
end
--start counter
set @i=1
--fill temporary variable
set @d = replicate(n'a', (datalength(@a) / 2))
--loop
while @i<=datalength(@a)/2
begin
--xor original+bogus+bogus encrypted
set @d = stuff(@d, @i, 1,
nchar(unicode(substring(@a, @i, 1)) ^
(unicode(substring(@b, @i, 1)) ^
unicode(substring(@c, @i, 1)))))
set @i=@i+1
end
--drop original sp
if @type='s'
execute ('drop procedure '+ @objname)
else
if @type='v'
execute ('drop view '+ @objname)
else
if @type='t'
execute ('drop trigger '+ @objname)
--remove encryption
--try to preserve case
set @d=replace((@d),'with encryption', '')
set @d=replace((@d),'with encryption', '')
set @d=replace((@d),'with encryption', '')
if charindex('with encryption',upper(@d) )>0
set @d=replace(upper(@d),'with encryption', '')
--replace sp
execute( @d)

go
set quoted_identifier off
go
set ansi_nulls on
go

You might also like