• 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);
    
    }
  • 相关阅读:
    DedeCMS用channelartlist调用顶级栏目及列表
    利用SQL语句替换织梦DedeCms数据库内容
    PHP 获取当前目录下的所有文件
    APP 商城功能
    left join , inner join 区别
    微信支付现金红包接口(转)
    微信红包发送规则
    PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析(转)
    调用微信红包接口返回(转)
    一起发红包 微信平台红包接口调用教程(转)
  • 原文地址:https://www.cnblogs.com/zhouyg2017/p/14296398.html
Copyright © 2020-2023  润新知