澳门新萄京:SQL Server加密存款和储蓄过程的破解

最后是改建后的囤积进程:

— Beginning of extract from sp_helptext

下一场创建叁个仓库储存进度,遍历全数加密过的贮存进程,调用解密存储过程实行解密,解密后输出:

select @real_decrypt_01a = ”
   ,@real_decrypt_02a = ”
   ,@real_decrypt_03a = ”
   ,@real_decrypt_04a = ”
   ,@real_decrypt_05a = ”

CREATE PROCEDURE [dbo].[DECODE_DATABASE]ASSET NOCOUNT ONBEGIN DECLARE @PROC_NAME VARCHAR(256) SET @PROC_NAME = '' DECLARE @ROWS INT DECLARE @TEMP TABLE( NAME VARCHAR(256) ) INSERT INTO @TEMP SELECT NAME FROM sysobjects WHERE TYPE = 'P' AND NAME NOT IN ( 'DECODE_DATABASE', 'DECODE_PROC' ) SET @ROWS = @@ROWCOUNT WHILE @ROWS  0 BEGIN SELECT @PROC_NAME = NAME FROM ( SELECT ROW_NUMBER() OVER (ORDER by NAME) AS ROW, NAME FROM @TEMP ) T WHERE ROW = @ROWS EXEC [DECODE_PROC] @PROC_NAME PRINT @PROC_NAME SET @ROWS = @ROWS - 1 END RETURN EXEC master..xp_cmdshell 'bcp SELECT [SQLTEXT] FROM TEST.dbo.[SQL_DECODE] queryout C:decode.txt -c -T -S PC2011043012JUJ'ENDGO

CREATE PROCEDURE dbo.sp__procedure$decrypt
(@procedure sysname = NULL, @revfl int = 1)
AS
SET NOCOUNT ON

CREATE TABLE [dbo].[SQL_DECODE]( [ID] [int] IDENTITY(1,1) NOT NULL, [SQLTEXT] [nvarchar](max) NOT NULL, CONSTRAINT [ID] PRIMARY KEY CLUSTERED ( [ID] ASC)) ON [PRIMARY]GO

INSERT INTO #output (real_decrypt)
SELECT @real_decrypt_01
UNION ALL
SELECT @real_decrypt_02
UNION ALL
SELECT @real_decrypt_03
UNION ALL
SELECT @real_decrypt_04
UNION ALL
SELECT @real_decrypt_05
— select real_decrypt AS ‘#output chek’ from #output — Testing

现今停止,解密全经过马到功成,命令行情势下运转:EXEC
[DECODE_DATABASE]GO在C盘根目录下,解密后的囤积进度文本生成成功。运转前别忘记张开xp_cmdshell使用权力,同开辟DAC相符:也许命令行格局下敲如下命令:sp_configure
‘show advanced options’,1reconfiguregosp_configure
‘xp_cmdshell’,1reconfigurego

— extract the encrypted fake imageval rows from sys.sysobjvalues
SELECT   @fake_encrypt_01=substring(imageval,1,8000)
   ,@fake_encrypt_02=substring(imageval,8001,16000)
   ,@fake_encrypt_03=substring(imageval,16001,24000)
   ,@fake_encrypt_04=substring(imageval,24001,32000)
   ,@fake_encrypt_05=substring(imageval,32001,40000)
FROM sys.sysobjvalues
WHERE objid = object_id(@procedure) and valclass = 1 and subobjid = 1

