public
Last active

Cookie Header Scanner iteration 2 for Part 1 of the PhantomJS blog post series

  • Download Gist
chs2-basic.coffee
CoffeeScript
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
###Copyright (C) 2013, Neohapsis, Inc. All rights reserved.
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#• Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#• Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or #other materials provided with the distribution.
#• Neither the name of Neohapsis, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#The above licensing was taken from the BSD licensing and is applied to CHS2 as well.
#Note that the CHS2 is provided as is, and is a royalty free open-source application.
#Feel free to modify, use, change, market, do whatever you want with it as long as you give the appropriate credit where credit is due (which means giving the authors the credit they deserve for writing it).
#
# chs2-basic.coffee - CoffeeScript for PhantomJS to perform Web site reconnaissance
# Original version by Ben Toews
# Extended by Brent Bandelgar
# For EU Article 29 (cookie directive) compliance
# CoffeeScript SPACE indented, 4 spaces per tab
# Tested against PhantomJS 1.7/Linux x86_64
#Basic PhantomJS includes
webpage = require 'webpage'
system = require 'system'
fs = require 'fs'
#Include our Semaphore.js
Semaphore = require './semaphore'
#in_file: text file of URLs, one per line (LF-delimited)
#out_prefix: desired output filename prefix
#exit if this script is not run with the correct number of arguments
if system.args.length < 3
console.log 'Usage: phantomjs chs2-basic.coffee [in_file] [out_prefix]'
phantom.exit()
else
# read in the text files of URLs
data = fs.read system.args[1]
# We'll keep track of completed URLs in a specifically named file too, so if the file exists, read it in for subsequent runs and skip them
# keep track of failures too but re-test, don't skip them
if fs.exists(system.args[2] + ".completed.txt")
completedData = fs.read system.args[2]+ ".completed.txt"
completedURLs = completedData.split '\n'
# semaphore so we don't exit until everything is done
sem = new Semaphore ->
phantom.exit()
# Keep track of our URLs
completedURLs = []
redirectURLs = []
# Iterate through the URLs extracted from the file
# Try '\r\n' if you need to use Windows-formatted text files
for url in data.split '\n'
#Try not to access a blank line as a URL, and don't redo a url that we have already completed
if url.length > 0
if url not in completedURLs
sem.increment()
do (url) ->
#instantiate a webpage
page = webpage.create()
# Define some settings overrides
#Bogus Safari for Windows User-Agent to get past some JS browser detection redirects
page.settings.userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; tr-TR) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27'
## Define callbacks
# Define timeout callback workaround the bug where PhantomJS just hangs on sites that need HTTP auth https://groups.google.com/forum/#!msg/phantomjs/Q1lsh02IPrw/GFetmArzt8YJ
#We'll wait a million ms based on the highest observed success time at 800000 when fetching 200 URLs at a time
page.timeout = delay 1000000, ->
statusSummaryString = "STATUS: #{url} - timeout"
console.log statusSummaryString
page.close()
sem.execute()
# splat: page.onUrlChanged = function(targetUrl){...}
# This is triggered each time the "Location"/"Address Bar" would be changed from a redirect
page.onUrlChanged = (targetUrl) ->
console.log "REDIRECT: FROM: #{url} TO: #{targetUrl}"
#Note that completedURLs is available in this scope,
if targetUrl in completedURLs
console.log "REDIRECT: Detected duplicate URL run for #{targetUrl}"
# end page.onUrlChanged
# splat: page.open(url) = function(status){...}
# Define the page.open callback. This can be considered the "main" routine.
page.open url, (status) ->
console.log "#{url} - #{status}"
# Check for page load success. The page object should be populated with interesting data now.
if status is 'success'
# output JSON of cookies from page, one JSON string per line
# format: url:(requested URL from input) pageURL:(resolved Location from the PhantomJS "Address Bar") cookie: object containing cookies set on the page
fs.write system.args[2] + ".jsoncookies", JSON.stringify({url:url,pageURL:page.url,cookie:page.cookies})+"\n", 'a'
#keep track of successful requested URLs
completedURLs.push(url)
fs.write system.args[2] + ".completed.txt", url + "\n", 'a'
#and the resolved URLs internally
completedURLs.push(page.url)
#end if successful page open
#else status is fail
else
fs.write system.args[2] + ".failed.txt", url + "\n", 'a'
#end else fail status
#clean up the page instance (PhantomJS 1.7+)
page.close()
sem.execute()
# End page.open() callback
## End callback definitions
#end do(url)
#end if url is not completed already
else
fs.write system.args[2] + ".skipped.txt", url + "\n", 'a'
sem.execute()
#end else
#end if url > 0
#end for url from input file
#end else
semaphore.js
JavaScript
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
/*###Copyright (C) 2013, Neohapsis, Inc. All rights reserved.
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#• Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#• Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or #other materials provided with the distribution.
#• Neither the name of Neohapsis, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#The above licensing was taken from the BSD licensing and is applied to CHS2 as well.
#Note that the CHS2 is provided as is, and is a royalty free open-source application.
#Feel free to modify, use, change, market, do whatever you want with it as long as you give the appropriate credit where credit is due (which means giving the authors the credit they deserve for writing it).
#
#
# semaphore.js by Ben Toews
*/
var Semaphore = function(callback, context) {
this.semaphore = 0;
this.callback = callback;
this.context = context || this;
};
Semaphore.prototype.increment = function() {
this.semaphore++;
};
Semaphore.prototype.execute = function() {
this.semaphore--;
if (this.semaphore <= 0 && this.callback) {
this.callback.apply(this.context, arguments); //this means that the args that actually reach the callback will be from the LAST async block to call .execute();
}
};
module.exports = Semaphore;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.