使用Python解密Windows DPAPI中的安全字符串



给定一个值存储为安全字符串的Windows DPAPI文件,如何在Python中解密这些值?安全字符串是用PowerShell创建的,如下所示。

$global:Credentials.AuthToken = Read-Host -AsSecureString -Prompt "Auth-Token:"

并且这些值使用DPAPI存储在Windows 10或类似计算机上。

使用Python从DPAPI文件中提取安全字符串,并将其提供给下面的函数。安全字符串将作为base64编码值存储。

注意:当您读取PowerShell创建的DPAPI文件时,请确保使用";utf-16-le";编码

import codecs
import win32crypt
import base64

def decrypt(b64string):
b64decodedstring = base64.b64decode(b64string)
clear = win32crypt.CryptUnprotectData(b64decodedstring, None, None, None, 0)
return clear[1].decode("utf-16-le")

对于Windows中的安全字符串,该值以base64编码的十六进制存储在磁盘上。因此,像这样提取明文值,通过函数运行两次,并将十六进制值编码回base64。

decrypt(codecs.encode(codecs.decode(decrypt(ValueExtractedFromDPAPIGoesHere), 'hex'), 'base64').decode())

注意:您将需要以DPAPI用户的身份运行Python,您正试图从该用户访问安全字符串

最新更新