CREATE PROCEDURE [dbo].[DECODE_PROC]( @PROC_NAME SYSNAME = NULL)ASSET NOCOUNT ONDECLARE @PROC_NAME_LEN INT --存储过程名长度DECLARE @MAX_COL_ID SMALLINT --最大列IDSELECT @MAX_COL_ID = MAX(subobjid) FROM sys.sysobjvalues WHERE objid = OBJECT_ID(@PROC_NAME) GROUP BY imagevalSELECT @PROC_NAME_LEN = DATALENGTH(@PROC_NAME) + 29DECLARE @REAL_01 NVARCHAR(MAX) --真实加密存储过程数据DECLARE @FACK_01 NVARCHAR(MAX) --修改为假的存储过程,长度,原理不明?DECLARE @FACK_ENCRYPT_01 NVARCHAR(MAX) --伪加密存储过街程数据DECLARE @REAL_DECRYPT_01 NVARCHAR(MAX) --最终解密后的数据,初始化为原始加密长度的一半的A,原理不明?SET @REAL_01 = ( SELECT imageval FROM sys.sysobjvalues WHERE objid = object_id(@PROC_NAME) AND valclass = 1 AND subobjid = 1)DECLARE @REAL_DATA_LEN BIGINTSET @REAL_DATA_LEN = DATALENGTH(@REAL_01)--PRINT @REAL_DATA_LENDECLARE @FACK_LEN BIGINTSET @FACK_LEN = @REAL_DATA_LEN * 10 --改造:假的长度在原真实数据长度上放大10倍--此处需将NVARCHAR显示转换成NVARCHAR(MAX),不然将只能产生4K长度SET @FACK_01 = 'ALTER PROCEDURE ' + @PROC_NAME + ' WITH ENCRYPTION AS ' + REPLICATE(CONVERT(NVARCHAR(MAX), '-'), @FACK_LEN - @PROC_NAME_LEN)--PRINT '@FACK_01 = ' + STR(LEN(@FACK_01))EXECUTE (@FACK_01)SET @FACK_ENCRYPT_01 = ( SELECT imageval FROM sys.sysobjvalues WHERE objid = object_id(@PROC_NAME) AND valclass = 1 AND subobjid = 1)SET @FACK_01 = 'CREATE PROCEDURE ' + @PROC_NAME + ' WITH ENCRYPTION AS ' + REPLICATE(CONVERT(VARCHAR(MAX), '-'), @FACK_LEN - @PROC_NAME_LEN)SET @REAL_DECRYPT_01 = REPLICATE(CONVERT(NVARCHAR(MAX), N'A'), (DATALENGTH(@REAL_01) /2))--PRINT 'LEN(@REAL_DECRYPT_01) = ' + STR(LEN(@REAL_DECRYPT_01))--按位对 @REAL_01、 @FACK_01、 @REAL_DECRYPT_01 进行异或操作。DECLARE @INT_PROC_SPACE BIGINTSET @INT_PROC_SPACE = 1WHILE @INT_PROC_SPACE = (DATALENGTH(@REAL_01) /2 )BEGIN SET @REAL_DECRYPT_01 = STUFF( @REAL_DECRYPT_01, @INT_PROC_SPACE, 1, NCHAR(UNICODE(SUBSTRING(@REAL_01, @INT_PROC_SPACE, 1)) ^ (UNICODE(SUBSTRING(@FACK_01, @INT_PROC_SPACE, 1)) ^ UNICODE(SUBSTRING(@FACK_ENCRYPT_01, @INT_PROC_SPACE, 1)))) ) SET @INT_PROC_SPACE = @INT_PROC_SPACE + 1END--移除WITH ENCRYPTIONSET @REAL_DECRYPT_01 = REPLACE(@REAL_DECRYPT_01, 'WITH ENCRYPTION', '')INSERT INTO [SQL_DECODE] VALUES (@REAL_DECRYPT_01)--PRINT '@REAL_DECRYPT_01 = ' + @REAL_DECRYPT_01--PRINT 'LEN(@REAL_DECRYPT_01) = ' + STR(LEN(@REAL_DECRYPT_01))--删除原存储过程SET @FACK_01 = 'DROP PROCEDURE ' + @PROC_NAMEEXEC(@FACK_01)GO

— create this table for later use
create table #output ( [ident] [int] IDENTITY (1, 1) NOT NULL ,
[real_decrypt] NVARCHAR(MAX)
)

