コード例 #1
0
        // Get cookies from gecko browser
        public static string GetCookie(string domain)
        {
            // Get firefox default profile directory
            string profile = GetFFProfileFolder();

            // Read cookies from file
            if (profile == null)
            {
                return(string.Empty);
            }
            string db_location = Path.Combine(profile, "cookies.sqlite");
            // Read data from table
            SQLite sSQLite = SQLite.ReadTable(db_location, "moz_cookies");

            if (sSQLite == null)
            {
                return(string.Empty);
            }
            // Get values from table: SELECT name, value FROM moz_cookies WHERE host = 'www.{0}' OR host = '.{0}' OR host = '.www.{0}' ORDER BY creationTime
            var list = new List <CC>();

            for (int i = 0; i < sSQLite.GetRowCount(); i++)
            {
                var host = sSQLite.GetValue(i, 4);
                if (!host.Equals(domain, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                var name         = sSQLite.GetValue(i, 2);
                var value        = sSQLite.GetValue(i, 3);
                var lastAccessed = sSQLite.GetValue(i, 6);
                list.Add(new CC(name, value, lastAccessed));
            }
            if (list.Count == 0)
            {
                return(string.Empty);
            }
            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());
        }
コード例 #2
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());
        }