Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/webrtc/handlers.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
module BeEF
7
module Extension
8
module WebRTC
9
10
#
11
# The http handler that manages the WebRTC signals sent from browsers to other browsers.
12
#
13
class SignalHandler
14
15
R = BeEF::Core::Models::RtcSignal
16
Z = BeEF::Core::Models::HookedBrowser
17
18
def initialize(data)
19
@data = data
20
setup()
21
end
22
23
def setup()
24
25
# validates the hook token
26
beef_hook = @data['beefhook'] || nil
27
(print_error "beefhook is null";return) if beef_hook.nil?
28
29
# validates the target hook token
30
target_beef_id = @data['results']['targetbeefid'] || nil
31
(print_error "targetbeefid is null";return) if target_beef_id.nil?
32
33
# validates the signal
34
signal = @data['results']['signal'] || nil
35
(print_error "Signal is null";return) if signal.nil?
36
37
# validates that a hooked browser with the beef_hook token exists in the db
38
zombie_db = Z.first(:session => beef_hook) || nil
39
(print_error "Invalid beefhook id: the hooked browser cannot be found in the database";return) if zombie_db.nil?
40
41
# validates that a target browser with the target_beef_token exists in the db
42
target_zombie_db = Z.first(:id => target_beef_id) || nil
43
(print_error "Invalid targetbeefid: the target hooked browser cannot be found in the database";return) if target_zombie_db.nil?
44
45
# save the results in the database
46
signal = R.new(
47
:hooked_browser_id => zombie_db.id,
48
:target_hooked_browser_id => target_zombie_db.id,
49
:signal => signal
50
)
51
signal.save
52
53
end
54
end
55
56
#
57
# The http handler that manages the WebRTC messages sent from browsers.
58
#
59
class MessengeHandler
60
61
Z = BeEF::Core::Models::HookedBrowser
62
63
def initialize(data)
64
@data = data
65
setup()
66
end
67
68
def setup()
69
# validates the hook token
70
beef_hook = @data['beefhook'] || nil
71
(print_error "beefhook is null";return) if beef_hook.nil?
72
73
# validates the target hook token
74
peer_id = @data['results']['peerid'] || nil
75
(print_error "peerid is null";return) if peer_id.nil?
76
77
# validates the message
78
message = @data['results']['message'] || nil
79
(print_error "Message is null";return) if message.nil?
80
81
# validates that a hooked browser with the beef_hook token exists in the db
82
zombie_db = Z.first(:session => beef_hook) || nil
83
(print_error "Invalid beefhook id: the hooked browser cannot be found in the database";return) if zombie_db.nil?
84
85
# validates that a browser with the peerid exists in the db
86
peer_zombie_db = Z.first(:id => peer_id) || nil
87
(print_error "Invalid peer_id: the peer hooked browser cannot be found in the database";return) if peer_zombie_db.nil?
88
89
# Writes the event into the BeEF Logger
90
BeEF::Core::Logger.instance.register('WebRTC', "Browser:#{zombie_db.id} received message from Browser:#{peer_zombie_db.id}: #{message}")
91
92
# Perform logic depending on message (updating database)
93
puts "message = '" + message + "'"
94
if (message == "ICE Status: connected")
95
# Find existing status message
96
stat = BeEF::Core::Models::Rtcstatus.first(:hooked_browser_id => zombie_db.id, :target_hooked_browser_id => peer_zombie_db.id) || nil
97
unless stat.nil?
98
stat.status = "Connected"
99
stat.updated_at = Time.now
100
stat.save
101
end
102
stat2 = BeEF::Core::Models::Rtcstatus.first(:hooked_browser_id => peer_zombie_db.id, :target_hooked_browser_id => zombie_db.id) || nil
103
unless stat2.nil?
104
stat2.status = "Connected"
105
stat2.updated_at = Time.now
106
stat2.save
107
end
108
elsif (message.end_with?("disconnected"))
109
stat = BeEF::Core::Models::Rtcstatus.first(:hooked_browser_id => zombie_db.id, :target_hooked_browser_id => peer_zombie_db.id) || nil
110
unless stat.nil?
111
stat.status = "Disconnected"
112
stat.updated_at = Time.now
113
stat.save
114
end
115
stat2 = BeEF::Core::Models::Rtcstatus.first(:hooked_browser_id => peer_zombie_db.id, :target_hooked_browser_id => zombie_db.id) || nil
116
unless stat2.nil?
117
stat2.status = "Disconnected"
118
stat2.updated_at = Time.now
119
stat2.save
120
end
121
elsif (message == "Stayin alive")
122
stat = BeEF::Core::Models::Rtcstatus.first(:hooked_browser_id => zombie_db.id, :target_hooked_browser_id => peer_zombie_db.id) || nil
123
unless stat.nil?
124
stat.status = "Stealthed!!"
125
stat.updated_at = Time.now
126
stat.save
127
end
128
stat2 = BeEF::Core::Models::Rtcstatus.first(:hooked_browser_id => peer_zombie_db.id, :target_hooked_browser_id => zombie_db.id) || nil
129
unless stat2.nil?
130
stat2.status = "Peer-controlled stealth-mode"
131
stat2.updated_at = Time.now
132
stat2.save
133
end
134
elsif (message == "Coming out of stealth...")
135
stat = BeEF::Core::Models::Rtcstatus.first(:hooked_browser_id => zombie_db.id, :target_hooked_browser_id => peer_zombie_db.id) || nil
136
unless stat.nil?
137
stat.status = "Connected"
138
stat.updated_at = Time.now
139
stat.save
140
end
141
stat2 = BeEF::Core::Models::Rtcstatus.first(:hooked_browser_id => peer_zombie_db.id, :target_hooked_browser_id => zombie_db.id) || nil
142
unless stat2.nil?
143
stat2.status = "Connected"
144
stat2.updated_at = Time.now
145
stat2.save
146
end
147
elsif (message.start_with?("execcmd"))
148
mod = /\(\/command\/(.*)\.js\)/.match(message)[1]
149
resp = /Result:.(.*)/.match(message)[1]
150
stat = BeEF::Core::Models::Rtcmodulestatus.new(:hooked_browser_id => zombie_db.id,
151
:target_hooked_browser_id => peer_zombie_db.id,
152
:command_module_id => mod,
153
:status => resp,
154
:created_at => Time.now,
155
:updated_at => Time.now)
156
stat.save
157
end
158
159
end
160
161
end
162
end
163
end
164
end
165
166