代码之家  ›  专栏  ›  技术社区  ›  Andrew Ensley

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

  •  4
  • Andrew Ensley  · 技术社区  · 15 年前

    我知道 this question 火狐3.0及更高版本将其cookie存储在一个sqlite数据库中。我的问题是:你能从其他数据库访问这个数据库吗? 桌面 程序可以添加cookie吗?

    我意识到这有安全隐患。不过,我一点也不想读。如果可能的话,我想可以设置一个cookie。我甚至不想覆盖cookie。如果还没有的话,我只想加一点。这是一个个人项目,我是为了好玩。

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

    额外学分:在Internet Explorer中设置相同的cookie也很酷。

    5 回复  |  直到 12 年前
        1
  •  9
  •   Piskvor left the building Rohit Kumar    15 年前

    对于ff3,可以使用 any SQLite wrapper -但是,检查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,或者它是否需要重新启动。

        2
  •  2
  •   Phil Lambert    13 年前

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

    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];
    }
    
        3
  •  2
  •   esanchma    12 年前

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

    伙计们,我把这个代码用在我的自制刮刀上。不要用火狐cookies做坏事,否则Mozilla的人会加密它们,我们也不能用它们做有趣和无害的事情。

        4
  •  1
  •   Buggabill    15 年前

    您需要使用一个sqlite连接器并连接到用户的cookie数据库文件。这位于它们的默认配置文件文件夹中,称为cookies.sqlite。退房 sqlite-manager 对于火狐。您可以查看firefox使用的所有表格。

    编辑:以下是指向提供程序的链接: System.Data.SQLite

        5
  •  1
  •   McAden    15 年前

    http://sqlite.phxsoftware.com/

    这对于处理.NET中的sqlite非常有用