Programmatically set a cookie browser (Firefox)

I know from this question that Firefox 3.0 and above store its cookies in a SQLite database. My question is: can you access this database from other desktop programs so that you can add a cookie?

I understand that this has security implications. However, I do not want to read them. If possible, I want to set one cookie. I don’t even want to overwrite the cookie. I just want to add it if it's already gone. This is a kind of personal project that I am working on for pleasure.

This question is mainly linguistic agnostic. I would prefer a solution in C #, but enough evidence of the concept in any language.

Extra credit: it would be great to set the same cookie in Internet Explorer, too

+4
source share
5 answers

For FF3, you can access the cookies.sqlite file with any SQLite shell - however, check if FF works - it could be a record-blocking file (not verified).

The database contains the following:

TABLE moz_cookies ( id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT, expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER ) 

Not sure about the primary key, it looks like it's the unix timestamp when the cookie was created; expiry and lastAccessed are also unix timestamps, the rest is self-evident.

Try INSERT INTO moz_cookies and see if FF immediately finds out about the new cookie or reloads.

+9
source

I know this question is really old, but I had the same problem and I never found a complete code sample (although the answers on this page pointed me in the right direction). NTN!

 public static void ClearFirefoxCookies() { int procCount = Process.GetProcessesByName("firefox").Length; if (procCount > 0) throw new ApplicationException(string.Format("There are {0} instances of Firefox still running", procCount)); try { using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + GetFirefoxCookiesFileName())) { conn.Open(); SQLiteCommand command = conn.CreateCommand(); command.CommandText = "delete from moz_cookies"; int count = command.ExecuteNonQuery(); } } catch (SQLiteException ex) { if (!(ex.ErrorCode == SQLiteErrorCode.Busy || ex.ErrorCode == SQLiteErrorCode.Locked)) throw new ApplicationException("The Firefox cookies.sqlite file is locked"); } } private static string GetFirefoxCookiesFileName() { string path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), @"Mozilla\Firefox\Profiles"); if (!System.IO.Directory.Exists(path)) throw new ApplicationException("Firefox profiles folder not found"); string[] fileNames = System.IO.Directory.GetFiles(path, "cookies.sqlite", System.IO.SearchOption.AllDirectories); if (fileNames.Length != 1 || !System.IO.File.Exists(fileNames[0])) throw new ApplicationException("Firefox cookies.sqlite file not found"); return fileNames[0]; } 
+2
source

Using http://www.ch-werner.de/javasqlite/overview-summary.html and http://ini4j.sourceforge.net/ . Works with current Firefox 11

 List<Map<String, String>> getCookies() throws Exception { String appdata = System.getenv("APPDATA"); File fappdata = new File(appdata); Path firefox = fappdata.toPath().resolve("Mozilla").resolve("Firefox"); File profilesini = firefox.resolve("profiles.ini").toFile(); Ini ini = new Ini(profilesini); //TODO:Detect more profiles than just assume default profile configuration Ini.Section section = ini.get("Profile0"); String profiledir = section.get("Path"); File cookiesfile = firefox.resolve(profiledir).resolve("cookies.sqlite").toFile(); String cookiesAdress = cookiesfile.getAbsolutePath(); Class.forName("SQLite.JDBCDriver"); Connection conn = DriverManager.getConnection("jdbc:sqlite:/"+cookiesAdress); Statement sta = conn.createStatement(); String query = "select * from moz_cookies"; ResultSet rs = sta.executeQuery(query); List<Map<String, String>> result = new ArrayList<>(); while(rs.next()) { Map<String, String> store = new HashMap<>(); store.put("id", String.valueOf(rs.getInt("moz_cookies.id"))); store.put("baseDomain", rs.getString("moz_cookies.baseDomain")); store.put("name", rs.getString("moz_cookies.name")); store.put("value", rs.getString("moz_cookies.value")); store.put("host", rs.getString("moz_cookies.host")); store.put("path", rs.getString("moz_cookies.path")); store.put("expiry", String.valueOf(rs.getInt("moz_cookies.expiry"))); store.put("lastAccessed", String.valueOf(rs.getInt("moz_cookies.lastAccessed"))); store.put("creationTime", String.valueOf(rs.getInt("moz_cookies.creationTime"))); store.put("isSecure", String.valueOf(rs.getInt("moz_cookies.isSecure"))); store.put("isHttpOnly", String.valueOf(rs.getInt("moz_cookies.isHttpOnly"))); result.add(store); } rs.close(); sta.close(); conn.close(); return result; } 

Guys, I use this code for my homegrown scraper. Don’t do evil with the help of Firefox cookies or the Mozilla guys will encrypt them and we won’t be able to do funny and harmless things with them.

+2
source

You will need to use the SQLite connector and connect to the die cookie of the user cookie. It is located in the default profile folder and is called cookie.sqlite. Check sqlite-manager for Firefox. You can view all the tables that Firefox uses with this.

Edit: Here is the link to the provider: System.Data.SQLite

+1
source

http://sqlite.phxsoftware.com/

This is great for working with SQLite in .NET.

+1
source

All Articles