Refactoring, allow multiple expressions per js field, remove env detection

main
Elbert Alias 8 years ago
parent 28d2afaa61
commit 66c703c2fa

@ -10436,6 +10436,9 @@
"cats": [ "cats": [
"12" "12"
], ],
"js": {
"jQuery.fn.jquery": "^([\\d.]+)\\;version:\\1"
},
"env": "^jQuery$", "env": "^jQuery$",
"icon": "jQuery.svg", "icon": "jQuery.svg",
"script": [ "script": [

@ -1,76 +1,58 @@
/** global: browser */ /** global: browser */
if ( typeof browser !== 'undefined' && typeof document.body !== 'undefined' ) { if ( typeof browser !== 'undefined' && typeof document.body !== 'undefined' ) {
var html = document.documentElement.outerHTML; try {
var html = document.documentElement.outerHTML;
if ( html.length > 50000 ) { if ( html.length > 50000 ) {
html = html.substring(0, 25000) + html.substring(html.length - 25000, html.length); html = html.substring(0, 25000) + html.substring(html.length - 25000, html.length);
} }
var scripts = Array.prototype.slice const scripts = Array.prototype.slice
.apply(document.scripts) .apply(document.scripts)
.filter(s => s.src) .filter(script => script.src)
.map(s => s.src); .map(script => script.src);
try {
browser.runtime.sendMessage({ browser.runtime.sendMessage({
id: 'analyze', id: 'analyze',
subject: { html }, subject: { html, scripts },
source: 'content.js' source: 'content.js'
}); });
browser.runtime.sendMessage({ const script = document.createElement('script');
id: 'analyze',
subject: { scripts },
source: 'content.js'
});
var container = document.createElement('wappalyzerData');
container.setAttribute('id', 'wappalyzerData');
container.setAttribute('style', 'display: none');
var script = document.createElement('script');
script.setAttribute('id', 'wappalyzerEnvDetection');
script.setAttribute('src', browser.extension.getURL('js/inject.js'));
container.addEventListener('wappalyzerEnvEvent', (event => { script.onload = () => {
browser.runtime.sendMessage({ addEventListener('message', event => {
id: 'JS_ready', if ( event.data.id !== 'js' ) {
subject: { },
source: 'content.js'
}, response => {
window.postMessage({patterns: response.patterns}, "*");
});
window.addEventListener('message', (event => {
if (event.data.js === undefined)
return; return;
var js = event.data.js ; }
browser.runtime.sendMessage({ browser.runtime.sendMessage({
id: 'analyze', id: 'analyze',
subject: { js }, subject: {
js: event.data.js
},
source: 'content.js' source: 'content.js'
}); });
}), true); }, true);
var env = event.target.childNodes[0].nodeValue;
document.documentElement.removeChild(container); ( chrome || browser ).runtime.sendMessage({
document.documentElement.removeChild(script); id: 'init_js',
subject: {},
env = env.split(' ').slice(0, 500);
browser.runtime.sendMessage({
id: 'analyze',
subject: { env },
source: 'content.js' source: 'content.js'
}, response => {
postMessage({
id: 'patterns',
patterns: response.patterns
}, '*');
}); });
}), true); };
script.setAttribute('id', 'wappalyzer');
script.setAttribute('src', browser.extension.getURL('js/inject.js'));
document.documentElement.appendChild(container); document.body.appendChild(script);
document.documentElement.appendChild(script); } catch (e) {
} catch(e) {
log(e); log(e);
} }
} }

@ -74,7 +74,8 @@ fetch('../apps.json')
.then(json => { .then(json => {
wappalyzer.apps = json.apps; wappalyzer.apps = json.apps;
wappalyzer.categories = json.categories; wappalyzer.categories = json.categories;
wappalyzer.parseJS();
wappalyzer.parseJsPatterns();
categoryOrder = Object.keys(wappalyzer.categories).sort((a, b) => wappalyzer.categories[a].priority - wappalyzer.categories[b].priority); categoryOrder = Object.keys(wappalyzer.categories).sort((a, b) => wappalyzer.categories[a].priority - wappalyzer.categories[b].priority);
}) })
@ -193,9 +194,9 @@ browser.webRequest.onCompleted.addListener(request => {
}; };
break; break;
case 'JS_ready': case 'init_js':
response = { response = {
patterns: wappalyzer.parsedJS patterns: wappalyzer.jsPatterns
}; };
break; break;

@ -1,43 +1,43 @@
(function() { (function() {
try { try {
var i, environmentVars = '', eEnv = document.createEvent('Events'), container = document.getElementById('wappalyzerData'); addEventListener('message', (event => {
if ( event.data.id !== 'patterns' ) {
eEnv.initEvent('wappalyzerEnvEvent', true, false); return;
}
for ( i in window ) {
environmentVars += i + ' '; const patterns = event.data.patterns || {};
}
container.appendChild(document.createComment(environmentVars)); const js = {};
container.dispatchEvent(eEnv);
Object.keys(patterns).forEach(appName => {
window.addEventListener('message', (event => { js[appName] = {};
if (event.data.patterns === undefined)
return; Object.keys(patterns[appName]).forEach(chain => {
var properties = event.data.patterns; js[appName][chain] = {};
var js = {};
Object.keys(properties).forEach(appname => { patterns[appName][chain].forEach((pattern, index) => {
Object.keys(properties[appname]).forEach(property => { const value = detect(chain);
var content = false;
if( content = JSdetection(property) ){ if ( value ) {
if ( js[appname] === undefined ) js[appName][chain][index] = value;
js[appname] = {}; }
js[appname][property] = properties[appname][property]; });
js[appname][property]["content"] = content; });
} });
});
}); postMessage({ id: 'js', js }, '*');
window.postMessage({js: js}, "*"); }), false);
}), false); } catch(e) {
} catch(e) { // Fail quietly
// Fail quietly }
}
}()); }());
function JSdetection(p){ function detect(chain) {
const objects = p.split('.'); const properties = chain.split('.');
const value = objects.reduce((parent, property) => {
return parent && parent.hasOwnProperty(property) ? parent[property] : null; const value = properties.reduce((parent, property) => {
}, window); return parent && parent.hasOwnProperty(property) ? parent[property] : null;
}, window);
return typeof value === 'string' ? value : !!value; return typeof value === 'string' ? value : !!value;
} }

@ -18,7 +18,7 @@ class Wappalyzer {
this.apps = {}; this.apps = {};
this.categories = {}; this.categories = {};
this.driver = {}; this.driver = {};
this.parsedJS = undefined; this.jsPatterns = {};
this.detected = {}; this.detected = {};
this.hostnameCache = {}; this.hostnameCache = {};
@ -85,7 +85,7 @@ class Wappalyzer {
if ( data.js ) { if ( data.js ) {
Object.keys(data.js).forEach(appName => { Object.keys(data.js).forEach(appName => {
this.analyzeJS(apps[appName], data.js[appName]); this.analyzeJs(apps[appName], data.js[appName]);
}); });
} }
@ -252,16 +252,14 @@ class Wappalyzer {
} }
/** /**
* Parse JS patterns * Parse JavaScript patterns
*/ */
parseJS() { parseJsPatterns() {
if ( this.parsedJS === undefined){ Object.keys(this.apps).forEach(appName => {
this.parsedJS = {}; if ( this.apps[appName].js ) {
Object.keys(this.apps).forEach(appName => { this.jsPatterns[appName] = this.parsePatterns(this.apps[appName].js);
if (this.apps[appName].js) }
this.parsedJS[appName] = this.parsePatterns(this.apps[appName].js); });
});
}
} }
resolveExcludes(apps) { resolveExcludes(apps) {
@ -508,16 +506,17 @@ class Wappalyzer {
} }
/** /**
* Analyze JS variables * Analyze JavaScript variables
*/ */
analyzeJS(app, js) { analyzeJs(app, results) {
Object.keys(js).forEach(property => { Object.keys(results).forEach(string => {
var content = js[property]["content"]; Object.keys(results[string]).forEach(index => {
delete js[property]["content"]; const pattern = this.jsPatterns[app.name][string][index];
js[property].forEach(pattern => { const value = results[string][index];
if ( pattern.regex.test(content) ) {
this.addDetected(app, pattern, 'js', content, property); if ( pattern.regex.test(value) ) {
} this.addDetected(app, pattern, 'js', value);
}
}); });
}); });
} }