Files
beef/core/main/command.rb
wheatley b6425e4a90 Release 0.5.2.0 (#2166)
* fixed offline zombie not deleting

* Bump jsdoc-to-markdown from 6.0.1 to 7.0.1 (#2161)

Bumps [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown) from 6.0.1 to 7.0.1.
- [Release notes](https://github.com/jsdoc2md/jsdoc-to-markdown/releases)
- [Commits](https://github.com/jsdoc2md/jsdoc-to-markdown/compare/v6.0.1...v7.0.1)

---
updated-dependencies:
- dependency-name: jsdoc-to-markdown
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bug: Core - 1785 Fixed public hook url configuration settings (#2163)

* added spec file for testing changes

* added local host getter to configuration class

* added default value 0.0.0.0 for local host if it's not set

* added port config getter with default

* added port config getter with default

* fixed spelling errors for port

* added public configuration values and validation

* removed logic from public port as it was not required

* added beef host to configuration class

* added beef port to configuration class and removed default http.port logic from public_port

* fixed rubocop errors and refactored spec tests

* added beef host configuration values used for external resources

* added beef url to configuration

* added spec file for testing changes

* added local host getter to configuration class

* added default value 0.0.0.0 for local host if it's not set

* added port config getter with default

* added port config getter with default

* fixed spelling errors for port

* added public configuration values and validation

* removed logic from public port as it was not required

* added beef host to configuration class

* added beef port to configuration class and removed default http.port logic from public_port

* fixed rubocop errors and refactored spec tests

* added beef host configuration values used for external resources

* added beef url to configuration

* created command spec file

* add before statement to load all enabled modules to test command class

* add spec to check if configuration instance exists by setting and accessing a config variable

* updated http proto for beef host

* reverting changes on this file, dev values set

* removed some unessessary checks

* fixed grammar test now we're only testing one configuration attribute

* added hook url for contextual usage

* refactoring admin_ui with new code usage

* fixed issue with the location of the beef.http.https.public_enabled

* refactored powershell module and extension

* adding the new config setting for public https beign enabled

* refactor qrcode extension

* replace video fake plugin refactor

* social engineering refactoring

* phonegap module refactoring

* exploit refactoing

* network module refactoing

* ipec module refactoring

* host module refactoring

* debug refactoring

* browser refactoring

* social engineering extension refactoring

* core main server refactoring

* core main console banner refactoring

* removing dev test

* fixed area with location of http.https.enabled

* changed the hook url definition to return the hook file path

* updated banners to use new configuration getters

* updated extensions and modules with the hook url change

* added new public.host configuration settings and validations for depicated usage of public

* updated to use public.port configuration

* added validation for old configuration public_port

* updated to use public https configuration setting

* updated config with new settings format

* fixed get to point to new locations

* fixed pointer to hook_file_path

* Update extensions/social_engineering/web_cloner/web_cloner.rb

Co-authored-by: bcoles <bcoles@gmail.com>

* updated enabled to enable

* making sure default configuration file does not have preset values

Co-authored-by: bcoles <bcoles@gmail.com>

* bumped versions to 0.5.2.0

* Usability: #2145. Added user input request for beef update within 'beef' install script (#2162)

* added user input request for beef update

* swaped git pull from system to backticks

* flags added for auto update and timout to input

* updated install.txt to reference the update-beef script (#2160)

Co-authored-by: Andrew Wheatley <a@andrews-mini.home>
Co-authored-by: Isaac Powell <36595182+DeezyE@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: bcoles <bcoles@gmail.com>
2021-09-12 21:33:02 +10:00

262 lines
8.5 KiB
Ruby

#
# Copyright (c) 2006-2021 Wade Alcorn - wade@bindshell.net
# Browser Exploitation Framework (BeEF) - http://beefproject.com
# See the file 'doc/COPYING' for copying permission
#
module BeEF
module Core
#
# @note This module contains a list of utils functions to use when writing commands
#
module CommandUtils
#
# Format a string to support multiline in javascript.
# @param [String] text String to convert
#
# @return [String] Formatted string
#
def format_multiline(text)
text.gsub(/\n/, '\n')
end
end
#
# @note The Command Module Context is being used when evaluating code in eruby.
# In other words, we use that code to add funky functions to the
# javascript templates of our commands.
#
class CommandContext < Erubis::Context
include BeEF::Core::CommandUtils
#
# Constructor
# @param [Hash] hash
#
def initialize(hash = nil)
super(hash)
end
end
#
# @note This class is the base class for all command modules in the framework.
# Two instances of this object are created during the execution of command module.
#
class Command
attr_reader :datastore, :path, :default_command_url, :beefjs_components, :friendlyname,
:config
attr_accessor :zombie, :command_id, :session_id
include BeEF::Core::CommandUtils
include BeEF::Core::Constants::Browsers
include BeEF::Core::Constants::CommandModule
#
# Super class controller
#
# @param [String] key command module key
#
def initialize(key)
@config = BeEF::Core::Configuration.instance
@key = key
@datastore = {}
@friendlyname = @config.get("beef.module.#{key}.name")
@output = ''
@path = @config.get("beef.module.#{key}.path")
@default_command_url = config.get("beef.module.#{key}.mount")
@id = @config.get("beef.module.#{key}.db.id")
@auto_update_zombie = false
@results = {}
@beefjs_components = {}
end
#
# This function is called just before the instructions are sent to hooked browser.
#
def pre_send; end
#
# Callback method. This function is called when the hooked browser sends results back.
#
def callback; end
#
# If the command requires some data to be sent back, this function will process them.
# @param [] head
# @param [Hash] params Hash of parameters
# @todo Determine argument "head" type
#
def process_zombie_response(head, params); end
#
# Returns true if the command needs configurations to work. False if not.
# @deprecated This command should not be used since the implementation of the new configuration system
#
def needs_configuration?
!@datastore.nil?
end
#
# Returns information about the command in a JSON format.
# @return [String] JSON formatted string
#
def to_json
{
'Name' => @friendlyname,
'Description' => BeEF::Core::Configuration.instance.get("beef.module.#{@key}.description"),
'Category' => BeEF::Core::Configuration.instance.get("beef.module.#{@key}.category"),
'Data' => BeEF::Module.get_options(@key)
}.to_json
end
#
# Builds the 'datastore' attribute of the command which is used to generate javascript code.
# @param [Hash] data Data to be inserted into the datastore
# @todo TODO Confirm argument "data" type
#
def build_datastore(data)
@datastore = JSON.parse data
rescue => e
print_error "Could not build datastore: #{e.message}"
end
#
# Sets the datastore for the callback function. This function is meant to be called by the CommandHandler
# @param [Hash] http_params HTTP parameters
# @param [Hash] http_headers HTTP headers
#
def build_callback_datastore(result, command_id, beefhook, http_params, http_headers)
@datastore = {'http_headers' => {}} # init the datastore
if !http_params.nil? && !http_headers.nil?
# get, check and add the http_params to the datastore
http_params.keys.each do |http_params_key|
unless BeEF::Filters.is_valid_command_module_datastore_key? http_params_key
print_error 'http_params_key is invalid'
return
end
http_params_value = Erubis::XmlHelper.escape_xml http_params[http_params_key]
unless BeEF::Filters.is_valid_command_module_datastore_param?(http_params_value)
print_error 'http_params_value is invalid'
return
end
# add the checked key and value to the datastore
@datastore[http_params_key] = http_params_value
end
# get, check and add the http_headers to the datastore
http_headers.keys.each do |http_header_key|
unless BeEF::Filters.is_valid_command_module_datastore_key? http_header_key
print_error 'http_header_key is invalid'
return
end
http_header_value = Erubis::XmlHelper.escape_xml http_headers[http_header_key][0]
unless BeEF::Filters.is_valid_command_module_datastore_param? http_header_value
print_error 'http_header_value is invalid'
return
end
# add the checked key and value to the datastore
@datastore['http_headers'][http_header_key] = http_header_value
end
end
@datastore['results'] = result
@datastore['cid'] = command_id
@datastore['beefhook'] = beefhook
end
#
# Returns the output of the command. These are the actual instructions sent to the browser.
# @return [String] The command output
#
def output
f = "#{@path}command.js"
unless File.exist? f
print_error "File does not exist: #{f}"
return
end
command = BeEF::Core::Models::Command.find(@command_id)
@eruby = Erubis::FastEruby.new(File.read(f))
#data = BeEF::Core::Configuration.instance.get "beef.module.#{@key}"
cc = BeEF::Core::CommandContext.new
cc['command_url'] = @default_command_url
cc['command_id'] = @command_id
JSON.parse(command['data']).each do |v|
cc[v['name']] = v['value']
end
execute if respond_to?(:execute)
@output = @eruby.evaluate cc
@output
end
# Saves the results received from the hooked browser
# @param [Hash] results Results from hooked browser
def save(results)
@results = results
end
#
# If nothing else than the file is specified,
# the function will map the file to a random path without any extension.
#
# @param [String] file File to be mounted
# @param [String] path URL path to mounted file
# @param [String] extension URL extension
# @param [Integer] count The amount of times this file can be accessed before being automatically unmounted
# @deprecated This function is possibly deprecated in place of the API
#
def map_file_to_url(file, path = nil, extension = nil, count = 1)
BeEF::Core::NetworkStack::Handlers::AssetHandler.instance.bind(file, path, extension, count)
end
#
# Tells the framework to load a specific module of the BeEFJS library that the command will be using.
# @param [String] component String of BeEFJS component to load
# @note Example: use 'beef.net.local'
#
def use(component)
return if @beefjs_components.include? component
component_path = '/'+component
component_path.gsub!(/beef./, '')
component_path.gsub!(/\./, '/')
component_path.replace "#{$root_dir}/core/main/client/#{component_path}.js"
raise "Invalid beefjs component for command module #{@path}" unless File.exist? component_path
@beefjs_components[component] = component_path
end
# @todo TODO Document
def oc_value(name)
option = BeEF::Core::Models::OptionCache.where(:name => name).first
return nil unless option
option.value
end
# @todo TODO Document
def apply_defaults
@datastore.each do |opt|
opt['value'] = oc_value(opt['name']) || opt['value']
end
end
private
@use_template
@eruby
@update_zombie
@results
end
end
end