forked from Lissy93/wapalyzer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wappalyzer.js
672 lines (534 loc) · 16 KB
/
wappalyzer.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/**
* Wappalyzer v5
*
* Created by Elbert Alias <elbert@alias.io>
*
* License: GPLv3 http://www.gnu.org/licenses/gpl-3.0.txt
*/
const validation = {
hostname: /(www.)?((.+?)\.(([a-z]{2,3}\.)?[a-z]{2,6}))$/,
hostnameBlacklist: /((local|dev(elopment)?|stag(e|ing)?|test(ing)?|demo(shop)?|admin|google|cache)\.|\/admin|\.local)/,
};
/**
* Enclose string in array
*/
function asArray(value) {
return value instanceof Array ? value : [value];
}
/**
*
*/
function asyncForEach(iterable, iterator) {
return Promise.all((iterable || [])
.map(item => new Promise(resolve => setTimeout(() => resolve(iterator(item)), 1))));
}
/**
* Mark application as detected, set confidence and version
*/
function addDetected(app, pattern, type, value, key) {
app.detected = true;
// Set confidence level
app.confidence[`${type} ${key ? `${key} ` : ''}${pattern.regex}`] = pattern.confidence === undefined ? 100 : parseInt(pattern.confidence, 10);
// Detect version number
if (pattern.version) {
const versions = [];
const matches = pattern.regex.exec(value);
let { version } = pattern;
if (matches) {
matches.forEach((match, i) => {
// Parse ternary operator
const ternary = new RegExp(`\\\\${i}\\?([^:]+):(.*)$`).exec(version);
if (ternary && ternary.length === 3) {
version = version.replace(ternary[0], match ? ternary[1] : ternary[2]);
}
// Replace back references
version = version.trim().replace(new RegExp(`\\\\${i}`, 'g'), match || '');
});
if (version && versions.indexOf(version) === -1) {
versions.push(version);
}
if (versions.length) {
// Use the longest detected version number
app.version = versions.reduce((a, b) => (a.length > b.length ? a : b));
}
}
}
}
function resolveExcludes(apps, detected) {
const excludes = [];
const detectedApps = Object.assign({}, apps, detected);
// Exclude app in detected apps only
Object.keys(detectedApps).forEach((appName) => {
const app = detectedApps[appName];
if (app.props.excludes) {
asArray(app.props.excludes).forEach((excluded) => {
excludes.push(excluded);
});
}
});
// Remove excluded applications
Object.keys(apps).forEach((appName) => {
if (excludes.indexOf(appName) > -1) {
delete apps[appName];
}
});
}
class Application {
constructor(name, props, detected) {
this.confidence = {};
this.confidenceTotal = 0;
this.detected = Boolean(detected);
this.excludes = [];
this.name = name;
this.props = props;
this.version = '';
}
/**
* Calculate confidence total
*/
getConfidence() {
let total = 0;
Object.keys(this.confidence).forEach((id) => {
total += this.confidence[id];
});
this.confidenceTotal = Math.min(total, 100);
return this.confidenceTotal;
}
}
class Wappalyzer {
constructor() {
this.apps = {};
this.categories = {};
this.driver = {};
this.jsPatterns = {};
this.detected = {};
this.hostnameCache = {};
this.adCache = [];
this.config = {
websiteURL: 'https://www.wappalyzer.com/',
twitterURL: 'https://twitter.com/Wappalyzer',
githubURL: 'https://github.com/AliasIO/Wappalyzer',
};
}
/**
* Log messages to console
*/
log(message, source, type) {
if (this.driver.log) {
this.driver.log(message, source || '', type || 'debug');
}
}
analyze(url, data, context) {
const apps = {};
const promises = [];
const startTime = new Date();
const {
scripts,
cookies,
headers,
js,
} = data;
let { html } = data;
if (this.detected[url.canonical] === undefined) {
this.detected[url.canonical] = {};
}
const metaTags = [];
// Additional information
let language = null;
if (html) {
if (typeof html !== 'string') {
html = '';
}
let matches = data.html.match(new RegExp('<html[^>]*[: ]lang="([a-z]{2}((-|_)[A-Z]{2})?)"', 'i'));
language = matches && matches.length ? matches[1] : data.language || null;
// Meta tags
const regex = /<meta[^>]+>/ig;
do {
matches = regex.exec(html);
if (!matches) {
break;
}
metaTags.push(matches[0]);
} while (matches);
}
Object.keys(this.apps).forEach((appName) => {
apps[appName] = this.detected[url.canonical] && this.detected[url.canonical][appName]
? this.detected[url.canonical][appName]
: new Application(appName, this.apps[appName]);
const app = apps[appName];
promises.push(this.analyzeUrl(app, url));
if (html) {
promises.push(this.analyzeHtml(app, html));
promises.push(this.analyzeMeta(app, metaTags));
}
if (scripts) {
promises.push(this.analyzeScripts(app, scripts));
}
if (cookies) {
promises.push(this.analyzeCookies(app, cookies));
}
if (headers) {
promises.push(this.analyzeHeaders(app, headers));
}
});
if (js) {
Object.keys(js).forEach((appName) => {
if (typeof js[appName] !== 'function') {
promises.push(this.analyzeJs(apps[appName], js[appName]));
}
});
}
return new Promise(async (resolve) => {
await Promise.all(promises);
Object.keys(apps).forEach((appName) => {
const app = apps[appName];
if (!app.detected || !app.getConfidence()) {
delete apps[app.name];
}
});
resolveExcludes(apps, this.detected[url]);
this.resolveImplies(apps, url.canonical);
this.cacheDetectedApps(apps, url.canonical);
this.trackDetectedApps(apps, url, language);
this.log(`Processing ${Object.keys(data).join(', ')} took ${((new Date() - startTime) / 1000).toFixed(2)}s (${url.hostname})`, 'core');
if (Object.keys(apps).length) {
this.log(`Identified ${Object.keys(apps).join(', ')} (${url.hostname})`, 'core');
}
this.driver.displayApps(this.detected[url.canonical], { language }, context);
return resolve();
});
}
/**
* Cache detected ads
*/
cacheDetectedAds(ad) {
this.adCache.push(ad);
}
/**
*
*/
robotsTxtAllows(url) {
return new Promise(async (resolve, reject) => {
const parsed = this.parseUrl(url);
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
return reject();
}
const robotsTxt = await this.driver.getRobotsTxt(parsed.host, parsed.protocol === 'https:');
if (robotsTxt.some(disallowedPath => parsed.pathname.indexOf(disallowedPath) === 0)) {
return reject();
}
return resolve();
});
}
/**
* Parse a URL
*/
parseUrl(url) {
const a = this.driver.document.createElement('a');
a.href = url;
a.canonical = `${a.protocol}//${a.host}${a.pathname}`;
return a;
}
/**
*
*/
static parseRobotsTxt(robotsTxt) {
const disallow = [];
let userAgent;
robotsTxt.split('\n').forEach((line) => {
let matches = /^User-agent:\s*(.+)$/i.exec(line.trim());
if (matches) {
userAgent = matches[1].toLowerCase();
} else if (userAgent === '*' || userAgent === 'wappalyzer') {
matches = /^Disallow:\s*(.+)$/i.exec(line.trim());
if (matches) {
disallow.push(matches[1]);
}
}
});
return disallow;
}
/**
*
*/
ping() {
if (Object.keys(this.hostnameCache).length > 50) {
this.driver.ping(this.hostnameCache);
this.hostnameCache = {};
}
if (this.adCache.length > 50) {
this.driver.ping({}, this.adCache);
this.adCache = [];
}
}
/**
* Parse apps.json patterns
*/
parsePatterns(patterns) {
if (!patterns) {
return [];
}
let parsed = {};
// Convert string to object containing array containing string
if (typeof patterns === 'string' || patterns instanceof Array) {
patterns = {
main: asArray(patterns),
};
}
Object.keys(patterns).forEach((key) => {
parsed[key] = [];
asArray(patterns[key]).forEach((pattern) => {
const attrs = {};
pattern.split('\\;').forEach((attr, i) => {
if (i) {
// Key value pairs
attr = attr.split(':');
if (attr.length > 1) {
attrs[attr.shift()] = attr.join(':');
}
} else {
attrs.string = attr;
try {
attrs.regex = new RegExp(attr.replace('/', '\/'), 'i'); // Escape slashes in regular expression
} catch (error) {
attrs.regex = new RegExp();
this.log(`${error.message}: ${attr}`, 'error', 'core');
}
}
});
parsed[key].push(attrs);
});
});
// Convert back to array if the original pattern list was an array (or string)
if ('main' in parsed) {
parsed = parsed.main;
}
return parsed;
}
/**
* Parse JavaScript patterns
*/
parseJsPatterns() {
Object.keys(this.apps).forEach((appName) => {
if (this.apps[appName].js) {
this.jsPatterns[appName] = this.parsePatterns(this.apps[appName].js);
}
});
}
resolveImplies(apps, url) {
let checkImplies = true;
const resolve = (appName) => {
const app = apps[appName];
if (app && app.props.implies) {
asArray(app.props.implies).forEach((implied) => {
[implied] = this.parsePatterns(implied);
if (!this.apps[implied.string]) {
this.log(`Implied application ${implied.string} does not exist`, 'core', 'warn');
return;
}
if (!(implied.string in apps)) {
apps[implied.string] = this.detected[url] && this.detected[url][implied.string]
? this.detected[url][implied.string]
: new Application(implied.string, this.apps[implied.string], true);
checkImplies = true;
}
// Apply app confidence to implied app
Object.keys(app.confidence).forEach((id) => {
apps[implied.string].confidence[`${id} implied by ${appName}`] = app.confidence[id] * (implied.confidence === undefined ? 1 : implied.confidence / 100);
});
});
}
};
// Implied applications
// Run several passes as implied apps may imply other apps
while (checkImplies) {
checkImplies = false;
Object.keys(apps).forEach(resolve);
}
}
/**
* Cache detected applications
*/
cacheDetectedApps(apps, url) {
Object.keys(apps).forEach((appName) => {
const app = apps[appName];
// Per URL
this.detected[url][appName] = app;
Object.keys(app.confidence)
.forEach((id) => {
this.detected[url][appName].confidence[id] = app.confidence[id];
});
});
if (this.driver.ping instanceof Function) {
this.ping();
}
}
/**
* Track detected applications
*/
trackDetectedApps(apps, url, language) {
if (!(this.driver.ping instanceof Function)) {
return;
}
const hostname = `${url.protocol}//${url.hostname}`;
Object.keys(apps).forEach((appName) => {
const app = apps[appName];
if (this.detected[url.canonical][appName].getConfidence() >= 100) {
if (
validation.hostname.test(url.hostname)
&& !validation.hostnameBlacklist.test(url.hostname)
) {
if (!(hostname in this.hostnameCache)) {
this.hostnameCache[hostname] = {
applications: {},
meta: {},
};
}
if (!(appName in this.hostnameCache[hostname].applications)) {
this.hostnameCache[hostname].applications[appName] = {
hits: 0,
};
}
this.hostnameCache[hostname].applications[appName].hits += 1;
if (apps[appName].version) {
this.hostnameCache[hostname].applications[appName].version = app.version;
}
}
}
});
if (hostname in this.hostnameCache) {
this.hostnameCache[hostname].meta.language = language;
}
this.ping();
}
/**
* Analyze URL
*/
analyzeUrl(app, url) {
const patterns = this.parsePatterns(app.props.url);
if (!patterns.length) {
return Promise.resolve();
}
return asyncForEach(patterns, (pattern) => {
if (pattern.regex.test(url.canonical)) {
addDetected(app, pattern, 'url', url.canonical);
}
});
}
/**
* Analyze HTML
*/
analyzeHtml(app, html) {
const patterns = this.parsePatterns(app.props.html);
if (!patterns.length) {
return Promise.resolve();
}
return asyncForEach(patterns, (pattern) => {
if (pattern.regex.test(html)) {
addDetected(app, pattern, 'html', html);
}
});
}
/**
* Analyze script tag
*/
analyzeScripts(app, scripts) {
const patterns = this.parsePatterns(app.props.script);
if (!patterns.length) {
return Promise.resolve();
}
return asyncForEach(patterns, (pattern) => {
scripts.forEach((uri) => {
if (pattern.regex.test(uri)) {
addDetected(app, pattern, 'script', uri);
}
});
});
}
/**
* Analyze meta tag
*/
analyzeMeta(app, metaTags) {
const patterns = this.parsePatterns(app.props.meta);
const promises = [];
if (!app.props.meta) {
return Promise.resolve();
}
metaTags.forEach((match) => {
Object.keys(patterns).forEach((meta) => {
const r = new RegExp(`(?:name|property)=["']${meta}["']`, 'i');
if (r.test(match)) {
const content = match.match(/content=("|')([^"']+)("|')/i);
promises.push(asyncForEach(patterns[meta], (pattern) => {
if (content && content.length === 4 && pattern.regex.test(content[2])) {
addDetected(app, pattern, 'meta', content[2], meta);
}
}));
}
});
});
return Promise.all(promises);
}
/**
* Analyze response headers
*/
analyzeHeaders(app, headers) {
const patterns = this.parsePatterns(app.props.headers);
const promises = [];
Object.keys(patterns).forEach((headerName) => {
if (typeof patterns[headerName] !== 'function') {
promises.push(asyncForEach(patterns[headerName], (pattern) => {
headerName = headerName.toLowerCase();
if (headerName in headers) {
headers[headerName].forEach((headerValue) => {
if (pattern.regex.test(headerValue)) {
addDetected(app, pattern, 'headers', headerValue, headerName);
}
});
}
}));
}
});
return promises ? Promise.all(promises) : Promise.resolve();
}
/**
* Analyze cookies
*/
analyzeCookies(app, cookies) {
const patterns = this.parsePatterns(app.props.cookies);
const promises = [];
Object.keys(patterns).forEach((cookieName) => {
if (typeof patterns[cookieName] !== 'function') {
const cookieNameLower = cookieName.toLowerCase();
promises.push(asyncForEach(patterns[cookieName], (pattern) => {
const cookie = cookies.find(_cookie => _cookie.name.toLowerCase() === cookieNameLower);
if (cookie && pattern.regex.test(cookie.value)) {
addDetected(app, pattern, 'cookies', cookie.value, cookieName);
}
}));
}
});
return promises ? Promise.all(promises) : Promise.resolve();
}
/**
* Analyze JavaScript variables
*/
analyzeJs(app, results) {
const promises = [];
Object.keys(results).forEach((string) => {
if (typeof results[string] !== 'function') {
promises.push(asyncForEach(Object.keys(results[string]), (index) => {
const pattern = this.jsPatterns[app.name][string][index];
const value = results[string][index];
if (pattern && pattern.regex.test(value)) {
addDetected(app, pattern, 'js', value, string);
}
}));
}
});
return promises ? Promise.all(promises) : Promise.resolve();
}
}
if (typeof module === 'object') {
module.exports = Wappalyzer;
}