Skip to content

Reading cookies from most Firefox versions…

Yesterday, I wrote about how to reading the cookies from Firefox 3.0 from Python. This code snippet extends the previous example by adding code which finds the cookie file on various different operating systems (Windows, Linux and Mac OS X). Hope this helps people who need to do this.

#! /usr/bin/env python
# Reading the cookie's from Firefox/Mozilla. Supports Firefox 3.0 and Firefox 2.x
# Author: Noah Fontes <nfontes AT cynigram DOT com>, 
#         Tim Ansell <mithro AT mithis DOT com>
# License: MIT
def sqlite2cookie(filename):
    from cStringIO import StringIO
    from pysqlite2 import dbapi2 as sqlite
    con = sqlite.connect(filename)
    cur = con.cursor()
    cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies")
    ftstr = ["FALSE","TRUE"]
    s = StringIO()
# Netscape HTTP Cookie File
# This is a generated file!  Do not edit.
    for item in cur.fetchall():
        s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
            item[0], ftstr[item[0].startswith('.')], item[1],
            ftstr[item[2]], item[3], item[4], item[5]))
    cookie_jar = cookielib.MozillaCookieJar()
    cookie_jar._really_load(s, '', True, True)
    return cookie_jar
import cookielib
import os
import sys
import logging
import ConfigParser
# Set up cookie jar paths
def _get_firefox_cookie_jar (path):
    profiles_ini = os.path.join(path, 'profiles.ini')
    if not os.path.exists(path) or not os.path.exists(profiles_ini):
        return None
    # Open profiles.ini and read the path for the first profile
    profiles_ini_reader = ConfigParser.ConfigParser();
    profile_name = profiles_ini_reader.get('Profile0', 'Path', True)
    profile_path = os.path.join(path, profile_name)
    if not os.path.exists(profile_path):
        return None
        if os.path.join(profile_path, 'cookies.sqlite'):
            return os.path.join(profile_path, 'cookies.sqlite')
        elif os.path.join(profile_path, 'cookies.txt'):
            return os.path.join(profile_path, 'cookies.txt')
def _get_firefox_nt_cookie_jar ():
    # See
        import _winreg
        import win32api
    except ImportError:
        logging.error('Cannot load winreg -- running windows and win32api loaded?')
    key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
        result = _winreg.QueryValueEx(key, 'AppData')
    except WindowsError:
        return None
        if ret[1] == _winreg.REG_EXPAND_SZ:
            result = win32api.ExpandEnvironmentStrings(ret[0])
            result = ret[0]
    return _get_firefox_cookie_jar(os.path.join(result, r'Mozilla\Firefox\Profiles'))
def _get_firefox_posix_cookie_jar ():
    return _get_firefox_cookie_jar(os.path.expanduser(r'~/.mozilla/firefox'))
def _get_firefox_mac_cookie_jar ():
    # First of all...
    result = _get_firefox_cookie_jar(os.path.expanduser(r'~/Library/Mozilla/Firefox/Profiles'))
    if result == None:
        result = _get_firefox_cookie_jar(os.path.expanduser(r'~/Library/Application Support/Firefox/Profiles'))
    return result
    'nt': _get_firefox_nt_cookie_jar,
    'posix': _get_firefox_posix_cookie_jar,
    'mac': _get_firefox_mac_cookie_jar
cookie_jar = None
    cookie_jar = FIREFOX_COOKIE_JARS[]()
except KeyError:
    cookie_jar = None
path = raw_input('Path to cookie jar file [%s]: ' % cookie_jar)
if path.strip():
    # Some input specified, set it
    cookie_jar = os.path.realpath(os.path.expanduser(path.strip()))
if cookie_jar.endswith('.sqlite'):
    cookie_jar = sqlite2cookie(cookie_jar)
    cookie_jar = cookielib.MozillaCookieJar(cookie_jar)

Edit: The latest version of this code can be found at and includes numerous fixes and updates.

{ 8 } Comments

  1. Paolo Bitta | May 14, 2009 at 10:22 pm | Permalink

    That’s a good job, thank you!

  2. mithro | May 15, 2009 at 11:08 am | Permalink

    You can find the code in an easier to edit format at Mithro’s Useful Bit repository.

  3. Chris | May 19, 2009 at 3:05 am | Permalink

    The windows code doesn’t appear to work at all.

    NameError: global name ‘ret’ is not defined, line 76

  4. mithro | May 19, 2009 at 1:44 pm | Permalink

    I updated the code in the repository. I don’t have a Windows computer to actually test against however.

  5. thegreatgrateful | June 9, 2010 at 9:20 am | Permalink

    thanks man!

  6. Will | August 23, 2010 at 5:09 am | Permalink

    I have the same error as Chris

    NameError: global name ‘ret’ is not defined

    I am currently using the copy of and from your repo.

  7. Will | August 23, 2010 at 5:17 am | Permalink

    sorry about the previous comment, I went the the site listed in the script:

    I see that it uses on line 68:

    ret = _winreg.QueryValueEx(key, name)
    except WindowsError:
    return None
    if ret[1] == _winreg.REG_EXPAND_SZ:
    return expandvars(ret[0])
    return ret[0]

    which your code closely mirrors :

    result = _winreg.QueryValueEx(key, ‘AppData’)
    except WindowsError:
    return None
    if ret[1] == _winreg.REG_EXPAND_SZ:
    result = win32api.ExpandEnvironmentStrings(ret[0])
    result = ret[0]

    It’s just that you put result = as opposed to ret = on that first line and then used ret further on.

  8. mithro | August 23, 2010 at 3:49 pm | Permalink

    I’ve updated the code once more, it might work now. As I said I can’t actually test this code as I don’t have a windows computer.

Post a Comment

Your email is never published nor shared. Required fields are marked *