Skip to content

Instantly share code, notes, and snippets.

@neo-brent
Last active December 19, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neo-brent/5963782 to your computer and use it in GitHub Desktop.
Save neo-brent/5963782 to your computer and use it in GitHub Desktop.
Cookie Header Scanner iteration 2 for Part 1 of the PhantomJS blog post series
###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
/*###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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment