以编程方式设置浏览器cookie(Firefox)

时间:2009-05-05 20:19:22

标签: c# internet-explorer firefox sqlite cookies

我从this question知道Firefox 3.0及以上将其cookie存储在SQLite数据库中。我的问题是:您是否可以通过其他桌面程序访问此数据库,以便添加Cookie?

我意识到这有安全隐患。但是,我根本不想阅读它们。我希望能够在可能的情况下设置一个cookie。我甚至不想覆盖cookie。如果不存在,我只想添加它。这是一个我正在努力的个人项目。

这个问题主要是语言不可知的。我更喜欢C#中的解决方案,但任何语言的概念证明都足够了。

额外信用:在Internet Explorer中设置相同的cookie也很酷

5 个答案:

答案 0 :(得分:9)

对于FF3,您可以使用any SQLite wrapper访问cookies.sqlite文件 - 但是,检查FF是否正在运行 - 它可能是对文件进行写锁定(未测试)。

数据库包含:

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

不确定主键,看起来它是创建cookie时的unix时间戳; expiry和lastAccessed也是unix时间戳,其余的都是不言自明的。

尝试INSERT INTO moz_cookies并查看FF是否立即知道新Cookie或是否需要重新启动。

答案 1 :(得分:2)

我知道这个问题真的很老了,但我遇到了同样的问题而且从未真正找到完整的代码示例(尽管本页的答案指向了正确的方向)。 HTH!

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 :(得分:2)

使用http://www.ch-werner.de/javasqlite/overview-summary.htmlhttp://ini4j.sourceforge.net/。适用于当前的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;

}

伙计们,我将这段代码用于自制的刮板器。不要使用Firefox cookie做恶,或者Mozilla会加密它们,我们也无法用它们做有趣和无害的事情

答案 3 :(得分:1)

您需要使用SQLite连接器并连接到用户的cookie db文件。它位于其默认配置文件文件夹中,名为cookies.sqlite。查看Firefox的sqlite-manager。您可以查看Firefox使用的所有表格。

修改:以下是指向提供商的链接:System.Data.SQLite

答案 4 :(得分:1)

http://sqlite.phxsoftware.com/

这非常适合在.NET中处理SQLite

相关问题