-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcaptchaparser.js
More file actions
313 lines (300 loc) · 9 KB
/
Copy pathcaptchaparser.js
File metadata and controls
313 lines (300 loc) · 9 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
const captcha_parse = (imgarr) => {
let captcha = "";
for (let x = 1; x < 44; x++) {
for (let y = 1; y < 179; y++) {
const condition1 =
imgarr[x][y - 1] === 255 &&
imgarr[x][y] === 0 &&
imgarr[x][y + 1] === 255;
const condition2 =
imgarr[x - 1][y] === 255 &&
imgarr[x][y] === 0 &&
imgarr[x + 1][y] === 255;
const condition3 = imgarr[x][y] !== 255 && imgarr[x][y] !== 0;
if (condition1 || condition2 || condition3) {
imgarr[x][y] = 255;
}
}
}
for (let j = 30; j < 181; j += 30) {
let matches = [];
const chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
for (let i = 0; i < chars.length; i++) {
let match = 0;
let black = 0;
const ch = chars.charAt(i);
const mask = bitmaps[ch];
for (let x = 0; x < 32; x++) {
for (let y = 0; y < 30; y++) {
let y1 = y + j - 30;
let x1 = x + 12;
if (imgarr[x1][y1] == mask[x][y] && mask[x][y] == 0) {
match += 1;
}
if (mask[x][y] == 0) {
black += 1;
}
}
}
const perc = match / black;
matches.push([perc, ch]);
}
captcha += matches.reduce(
function (a, b) {
return a[0] > b[0] ? a : b;
},
[0, 0]
)[1];
}
return captcha;
};
const uri_to_img_data = (URI) => {
return new Promise(function (resolve, reject) {
if (URI == null) return reject();
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
let image = new Image();
image.addEventListener(
"load",
function () {
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0, canvas.width, canvas.height);
resolve(context.getImageData(0, 0, canvas.width, canvas.height));
},
false
);
image.src = URI;
});
};
const fill_captcha = (imgb64) => {
const URI = imgb64;
uri_to_img_data(URI).then((imageData) => {
let arr = [];
let newArr = [];
for (let i = 0; i < imageData["data"].length; i += 4) {
let gval =
imageData["data"][i] * 0.299 +
imageData["data"][i + 1] * 0.587 +
imageData["data"][i + 2] * 0.114;
arr.push(gval);
}
while (arr.length) newArr.push(arr.splice(0, 180));
const res = captcha_parse(newArr);
document.getElementById("captchaCheck").value = res;
});
};
const solve_captcha = (img) => {
const image = img.src;
fill_captcha(image);
};
const pre_img = (img) => {
let avg = 0;
img.forEach((e) => e.forEach((f) => (avg += f)));
avg /= 24 * 22;
var bits = new Array(img.length);
for (let i = 0; i < img.length; i += 1) {
bits[i] = new Array(img[0].length);
for (let j = 0; j < img[0].length; j += 1) {
if (img[i][j] > avg) {
bits[i][j] = 1;
} else {
bits[i][j] = 0;
}
}
}
return bits;
};
const saturation = (d) => {
saturate = new Array(d.length / 4);
for (let i = 0; i < d.length; i += 4) {
min = Math.min(d[i], d[i + 1], d[i + 2]);
max = Math.max(d[i], d[i + 1], d[i + 2]);
saturate[i / 4] = Math.round(((max - min) * 255) / max);
}
var img = new Array(40);
for (let i = 0; i < 40; i += 1) {
img[i] = new Array(200);
for (let j = 0; j < 200; j += 1) {
img[i][j] = saturate[i * 200 + j];
}
}
bls = new Array(6);
for (let i = 0; i < 6; i += 1) {
c = 0;
d = 0;
x1 = (i + 1) * 25 + 2;
y1 = 7 + 5 * (i % 2) + 1;
x2 = (i + 2) * 25 + 1;
y2 = 35 - 5 * ((i + 1) % 2);
bls[i] = img.slice(y1, y2).map((i) => i.slice(x1, x2));
}
return bls;
};
const flatten = (arr) => {
var bits = new Array(arr.length * arr[0].length);
for (let i = 0; i < arr.length; i += 1) {
for (let j = 0; j < arr[0].length; j += 1) {
bits[i * arr[0].length + j] = arr[i][j];
}
}
return bits;
};
const mat_mul = (a, b) => {
let x = a.length,
z = a[0].length,
y = b[0].length;
let productRow = Array.apply(null, new Array(y)).map(
Number.prototype.valueOf,
0
);
let product = new Array(x);
for (let p = 0; p < x; p++) {
product[p] = productRow.slice();
}
for (let i = 0; i < x; i++) {
for (let j = 0; j < y; j++) {
for (let k = 0; k < z; k++) {
product[i][j] += a[i][k] * b[k][j];
}
}
}
return product;
};
const mat_add = (a, b) => {
let x = a.length;
let c = new Array(x);
for (let i = 0; i < x; i++) {
c[i] = a[i] + b[i];
}
return c;
};
const max_soft = (a) => {
var n = [...a];
let s = 0;
n.forEach((f) => {
s += Math.exp(f);
});
for (let i = 0; i < a.length; i++) {
n[i] = Math.exp(a[i]) / s;
}
return n;
};
const HEIGHT = 40;
const WIDTH = 200;
const solve = (img, textB) => {
fetch(
"chrome-extension://jfmlhhjlkbliphgkmeingeacbijcilcl/js/captcha/weights.json"
)
.then((response) => response.json())
.then((data) => {
const weights = data.weights;
const biases = data.biases;
const label_txt = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
const pd = ctx.getImageData(0, 0, 200, 40);
let bls = saturation(pd.data);
out = "";
for (let i = 0; i < 6; i += 1) {
bls[i] = pre_img(bls[i]);
bls[i] = [flatten(bls[i])];
bls[i] = mat_mul(bls[i], weights);
bls[i] = mat_add(...bls[i], biases);
bls[i] = max_soft(bls[i]);
bls[i] = bls[i].indexOf(Math.max(...bls[i]));
out += label_txt[bls[i]];
}
textB.value = out;
});
};
function myMain(evt) {
if (document.URL.match("https://vtopcc.vit.ac.in/vtop/initialProcess")) {
const jsInitChecktimer = setInterval(checkForJS_Finish, 111);
function checkForJS_Finish() {
let element = document.querySelector('img[alt="vtopCaptcha"]');
if (element) {
clearInterval(jsInitChecktimer);
solve_captcha(element);
let observer = new MutationObserver(function () {
solve_captcha(document.querySelector('img[alt="vtopCaptcha"]'));
});
observer.observe(document.getElementById("captchaRefresh"), {
childList: true,
});
}
}
} else if (
document.URL.match("https://vtop.vitap.ac.in/vtop/initialProcess")
) {
const jsInitChecktimer = setInterval(checkForJS_Finish, 111);
function checkForJS_Finish() {
let element = document.querySelector('img[alt="vtopCaptcha"]');
if (element) {
clearInterval(jsInitChecktimer);
solve_captcha(element);
let observer = new MutationObserver(function () {
solve_captcha(document.querySelector('img[alt="vtopCaptcha"]'));
});
observer.observe(document.getElementById("captchaRefresh"), {
childList: true,
});
}
}
} else if (document.URL.match("https://vtop.vitap.ac.in/vtop/login")) {
function solveVtopLoginCaptcha() {
var img = document.getElementsByClassName(
"form-control img-fluid bg-light border-0"
)[0];
var textB = document.getElementById("captchaStr");
if (img && textB) {
img.style.setProperty("height", "40px", "important");
img.style.setProperty("width", "200px", "important");
solve(img, textB);
var submitB = document.getElementById("submitBtn");
if (submitB) submitB.focus();
}
}
solveVtopLoginCaptcha();
var captchaBlock = document.getElementById("captchaBlock");
if(captchaBlock) {
var observer = new MutationObserver(function () {
solveVtopLoginCaptcha();
});
observer.observe(captchaBlock, { childList: true });
}
} else if (location.hostname === "registration.vitap.ac.in") {
function solveRegistrationCaptcha() {
var img = document.getElementById("captcha_id") ||
document.querySelector('img[name="imgCaptcha"]');
var textB = document.getElementById("captchaString") ||
document.getElementById("captchaStringProgInfo");
if (img && textB) {
img.style.setProperty("height", "40px", "important");
img.style.setProperty("width", "200px", "important");
solve(img, textB);
var submitB = document.getElementById("loginButton") ||
document.querySelector(".btn-action-proceed");
if (submitB) submitB.focus();
}
}
solveRegistrationCaptcha();
var captchaContainer = document.getElementById("test");
if (captchaContainer) {
var observer = new MutationObserver(function () {
solveRegistrationCaptcha();
});
observer.observe(captchaContainer, { childList: true });
}
} else {
var img = document.getElementById("captcha_id");
img.style.height = "40px!important";
img.style.width = "200px!important";
var textB = document.getElementById("captchaString");
var submitB = document.getElementById("loginButton");
solve(img, textB);
submitB.focus();
}
}
window.addEventListener("load", myMain, false);