You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

565 lines
12 KiB

/**
8 years ago
* Wappalyzer v4
*
* Created by Elbert Alias <elbert@alias.io>
*
* License: GPLv3 http://www.gnu.org/licenses/gpl-3.0.txt
*/
var wappalyzer = (function() {
13 years ago
//'use strict';
/**
* Application class
*/
var Application = function(app, detected) {
this.app = app;
this.confidence = {};
this.confidenceTotal = 0;
this.detected = Boolean(detected);
this.excludes = [];
this.version = '';
this.versions = [];
};
Application.prototype = {
/**
* Calculate confidence total
*/
getConfidence: function() {
var total = 0, id;
for ( id in this.confidence ) {
total += this.confidence[id];
}
return this.confidenceTotal = Math.min(total, 100);
},
/**
* Resolve version number (find the longest version number that contains all shorter detected version numbers)
*/
getVersion: function() {
11 years ago
var i, resolved;
if ( !this.versions.length ) {
return;
}
this.versions.sort(function(a, b) {
11 years ago
return a.length - b.length;
});
resolved = this.versions[0];
11 years ago
for ( i = 1; i < this.versions.length; i++ ) {
if ( this.versions[i].indexOf(resolved) === -1 ) {
11 years ago
break;
}
resolved = this.versions[i];
}
return this.version = resolved;
},
setDetected: function(pattern, type, value, key) {
this.detected = true;
// Set confidence level
this.confidence[type + ' ' + ( key ? key + ' ' : '' ) + pattern.regex] = pattern.confidence ? pattern.confidence : 100;
// Detect version number
if ( pattern.version ) {
var
version = pattern.version,
11 years ago
matches = pattern.regex.exec(value);
if ( matches ) {
matches.forEach(function(match, i) {
// Parse ternary operator
var ternary = new RegExp('\\\\' + i + '\\?([^:]+):(.*)$').exec(version);
if ( ternary && ternary.length === 3 ) {
w.log({ match: match, i: i, ternary: ternary });
version = version.replace(ternary[0], match ? ternary[1] : ternary[2]);
w.log({ version: version });
}
// Replace back references
version = version.replace(new RegExp('\\\\' + i, 'g'), match ? match : '');
});
if ( version && this.versions.indexOf(version) < 0 ) {
this.versions.push(version);
}
this.getVersion();
}
}
}
};
/**
* Call driver functions
*/
var driver = function(func, args) {
if ( typeof w.driver[func] !== 'function' ) {
w.log('not implemented: w.driver.' + func, 'warn');
return;
}
if ( func !== 'log' ) {
w.log('w.driver.' + func);
}
return w.driver[func](args);
};
/**
* Parse apps.json patterns
*/
8 years ago
var parsePatterns = function(patterns) {
var
8 years ago
key,
parsed = {};
8 years ago
// Convert array to object containing array
if ( patterns instanceof Array ) {
patterns = { main: patterns }
}
// Convert string to object containing array containing string
if ( typeof patterns === 'string' ) {
8 years ago
patterns = { main: [ patterns ] };
}
8 years ago
for ( key in patterns ) {
parsed[key] = [];
8 years ago
// Convert string to array containing string
if ( typeof patterns[key] === 'string' ) {
patterns[key] = [ patterns[key] ];
}
8 years ago
patterns[key].forEach(function(pattern) {
var attrs = {};
8 years ago
pattern.split('\\;').forEach(function(attr, i) {
if ( i ) {
// Key value pairs
attr = attr.split(':');
8 years ago
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 (e) {
attrs.regex = new RegExp();
w.log(e + ': ' + attr, 'error');
}
}
8 years ago
});
parsed[key].push(attrs);
});
8 years ago
}
8 years ago
// Convert back to array if the original pattern list was an array (or string)
if ( parsed.hasOwnProperty('main') ) {
parsed = parsed.main;
}
return parsed;
};
/**
* Main script
*/
var w = {
apps: {},
cats: null,
ping: { hostnames: {} },
adCache: [],
detected: {},
config: {
websiteURL: 'https://wappalyzer.com/',
twitterURL: 'https://twitter.com/Wappalyzer',
10 years ago
githubURL: 'https://github.com/AliasIO/Wappalyzer',
},
/**
* Log messages to console
*/
log: function(message, type) {
if ( type === undefined ) {
type = 'debug';
}
if ( typeof message === 'object' ) {
message = JSON.stringify(message);
}
driver('log', { message: message, type: type });
},
/**
* Initialize
*/
init: function() {
w.log('w.init');
// Checks
if ( w.driver === undefined ) {
w.log('no driver, exiting');
12 years ago
return;
}
// Initialize driver
driver('init');
},
/**
* Analyze the request
*/
analyze: function(hostname, url, data) {
var
8 years ago
app, confirmMatch, type,
apps = {};
w.log('w.analyze');
if ( w.apps === undefined || w.categories === undefined ) {
w.log('apps.json not loaded, check for syntax errors');
return;
}
8 years ago
// Remove hash from URL
data.url = url = url.split('#')[0];
if ( typeof data.html !== 'string' ) {
data.html = '';
}
if ( w.detected[url] === undefined ) {
w.detected[url] = {};
}
11 years ago
for ( app in w.apps ) {
apps[app] = w.detected[url] && w.detected[url][app] ? w.detected[url][app] : new Application(app);
for ( type in w.apps[app] ) {
8 years ago
confirmMatch = function(pattern, value, key) {
apps[app].setDetected(pattern, type, value, key);
}
switch ( type ) {
case 'url':
8 years ago
if ( url ) {
8 years ago
w.analyzeUrl(parsePatterns(w.apps[app][type]), url, confirmMatch);
8 years ago
}
break;
case 'html':
8 years ago
if ( data.html ) {
8 years ago
w.analyzeHtml(parsePatterns(w.apps[app][type]), data.html, confirmMatch);
}
break;
case 'script':
8 years ago
if ( data.html ) {
8 years ago
w.analyzeScript(parsePatterns(w.apps[app][type]), data.html, confirmMatch);
}
break;
case 'meta':
8 years ago
if ( data.html ) {
8 years ago
w.analyzeMeta(parsePatterns(w.apps[app][type]), data.html, confirmMatch);
}
break;
case 'headers':
8 years ago
if ( data.hasOwnProperty('headers') && data.headers ) {
8 years ago
w.analyzeHeaders(parsePatterns(w.apps[app][type]), data.headers, confirmMatch);
}
break;
case 'env':
8 years ago
if ( data.hasOwnProperty('env') && data.env ) {
8 years ago
w.analyzeEnv(parsePatterns(w.apps[app][type]), data.env, confirmMatch);
}
break;
default:
}
}
}
for ( app in apps ) {
if ( !apps[app].detected ) {
delete apps[app];
}
}
8 years ago
w.resolveExcludes(apps);
w.resolveImplies(apps, url);
w.cacheDetectedApps(apps, url);
w.trackDetectedApps(apps, url, hostname, data.html);
w.log(Object.keys(apps).length + ' apps detected: ' + Object.keys(apps).join(', ') + ' on ' + url);
driver('displayApps');
},
resolveExcludes: function(apps) {
var
app,
excludes = [];
// Exclude app in detected apps only
for ( app in apps ) {
8 years ago
if ( w.apps[app].excludes ) {
if ( typeof w.apps[app].excludes === 'string' ) {
w.apps[app].excludes = [ w.apps[app].excludes ];
}
w.apps[app].excludes.forEach(function(excluded) {
excludes.push(excluded);
});
}
}
// Remove excluded applications
for ( app in apps ) {
if ( excludes.indexOf(app) !== -1 ) {
delete apps[app];
}
}
8 years ago
},
resolveImplies: function(apps, url) {
var
confidence,
id,
checkImplies = true;
// Implied applications
// Run several passes as implied apps may imply other apps
11 years ago
while ( checkImplies ) {
checkImplies = false;
11 years ago
for ( app in apps ) {
confidence = apps[app].confidence;
if ( w.apps[app] && w.apps[app].implies ) {
// Cast strings to an array
if ( typeof w.apps[app].implies === 'string' ) {
w.apps[app].implies = [ w.apps[app].implies ];
}
w.apps[app].implies.forEach(function(implied) {
8 years ago
implied = parsePatterns(implied)[0];
if ( !w.apps[implied.string] ) {
w.log('Implied application ' + implied.string + ' does not exist', 'warn');
return;
}
if ( !apps.hasOwnProperty(implied.string) ) {
apps[implied.string] = w.detected[url] && w.detected[url][implied.string] ? w.detected[url][implied.string] : new Application(implied.string, true);
11 years ago
11 years ago
checkImplies = true;
}
// Apply app confidence to implied app
for ( id in confidence ) {
apps[implied.string].confidence[id + ' implied by ' + app] = confidence[id] * ( implied.confidence ? implied.confidence / 100 : 1 );
}
});
}
}
}
8 years ago
},
8 years ago
/**
* Cache detected applications
*/
cacheDetectedApps: function(apps, url) {
var app, id, confidence;
for ( app in apps ) {
confidence = apps[app].confidence;
// Per URL
w.detected[url][app] = apps[app];
for ( id in confidence ) {
w.detected[url][app].confidence[id] = confidence[id];
}
8 years ago
}
},
8 years ago
/**
* Track detected applications
*/
trackDetectedApps: function(apps, url, hostname, html) {
var app, match;
for ( app in apps ) {
if ( w.detected[url][app].getConfidence() >= 100 ) {
if ( /(www.)?((.+?)\.(([a-z]{2,3}\.)?[a-z]{2,6}))$/.test(hostname) && !/((local|dev(elopment)?|stag(e|ing)?|test(ing)?|demo(shop)?|admin|google|cache)\.|\/admin|\.local)/.test(url) ) {
if ( !w.ping.hostnames.hasOwnProperty(hostname) ) {
8 years ago
w.ping.hostnames[hostname] = {
applications: {},
meta: {}
};
}
if ( !w.ping.hostnames[hostname].applications.hasOwnProperty(app) ) {
8 years ago
w.ping.hostnames[hostname].applications[app] = {
hits: 0
};
}
w.ping.hostnames[hostname].applications[app].hits ++;
8 years ago
if ( apps[app].version ) {
w.ping.hostnames[hostname].applications[app].version = apps[app].version;
}
} else {
w.log('Ignoring hostname "' + hostname + '"');
}
}
}
// Additional information
if ( w.ping.hostnames.hasOwnProperty(hostname) ) {
8 years ago
match = html.match(/<html[^>]*[: ]lang="([a-z]{2}((-|_)[A-Z]{2})?)"/i);
8 years ago
if ( match && match.length ) {
w.ping.hostnames[hostname].meta['language'] = match[1];
}
}
if ( Object.keys(w.ping.hostnames).length >= 50 || w.adCache.length >= 50 ) {
driver('ping');
}
8 years ago
},
/**
* Analyze URL
*/
analyzeUrl: function(patterns, url, confirmMatch) {
patterns.forEach(function(pattern) {
if ( pattern.regex.test(url) ) {
confirmMatch(pattern, url);
}
});
},
/**
* Analyze HTML
*/
analyzeHtml: function(patterns, html, confirmMatch) {
patterns.forEach(function(pattern) {
if ( pattern.regex.test(html) ) {
confirmMatch(pattern, html);
}
});
},
/**
* Analyze script tag
*/
analyzeScript: function(patterns, html, confirmMatch) {
var regex = new RegExp('<script[^>]+src=("|\')([^"\']+)', 'ig');
patterns.forEach(function(pattern) {
var match;
while ( match = regex.exec(html) ) {
if ( pattern.regex.test(match[2]) ) {
confirmMatch(pattern, match[2]);
}
}
});
},
/**
* Analyze meta tag
*/
analyzeMeta: function(patterns, html, confirmMatch) {
var
8 years ago
content, match, meta,
8 years ago
regex = /<meta[^>]+>/ig;
while ( match = regex.exec(html) ) {
for ( meta in patterns ) {
if ( new RegExp('(name|property)=["\']' + meta + '["\']', 'i').test(match) ) {
content = match.toString().match(/content=("|')([^"']+)("|')/i);
patterns[meta].forEach(function(pattern) {
if ( content && content.length === 4 && pattern.regex.test(content[2]) ) {
confirmMatch(pattern, content[2], meta);
}
});
}
}
}
},
/**
* analyze response headers
*/
analyzeHeaders: function(patterns, headers, confirmMatch) {
var header;
8 years ago
for ( header in patterns ) {
patterns[header].forEach(function(pattern) {
header = header.toLowerCase();
if ( headers.hasOwnProperty(header) && pattern.regex.test(headers[header]) ) {
confirmMatch(pattern, headers[header], header);
}
});
}
},
/**
* Analyze environment variables
*/
analyzeEnv: function(patterns, envs, confirmMatch) {
var env;
patterns.forEach(function(pattern) {
for ( env in envs ) {
if ( pattern.regex.test(envs[env]) ) {
confirmMatch(pattern, envs[env]);
}
}
});
}
};
return w;
})();
// CommonJS package
// See http://wiki.commonjs.org/wiki/CommonJS
if ( typeof exports === 'object' ) {
exports.wappalyzer = wappalyzer;
}