mercredi 6 avril 2016

read cookie in C# console application

I want to get information from cookie file of google chrome, I see, this file is a database of sqlite and can open by sqlite brower. but when I read it by c#, I got a exception: The database disk image is malformed\r\nmalformed database schema (is_transient) help me, plz

below is my code:

private static string GetChromeCookiePath()
    {
        string s = Environment.GetFolderPath(
            Environment.SpecialFolder.LocalApplicationData);
        s += @"\Google\Chrome\User Data\Default\Cookies";

        if (!File.Exists(s))
            return string.Empty;

        return s;
    }

    public String GetCookie_Chrome()
    {
        String strHost = "www.facebook.com";
        String strField = "current";
        String Value = string.Empty;
        bool fRtn = false;
        string strPath, strDb;

        // Check to see if Chrome Installed
        strPath = GetChromeCookiePath();
        if (string.Empty == strPath) // Nope, perhaps another browser
            return "";

        try
        {
            //strDb = "Data Source=" + strPath + ";pooling=false";
            strDb = "data source="+ strPath + "; Version=3";

            using (SQLiteConnection conn = new SQLiteConnection(strDb))
            {
                using (SQLiteCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT name,encrypted_value FROM cookies WHERE host_key LIKE '%" +
                                      strHost + "%' AND name LIKE '%" + strField + "%';";

                    conn.Open();
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Value = reader.GetString(0);
                            if (!Value.Equals(string.Empty))
                            {
                                var encryptedValue = (byte[])reader[1];
                                var decryptedValue = System.Security.Cryptography.ProtectedData.Unprotect(encryptedValue, null, System.Security.Cryptography.DataProtectionScope.CurrentUser);
                                Value = Encoding.ASCII.GetString(decryptedValue);

                                fRtn = true;
                                break;
                            }
                        }
                    }
                    conn.Close();
                }
            }
        }
        catch (Exception e)
         {
            Value = string.Empty;
            fRtn = false;
        }
        return Value;

    }

Aucun commentaire:

Enregistrer un commentaire