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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 #! /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() s.write("""\ # Netscape HTTP Cookie File # http://www.netscape.com/newsref/std/cookie_spec.html # 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])) s.seek(0) 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(); profiles_ini_reader.readfp(open(profiles_ini)) 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 else: 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 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473846 try: 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') try: result = _winreg.QueryValueEx(key, 'AppData') except WindowsError: return None else: key.Close() if ret[1] == _winreg.REG_EXPAND_SZ: result = win32api.ExpandEnvironmentStrings(ret[0]) else: 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 FIREFOX_COOKIE_JARS = { 'nt': _get_firefox_nt_cookie_jar, 'posix': _get_firefox_posix_cookie_jar, 'mac': _get_firefox_mac_cookie_jar } cookie_jar = None try: cookie_jar = FIREFOX_COOKIE_JARS[os.name]() 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) else: cookie_jar = cookielib.MozillaCookieJar(cookie_jar)
Edit: The latest version of this code can be found at http://blog.mithis.com/cgi-bin/gitweb.cgi and includes numerous fixes and updates.







{ 8 } Comments
That’s a good job, thank you!
You can find the code in an easier to edit format at Mithro’s Useful Bit repository.
The windows code doesn’t appear to work at all.
NameError: global name ‘ret’ is not defined, line 76
I updated the code in the repository. I don’t have a Windows computer to actually test against however.
thanks man!
I have the same error as Chris
NameError: global name ‘ret’ is not defined
I am currently using the copy of firefox_finder.py and firefox3_repack.py from your repo.
sorry about the previous comment, I went the the site listed in the script:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473846
I see that it uses on line 68:
ret = _winreg.QueryValueEx(key, name)
except WindowsError:
return None
else:
key.Close()
if ret[1] == _winreg.REG_EXPAND_SZ:
return expandvars(ret[0])
else:
return ret[0]
which your code closely mirrors :
result = _winreg.QueryValueEx(key, ‘AppData’)
except WindowsError:
return None
else:
key.Close()
if ret[1] == _winreg.REG_EXPAND_SZ:
result = win32api.ExpandEnvironmentStrings(ret[0])
else:
result = ret[0]
It’s just that you put result = as opposed to ret = on that first line and then used ret further on.
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.