• 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}AppDataLocalGoogleChromeUser DataDefaultCookies";
        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用户名AppDataLocalGoogleChromeUser DataLocal 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);
    
    }
  • 相关阅读:
    关于闭包和作用域的问题
    中文字体@font-face的导入
    一个跑马灯插件(持续优化)
    关于JS的clone()函数编写的一些问题
    函数的自执行,变量提升和函数提升
    Android 之Map容器替换 SparseArray,ArrayMap,ArraySet
    Anndroid GC 那些事
    Spark Streaming实时计算
    REDIS基础要点
    zookeeper要点总结
  • 原文地址:https://www.cnblogs.com/zhouyg2017/p/14296398.html
Copyright © 2020-2023  润新知