Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/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
7
module BeEF
8
module Core
9
#
10
# @note This module contains a list of utils functions to use when writing commands
11
#
12
module CommandUtils
13
#
14
# Format a string to support multiline in javascript.
15
# @param [String] text String to convert
16
#
17
# @return [String] Formatted string
18
#
19
def format_multiline(text)
20
text.gsub(/\n/, '\n')
21
end
22
end
23
24
#
25
# @note The Command Module Context is being used when evaluating code in eruby.
26
# In other words, we use that code to add funky functions to the
27
# javascript templates of our commands.
28
#
29
class CommandContext < Erubis::Context
30
include BeEF::Core::CommandUtils
31
#
32
# Constructor
33
# @param [Hash] hash
34
#
35
def initialize(hash = nil)
36
super(hash)
37
end
38
end
39
40
#
41
# @note This class is the base class for all command modules in the framework.
42
# Two instances of this object are created during the execution of command module.
43
#
44
class Command
45
attr_reader :datastore, :path, :default_command_url, :beefjs_components, :friendlyname,
46
:config
47
attr_accessor :zombie, :command_id, :session_id
48
49
include BeEF::Core::CommandUtils
50
include BeEF::Core::Constants::Browsers
51
include BeEF::Core::Constants::CommandModule
52
53
#
54
# Super class controller
55
#
56
# @param [String] key command module key
57
#
58
def initialize(key)
59
@config = BeEF::Core::Configuration.instance
60
61
@key = key
62
@datastore = {}
63
@friendlyname = @config.get("beef.module.#{key}.name")
64
@output = ''
65
@path = @config.get("beef.module.#{key}.path")
66
@default_command_url = config.get("beef.module.#{key}.mount")
67
@id = @config.get("beef.module.#{key}.db.id")
68
@auto_update_zombie = false
69
@results = {}
70
@beefjs_components = {}
71
end
72
73
#
74
# This function is called just before the instructions are sent to hooked browser.
75
#
76
def pre_send; end
77
78
#
79
# Callback method. This function is called when the hooked browser sends results back.
80
#
81
def callback; end
82
83
#
84
# If the command requires some data to be sent back, this function will process them.
85
# @param [] head
86
# @param [Hash] params Hash of parameters
87
# @todo Determine argument "head" type
88
#
89
def process_zombie_response(head, params); end
90
91
#
92
# Returns true if the command needs configurations to work. False if not.
93
# @deprecated This command should not be used since the implementation of the new configuration system
94
#
95
def needs_configuration?
96
!@datastore.nil?
97
end
98
99
#
100
# Returns information about the command in a JSON format.
101
# @return [String] JSON formatted string
102
#
103
def to_json(*_args)
104
{
105
'Name' => @friendlyname,
106
'Description' => BeEF::Core::Configuration.instance.get("beef.module.#{@key}.description"),
107
'Category' => BeEF::Core::Configuration.instance.get("beef.module.#{@key}.category"),
108
'Data' => BeEF::Module.get_options(@key)
109
}.to_json
110
end
111
112
#
113
# Builds the 'datastore' attribute of the command which is used to generate javascript code.
114
# @param [Hash] data Data to be inserted into the datastore
115
# @todo TODO Confirm argument "data" type
116
#
117
def build_datastore(data)
118
@datastore = JSON.parse data
119
rescue StandardError => e
120
print_error "Could not build datastore: #{e.message}"
121
end
122
123
#
124
# Sets the datastore for the callback function. This function is meant to be called by the CommandHandler
125
# @param [Hash] http_params HTTP parameters
126
# @param [Hash] http_headers HTTP headers
127
#
128
def build_callback_datastore(result, command_id, beefhook, http_params, http_headers)
129
@datastore = { 'http_headers' => {} } # init the datastore
130
131
if !http_params.nil? && !http_headers.nil?
132
# get, check and add the http_params to the datastore
133
http_params.keys.each do |http_params_key|
134
unless BeEF::Filters.is_valid_command_module_datastore_key? http_params_key
135
print_error 'http_params_key is invalid'
136
return
137
end
138
139
http_params_value = Erubis::XmlHelper.escape_xml http_params[http_params_key]
140
unless BeEF::Filters.is_valid_command_module_datastore_param?(http_params_value)
141
print_error 'http_params_value is invalid'
142
return
143
end
144
145
# add the checked key and value to the datastore
146
@datastore[http_params_key] = http_params_value
147
end
148
149
# get, check and add the http_headers to the datastore
150
http_headers.keys.each do |http_header_key|
151
unless BeEF::Filters.is_valid_command_module_datastore_key? http_header_key
152
print_error 'http_header_key is invalid'
153
return
154
end
155
156
http_header_value = Erubis::XmlHelper.escape_xml http_headers[http_header_key][0]
157
unless BeEF::Filters.is_valid_command_module_datastore_param? http_header_value
158
print_error 'http_header_value is invalid'
159
return
160
end
161
162
# add the checked key and value to the datastore
163
@datastore['http_headers'][http_header_key] = http_header_value
164
end
165
end
166
167
@datastore['results'] = result
168
@datastore['cid'] = command_id
169
@datastore['beefhook'] = beefhook
170
end
171
172
#
173
# Returns the output of the command. These are the actual instructions sent to the browser.
174
# @return [String] The command output
175
#
176
def output
177
f = "#{@path}command.js"
178
unless File.exist? f
179
print_error "File does not exist: #{f}"
180
return
181
end
182
183
command = BeEF::Core::Models::Command.find(@command_id)
184
185
@eruby = Erubis::FastEruby.new(File.read(f))
186
187
# data = BeEF::Core::Configuration.instance.get "beef.module.#{@key}"
188
cc = BeEF::Core::CommandContext.new
189
cc['command_url'] = @default_command_url
190
cc['command_id'] = @command_id
191
JSON.parse(command['data']).each do |v|
192
cc[v['name']] = v['value']
193
end
194
195
execute if respond_to?(:execute)
196
197
@output = @eruby.evaluate cc
198
@output
199
end
200
201
# Saves the results received from the hooked browser
202
# @param [Hash] results Results from hooked browser
203
def save(results)
204
@results = results
205
end
206
207
#
208
# If nothing else than the file is specified,
209
# the function will map the file to a random path without any extension.
210
#
211
# @param [String] file File to be mounted
212
# @param [String] path URL path to mounted file
213
# @param [String] extension URL extension
214
# @param [Integer] count The amount of times this file can be accessed before being automatically unmounted
215
# @deprecated This function is possibly deprecated in place of the API
216
#
217
def map_file_to_url(file, path = nil, extension = nil, count = 1)
218
BeEF::Core::NetworkStack::Handlers::AssetHandler.instance.bind(file, path, extension, count)
219
end
220
221
#
222
# Tells the framework to load a specific module of the BeEFJS library that the command will be using.
223
# @param [String] component String of BeEFJS component to load
224
# @note Example: use 'beef.net.local'
225
#
226
def use(component)
227
return if @beefjs_components.include? component
228
229
component_path = '/' + component
230
component_path.gsub!(/beef./, '')
231
component_path.gsub!(/\./, '/')
232
component_path.replace "#{$root_dir}/core/main/client/#{component_path}.js"
233
234
raise "Invalid beefjs component for command module #{@path}" unless File.exist? component_path
235
236
@beefjs_components[component] = component_path
237
end
238
239
# @todo TODO Document
240
def oc_value(name)
241
option = BeEF::Core::Models::OptionCache.where(name: name).first
242
return nil unless option
243
244
option.value
245
end
246
247
# @todo TODO Document
248
def apply_defaults
249
@datastore.each do |opt|
250
opt['value'] = oc_value(opt['name']) || opt['value']
251
end
252
end
253
254
@use_template
255
@eruby
256
@update_zombie
257
@results
258
end
259
end
260
end
261
262