From 8ccfbe20db17b93dc459f6f98a26e19b6c2eb554 Mon Sep 17 00:00:00 2001 From: Tal Wrii Date: Tue, 7 Nov 2017 23:04:13 +0100 Subject: [PATCH] Initial commit --- README.md | 35 +++++++++++++++++++++++++ cookiefire | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ curlfire | 29 +++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 README.md create mode 100755 cookiefire create mode 100755 curlfire diff --git a/README.md b/README.md new file mode 100644 index 0000000..d3dddf2 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# curlfire +Run curl with the current firefox cookies. This is useful for interacting with logged in websites. + +# Attribution + +This code is Adapted from [this Stack Exchange answer](https://superuser.com/questions/666167/how-do-i-use-firefox-cookies-with-wget) +# Usage + +``` +# Fetch google with the cookies from the default profile +curlfire http://www.google.com/ + +# Fetch google with the cookies from the blah profile +curlfire -P blah http://www.google.com/ + +# Getting cookies +cookiefire > /tmp/cookies +curl -b /tmp/cookies http://www.google.com/ +``` + +# Installation +``` +cd ~ +git clone https://github.com/talwrii/curlfire +echo 'PATH=$PATH:~/curlfire' >> ~/.bashrc +``` + +# Alternative and prior work + +* Adapted from [this stack overflow answer](https://superuser.com/questions/666167/how-do-i-use-firefox-cookies-with-wget) +* Firebug and friends allow you to copy requests as curl. This can be suitable for debugging. +* Cookies can be exported manually from within firefox + +All of these approaches can be problematic when automating tasks. + diff --git a/cookiefire b/cookiefire new file mode 100755 index 0000000..e178fcc --- /dev/null +++ b/cookiefire @@ -0,0 +1,75 @@ +#!/bin/bash +# -*- mode:sh -*- + +die() { + echo >&2 "$*" + exit 1 +} + + + +cleanup() { + rm -f "$tmpfile" +} +trap cleanup EXIT INT QUIT TERM + +# Run older ld (pseudo condition) + +if [ "$#" == "0" ]; then + profile=default +elif [ "$#" == "1" ]; then + profile=$1 +else + die "usage $0 [profile]" +fi; + + +extract_cookies() { + +if [ "$#" -ge 1 ]; then + sqlfile="$1" +else + if tty -s; then + sqlfile=$(ls -t ~/.mozilla/firefox/*/cookies.sqlite | head -1) + + 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 +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 + +} + +tmpfile="$(mktemp /tmp/cookies.sqlite.XXXXXXXXXX)" +curlcookies="$(mktemp /tmp/curlcookies.XXXXXXXXXX)" +echo $HOME/.mozilla/firefox/*.$profile/cookies.sqlite | { read cookie_file ; +extract_cookies "$cookie_file" ; +} + + diff --git a/curlfire b/curlfire new file mode 100755 index 0000000..ab9dccb --- /dev/null +++ b/curlfire @@ -0,0 +1,29 @@ +#!/bin/bash + +#!/bin/bash +set -o errexit +set -o nounset +set -o pipefail +profile=default +skip= +ARGS=() +for var in "$@"; do + # Ignore known bad arguments + case "$var" in + -P) + skip=yes + ;; + *) + if [ -z "$skip" ]; then + ARGS+=("$var") + else + profile=$var + fi; + skip= + esac; +done + + +curlcookies="$(mktemp /tmp/curlcookies.XXXXXXXXXX)" +cookiefire "$profile" > "$curlcookies" +curl -b "$curlcookies" "$ARGS" ;