C# 獲取並解密谷歌瀏覽器Cookie


最近有個需求要讀取谷歌瀏覽器的cookie,在網上找了好久都沒找到C#版的,最后參考這篇文章(http://www.meilongkui.com/archives/1904,感興趣的去看下,很硬核)寫了個C#的。

首先引入兩個nuget包,一個sqlite用於讀取cookie,一個bouncycastle用於解密

using (SqliteConnection connection = new SqliteConnection())
{
    var userprofilePath = Environment.GetEnvironmentVariable("USERPROFILE");
    connection.ConnectionString = $@"DataSource={userprofilePath}\AppData\Local\Google\Chrome\User Data\Default\Cookies";
    connection.Open();
    SqliteCommand command = new SqliteCommand("select host_key,name,encrypted_value from cookies where host_key='.baidu.com'", connection);
    SqliteDataReader dataReader = command.ExecuteReader();
    dataReader.Read();
    byte[] encryptedValue = (byte[])dataReader["encrypted_value"];

    int keyLength = 256 / 8;
    int nonceLength = 96 / 8;
    String kEncryptionVersionPrefix = "v10";
    int GCM_TAG_LENGTH = 16;
   //字符串內容取自C:\Users\用戶名\AppData\Local\Google\Chrome\User Data\Local State文件的encrypted_key
    byte[] encryptedKeyBytes = Convert.FromBase64String("RFBBUEkBAAAA0Iyd3wEV0RGMegDAT8KX6wEAAAA3PHk3a5NmQpRxjGtdwCCCAAAAAAIAAAAAABBmAAAAAQAAIAAAALd7GZJyVqp7yQUBIEUvv0cwGN/mdUVrvAqqgbdJyJwoAAAAAA6AAAAAAgAAIAAAAPjIbfKCXRBggBNixV8sG409GYD9QRUHpiRMf/7s7Nm7MAAAABobpenJlhdxFJQw5PI1Fk/X0COpn+HZUxNl+GahUsmydEdXWJg0w5KmZjC7QjKJ/EAAAAA/rz1g3B2SdeXFMesLCZ/5O+xEDYxjeUP1hCw4Fa9rrLeUWpLkmmgL9JRNvSaiMfISpGXcWsr5zvhOLaF2kJ81");

    encryptedKeyBytes = encryptedKeyBytes.Skip("DPAPI".Length).Take(encryptedKeyBytes.Length- "DPAPI".Length).ToArray();

    var keyBytes = System.Security.Cryptography.ProtectedData.Unprotect(encryptedKeyBytes, null, System.Security.Cryptography.DataProtectionScope.CurrentUser);

    var nonce = encryptedValue.Skip(kEncryptionVersionPrefix.Length).Take( nonceLength).ToArray();

    encryptedValue = encryptedValue.Skip(kEncryptionVersionPrefix.Length + nonceLength).Take(encryptedValue.Length- (kEncryptionVersionPrefix.Length + nonceLength)).ToArray();

    var str = AesGcmDecrypt(keyBytes, nonce, encryptedValue);
    Console.WriteLine($"{dataReader["host_key"]}-{dataReader["name"]}-{str}");
    connection.Close();
}


public static string AesGcmDecrypt(byte[] keyBytes, byte[] nonce, byte[] encryptedValue)
{
    
    GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
    AeadParameters aeadParameters = new AeadParameters(
        new KeyParameter(keyBytes),
        128,
        nonce);
    gcmBlockCipher.Init(false, aeadParameters);
    byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(encryptedValue.Length)];
    int length = gcmBlockCipher.ProcessBytes(encryptedValue, 0, encryptedValue.Length, plaintext, 0);
    gcmBlockCipher.DoFinal(plaintext, length);
    return Encoding.UTF8.GetString(plaintext);

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM