Skip to content

Instantly share code, notes, and snippets.

@coresh
Forked from hackerb9/extract_cookies.sh
Created September 18, 2025 07:31
Show Gist options
  • Save coresh/7ce7084eb64dd94bcb07c50820d3c337 to your computer and use it in GitHub Desktop.
Save coresh/7ce7084eb64dd94bcb07c50820d3c337 to your computer and use it in GitHub Desktop.

Revisions

  1. @hackerb9 hackerb9 revised this gist Jul 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion extract_cookies.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/sh -e
    #!/bin/bash -e
    # extract_cookies.sh:
    #
    # Convert from Firefox's cookies.sqlite format to Netscape cookies,
  2. @hackerb9 hackerb9 revised this gist Aug 9, 2017. No changes.
  3. @hackerb9 hackerb9 revised this gist Aug 9, 2017. 1 changed file with 70 additions and 23 deletions.
    93 changes: 70 additions & 23 deletions extract_cookies.sh
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,80 @@
    #!/usr/bin/env sh
    #$ extract_cookies $HOME/.mozilla/firefox/*/cookies.sqlite > /tmp/cookies.txt
    #$ wget --load-cookies=/tmp/cookies.txt http://mysite.com
    #$ # OR
    #$ curl --cookie /tmp/cookies.txt http://mysite.com
    #!/bin/sh -e
    # 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?)

    # 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://mysite.com

    # USING WITH CURL:
    # $ curl --cookie /tmp/cookies.txt http://mysite.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 1
    exit 0
    }
    trap cleanup EXIT INT QUIT TERM

    trap cleanup \
    EXIT INT QUIT TERM

    # 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);
    if [ "$#" -ge 1 ]; then
    SQLFILE="$1"
    else
    if tty -s; then
    SQLFILE=$(ls -t ~/.mozilla/firefox/*/cookies.sqlite | head -1)
    else
    SQLFILE="-" # Will use 'cat' below to read stdin
    fi
    fi

    if [ "$SQLFILE" != "-" -a ! -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 $1 >> $TMPFILE
    sqlite3 -separator ' ' $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;
    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

    cleanup
  4. @spk spk created this gist Feb 22, 2013.
    33 changes: 33 additions & 0 deletions extract_cookies.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #!/usr/bin/env sh
    #$ extract_cookies $HOME/.mozilla/firefox/*/cookies.sqlite > /tmp/cookies.txt
    #$ wget --load-cookies=/tmp/cookies.txt http://mysite.com
    #$ # OR
    #$ curl --cookie /tmp/cookies.txt http://mysite.com

    cleanup() {
    rm -f $TMPFILE
    exit 1
    }

    trap cleanup \
    EXIT INT QUIT TERM

    # 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);

    # We have to copy cookies.sqlite, because FireFox has a lock on it
    TMPFILE=`mktemp /tmp/cookies.sqlite.XXXXXXXXXX`
    cat $1 >> $TMPFILE
    sqlite3 -separator ' ' $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