2009-05-05 2 views
4

私はthis questionから、Firefox 3.0以降ではそのクッキーをSQLiteデータベースに保存していることを知っています。私の質問です:他のデスクトッププログラムからこのデータベースにアクセスして、クッキーを追加することができますか?ブラウザのクッキーをプログラムで設定する(Firefox)

私はこれにセキュリティの影響があることを認識しています。しかし、私はそれらをまったく読んでいません。可能であれば、1つのクッキーを設定できるようにしたい。私はクッキーを上書きしたくない。私はちょうどそれが既にない場合、それを追加したいです。これは私が楽しいために取り組んでいる個人的なプロジェクトのようなものです。

この質問はほとんど言語に依存しません。私はC#のソリューションを好むだろうが、どの言語でも概念の証明があれば十分だろう。

エクストラクレジット:Internet Explorerで同じクッキーを設定してクールになる、あまりにも

+0

+1興味深いプロジェクトのようなサウンドです。 –

答えて

9

が、あなたはany SQLite wrapperでcookies.sqliteファイルにアクセスすることができます - しかし、FFが実行されているかどうか確認してください - それは、書き込みロックすることができるファイルを(未検証)。

データベースは、この含まれています。それはクッキーが作成されたときのUNIXタイムスタンプがあるよう

が主キーについて
TABLE moz_cookies (
    id INTEGER PRIMARY KEY, 
    name TEXT, 
    value TEXT, 
    host TEXT, 
    path TEXT, 
    expiry INTEGER, 
    lastAccessed INTEGER, 
    isSecure INTEGER, 
    isHttpOnly INTEGER 
) 

わからない、それが見えます。 expiryとlastAccessedもunixタイムスタンプで、残りは自明です。

INSERT INTO moz_cookiesを試し、FFが新しいクッキーを直ちに認識するか、再起動が必要かどうかを確認してください。

+0

ありがとうございます。これは私が探していたものです。 –

+3

Firefox 3.5はCookieファイルを排他的にロックするので、これ以上は機能しません。それはWONTFIXとして閉鎖されています。 https://bugzilla.mozilla.org/show_bug.cgiを参照してください。id = 476167 –

+0

これはまだ実行されていないFFでも行うことができます(あまり役に立たないと思いますが、私は認めます)。 – Piskvor

1

あなたはSQLiteのコネクタを使用して、ユーザーのクッキーのdbファイルに接続する必要があります。これはデフォルトのプロファイルフォルダにあり、cookies.sqliteと呼ばれます。 Firefoxの場合はsqlite-managerをご覧ください。 Firefoxで使用するすべてのテーブルを表示することができます。

編集:ここでは、プロバイダへのリンクです:FF3のためにSystem.Data.SQLite

+1

System.Data.SQLiteをお勧めします。ドキュメントは素晴らしいものではありませんが、機能はそこにあります。これは標準のADO.NETプロバイダであり、問​​題なくC#デスクトップアプリケーションで使用しました。 –

1

http://sqlite.phxsoftware.com/

これが答えても(私はこの質問は本当に古いですけど、私は同じ問題を抱えていたし、実際のコードの完全なサンプルを発見したことはありません.NET

2

でのSQLiteに対処するための素晴らしいですこのページで正しい方向に私を指摘した)。 HTH! http://www.ch-werner.de/javasqlite/overview-summary.htmlhttp://ini4j.sourceforge.net/を使用して

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

。現在の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のクッキーで悪をしないでください。そうしないと、Mozillaの人たちがそれらを暗号化し、私たちはそれらと面白く無害なことをすることもできなくなります。

関連する問題