Encrypted cookies in Chrome

前端 未结 6 2005
挽巷
挽巷 2020-11-27 15:36

I am currently working on a C# forms application that needs to access a specific cookie on my computer, which I can do perfectly fine. Here\'s the issue:

Google stor

6条回答
  •  失恋的感觉
    2020-11-27 16:11

    I've run into this same problem, and the code below provides a working example for anyone who is interested. All credit to Scherling, as the DPAPI was spot on.

    public class ChromeCookieReader
    {
        public IEnumerable> ReadCookies(string hostName)
        {
            if (hostName == null) throw new ArgumentNullException("hostName");
    
            var dbPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Google\Chrome\User Data\Default\Cookies";
            if (!System.IO.File.Exists(dbPath)) throw new System.IO.FileNotFoundException("Cant find cookie store",dbPath); // race condition, but i'll risk it
    
            var connectionString = "Data Source=" + dbPath + ";pooling=false";
    
            using (var conn = new System.Data.SQLite.SQLiteConnection(connectionString))
            using (var cmd = conn.CreateCommand())
            {
                var prm = cmd.CreateParameter();
                prm.ParameterName = "hostName";
                prm.Value = hostName;
                cmd.Parameters.Add(prm);
    
                cmd.CommandText = "SELECT name,encrypted_value FROM cookies WHERE host_key = @hostName";
    
                conn.Open();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var encryptedData = (byte[]) reader[1];
                        var decodedData = System.Security.Cryptography.ProtectedData.Unprotect(encryptedData, null, System.Security.Cryptography.DataProtectionScope.CurrentUser);
                        var plainText = Encoding.ASCII.GetString(decodedData); // Looks like ASCII
    
                        yield return Tuple.Create(reader.GetString(0), plainText);
                    }
                }
                conn.Close();
            }
        }
    }
    

提交回复
热议问题