我正在尝试读取cookie的内容(用于脚本中的身份验证),但该值在chrome sqlite数据库中存储为某种加密值。有没有办法使用PowerShell解密它?
现在我可以使用这样的脚本读取数据库中的值:
[string]$sqlite_library_path = "C:\Path\To\System.Data.SQLite.dll"
[string]$db_data_source = "C:\Users\$env:USERNAME\AppData\Local\Google\Chrome\User Data\Default\Cookies"
[string]$db_query = "SELECT * FROM cookies WHERE name='cookiename' AND host_key='servername'"
[void][System.Reflection.Assembly]::LoadFrom($sqlite_library_path)
$db_dataset = New-Object System.Data.DataSet
$db_data_adapter = New-Object System.Data.SQLite.SQLiteDataAdapter($db_query,"Data Source=$db_data_source")
[void]$db_data_adapter.Fill($db_dataset)
$db_dataset.Tables[0].encrypted_value
问题是返回的加密值无法使用。如何将其转换为可用值?
答案 0 :(得分:2)
从这个答案适应Powershell:Encrypted cookies in Chrome
# Load System.Security assembly
Add-Type -AssemblyName System.Security
# Decrypt cookie
$ByteArr = [System.Security.Cryptography.ProtectedData]::Unprotect(
$db_dataset.Tables[0].encrypted_value,
$null,
[System.Security.Cryptography.DataProtectionScope]::CurrentUser
)
# Convert to string
$DecryptedCookie = [System.Text.Encoding]::ASCII.GetString($ByteArr)
我还建议您改变获取cookie DB路径的方式,因为它不可靠。事实上,它在我的机器上不起作用,因为我已经重命名了用户,但是配置文件夹仍然保留了它的旧名称。请改用Environment.GetFolderPath方法:
# Get Cookies DB path
[string]$db_data_source = Join-Path -Path [Environment]::GetFolderPath('LocalApplicationData') -ChildPath 'Google\Chrome\User Data\Default\Cookies'
答案 1 :(得分:0)
sqlite
命令。这是一个适用于MSYS2上的Bash的有效的1行示例( )(分布在多行上以提高可读性)。
根据需要填写SQL查询中的''
空白。
sqlite3 "${LOCALAPPDATA}\\Google\\Chrome\\User Data\\Default\\Cookies" \
".mode quote" \
"SELECT encrypted_value FROM cookies WHERE host_key = '' AND name = '' AND path = '/'"|\
powershell -command 'function main() {
$newline = [System.Text.Encoding]::ASCII.GetBytes(([char]10).ToString());
$stdout = [System.Console]::OpenStandardOutput();
try {
for ($n = 0; ($s = [System.Console]::In.ReadLine()) -ne $null; ++$n) {
if ($n) { $stdout.Write($newline, 0, $newline.Length); }
$s = [System.Text.RegularExpressions.Regex]::Match($s,
"^X" + [char]39 + "(.*)" + [char]39 + "$").Groups[1].Value;
$b = [byte[]]::new($s.Length / 2);
For ($i=0; $i -lt $s.Length; $i += 2) {
$b[$i / 2] = [System.Convert]::ToByte($s.Substring($i, 2), 16);
}
Add-Type -AssemblyName System.Security;
$output = [System.Security.Cryptography.ProtectedData]::Unprotect($b, $null,
[System.Security.Cryptography.DataProtectionScope]::CurrentUser);
$stdout.Write($output, 0, $output.Length);
}
} finally {
$stdout.Close();
}
}
main'