CREATE procedure dbo.spDecryptSP<br> @spName varchar(50),<br> @Replace bit = 0<br>As<br>/*<br>Developer: Jonathan Spinks<br>Date: 24/07/03<br>Description: Decrypts SQL 2000 stored procedures<br><br>Inputs: <br> @spName = the name of the stored procedure you wish to decrypt<br> @Replace = 0 or 1. 0 if you wish to display the unencrypted sp<br> 1 if you wish to replace the encrypted with the unencrypted sp<br><br>If this procedure returns "<@spName> is to large to decrypt"<br>this stored procedure can be modified to decrypt it.<br>Your starting point is to check out how many rows are returned <br>in the syscomments table for the stored procedures object_id.<br><br>If you just wish to display the sp then ensure that your result<br>pane is turned to 'Results in Text'.<br>In Query analyser (2k) Query => 'Results in Text'.<br><br>If your returned information looks 'cut short' first check<br>that your i_SQL query window is returning the entire string.<br>In Query analyser (2k) Tools => Options, Results tab,<br>Maximum characters per column = 8000.<br><br>Happy decrypting<br><br>Original idea: shoeboy <shoeboy@adequacy.org><br>Adapted by: Joseph Gama Copyright ?1999-2002 SecurityFocus <br>Adapted Source: http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=505&lngWId=5<br>Enhanced by: Jonathan Spinks Copyright ?2003 I.S. Software Developments. WASH<br>*/<br>DECLARE <br> @a1 nvarchar(4000), @b1 nvarchar(4000), @c1 nvarchar(4000), @d1 nvarchar(4000), <br> @a2 nvarchar(4000), @b2 nvarchar(4000), @c2 nvarchar(4000), @d2 nvarchar(4000), <br> @a3 nvarchar(4000), @b3 nvarchar(4000), @c3 nvarchar(4000), @d3 nvarchar(4000), <br> @a4 nvarchar(4000), @b4 nvarchar(4000), @c4 nvarchar(4000), @d4 nvarchar(4000), <br> @a5 nvarchar(4000), @b5 nvarchar(4000), @c5 nvarchar(4000), @d5 nvarchar(4000), <br> @a6 nvarchar(4000), @b6 nvarchar(4000), @c6 nvarchar(4000), @d6 nvarchar(4000), <br> @a7 nvarchar(4000), @b7 nvarchar(4000), @c7 nvarchar(4000), @d7 nvarchar(4000), <br> @a8 nvarchar(4000), @b8 nvarchar(4000), @c8 nvarchar(4000), @d8 nvarchar(4000), <br> @a9 nvarchar(4000), @b9 nvarchar(4000), @c9 nvarchar(4000), @d9 nvarchar(4000), <br> @a0 nvarchar(4000), @b0 nvarchar(4000), @c0 nvarchar(4000), @d0 nvarchar(4000), <br> @a11 nvarchar(4000), @b11 nvarchar(4000), @c11 nvarchar(4000), @d11 nvarchar(4000), <br> @a12 nvarchar(4000), @b12 nvarchar(4000), @c12 nvarchar(4000), @d12 nvarchar(4000), <br> @a13 nvarchar(4000), @b13 nvarchar(4000), @c13 nvarchar(4000), @d13 nvarchar(4000), <br> @a14 nvarchar(4000), @b14 nvarchar(4000), @c14 nvarchar(4000), @d14 nvarchar(4000), <br> @a15 nvarchar(4000), @b15 nvarchar(4000), @c15 nvarchar(4000), @d15 nvarchar(4000), <br> @a16 nvarchar(4000), @b16 nvarchar(4000), @c16 nvarchar(4000), @d16 nvarchar(4000), <br> @a17 nvarchar(4000), @b17 nvarchar(4000), @c17 nvarchar(4000), @d17 nvarchar(4000), <br> @a18 nvarchar(4000), @b18 nvarchar(4000), @c18 nvarchar(4000), @d18 nvarchar(4000), <br> @a19 nvarchar(4000), @b19 nvarchar(4000), @c19 nvarchar(4000), @d19 nvarchar(4000), <br> @a20 nvarchar(4000), @b20 nvarchar(4000), @c20 nvarchar(4000), @d20 nvarchar(4000), <br> @Perm nvarchar(4000),<br> @i int<br><br>if not exists(SELECT * FROM syscomments WHERE id = object_id(@spName))<br>Begin<br> print @spName +' cannot be found'<br> return<br>End<br><br>if exists(SELECT * FROM syscomments WHERE id = object_id(@spName) and encrypted = 0)<br>Begin<br> print @spName +' is not encrypted'<br> return<br>End<br><br>if (SELECT count(*) FROM syscomments WHERE id = object_id(@spName)) > 10<br>Begin<br> print @spName +' is to large to decrypt'<br> return<br>End<br><br>-- Get a list of the current permissions on the encrypted stored procedure<br>declare curPerm cursor fast_forward for<br> select '['+ u.name +']' as name, p.actadd, p.actmod<br> from dbo.syspermissions p inner join dbo.sysusers u<br> On p.grantee = u.uid<br> where p.id = object_id(@spName)<br>open curPerm<br><br>Set @Perm = ''<br>while 1 = 1<br>begin<br> declare @name sysname,<br> @actadd smallint,<br> @actmod smallint<br> fetch next from curPerm into @name, @actadd, @actmod<br> if @@fetch_status <> 0<br> break<br> -- For each permission in the list construct a GRANT or DENY command<br> if @actadd & 32 = 32<br> Set @Perm = @Perm +'GRANT EXECUTE ON '+ @spName +' TO '+ @name +char(13)+char(10)+'Go'+char(13)+char(10)<br> else if @actmod & 32 = 32<br> Set @Perm = @Perm +'DENY EXECUTE ON '+ @spName +' TO '+ @name +char(13)+char(10)+'Go'+char(13)+char(10)<br>end<br>close curPerm<br>deallocate curPerm<br><br>-- Get encrypted stored procedure<br>SELECT @a1=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 1<br>SELECT @a2=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 2<br>SELECT @a3=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 3<br>SELECT @a4=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 4<br>SELECT @a5=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 5<br>SELECT @a6=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 6<br>SELECT @a7=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 7<br>SELECT @a8=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 8<br>SELECT @a9=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 9<br>SELECT @a0=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 10<br>SELECT @a11=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 11<br>SELECT @a12=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 12<br>SELECT @a13=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 13<br>SELECT @a14=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 14<br>SELECT @a15=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 15<br>SELECT @a16=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 16<br>SELECT @a17=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 17<br>SELECT @a18=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 18<br>SELECT @a19=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 19<br>SELECT @a20=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 20<br><br><br>-- Create blank stored procedure<br>SET @b1='ALTER PROCEDURE '+ @spName +' WITH ENCRYPTION AS '+ REPLICATE('-', Len(@a1))<br>SET @b2=REPLICATE(N'-', len(@a2))<br>SET @b3=REPLICATE(N'-', len(@a3))<br>SET @b4=REPLICATE(N'-', len(@a4))<br>SET @b5=REPLICATE(N'-', len(@a5))<br>SET @b6=REPLICATE(N'-', len(@a6))<br>SET @b7=REPLICATE(N'-', len(@a7))<br>SET @b8=REPLICATE(N'-', len(@a8))<br>SET @b9=REPLICATE(N'-', len(@a9))<br>SET @b0=REPLICATE(N'-', len(@a0))<br>SET @b11=REPLICATE(N'-', len(@a11))<br>SET @b12=REPLICATE(N'-', len(@a12))<br>SET @b13=REPLICATE(N'-', len(@a13))<br>SET @b14=REPLICATE(N'-', len(@a14))<br>SET @b15=REPLICATE(N'-', len(@a15))<br>SET @b16=REPLICATE(N'-', len(@a16))<br>SET @b17=REPLICATE(N'-', len(@a17))<br>SET @b18=REPLICATE(N'-', len(@a18))<br>SET @b19=REPLICATE(N'-', len(@a19))<br>SET @b20=REPLICATE(N'-', len(@a20))<br><br><br>-- Wrap in transaction so original stored procedure can be restored<br>Begin transaction ReplaceSP<br><br> Execute (@b1 + @b2 + @b3 + @b4 + @b5 + @b6 + @b7 + @b8 + @b9 + @b0 + @b11 + @b12 + @b13 + @b14 + @b15 + @b16 + @b17 + @b18 + @b19 + @b20 )<br><br> -- Get blank encrypted stored procedure<br> SELECT @c1=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 1<br> SELECT @c2=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 2<br> SELECT @c3=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 3<br> SELECT @c4=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 4<br> SELECT @c5=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 5<br> SELECT @c6=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 6<br> SELECT @c7=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 7<br> SELECT @c8=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 8<br> SELECT @c9=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 9<br> SELECT @c0=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 0<br> SELECT @c11=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 11<br> SELECT @c12=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 12<br> SELECT @c13=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 13<br> SELECT @c14=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 14<br> SELECT @c15=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 15<br> SELECT @c16=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 16<br> SELECT @c17=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 17<br> SELECT @c18=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 18<br> SELECT @c19=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 19<br> SELECT @c20=ctext FROM syscomments WHERE id = object_id(@spName) and colid = 20<br><br>If @Replace = 0 <br> Rollback Transaction ReplaceSP<br>Else<br> Commit Transaction ReplaceSP<br><br>SET @b1='CREATE PROCEDURE '+ @spName +' WITH ENCRYPTION AS '+ REPLICATE('-', Len(@a1))<br><br>-- initalise the output variables<br>Set @d1 = ''<br>Set @d2 = ''<br>Set @d3 = ''<br>Set @d4 = ''<br>Set @d5 = ''<br>Set @d6 = ''<br>Set @d7 = ''<br>Set @d8 = ''<br>Set @d9 = ''<br>Set @d0 = ''<br>Set @d11 = ''<br>Set @d12 = ''<br>Set @d13 = ''<br>Set @d14 = ''<br>Set @d15 = ''<br>Set @d16 = ''<br>Set @d17 = ''<br>Set @d18 = ''<br>Set @d19 = ''<br>Set @d20 = ''<br><br>-- Set the counter to one<br>Set @i = 1 <br>WHILE @i < datalength(@a1)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d1 = @d1 + NCHAR(UNICODE(substring(@a1, @i, 1)) ^ (UNICODE(substring(@b1, @i, 1)) ^ UNICODE(substring(@c1, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a2)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d2 = @d2 + NCHAR(UNICODE(substring(@a2, @i, 1)) ^ (UNICODE(substring(@b2, @i, 1)) ^ UNICODE(substring(@c2, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a3)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d3 = @d3 + NCHAR(UNICODE(substring(@a3, @i, 1)) ^ (UNICODE(substring(@b3, @i, 1)) ^ UNICODE(substring(@c3, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a4)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d4 = @d4 + NCHAR(UNICODE(substring(@a4, @i, 1)) ^ (UNICODE(substring(@b4, @i, 1)) ^ UNICODE(substring(@c4, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a5)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d5 = @d5 + NCHAR(UNICODE(substring(@a5, @i, 1)) ^ (UNICODE(substring(@b5, @i, 1)) ^ UNICODE(substring(@c5, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a6)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d6 = @d6 + NCHAR(UNICODE(substring(@a6, @i, 1)) ^ (UNICODE(substring(@b6, @i, 1)) ^ UNICODE(substring(@c6, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a7)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d7 = @d7 + NCHAR(UNICODE(substring(@a7, @i, 1)) ^ (UNICODE(substring(@b7, @i, 1)) ^ UNICODE(substring(@c7, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a8)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d8 = @d8 + NCHAR(UNICODE(substring(@a8, @i, 1)) ^ (UNICODE(substring(@b8, @i, 1)) ^ UNICODE(substring(@c8, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a9)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d9 = @d9 + NCHAR(UNICODE(substring(@a9, @i, 1)) ^ (UNICODE(substring(@b9, @i, 1)) ^ UNICODE(substring(@c9, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a0)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d0 = @d0 + NCHAR(UNICODE(substring(@a0, @i, 1)) ^ (UNICODE(substring(@b0, @i, 1)) ^ UNICODE(substring(@c0, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1 <br>WHILE @i < datalength(@a11)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d11 = @d11 + NCHAR(UNICODE(substring(@a11, @i, 1)) ^ (UNICODE(substring(@b11, @i, 1)) ^ UNICODE(substring(@c11, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a12)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d12 = @d12 + NCHAR(UNICODE(substring(@a12, @i, 1)) ^ (UNICODE(substring(@b12, @i, 1)) ^ UNICODE(substring(@c12, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a13)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d13 = @d13 + NCHAR(UNICODE(substring(@a13, @i, 1)) ^ (UNICODE(substring(@b13, @i, 1)) ^ UNICODE(substring(@c13, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a14)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d14 = @d14 + NCHAR(UNICODE(substring(@a14, @i, 1)) ^ (UNICODE(substring(@b14, @i, 1)) ^ UNICODE(substring(@c14, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a15)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d15 = @d15 + NCHAR(UNICODE(substring(@a15, @i, 1)) ^ (UNICODE(substring(@b15, @i, 1)) ^ UNICODE(substring(@c15, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a16)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d16 = @d16 + NCHAR(UNICODE(substring(@a16, @i, 1)) ^ (UNICODE(substring(@b16, @i, 1)) ^ UNICODE(substring(@c16, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a17)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d17 = @d17 + NCHAR(UNICODE(substring(@a17, @i, 1)) ^ (UNICODE(substring(@b17, @i, 1)) ^ UNICODE(substring(@c17, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a18)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d18 = @d18 + NCHAR(UNICODE(substring(@a18, @i, 1)) ^ (UNICODE(substring(@b18, @i, 1)) ^ UNICODE(substring(@c18, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a19)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d19 = @d19 + NCHAR(UNICODE(substring(@a19, @i, 1)) ^ (UNICODE(substring(@b19, @i, 1)) ^ UNICODE(substring(@c19, @i, 1))))<br> SET @i=@i+1<br>END<br><br>-- Set the counter to one<br>Set @i = 1<br>WHILE @i < datalength(@a20)/2 + 1<br>BEGIN<br> -- (unencrypted original) = (encrypted original) XOR (unencrypted blank) XOR (encrypted blank)<br> SET @d20 = @d20 + NCHAR(UNICODE(substring(@a20, @i, 1)) ^ (UNICODE(substring(@b20, @i, 1)) ^ UNICODE(substring(@c20, @i, 1))))<br> SET @i=@i+1<br>END<br><br>if @Replace = 0<br>Begin<br> -- Output the unencrypted stored procedure to the screen<br> select @d1 as 'Unencrypted Stored Procedure'<br> select @d2<br> select @d3 <br> select @d4 <br> select @d5<br> select @d6<br> select @d7<br> select @d8<br> select @d9<br> select @d0<br> select @d11<br> select @d12<br> select @d13 <br> select @d14 <br> select @d15<br> select @d16<br> select @d17<br> select @d18<br> select @d19<br> select @d20<br><br> -- Output any permissions that were on the encrypted stored procedure<br> Select @Perm as 'Permissions'<br>End<br>Else<br>Begin<br> -- Drop the encrypted stored procedure<br> Exec('Drop Procedure '+ @spName)<br> -- Remove the 'WITH ENCRYPTION' command from the stored procedure <br> Set @d1 = Replace(@d1, 'WITH ENCRYPTION', '')<br> -- Create the unencrypted stored procedure<br> Exec(@d1 + @d2 + @d3 + @d4 + @d5 + @d6 + @d7 + @d8 + @d9 + @d0 + @d11 + @d12 + @d13 + @d14 + @d15 + @d16 + @d17 + @d18 + @d19 + @d20)<br> -- Apply any permissions that were on the encrypted stored procedure<br> Exec(@Perm)<br>End<br>GO