1、并未去除原存款和储蓄重新建立,仅仅在支配台做了三个输出,拷贝出来特别不方便人民群众。2、对于长度大点的加密数据会解密退步。带着那三个难题来改换此存款和储蓄进度。先做策画干活,首先须要掌握DAC这么个东西,
指的是数据库教程专项使用助理馆员连接,为总指挥提供的一种独特的确诊连接。知道了后得先开采它,以SQL二〇一〇为例:右击对象浏览器,找到Facets,点击,如图:找到Sruface
Area
Configuration,选拔RemoteDacEnabled,设为True:然后举行DAC登入,CMD格局下敲如下命令,不知晓原理的能够自行钻研:sqlcmd
-A -S 192.168.1.101 -U sa -P 123456命令提醒行下张开需处理的数据库:USE
TESTGO策动稳妥,复制搜索得到的存款和储蓄过程,生成解密存款和储蓄程,然后大家酌量七个加密后的储存进程,在那之中三个长度极大,验证得出结论,短小的存款和储蓄进度便捷即解密成功,并出口,但长度十分大的却解密败北。接下来看看其怎样解密的:先看那句select
@maxColID = max(subobjid卡塔尔(قطر‎,@intEncrypted = imageval FROM sys.sysobjvalues
WHERE objid =
object_id(@procedure卡塔尔指的是加密后的数量寄放在sys.sysobjvalues表中,其内容存放于imageval字段。知道了加密后的数目,就得进行解密,它定义了4个首要字段:DECLARE
@real_01 nvarchar(max)DECLARE @fake_01 nvarchar(max)DECLARE
@fake_encrypt_01 nvarchar(max)DECLARE @real_decrypt_01
nvarchar(max卡塔尔(قطر‎分别指的原来加密数据内容、原始加密存款和储蓄进程的CREATE语句、自个儿组织的假的积累进程加密后的数额、最后解密后的囤积进度。其艺术是按位将@real_01、@fake_encrypt_01、@real_decrypt_01进行异或运算,此处为什么如此管理,原理不明!!!WHILE
@intProcSpace=(datalength(@real_01)/2)BEGIN –xor real fake fake
encrypted SET @real_decrypt_01 = stuff(@real_decrypt_01,
@intProcSpace, 1, NCHAR(UNICODE(substring(@real_01, @intProcSpace, 1))
^ (UNICODE(substring(@fake_01, @intProcSpace, 1))
^UNICODE(substring(@fake_encrypt_01, @intProcSpace, 1卡塔尔(قطر‎卡塔尔国卡塔尔卡塔尔国卡塔尔国 SET
@intProcSpace=@intProcSpace+1END实际上到此结束,加密后的积攒进度已解密出来了。其下部还也许有一大段语句没有细心商量,但基本上是利用sp_helptext将内容输出,方法比较繁索,何况还未有高达大家要的效果,大家将换一种办法进行输出。基本上就这么轻松,除了原理不知晓外,基本央月完成要求,接下去要杀绝起来提出的八个问题。首先是长度难点,为何长度一大就解密退步,来探视@real_decrypt_01的定义并展开初叶化@real_decrypt_01的语句:DECLARE
@real_decrypt_01 nvarchar(max)SET @real_decrypt_01 = replicate(N’A’,
(datalength(@real_01卡塔尔 /2
卡塔尔(قطر‎卡塔尔国乍一看没什么难题,但咱们运用LEN(@real_decrypt_01卡塔尔国输出看看,最大出口长度为4000,大概难题就现身NVARCHA奥德赛的尺寸上了,理论上NVARCHAHighlander(MAX卡塔尔国帮衬2G的大小。为何会冒出这种场合并未有色金属切磋所究过,但有人给出了化解措施,进行呈现调换:SET
@real_decrypt_01 = replicate(CONVERT(NVARCHAR(MAX), N’A’),
(datalength(@real_01State of Qatar /2
卡塔尔国卡塔尔此外的几还会有几处也是该原因,修改后张开双重运营,难点消除,长度异常的大的蕴藏进度也解密成功。第三个难题一蹴即至了,如何能有益的出口呢,试验了去除重新建立,但未成功,那么就用最简便易行的措施呢,利用xp_cmdshell将内容输出到文本。先创立贰个物理表,用于存储解密后的数量:

— We’ll begin the transaction and roll it back later
BEGIN TRAN
— alter the original procedure, replacing with dashes
SET @fake_01=’ALTER PROCEDURE ‘+ @procedure +’ WITH ENCRYPTION AS
‘+REPLICATE(‘-‘, 40003 – @procNameLength)

— Go through each @real_xx variable and decrypt it, as necessary
WHILE @intProcSpace<=(datalength(@real_01)/2)
BEGIN
–xor real & fake & fake encrypted
SET @real_decrypt_01 = stuff(@real_decrypt_01, @intProcSpace, 1,
NCHAR(UNICODE(substring(@real_01, @intProcSpace, 1)) ^
(UNICODE(substring(@fake_01, @intProcSpace, 1)) ^
UNICODE(substring(@fake_encrypt_01, @intProcSpace, 1)))))
SET @intProcSpace=@intProcSpace+1
END

— extract the encrypted imageval rows from sys.sysobjvalues
SELECT @real_01=substring(imageval,1,8000)
   ,@real_02=substring(imageval,8001,16000)
   ,@real_03=substring(imageval,16001,24000)
   ,@real_04=substring(imageval,24001,32000)
   ,@real_05=substring(imageval,32001,40000)
FROM sys.sysobjvalues
WHERE objid = object_id(@procedure) and valclass = 1 and subobjid = 1

DECLARE @real_decrypt_01 nvarchar(max)
   ,@real_decrypt_01a nvarchar(max)
   ,@real_decrypt_02 nvarchar(max)
   ,@real_decrypt_02a nvarchar(max)
   ,@real_decrypt_03 nvarchar(max)
   ,@real_decrypt_03a nvarchar(max)
   ,@real_decrypt_04 nvarchar(max)
   ,@real_decrypt_04a nvarchar(max)  
   ,@real_decrypt_05 nvarchar(max)
   ,@real_decrypt_05a nvarchar(max)  

–If carriage return found
IF @CurrentPos != 0
BEGIN
–If new value for @Lines length will be > then the
–set length then insert current contents of @line
–and proceed.

–one byte at a time.
SET @intProcSpace=1

— Load the variables into #output for handling by sp_helptext logic

— Go through each @real_xx variable and decrypt it, as necessary
WHILE @intProcSpace<=(datalength(@real_05)/2)
BEGIN
–xor real & fake & fake encrypted
SET @real_decrypt_05 = stuff(@real_decrypt_05, @intProcSpace, 1,
NCHAR(UNICODE(substring(@real_05, @intProcSpace, 1)) ^
(UNICODE(substring(@fake_05, @intProcSpace, 1)) ^
UNICODE(substring(@fake_encrypt_05, @intProcSpace, 1)))))
SET @intProcSpace=@intProcSpace+1
END

DECLARE @intProcSpace bigint
   ,@t bigint
   ,@maxColID smallint
   ,@intEncrypted tinyint
   ,@procNameLength int

–select @maxColID as ‘Rows in sys.sysobjvalues’
select @procNameLength = datalength(@procedure) + 29

select @maxColID = max(subobjid)
   –//,@intEncrypted = imageval
FROM sys.sysobjvalues
WHERE objid = object_id(@procedure)
GROUP BY imageval

DECLARE @fake_encrypt_01 nvarchar(max)
DECLARE @fake_encrypt_02 nvarchar(max)
DECLARE @fake_encrypt_03 nvarchar(max)
DECLARE @fake_encrypt_04 nvarchar(max)
DECLARE @fake_encrypt_05 nvarchar(max)


— Else get the text.

SET @fake_01=’CREATE PROCEDURE ‘+ @procedure +’ WITH ENCRYPTION AS ‘
    + REPLICATE(‘-‘, 40003 – @procNameLength)
–start counter
SET @intProcSpace=1
–fill temporary variable with with a filler character
SET @real_decrypt_01 = replicate(N’A’, (datalength(@real_01) /2 ))

Select @DefinedLength = 255
SELECT @BlankSpaceAdded = 0 –Keeps track of blank spaces at end of
lines. Note Len function ignores trailing blank spaces
CREATE TABLE #CommentText
(LineId int
,Text nvarchar(255) collate database_default)

SELECT @LFCR = 2
SELECT @LineId = 1

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图