Read Google chrome SAML cookies.

By | August 23, 2018

Recently I had to investigate for a situation where a user would sign into a Google chrome account and a desktop application on the same machine had to somehow pick up the cookie and use it to communicate to web services, very similar to Cross browser \ application SSO. 

The solution below was found on Stack Overflow. The code below allows a user to query the Chrome storage and retrieve the cookies for a specific domain.

[Code language=’c#’]
public IEnumerable> ReadCookies(string domain)
{
if (domain== null) throw new ArgumentNullException(“domain”);

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 = “domain”;
prm.Value = domain;
cmd.Parameters.Add(prm);

cmd.CommandText = “SELECT name,encrypted_value FROM cookies WHERE host_key = @domain”;

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();
}
[/Code]

Credit to the following article on Stack Overflow.

Leave a Reply

Your email address will not be published.