Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/handlers/modules/command.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 Core
8
module Handlers
9
module Modules
10
module Command
11
# Adds the command module instructions to a hooked browser's http response.
12
# @param [Object] command Command object
13
# @param [Object] hooked_browser Hooked Browser object
14
def add_command_instructions(command, hooked_browser)
15
if hooked_browser.nil?
16
(print_error 'hooked_browser is nil'
17
return)
18
end
19
if hooked_browser.session.nil?
20
(print_error 'hooked_browser.session is nil'
21
return)
22
end
23
if command.nil?
24
(print_error 'hooked_browser is nil'
25
return)
26
end
27
if command.command_module_id.nil?
28
(print_error 'hooked_browser.command_module_id is nil'
29
return)
30
end
31
32
config = BeEF::Core::Configuration.instance
33
# @note get the command module
34
command_module = BeEF::Core::Models::CommandModule.where(id: command.command_module_id).first
35
if command_module.nil?
36
(print_error 'command_module is nil'
37
return)
38
end
39
if command_module.path.nil?
40
(print_error 'command_module.path is nil'
41
return)
42
end
43
44
if command_module.path.match(/^Dynamic/)
45
command_module = BeEF::Modules::Commands.const_get(command_module.path.split('/').last.capitalize).new
46
else
47
key = BeEF::Module.get_key_by_database_id(command.command_module_id)
48
if key.nil?
49
(print_error "Could not find command module with ID #{command.command_module_id}"
50
return)
51
end
52
command_module = BeEF::Core::Command.const_get(config.get("beef.module.#{key}.class")).new(key)
53
end
54
55
command_module.command_id = command.id
56
command_module.session_id = hooked_browser.session
57
command_module.build_datastore(command.data)
58
command_module.pre_send
59
60
build_missing_beefjs_components(command_module.beefjs_components) unless command_module.beefjs_components.empty?
61
62
ws = BeEF::Core::Websocket::Websocket.instance
63
64
if config.get('beef.extension.evasion.enable')
65
evasion = BeEF::Extension::Evasion::Evasion.instance
66
@output = evasion.obfuscate(command_module.output)
67
else
68
@output = command_module.output
69
end
70
71
# TODO: antisnatchor: remove this gsub crap adding some hook packing.
72
if config.get('beef.http.websocket.enable') && ws.getsocket(hooked_browser.session)
73
# content = command_module.output.gsub('//
74
# //
75
# // Copyright (c) 2006-2025 Wade Alcorn - [email protected]
76
# // Browser Exploitation Framework (BeEF) - https://beefproject.com
77
# // See the file 'doc/COPYING' for copying permission
78
# //
79
# //', "")
80
ws.send(@output, hooked_browser.session)
81
else
82
@body << (@output + "\n\n")
83
end
84
# @note prints the event to the console
85
if BeEF::Settings.console?
86
name = command_module.friendlyname || kclass
87
print_info "Hooked browser [id:#{hooked_browser.id}, ip:#{hooked_browser.ip}] has been sent instructions from command module [cid:#{command.id}, mod: #{command.command_module_id}, name:'#{name}']"
88
end
89
90
# @note flag that the command has been sent to the hooked browser
91
command.instructions_sent = true
92
command.save!
93
end
94
end
95
end
96
end
97
end
98
end
99
100