This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
492 lines (439 loc) · 13.4 KB
/
index.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"use strict";
require("dotenv").config();
const getPort = require("get-port");
const pptrFirefox = require("puppeteer-firefox");
const webExt = require("web-ext").default;
const util = require("util");
const same = require("./same");
const crawler = require("./crawler");
const config = {
sites: [
{
baseUrl: "https://madales.altervista.org/",
auth: {
login: async (page, profile) => {
await page.goto(
"https://madales.altervista.org/arhunt/public/signin"
);
await page.type("#nickname", profile.userid);
await page.type("#password", profile.password);
await page.click("#form-signin button");
await page.waitForNavigation({ waitUntil: "load" });
},
logout: async page => {
await page.goto(
"https://madales.altervista.org/arhunt/public/signout"
);
},
profiles: {
alice: {
userid: "alicehacker",
password: "password"
},
bob: {
userid: "bobhacker",
password: "password"
}
}
},
navigation: {
homePageUrl: "https://madales.altervista.org/arhunt/public/home",
includeDomains: [],
excludeUrls: [
"https://madales.altervista.org/arhunt/public/signin",
"https://madales.altervista.org/arhunt/public/signout"
]
}
},
{
baseUrl: "http://172.18.0.1:8080/",
auth: {
login: async (page, profile) => {
await page.goto("http://172.18.0.1:8080/login/index.php");
await page.type("#username", profile.userid);
await page.type("#password", profile.password);
await page.click("#loginbtn");
try {
await page.waitForNavigation({ timeout: 7500, waitUntil: "load" });
} catch {}
},
logout: async page => {
await page.goto("http://172.18.0.1:8080/login/logout.php");
const logoutButton = await page.$("single_button5d01fcf3d72bd14");
await logoutButton.click();
try {
await page.waitForNavigation({ timeout: 7500, waitUntil: "load" });
} catch {}
},
profiles: {
alice: {
userid: "alice",
password: "Passw0rd*"
},
bob: {
userid: "bob",
password: "Passw0rd*"
}
}
},
navigation: {
homePageUrl: "http://172.18.0.1:8080/my/",
includeDomains: [],
excludeUrls: [url => url.indexOf("logout") !== -1]
}
}
]
};
(async () => {
const CDPPort = await getPort();
await webExt.cmd.run(
{
sourceDir: process.env.MITCH_EXT,
firefox: pptrFirefox.executablePath(),
args: [`-juggler=${CDPPort}`, "-headless"]
},
{
// These are non CLI related options for each function.
// You need to specify this one so that your NodeJS application
// can continue running after web-ext is finished.
shouldExitProgram: false
}
);
const browserWSEndpoint = `ws://127.0.0.1:${CDPPort}`;
const browser = await pptrFirefox.connect({
browserWSEndpoint
});
const page = await browser.newPage();
await page.setUserAgent(
"Mozilla/5.0 (X11; Linux i586; rv:63.0) Gecko/20100101 Firefox/63.0"
);
// await page.setViewport({ width: 1024, height: 768 });
const site = config.sites[0];
await page.goto(site.baseUrl);
/*
console.log("Looking for Mitch...");
if ("Hello Mitch!" !== (await queryMitch(page, "echo", "Hello Mitch!"))) {
console.error("Mitch is not available");
return;
}
console.log("Mitch is ready");
await site.auth.login(page, site.auth.profiles.alice);
await queryMitch(page, "start_Alice1");
console.log("start_Alice1");
await navigate(page, site);
await queryMitch(page, "finished_Alice1");
console.log("finished_Alice1");
await site.auth.logout(page);
await queryMitch(page, "logged_out_Alice1");
console.log("logged_out_Alice1");
await site.auth.login(page, site.auth.profiles.bob);
await queryMitch(page, "logged_in_Bob");
console.log("logged_in_Bob");
await site.auth.logout(page);
await queryMitch(page, "logged_out_Bob");
console.log("logged_out_Bob");
await site.auth.login(page, site.auth.profiles.alice);
await queryMitch(page, "logged_in_Alice2");
console.log("logged_in_Alice2");
await site.auth.logout(page);
await queryMitch(page, "logged_out_Alice2");
console.log("logged_out_Alice2");
console.log("SUCCESS");
console.log(
"Collected sensitive requests: " +
(await queryMitch(page, "collected_sensitive_requests"))
);
console.log(
"Collected total requests: " +
(await queryMitch(page, "collected_total_requests"))
);
console.log(
util.inspect(await queryMitch(page, "guessCSRFs"), {
showHidden: false,
depth: null
})
);
/* */
await site.auth.login(page, site.auth.profiles.alice);
await navigate(page, site);
await site.auth.logout(page);
await browser.close();
})();
async function navigate(page, site) {
page.on("popup", async popup => {
await popup.close();
});
page.on("dialog", async dialog => {
await dialog.accept("Lorem ipsum");
});
let httpStatusCodeCountStats = {};
page.on("requestfinished", req => {
if (req.isNavigationRequest()) {
console.log("[S] " + req.response().status() + " " + req.url());
}
// Update stats
if (
typeof httpStatusCodeCountStats[req.response().status()] === "undefined"
) {
httpStatusCodeCountStats[req.response().status()] = 0;
}
httpStatusCodeCountStats[req.response().status()]++;
});
let crawlStats;
let linkArray, linkHandleArray;
await crawler.crawl(
async req => {
if (req.request === "page") {
let attempts = 3;
while (attempts-- > 0) {
try {
[linkArray, linkHandleArray] = await filterLinks(
await findLinksInPage(page),
linkIsAllowed,
{
site: async () => site,
location: async () => await page.url()
}
);
return { reply: "page", links: linkArray, url: await page.url() };
} catch {}
}
throw { name: "TerminateRequest" };
} else if (req.request === "follow") {
try {
await visitLinkInPage(
page,
req.link,
linkHandleArray[linkArray.indexOf(req.link)]
);
return { reply: "done" };
} catch (err) {
console.log("[E]", err);
if (
typeof err.message === "string" &&
err.message.startsWith("Protocol error")
) {
throw { name: "TerminateRequest" };
} else if (err.name === "LinkNotVisitableError") {
return { reply: "skip" };
} else if (err.name === "FormNotFillableError") {
return { reply: "mark" };
} else {
throw err;
}
}
} else if (req.request === "home") {
await page.goto(site.navigation.homePageUrl);
return { reply: "done" };
} else if (req.request === "set-stats") {
crawlStats = req.stats;
return { reply: "done" };
}
},
{ dynamicLinks: true }
);
console.log({
distinctFollowUrlsCount: crawlStats.distinctFollowUrls.length,
distinctFollowedUrlsCount: crawlStats.distinctFollowedUrls.length,
graphNodesCount: crawlStats.graphNodesCount,
distinctPageUrlsCount: crawlStats.distinctPageUrls.length,
familiarPagesCount: crawlStats.familiarPagesCount,
httpStatusCodeCount: httpStatusCodeCountStats
});
}
async function findLinksInPage(page) {
const linkHandleArray = await page.$$("a, form");
const linkArray = await Promise.all(
linkHandleArray.map(linkHandle =>
page.evaluate(linkEl => {
const isForm = linkEl.tagName === "FORM";
const linkUrlString =
(isForm ? linkEl.action : linkEl.href) || location.href;
const linkUrl = new URL(linkUrlString);
const link = {};
link.type = isForm ? "FORM" : "A";
link.method = isForm ? linkEl.method : "GET";
link.url = linkUrlString;
link.dompath = (e => {
const dompath = [];
while (e !== document.body) {
dompath.unshift(e.tagName);
e = e.parentElement;
}
return dompath;
})(linkEl);
link.action = linkUrl.pathname.split("/").filter(s => s.trim() !== "");
link.params = {};
linkUrl.searchParams.forEach((value, key) => {
link.params[key] = value;
});
if (isForm) {
const formData = new FormData(linkEl);
Array.from(formData.entries()).forEach(([key, value]) => {
link.params[key] = value;
});
}
return link;
}, linkHandle)
)
);
return [linkArray, linkHandleArray];
}
function linkIsAllowed(link, args) {
return (
!args.site.navigation.excludeUrls.some(
urlOrPredicate =>
(typeof urlOrPredicate === "string" &&
same.url(link.url, urlOrPredicate)) ||
(typeof urlOrPredicate === "function" && urlOrPredicate(link.url))
) &&
(args.site.navigation.includeDomains.some(url =>
same.domain(link.url, url)
) ||
same.domain(link.url, args.location))
);
}
async function filterLinks([linkArray, linkHandleArray], filterFn, argsFns) {
const args = {};
for (let key in argsFns) {
if (argsFns.hasOwnProperty(key)) {
args[key] = await argsFns[key]();
}
}
const linkFilteredArray = [];
const filterMask = [];
for (let i = 0; i < linkArray.length; i++) {
if (filterFn(linkArray[i], args)) {
linkFilteredArray.push(linkArray[i]);
filterMask.push(true);
} else {
filterMask.push(false);
}
}
const linkHandleFilteredArray = linkHandleArray.filter(
(_, i) => filterMask[i]
);
return [linkFilteredArray, linkHandleFilteredArray];
}
async function visitLinkInPage(page, link, linkHandle) {
if (link.type === "FORM") {
await fillForm(page, linkHandle);
const formValid = await page.evaluate(
formEl => formEl.checkValidity(),
linkHandle
);
if (!formValid) {
throw { name: "FormNotFillableError" };
}
try {
const submitButton = await linkHandle.$(
'input[type="submit"], button[type="submit"]'
);
await submitButton.focus();
await submitButton.type("\n");
} catch (err) {
throw { name: "LinkNotVisitableError", message: err.message };
}
try {
await page.waitForNavigation({
timeout: 7500,
waitUntil: "load"
});
} catch {}
} else {
const linkWithoutNavigation =
same.url(await page.url(), link.url) && link.url.indexOf("#") !== -1;
try {
linkHandle.focus();
linkHandle.type("\n");
if (!linkWithoutNavigation) {
try {
await page.waitForNavigation({
timeout: 7500,
waitUntil: "load"
});
} catch {}
} else {
try {
await page.waitForNavigation({
timeout: 2000,
waitUntil: "load"
});
} catch {}
}
} catch (err) {
throw { name: "LinkNotVisitableError", message: err.message };
}
}
}
async function fillForm(page, formHandle) {
// I don't manage file uploads at the moment...
if ((await formHandle.$$('input[type="file"]')).length > 0) {
throw { name: "FormNotFillableError" };
}
const textInputHandleArray = await formHandle.$$(
'input[type="text"], input[type="search"]'
);
for (let textInputHandle of textInputHandleArray) {
try {
await textInputHandle.type("Lorem ipsum");
} catch (err) {
throw { name: "FormNotFillableError", message: err.message };
}
}
const textareaHandleArray = await formHandle.$$("textarea");
for (let textareaHandle of textareaHandleArray) {
try {
await textareaHandle.type("Lorem ipsum dolor sit amet");
} catch (err) {
throw { name: "FormNotFillableError", message: err.message };
}
}
const radioInputHandleArray = await formHandle.$$('input[type="radio"]');
for (let radioInputHandle of radioInputHandleArray) {
try {
await radioInputHandle.click();
} catch (err) {
throw { name: "FormNotFillableError", message: err.message };
}
}
const checkboxInputHandleArray = await formHandle.$$(
'input[type="checkbox"]'
);
for (let checkboxInputHandle of checkboxInputHandleArray) {
try {
await checkboxInputHandle.click();
} catch (err) {
throw { name: "FormNotFillableError", message: err.message };
}
}
// ...
}
async function queryMitch(page, requestType, requestData = null) {
return JSON.parse(
await page.evaluate(
async (requestType, requestDataJson) =>
await new Promise((resolve, reject) => {
const mRequestId = "" + performance.now();
window.addEventListener(
"mitchreply." + mRequestId,
e => {
resolve(JSON.parse(e.detail).dataJson);
},
{ once: true }
);
window.dispatchEvent(
new CustomEvent("mitchrequest", {
detail: JSON.stringify({
id: mRequestId,
type: requestType,
dataJson: requestDataJson
})
})
);
}),
requestType,
JSON.stringify(requestData)
)
);
}