破解加密存储过程、触发器、视图和函数的脚本

时间:2022-10-20 18:23:25
SET QUOTED_IDENTIFIER ON 

GO

SET ANSI_NULLS ON 

GO



ALTER  PROCEDURE sp_decrypt

(

	@objectName 	varchar(50)

)

AS

BEGIN

	SET NOCOUNT ON

	

	--破解字节不受限制,适用于SQLSERVER2000存储过程,函数,视图,触发器

	--修正上一版视图触发器不能正确解密错误

	--发现有错,请E_MAIL:CSDNj9988@tom.com



	BEGIN TRAN



	DECLARE @objectname1 	varchar(100),

		@orgvarbin 	varbinary(8000)



	DECLARE @sql1 		nvarchar(4000),

		@sql2 		varchar(8000),

		@sql3 		nvarchar(4000),

		@sql4 		nvarchar(4000)



	DECLARE @OrigSpText1 	nvarchar(4000), 

		@OrigSpText2 	nvarchar(4000), 

		@OrigSpText3 	nvarchar(4000), 

		@resultsp 	nvarchar(4000)



	DECLARE @i 		int,

		@status 	int,

		@type 		varchar(10),

		@parentid 	int



	DECLARE @colid 		int,

		@n 		int,

		@q 		int,

		@j 		int,

		@k 		int,

		@encrypted 	int,

		@number 	int



	-- 获得对象的类型和父对象ID

	SELECT @type=xtype,@parentid=parent_obj 

	FROM sysobjects 

	WHERE id=object_id(@ObjectName)	



	CREATE TABLE #temp(

		number 		int,

		colid 		int,

		ctext 		varbinary(8000),

		encrypted 	int,

		status 		int

	)



	INSERT #temp 

	SELECT number,colid,ctext,encrypted,status 

	FROM syscomments 

	WHERE id = object_id(@objectName)



	SELECT @number=max(number) FROM #temp



	SET @k=0



	WHILE @k<=@number 

	BEGIN

		IF EXISTS(SELECT 1 FROM syscomments WHERE id=object_id(@objectname) and number=@k)

		BEGIN

			-- 存储过程

			IF @type='P'

				SET @sql1=(CASE 

					   	WHEN @number>1 THEN 

							'ALTER PROCEDURE '+ @objectName +';'+RTRIM(@k)+' WITH ENCRYPTION AS '

					  	ELSE 

							'ALTER PROCEDURE '+ @objectName+' WITH ENCRYPTION AS '

					   END)



			-- 触发器

			IF @type='TR'

			BEGIN

				DECLARE @parent_obj 		varchar(255),

					@tr_parent_xtype 	varchar(10)



				SELECT @parent_obj=parent_obj 

				FROM sysobjects 

				WHERE id=object_id(@objectName)



				SELECT @tr_parent_xtype=xtype 

				FROM sysobjects 

				WHERE id=@parent_obj



				IF @tr_parent_xtype='V'

				BEGIN

					SET @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTERD OF INSERT AS PRINT 1 '

				END

				ELSE

				BEGIN

					SET @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '

				END



			END



			-- 函数

			IF @type='FN' OR @type='TF' OR @type='IF'

				SET @sql1=(CASE @type 

						WHEN 'TF' THEN 

							'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '

						WHEN 'FN' THEN

							'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns char(1) with encryption as begin return @a end'

						WHEN 'IF' THEN

							'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns table with encryption as return select @a as a'

					   END)



			-- 视图

			IF @type='V'

				SET @sql1='ALTER VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f'



			-- 以下这段代码的逻辑是什么?

			SET @q=LEN(@sql1)

			SET @sql1=@sql1+REPLICATE('-',4000-@q)			

			SELECT @sql2=REPLICATE('-',8000)

			SET @sql3='exec(@sql1'



			SELECT @colid=max(colid) from #temp where number=@k



			SET @n=1



			WHILE @n<=CEILING(1.0*(@colid-1)/2) AND LEN(@sql3)<=3996

			BEGIN 

				SET @sql3=@sql3+'+@'

				SET @n=@n+1

			END



			SET @sql3=@sql3+')'

			EXEC sp_executesql @sql3,N'@Sql1 nvarchar(4000),@ varchar(8000)',@sql1=@sql1,@=@sql2

			--------------------------------------------------------------------------------------------



		END



		SET @k=@k+1

	END



	SET @k=0



	WHILE @k<=@number 

	BEGIN

		IF EXISTS(SELECT 1 FROM syscomments WHERE id=object_id(@objectname) AND number=@k)

		BEGIN

			SELECT @colid=MAX(colid) FROM #temp WHERE number=@k

	 

			SET @n=1



			WHILE @n<=@colid

			BEGIN

				SELECT @OrigSpText1=ctext,@encrypted=encrypted,@status=status 

				FROM #temp 

				WHERE colid=@n AND number=@k



				SET @OrigSpText3=(SELECT ctext FROM syscomments WHERE id=object_id(@objectName) AND colid=@n AND number=@k)



				-----------------------------------------------------------------------------

				IF @n=1

				BEGIN

					IF @type='P'

						SET @OrigSpText2=(CASE 

									WHEN @number>1 THEN 

										'CREATE PROCEDURE '+ @objectName +';'+RTRIM(@k)+' WITH ENCRYPTION AS '

									ELSE 

										'CREATE PROCEDURE '+ @objectName +' WITH ENCRYPTION AS '

								  END)





					IF @type='FN' OR @type='TF' OR @type='IF'

						SET @OrigSpText2=(CASE @type 

									WHEN 'TF' THEN 

										'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '

									WHEN 'FN' THEN

										'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns char(1) with encryption as begin return @a end'

									WHEN 'IF' THEN

										'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns table with encryption as return select @a as a'

									END)



					IF @type='TR' 

					BEGIN

						IF @tr_parent_xtype='V'

						BEGIN

							SET @OrigSpText2='CREATE TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTEAD OF INSERT AS PRINT 1 '

						END

						ELSE

						BEGIN

							SET @OrigSpText2='CREATE TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '

						END

					END



					IF @type='V'

						SET @OrigSpText2='CREATE VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f'

							

					SET @q=4000-LEN(@OrigSpText2)

					SET @OrigSpText2=@OrigSpText2+REPLICATE('-',@q)

				END

				ELSE

				BEGIN

					SET @OrigSpText2=REPLICATE('-', 4000)

				END

				-----------------------------------------------------------------------------



				SET @i=1



				SET @resultsp = REPLICATE(N'A', (DATALENGTH(@OrigSpText1) / 2))



				WHILE @i<=DATALENGTH(@OrigSpText1)/2

				BEGIN

					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+1

				END



				SET @orgvarbin=CAST(@OrigSpText1 as varbinary(8000))

				SET @resultsp=(CASE 

							WHEN @encrypted=1 THEN 

								@resultsp 

							ELSE 

								CONVERT(nvarchar(4000),	CASE 

												WHEN @status&2=2 THEN 

													uncompress(@orgvarbin) 

												ELSE 

													@orgvarbin 

												END)

						END)



				PRINT @resultsp



				SET @n=@n+1

			END

		END



		SET @k=@k+1

	END



	DROP TABLE #temp



	ROLLBACK TRAN

END











GO

SET QUOTED_IDENTIFIER OFF 

GO

SET ANSI_NULLS ON 

GO
 
我觉得这个代码很有用,至少我们在破解别人数据库的时候是可以用到的。但是,我没有弄懂其中一些代码的含义,所以谁获得相应的注释的话,请发送一个给我,谢谢!pen_zhaohui@126.com