Skip to content

Instantly share code, notes, and snippets.

@herrcore
Created January 26, 2016 06:27
Show Gist options
  • Save herrcore/8716ab61ca283c5617dd to your computer and use it in GitHub Desktop.
Save herrcore/8716ab61ca283c5617dd to your computer and use it in GitHub Desktop.
Simple CasperJS script to load page with fake referrer and follow all redirects. The HTML for the final page is printed along with the redirect URLs.
//setup casper
var casper = require('casper').create({
verbose: true,
//Fake the user agent
pageSettings: {
userAgent: 'Mozilla/5.0 (Windows NT 5.1; chromeframe/25.0.1364.152) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22'
},
logLevel: "debug"
//logLevel: "error"
});
phantom.cookiesEnabled = true;
var referrer = "";
var target = "";
//get referrer and target from cli
//EXAMPLE: casperjs redirect_hunter.js "http://referrer.com" "http://target.com"
if(casper.cli.has(0)){
referrer = casper.cli.get(0);
}else{
casper.echo("Error: No referrer specified.")
casper.exit();
}
if(casper.cli.has(1)){
target = casper.cli.get(1);
}else{
casper.echo("Error: No referrer specified.")
casper.exit();
}
casper.start();
//handler to print the url for each redirect
casper.on('navigation.requested', function(url, navigationType, navigationLocked, isMainFrame) {
this.echo('Loading URL: ' + url);
this.echo('Will actually navigate: ' + navigationLocked);
this.echo('Caused by: ' + navigationType);
this.echo('Main Frame: ' + isMainFrame);
});
//first load the referrer page
//WARNING: referrer must be a valid website that loads
casper.thenOpen(referrer, function() {
});
//fake the referrer by injecting an href with the target into the referrer page and clicking it
casper.then(function() {
this.evaluate(function(targeturl) {
var a = document.createElement('a');
a.setAttribute('id', 'fakereferrer');
a.href = targeturl;
document.body.appendChild(a);
document.querySelector('form').submit();
}, {
targeturl: target
});
this.click('#fakereferrer'); //Fake a click on the href to add bubble-search as the referrer
this.wait(5000)
});
casper.then(function(){
this.echo('Final URL: ' + this.getCurrentUrl());
//print the HTML from the final page
this.echo(this.getHTML());
this.capture("page.png");
this.exit();
});
casper.run(function() {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment