【问题标题】:How can I read/decrypt the encrypted_value column in the chrome sqlite database using powershell?如何使用 powershell 读取/解密 chrome sqlite 数据库中的 encrypted_value 列?
【发布时间】:2019-08-20 10:14:08
【问题描述】:

我正在尝试读取 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

问题是返回的加密值不可用。如何将其转换为可用值?

【问题讨论】:

    标签: sqlite google-chrome powershell cookies encryption


    【解决方案1】:

    从此答案适应 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 数据库路径的方式,因为它不可靠。事实上,它在我的机器上不起作用,因为我已经重命名了用户,但配置文件文件夹仍然保留它的旧名称。改用Environment.GetFolderPath 方法:

    # Get Cookies DB path
    [string]$db_data_source = Join-Path -Path [Environment]::GetFolderPath('LocalApplicationData') -ChildPath 'Google\Chrome\User Data\Default\Cookies'
    

    【讨论】:

      【解决方案2】:

      对于那些没有/想要 SQLite 程序集的人,可以使用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'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-26
        • 1970-01-01
        相关资源
        最近更新 更多