示例#1
0
        public static string GetCookie(string domain, ref string preferredBrowser)
        {
            string SqliteFile = "Cookies";
            var    list       = new List <CC>();

            // Database
            string tempCookieLocation = "";

            IEnumerable <string> browserList = Paths.chromiumBasedBrowsers;

            if (!string.IsNullOrWhiteSpace(preferredBrowser))
            {
                var pb = preferredBrowser;
                var x  = Paths.chromiumBasedBrowsers.Where(b => b.IndexOf(pb, StringComparison.OrdinalIgnoreCase) != -1);
                browserList = x.Concat(Paths.chromiumBasedBrowsers.Where(b => b.IndexOf(pb, StringComparison.OrdinalIgnoreCase) == -1));
            }

            // Search all browsers
            foreach (string browser in browserList)
            {
                string Browser = Paths.GetUserData(browser) + SqliteFile;
                if (File.Exists(Browser)) //Must operate on a copy as it may be locked by the browser.
                {
                    tempCookieLocation = Environment.GetEnvironmentVariable("temp") + "\\browserCookies";
                    if (File.Exists(tempCookieLocation))
                    {
                        File.Delete(tempCookieLocation);
                    }
                    File.Copy(Browser, tempCookieLocation);
                }
                else
                {
                    continue;
                }

                // Read chrome database
                SQLite sSQLite = new SQLite(tempCookieLocation);
                sSQLite.ReadTable("cookies");

                var found = false;
                for (int i = 0; i < sSQLite.GetRowCount(); i++)
                {
                    string hostKey = sSQLite.GetValue(i, 1);
                    if (!hostKey.Equals(domain, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    // Get data from database
                    string name           = sSQLite.GetValue(i, 2);
                    string encryptedValue = sSQLite.GetValue(i, 12);
                    string lastAccessUtc  = sSQLite.GetValue(i, 8);

                    // If no data => break
                    if (string.IsNullOrEmpty(name))
                    {
                        break;
                    }

                    var value = Crypt.GetUTF8(Crypt.decryptChrome(encryptedValue, Browser));
                    list.Add(new CC(name, value, lastAccessUtc));
                    found = true;
                    continue;
                }

                if (found)
                {
                    var b = Path.GetFileName(browser);
                    preferredBrowser = b.Equals("browser", StringComparison.OrdinalIgnoreCase) ? Path.GetFileName(Path.GetDirectoryName(browser)) : b;
                    break; //only return the cookies from the first browser found.
                }
            }

            if (list.Count == 0)
            {
                return(string.Empty);                 //no cookies
            }
            //Build cookie string
            var    sb        = new StringBuilder();
            string delimiter = string.Empty;

            foreach (var cc in list.OrderBy(m => m.lastAccessed))
            {
                sb.Append(delimiter);
                sb.Append(cc.name);
                sb.Append('=');
                sb.Append(cc.value);
                delimiter = "; ";
            }

            return(sb.ToString());
        }