Sync with AliasIO (#1)

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

* Add js field to NPM driver

* Port env to js
main
Camille Barneaud 8 years ago committed by GitHub
parent 28d2afaa61
commit a1079095dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,7 +6,7 @@ set -eu
echo "Validating apps.json..." echo "Validating apps.json..."
jsonlint-cli -s schema.json src/apps.json jsonlint-cli -tps schema.json src/apps.json > /tmp/apps.json && mv /tmp/apps.json src/apps.json
echo "Validating regular expressions..." echo "Validating regular expressions..."

@ -35,9 +35,9 @@
}, },
"required": true "required": true
}, },
"env": { "js": {
"type": [ "string", "array" ], "type": "object",
"items": { "additionalProperties": {
"type": "string" "type": "string"
} }
}, },

File diff suppressed because it is too large Load Diff

@ -37,6 +37,8 @@ class Driver {
this.wappalyzer.apps = json.apps; this.wappalyzer.apps = json.apps;
this.wappalyzer.categories = json.categories; this.wappalyzer.categories = json.categories;
this.wappalyzer.parseJsPatterns();
this.wappalyzer.driver.log = (message, source, type) => this.log(message, source, type); this.wappalyzer.driver.log = (message, source, type) => this.log(message, source, type);
this.wappalyzer.driver.displayApps = detected => this.displayApps(detected); this.wappalyzer.driver.displayApps = detected => this.displayApps(detected);
} }
@ -120,34 +122,15 @@ class Driver {
.finally(() => { .finally(() => {
this.timer('browser.wait end'); this.timer('browser.wait end');
const headers = {}; const headers = this.getHeaders(browser);
const html = this.getHtml(browser);
browser.resources['0'].response.headers._headers.forEach(header => { const scripts = this.getScripts(browser);
if ( !headers[header[0]] ){ const js = this.getJs(browser);
headers[header[0]] = [];
}
headers[header[0]].push(header[1]);
});
let html = '';
try {
html = browser.html();
} catch ( e ) {
this.wappalyzer.log(error.message, 'browser', 'error');
}
const vars = Object.getOwnPropertyNames(browser.window);
const scripts = Array.prototype.slice
.apply(browser.document.scripts)
.filter(s => s.src)
.map(s => s.src);
this.wappalyzer.analyze(pageUrl.hostname, pageUrl.href, { this.wappalyzer.analyze(pageUrl.hostname, pageUrl.href, {
headers, headers,
html, html,
env: vars, js,
scripts scripts
}); });
@ -160,6 +143,70 @@ class Driver {
}); });
} }
getHeaders(browser) {
const headers = {};
browser.resources['0'].response.headers._headers.forEach(header => {
if ( !headers[header[0]] ){
headers[header[0]] = [];
}
headers[header[0]].push(header[1]);
});
return headers;
}
getHtml(browser) {
let html = '';
try {
html = browser.html();
} catch ( e ) {
this.wappalyzer.log(error.message, 'browser', 'error');
}
return html;
}
getScripts(browser) {
const scripts = Array.prototype.slice
.apply(browser.document.scripts)
.filter(s => s.src)
.map(s => s.src);
return scripts;
}
getJs(browser) {
const patterns = this.wappalyzer.jsPatterns;
const js = {};
Object.keys(patterns).forEach(appName => {
js[appName] = {};
Object.keys(patterns[appName]).forEach(chain => {
js[appName][chain] = {};
patterns[appName][chain].forEach((pattern, index) => {
const properties = chain.split('.');
let value = properties.reduce((parent, property) => {
return parent && parent.hasOwnProperty(property) ? parent[property] : null;
}, browser.window);
value = typeof value === 'string' ? value : !!value;
if ( value ) {
js[appName][chain][index] = value;
}
});
});
});
return js;
}
crawl(pageUrl, index = 1, depth = 1) { crawl(pageUrl, index = 1, depth = 1) {
this.timer('crawl'); this.timer('crawl');

@ -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 = detectJs(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 detectJs(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,20 @@ class Wappalyzer {
} }
/** /**
* Analyze JS variables * Analyze JavaScript variables
*/ */
analyzeJS(app, js) { analyzeJs(app, results) {
Object.keys(js).forEach(property => {
var content = js[property]["content"]; console.log(app, results);
delete js[property]["content"];
js[property].forEach(pattern => { Object.keys(results).forEach(string => {
if ( pattern.regex.test(content) ) { Object.keys(results[string]).forEach(index => {
this.addDetected(app, pattern, 'js', content, property); const pattern = this.jsPatterns[app.name][string][index];
} const value = results[string][index];
if ( pattern.regex.test(value) ) {
this.addDetected(app, pattern, 'js', value);
}
}); });
}); });
} }