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
|
#!/usr/bin/env bash
# copied from https://superuser.com/a/1239036
# extract_cookies.sh:
#
# Convert from Firefox's cookies.sqlite format to Netscape cookies,
# which can then be used by wget and curl. (Why don't wget and curl
# just use libsqlite if it's installed? Mysteries abound.)
#
# Note: This script reads directly from the standard cookie jar file,
# which means cookies which are kept only in memory ("session cookies")
# will not be extracted. You will need an extension to do that.
# USAGE:
#
# $ extract_cookies.sh > /tmp/cookies.txt
# or
# $ extract_cookies.sh ~/.mozilla/firefox/*default*/cookies.sqlite > /tmp/cookies.txt
# USING WITH WGET:
# $ wget --load-cookies=/tmp/cookies.txt http://example.com
# USING WITH CURL:
# $ curl --cookie /tmp/cookies.txt http://example.com
# Note: If you do not specify an SQLite filename, this script will
# intelligently find it for you.
#
# A) Usually it will check all profiles under ~/.mozilla/firefox/ and
# use the cookies.sqlite that was updated most recently.
#
# B) If you've redirected stdin (with < or |) , then that will be used.
# HISTORY: I believe this is circa 2010 from:
# http://slacy.com/blog/2010/02/using-cookies-sqlite-in-wget-or-curl/
# However, that site is down now.
# Cleaned up by Hackerb9 (2017) to be more robust and require less typing.
cleanup() {
rm -f "$TMPFILE"
exit 0
}
trap cleanup EXIT INT QUIT TERM
if [ "$#" -ge 1 ]; then
SQLFILE="$1"
else
SQLFILE="$HOME/.mozilla/firefox/default/cookies.sqlite"
fi
if ! [ -r "$SQLFILE" ]; then
echo "Error. File $SQLFILE is not readable." >&2
exit 1
fi
# We have to copy cookies.sqlite, because FireFox has a lock on it
TMPFILE=$(mktemp /tmp/cookies.sqlite.XXXXXXXXXX)
cat "$SQLFILE" >>"$TMPFILE"
# This is the format of the sqlite database:
# CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT,expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER);
echo "# Netscape HTTP Cookie File"
sqlite3 -separator $'\t' "$TMPFILE" <<EOF
.mode tabs
.header off
select host,
case substr(host,1,1)='.' when 0 then 'FALSE' else 'TRUE' end,
path,
case isSecure when 0 then 'FALSE' else 'TRUE' end,
expiry,
name,
value
from moz_cookies;
EOF
cleanup
|