2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core'
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# The module base class is responsible for providing the common interface
|
|
|
|
# that is used to interact with modules at the most basic levels, such as
|
|
|
|
# by inspecting a given module's attributes (name, dsecription, version,
|
|
|
|
# authors, etc) and by managing the module's data store.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Module
|
2014-10-16 18:14:56 +00:00
|
|
|
autoload :Arch, 'msf/core/module/arch'
|
2014-10-17 14:02:12 +00:00
|
|
|
autoload :Author, 'msf/core/module/author'
|
2014-10-16 19:51:32 +00:00
|
|
|
autoload :AuxiliaryAction, 'msf/core/module/auxiliary_action'
|
2014-10-16 13:51:07 +00:00
|
|
|
autoload :Compatibility, 'msf/core/module/compatibility'
|
2014-10-16 13:31:43 +00:00
|
|
|
autoload :DataStore, 'msf/core/module/data_store'
|
2014-10-16 19:51:32 +00:00
|
|
|
autoload :Deprecated, 'msf/core/module/deprecated'
|
2014-10-16 20:24:59 +00:00
|
|
|
autoload :FullName, 'msf/core/module/full_name'
|
2014-10-16 19:51:32 +00:00
|
|
|
autoload :HasActions, 'msf/core/module/has_actions'
|
2014-10-16 18:01:42 +00:00
|
|
|
autoload :ModuleInfo, 'msf/core/module/module_info'
|
2014-10-16 13:31:43 +00:00
|
|
|
autoload :ModuleStore, 'msf/core/module/module_store'
|
2014-10-16 20:51:59 +00:00
|
|
|
autoload :Network, 'msf/core/module/network'
|
2014-10-16 16:14:42 +00:00
|
|
|
autoload :Options, 'msf/core/module/options'
|
2014-10-16 19:51:32 +00:00
|
|
|
autoload :Platform, 'msf/core/module/platform'
|
|
|
|
autoload :PlatformList, 'msf/core/module/platform_list'
|
2014-10-17 16:45:03 +00:00
|
|
|
autoload :Privileged, 'msf/core/module/privileged'
|
2014-10-16 19:39:33 +00:00
|
|
|
autoload :Rank, 'msf/core/module/rank'
|
2014-10-16 19:51:32 +00:00
|
|
|
autoload :Reference, 'msf/core/module/reference'
|
2014-10-16 20:27:54 +00:00
|
|
|
autoload :Search, 'msf/core/module/search'
|
2014-10-16 20:03:21 +00:00
|
|
|
autoload :SiteReference, 'msf/core/module/reference'
|
2014-10-16 19:51:32 +00:00
|
|
|
autoload :Target, 'msf/core/module/target'
|
2014-10-16 19:23:21 +00:00
|
|
|
autoload :Type, 'msf/core/module/type'
|
2014-10-16 14:39:30 +00:00
|
|
|
autoload :UI, 'msf/core/module/ui'
|
2014-10-17 16:31:56 +00:00
|
|
|
autoload :UUID, 'msf/core/module/uuid'
|
2014-10-16 13:14:24 +00:00
|
|
|
|
2014-10-16 18:14:56 +00:00
|
|
|
include Msf::Module::Arch
|
2014-10-17 14:02:12 +00:00
|
|
|
include Msf::Module::Author
|
2014-10-16 13:51:07 +00:00
|
|
|
include Msf::Module::Compatibility
|
2014-10-16 13:31:43 +00:00
|
|
|
include Msf::Module::DataStore
|
2014-10-16 20:24:59 +00:00
|
|
|
include Msf::Module::FullName
|
2014-10-16 18:01:42 +00:00
|
|
|
include Msf::Module::ModuleInfo
|
2014-10-16 13:14:24 +00:00
|
|
|
include Msf::Module::ModuleStore
|
2014-10-16 20:51:59 +00:00
|
|
|
include Msf::Module::Network
|
2014-10-16 16:14:42 +00:00
|
|
|
include Msf::Module::Options
|
2014-10-17 16:45:03 +00:00
|
|
|
include Msf::Module::Privileged
|
2014-10-16 19:39:33 +00:00
|
|
|
include Msf::Module::Rank
|
2014-10-16 20:27:54 +00:00
|
|
|
include Msf::Module::Search
|
2014-10-16 19:23:21 +00:00
|
|
|
include Msf::Module::Type
|
2014-10-16 14:39:30 +00:00
|
|
|
include Msf::Module::UI
|
2014-10-17 16:31:56 +00:00
|
|
|
include Msf::Module::UUID
|
2013-08-30 21:28:33 +00:00
|
|
|
|
|
|
|
# Make include public so we can runtime extend
|
|
|
|
public_class_method :include
|
|
|
|
|
|
|
|
class << self
|
|
|
|
include Framework::Offspring
|
|
|
|
|
|
|
|
#
|
|
|
|
# This attribute holds the non-duplicated copy of the module
|
|
|
|
# implementation. This attribute is used for reloading purposes so that
|
|
|
|
# it can be re-duplicated.
|
|
|
|
#
|
|
|
|
attr_accessor :orig_cls
|
|
|
|
|
|
|
|
#
|
|
|
|
# The path from which the module was loaded.
|
|
|
|
#
|
|
|
|
attr_accessor :file_path
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the class reference to the framework
|
|
|
|
#
|
|
|
|
def framework
|
2014-05-02 23:52:58 +00:00
|
|
|
self.class.framework
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method allows modules to tell the framework if they are usable
|
|
|
|
# on the system that they are being loaded on in a generic fashion.
|
|
|
|
# By default, all modules are indicated as being usable. An example of
|
|
|
|
# where this is useful is if the module depends on something external to
|
|
|
|
# ruby, such as a binary.
|
|
|
|
#
|
|
|
|
def self.is_usable
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates an instance of an abstract module using the supplied information
|
|
|
|
# hash.
|
|
|
|
#
|
|
|
|
def initialize(info = {})
|
|
|
|
@module_info_copy = info.dup
|
|
|
|
|
|
|
|
self.module_info = info
|
|
|
|
generate_uuid
|
|
|
|
|
|
|
|
set_defaults
|
|
|
|
|
|
|
|
# Initialize module compatibility hashes
|
|
|
|
init_compat
|
|
|
|
|
|
|
|
# Fixup module fields as needed
|
|
|
|
info_fixups
|
|
|
|
|
|
|
|
# Transform some of the fields to arrays as necessary
|
2014-10-17 13:46:13 +00:00
|
|
|
self.author = Msf::Author.transform(module_info['Author'])
|
2013-08-30 21:28:33 +00:00
|
|
|
self.arch = Rex::Transformer.transform(module_info['Arch'], Array, [ String ], 'Arch')
|
|
|
|
self.platform = PlatformList.transform(module_info['Platform'])
|
|
|
|
self.references = Rex::Transformer.transform(module_info['References'], Array, [ SiteReference, Reference ], 'Ref')
|
|
|
|
|
|
|
|
# Create and initialize the option container for this module
|
|
|
|
self.options = OptionContainer.new
|
|
|
|
self.options.add_options(info['Options'], self.class)
|
|
|
|
self.options.add_advanced_options(info['AdvancedOptions'], self.class)
|
|
|
|
self.options.add_evasion_options(info['EvasionOptions'], self.class)
|
|
|
|
|
|
|
|
# Create and initialize the data store for this module
|
|
|
|
self.datastore = ModuleDataStore.new(self)
|
|
|
|
|
|
|
|
# Import default options into the datastore
|
|
|
|
import_defaults
|
|
|
|
|
|
|
|
self.privileged = module_info['Privileged'] || false
|
|
|
|
self.license = module_info['License'] || MSF_LICENSE
|
|
|
|
|
|
|
|
# Allow all modules to track their current workspace
|
|
|
|
register_advanced_options(
|
|
|
|
[
|
|
|
|
OptString.new('WORKSPACE', [ false, "Specify the workspace for this module" ]),
|
|
|
|
OptBool.new('VERBOSE', [ false, 'Enable detailed status messages', false ])
|
|
|
|
], Msf::Module)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates a fresh copy of an instantiated module
|
|
|
|
#
|
|
|
|
def replicant
|
|
|
|
|
|
|
|
obj = self.class.new
|
|
|
|
self.instance_variables.each { |k|
|
|
|
|
v = instance_variable_get(k)
|
|
|
|
v = v.dup rescue v
|
|
|
|
obj.instance_variable_set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.datastore = self.datastore.copy
|
|
|
|
obj.user_input = self.user_input
|
|
|
|
obj.user_output = self.user_output
|
|
|
|
obj.module_store = self.module_store.clone
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the unduplicated class associated with this module.
|
|
|
|
#
|
|
|
|
def orig_cls
|
2014-05-02 23:52:58 +00:00
|
|
|
self.class.orig_cls
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The path to the file in which the module can be loaded from.
|
|
|
|
#
|
|
|
|
def file_path
|
|
|
|
self.class.file_path
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Checks to see if the target is vulnerable, returning unsupported if it's
|
|
|
|
# not supported.
|
|
|
|
#
|
|
|
|
# This method is designed to be overriden by exploit modules.
|
|
|
|
#
|
|
|
|
def check
|
|
|
|
Msf::Exploit::CheckCode::Unsupported
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the current workspace
|
|
|
|
#
|
|
|
|
def workspace
|
|
|
|
self.datastore['WORKSPACE'] ||
|
|
|
|
(framework.db and framework.db.active and framework.db.workspace and framework.db.workspace.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the username that instantiated this module, this tries a handful of methods
|
|
|
|
# to determine what actual user ran this module.
|
|
|
|
#
|
|
|
|
def owner
|
|
|
|
# Generic method to configure a module owner
|
|
|
|
username = self.datastore['MODULE_OWNER'].to_s.strip
|
|
|
|
|
|
|
|
# Specific method used by the commercial products
|
|
|
|
if username.empty?
|
|
|
|
username = self.datastore['PROUSER'].to_s.strip
|
|
|
|
end
|
|
|
|
|
|
|
|
# Fallback when neither prior method is available, common for msfconsole
|
|
|
|
if username.empty?
|
|
|
|
username = (ENV['LOGNAME'] || ENV['USERNAME'] || ENV['USER'] || "unknown").to_s.strip
|
|
|
|
end
|
|
|
|
|
|
|
|
username
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Scans the parent module reference to populate additional information. This
|
|
|
|
# is used to inherit common settings (owner, workspace, parent uuid, etc).
|
|
|
|
#
|
|
|
|
def register_parent(ref)
|
|
|
|
self.datastore['WORKSPACE'] = (ref.datastore['WORKSPACE'] ? ref.datastore['WORKSPACE'].dup : nil)
|
|
|
|
self.datastore['PROUSER'] = (ref.datastore['PROUSER'] ? ref.datastore['PROUSER'].dup : nil)
|
|
|
|
self.datastore['MODULE_OWNER'] = ref.owner.dup
|
|
|
|
self.datastore['ParentUUID'] = ref.uuid.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Return a comma separated list of supported platforms, if any.
|
|
|
|
#
|
|
|
|
def platform_to_s
|
2014-05-02 23:52:58 +00:00
|
|
|
platform.all? ? "All" : platform.names.join(", ")
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Checks to see if this module is compatible with the supplied platform
|
|
|
|
#
|
|
|
|
def platform?(what)
|
|
|
|
(platform & what).empty? == false
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns true if this module is being debugged. The debug flag is set
|
|
|
|
# by setting datastore['DEBUG'] to 1|true|yes
|
|
|
|
#
|
|
|
|
def debugging?
|
|
|
|
(datastore['DEBUG'] || '') =~ /^(1|t|y)/i
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Support fail_with for all module types, allow specific classes to override
|
|
|
|
#
|
|
|
|
def fail_with(reason, msg=nil)
|
|
|
|
raise RuntimeError, "#{reason.to_s}: #{msg}"
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Just some handy quick checks
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns false since this is the real module
|
|
|
|
#
|
|
|
|
def self.cached?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The array of zero or more platforms.
|
|
|
|
#
|
|
|
|
attr_reader :platform
|
|
|
|
#
|
|
|
|
# The reference count for the module.
|
|
|
|
#
|
|
|
|
attr_reader :references
|
2014-10-16 16:14:42 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# The license under which this module is provided.
|
|
|
|
#
|
|
|
|
attr_reader :license
|
|
|
|
|
|
|
|
#
|
|
|
|
# The job identifier that this module is running as, if any.
|
|
|
|
#
|
|
|
|
attr_accessor :job_id
|
|
|
|
|
|
|
|
#
|
|
|
|
# The last exception to occur using this module
|
|
|
|
#
|
|
|
|
attr_accessor :error
|
|
|
|
|
2014-10-17 16:31:56 +00:00
|
|
|
protected
|
2013-08-30 21:28:33 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Sets the modules unsupplied info fields to their default values.
|
|
|
|
#
|
|
|
|
def set_defaults
|
|
|
|
self.module_info = {
|
|
|
|
'Name' => 'No module name',
|
|
|
|
'Description' => 'No module description',
|
|
|
|
'Version' => '0',
|
|
|
|
'Author' => nil,
|
|
|
|
'Arch' => nil, # No architectures by default.
|
|
|
|
'Platform' => [], # No platforms by default.
|
|
|
|
'Ref' => nil,
|
|
|
|
'Privileged' => false,
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
}.update(self.module_info)
|
|
|
|
self.module_store = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Checks to see if a derived instance of a given module implements a method
|
|
|
|
# beyond the one that is provided by a base class. This is a pretty lame
|
|
|
|
# way of doing it, but I couldn't find a better one, so meh.
|
|
|
|
#
|
|
|
|
def derived_implementor?(parent, method_name)
|
|
|
|
(self.method(method_name).to_s.match(/#{parent}[^:]/)) ? false : true
|
|
|
|
end
|
|
|
|
|
2014-10-17 14:02:12 +00:00
|
|
|
attr_writer :platform, :references # :nodoc:
|
2013-08-30 21:28:33 +00:00
|
|
|
attr_writer :privileged # :nodoc:
|
|
|
|
attr_writer :license # :nodoc:
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
end
|
2008-12-21 04:18:17 +00:00
|
|
|
end
|