Skip to content
Advertisement

how to use c# to insert a new record to chrome cookies file

I have find where chrome cookie file is, and I use sqliteBrowser open it and find cookies table’s structure.

And I also successfully read cookies from this file,although the cookies value is encrypted,but I still find some way to decrypted it.

I also noticed that the cookies table’s creation_utc field is using the time count since 1601.01.01.

All I want to say is that I have do some work and already know the chrome cookies file’s structure.

But the question is when I want to insert a new record to this file using C#’s System.data.sqlite, my program just could not insert this record and go to some state like dead lock,I notice that this situation is caused by that executeNoQuery method does not returned!!!

the same sql syntax works fine in my test sqlite file,so I guess that the question is caused by the chrome cookies file itself.

Can any one help me? thank you!

following is my code trying to set cookie:

        private static bool SetCookie_Chrome(string strHost, string strField, string value,DateTime expiresDate)
    {
        bool fRtn = false;
        string strPath, strDb;


        strPath = GetChromeCookiePath();
        //strPath= GetTestCookiePath();
        if (string.Empty == strPath) // Nope, perhaps another browser
            return false;

        try
        {
            strDb = "Data Source=" + strPath + ";pooling=false";

            SQLiteConnection conn = new SQLiteConnection(strDb);

            SQLiteCommand cmd = conn.CreateCommand();

            Byte[] encryptedValue=getEncryptedValue(value);

            cmd.CommandText = "INSERT INTO `cookies`(`creation_utc`,`host_key`,`name`,`value`,`path`,`expires_utc`,`secure`,`httponly`,`last_access_utc`,`has_expires`,`persistent`,`priority`,`encrypted_value`) "+
                "VALUES ("+TimeUtil.get_creation_utc()+",'"+strHost+"','"+strField+"','','/',"+
                TimeUtil.get_specific_utc(expiresDate.Year,expiresDate.Month,expiresDate.Day,expiresDate.Hour,expiresDate.Minute,expiresDate.Second)+
                ",0,0," + TimeUtil.get_creation_utc() + ",1,1,1,@evalue);";
            cmd.Parameters.Add("@evalue", System.Data.DbType.Binary).Value = encryptedValue;
            conn.Open();
            int rtV=cmd.ExecuteNonQuery();
            conn.Close();
            fRtn = true;
        }


        catch (Exception e)
        {
            fRtn = false;
        }
        return fRtn;
    }

Advertisement

Answer

If some one want to modify chrome cookie file, you could reference my code posted above.Actually, the code is right, the reason why I encounter the problem is quoted from the above comment:

The reason why this situation happens is because I have another connection to the same sql file active, so like what the file lock works, it just lock the file in case you read some dirty value.

Hope this question could help others.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement