未公开的SQL Server口令的加密函数

如果对mssql的用户信息有兴趣的,可能会发现master.dbo.sysxlogins里面存放着用户的口令,可是呢,password字段如果不是null就是一堆看不懂的binary,这个口令是怎么加密的呢?

  其实只要仔细看看master.dbo.sp_addlogin就知道了,mssql的sp都可以看到代码,真是不错。

  让我们来看看它是怎么做的,注意这一行select @passwd = pwdencrypt(@passwd),这个时后@passwd就被加密了,让我们也来试一下

  declare @clearpwd varchar(255) 

  declare @encryptedpwd varbinary(255)

  select @clearpwd = ‘test’

  select @encryptedpwd = convert(varbinary(255), pwdencrypt(@clearpwd))

  select @encryptedpwd

  看上去不错,确实被加密了,可是我怎么还原呢?

  

  口令加密都是单向的,用加密后的密文来比较就可以了。

  继续看看其它用户相关的sp,可以发现master.dbo.sp_password里面有口令比较的内容。

  pwdcompare(@old, password, (case when xstatus&2048 = 2048 then 1 else 0 end))

  不用去理会xstatus,这是一个状态掩码,一般我们用的时候就直接用0就可以了

  declare @clearpwd varchar(255) 

  declare @encryptedpwd varbinary(255)

  select @clearpwd = ‘test’

  select @encryptedpwd = convert(varbinary(255), pwdencrypt(@clearpwd))

  select pwdcompare(@clearpwd, @encryptedpwd, 0)

  select pwdcompare(‘errorpassword’, @encryptedpwd, 0)

  这样我们就可以使用这两个函数来加密自己的密码了

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