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 -quacy.org> --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 dbo.sysobjects inner join dbo.sysobjects sysobjects_1 on dbo.sysobjects.parent_obj = sysobjects_1.id where (dbo.sysobjects.type = 'tr') and (dbo.sysobjects.name = @objname)) set @trigtype=(select case when dbo.sysobjects.deltrig > 0 then 'delete' when dbo.sysobjects.instrig > 0 then 'insert' when dbo.sysobjects.updtrig > 0 then 'update' end from dbo.sysobjects inner join dbo.sysobjects sysobjects_1 on dbo.sysobjects.parent_obj = sysobjects_1.id where (dbo.sysobjects.type = 'tr') and (dbo.sysobjects.name = @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 dbo.dtproperties.* from dbo.dtproperties'+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 dbo.dtproperties.* from dbo.dtproperties'+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