-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbruteforcer.js
executable file
·216 lines (195 loc) · 6.28 KB
/
bruteforcer.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env casperjs
var utils = require('utils');
var fs = require('fs');
var casper=require('casper').create({
verbose: true,
// logLevel: "debug",
pageSettings: {
javascriptEnabled: true,
localToRemoteUrlAccessEnabled: true,
loadImages: true,
loadPlugins: true,
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'
}
});
var configFile = './bruteforcer.config.js';
if (!fs.exists(configFile)) {
casper.echo('cannot find '+configFile+'!');
casper.exit();
}
var config = JSON.parse(fs.read(configFile));
var devilPuzzleUrl = 'http://regexcrossword.com/playerpuzzles/'+config.puzzle_id;
function permute1d(input, usedChars, permArr) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute1d(input, usedChars, permArr);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr;
}
function permute2d(permutations) {
permutations2d = [];
for (var i=0; i < permutations.length; ++i) {
permute2d_helper(permutations, [permutations[i]], permutations2d, [i]);
}
return permutations2d;
}
function permute2d_helper(permutations, input2d, permutations2d, indexesTried) {
if (input2d.length == 5) {
var row = [];
for(var i=0; i<5; ++i) {
row.push(input2d[i].join(''));
}
permutations2d.push(row);
return permutations2d;
}
for (var i=0; i < permutations.length; ++i) {
if (indexesTried.indexOf(i) != -1) {
continue;
}
var newIndexesTried = indexesTried.slice(0);
var newinput = input2d.slice(0);
newinput.push(permutations[i]);
if (validate2d(newinput)) {
newIndexesTried.push(i);
permute2d_helper(permutations, newinput, permutations2d, newIndexesTried);
}
}
return permutations2d;
}
function validate2d(input2d) {
/* invert rows for columns */
var inverted = [];
for (var i=0; i < input2d.length; ++i) {
for (var j=0; j < 5; ++j) {
if (typeof inverted[j] == 'undefined') {
inverted[j] = [];
}
inverted[j][i] = input2d[i][j];
}
}
for(var i=0; i < 5; ++i) {
if (checkDupes(inverted[i]) === false) {
return false;
}
}
return true;
}
function checkDupes(input) {
var results = [];
var arr = input.slice(0);
var ct = arr.length;
for(i=0; i < ct; ++i) {
var x = arr.pop();
if (arr.indexOf(x) != -1) {
return false;
}
}
return true;
}
var permutations2d = null;
var tempfile = './permutations2d.txt';
if (fs.exists(tempfile)) {
permutations2d = JSON.parse(fs.read(tempfile));
// utils.dump(permutations2d);
}
else {
casper.echo('Generating solutions file on first run, may take some time');
var characters = [1, 2, 3, 4, 5];
var permutations = permute1d(characters, [], []);
var permutations2d = permute2d(permutations);
fs.write(tempfile, JSON.stringify(permutations2d), 'w');
casper.echo('Solutions file written');
}
var sitempfile = './solutionsIndex.txt';
var solutionsIndex = 0;
if (fs.exists(sitempfile)) {
solutionsIndex = fs.read(sitempfile);
}
var puzzleSubmitCount = 0;
casper.on('puzzle.submit', function () {
var solution = getSolution();
if (solution === null) {
this.echo('tried all solutions, found none');
exit(0);
}
var my_soln = {
"puzzle_id": config.puzzle_id,
"answer": solution,
"user_id": config.user_id
}
this.thenOpen(config.baseUrl + '/api/solved',
{
method: 'post',
data: JSON.stringify(my_soln),
headers: {
'Accept' : 'application/json',
'Content-Type': 'application/json',
'Host': 'regexcrossword.com'
}
},
function(response) {
var responseBody = null;
try {
responseBody = JSON.parse(this.page.plainText);
} catch(e) {
}
if (response.status == 200) {
this.echo('we are done!');
utils.dump(my_soln);
this.exit();
}
else if (response.status == 400 && responseBody && responseBody.messages.length == 1 && responseBody.messages[0] == 'answer not valid.') {
this.echo('failed at attempt ' +solutionsIndex);
fs.write(sitempfile, solutionsIndex, 'w');
this.wait(100, function() {
this.emit('puzzle.submit');
});
}
else {
this.echo('shit fuck!');
utils.dump(response);
utils.dump(responseBody);
this.exit();
}
}
);
});
function getSolution() {
if (solutionsIndex < permutations2d.length) {
return permutations2d[solutionsIndex++];
}
return null;
}
casper.start(config.facebookSigninUrl);
casper.then(function() {
this.fill('#login_form', {email: config.user, pass: config.pass});
this.click('[name=login]');
});
casper.thenOpen(config.baseUrl);
casper.waitForResource(function(resource) {
var re = /\/api\/puzzles/;
if (resource.url.match(re)) {
var matches = /user_id=([0-9]+)/.exec(resource.url);
if (matches) {
config.user_id = matches[1];
}
else {
casper.echo("Cannot determine facebook user_id. aborting!\n");
casper.exit();
}
return true;
}
return false;
}, function() {
this.echo('logged in to facebook and loaded!');
});
casper.thenOpen(devilPuzzleUrl);
casper.emit('puzzle.submit');
casper.run();