2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
module Msf
|
2015-04-16 16:02:01 +00:00
|
|
|
|
|
|
|
autoload :Opt, 'msf/core/opt'
|
|
|
|
|
|
|
|
autoload :OptBase, 'msf/core/opt_base'
|
|
|
|
|
|
|
|
autoload :OptAddress, 'msf/core/opt_address'
|
2017-05-02 14:32:11 +00:00
|
|
|
autoload :OptAddressLocal, 'msf/core/opt_address_local'
|
2015-04-16 16:02:01 +00:00
|
|
|
autoload :OptAddressRange, 'msf/core/opt_address_range'
|
|
|
|
autoload :OptBool, 'msf/core/opt_bool'
|
|
|
|
autoload :OptEnum, 'msf/core/opt_enum'
|
|
|
|
autoload :OptInt, 'msf/core/opt_int'
|
2017-08-05 07:21:31 +00:00
|
|
|
autoload :OptFloat, 'msf/core/opt_float'
|
2015-04-16 16:02:01 +00:00
|
|
|
autoload :OptPath, 'msf/core/opt_path'
|
|
|
|
autoload :OptPort, 'msf/core/opt_port'
|
|
|
|
autoload :OptRaw, 'msf/core/opt_raw'
|
|
|
|
autoload :OptRegexp, 'msf/core/opt_regexp'
|
|
|
|
autoload :OptString, 'msf/core/opt_string'
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
2015-04-03 21:00:04 +00:00
|
|
|
# The options purpose in life is to associate named options with arbitrary
|
|
|
|
# values at the most simplistic level. Each {Msf::Module} contains an
|
|
|
|
# OptionContainer that is used to hold the various options that the module
|
|
|
|
# depends on. Example of options that are stored in the OptionContainer are
|
|
|
|
# rhost and rport for payloads or exploits that need to connect to a host
|
|
|
|
# and port, for instance.
|
|
|
|
#
|
|
|
|
# The core supported option types are:
|
|
|
|
#
|
|
|
|
# * {OptString} - Multi-byte character string
|
|
|
|
# * {OptRaw} - Multi-byte raw string
|
|
|
|
# * {OptBool} - Boolean true or false indication
|
|
|
|
# * {OptPort} - TCP/UDP service port
|
|
|
|
# * {OptAddress} - IP address or hostname
|
|
|
|
# * {OptPath} - Path name on disk or an Object ID
|
|
|
|
# * {OptInt} - An integer value
|
2017-08-05 07:21:31 +00:00
|
|
|
# * {OptFloat} - A float value
|
2015-04-03 21:00:04 +00:00
|
|
|
# * {OptEnum} - Select from a set of valid values
|
|
|
|
# * {OptAddressRange} - A subnet or range of addresses
|
|
|
|
# * {OptRegexp} - Valid Ruby regular expression
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
2014-11-03 22:20:21 +00:00
|
|
|
class OptionContainer < Hash
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Merges in the supplied options and converts them to a OptBase
|
|
|
|
# as necessary.
|
|
|
|
#
|
|
|
|
def initialize(opts = {})
|
|
|
|
self.sorted = []
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
add_options(opts)
|
2014-04-02 21:51:33 +00:00
|
|
|
end
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Return the value associated with the supplied name.
|
|
|
|
#
|
|
|
|
def [](name)
|
|
|
|
return get(name)
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Return the option associated with the supplied name.
|
|
|
|
#
|
|
|
|
def get(name)
|
2013-08-30 21:28:33 +00:00
|
|
|
begin
|
2014-11-03 22:20:21 +00:00
|
|
|
return fetch(name)
|
|
|
|
rescue
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Returns whether or not the container has any options,
|
|
|
|
# excluding advanced (and evasions).
|
|
|
|
#
|
|
|
|
def has_options?
|
|
|
|
each_option { |name, opt|
|
|
|
|
return true if (opt.advanced? == false)
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
}
|
2007-03-09 06:12:28 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
return false
|
|
|
|
end
|
2005-09-24 19:17:07 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Returns whether or not the container has any advanced
|
|
|
|
# options.
|
|
|
|
#
|
|
|
|
def has_advanced_options?
|
|
|
|
each_option { |name, opt|
|
|
|
|
return true if (opt.advanced? == true)
|
|
|
|
}
|
2007-03-09 06:12:28 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
return false
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Returns whether or not the container has any evasion
|
|
|
|
# options.
|
|
|
|
#
|
|
|
|
def has_evasion_options?
|
|
|
|
each_option { |name, opt|
|
|
|
|
return true if (opt.evasion? == true)
|
|
|
|
}
|
2007-03-09 06:12:28 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
return false
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Removes an option.
|
|
|
|
#
|
|
|
|
def remove_option(name)
|
|
|
|
delete(name)
|
|
|
|
sorted.each_with_index { |e, idx|
|
|
|
|
sorted[idx] = nil if (e[0] == name)
|
2013-08-30 21:28:33 +00:00
|
|
|
}
|
2014-11-03 22:20:21 +00:00
|
|
|
sorted.delete(nil)
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Adds one or more options.
|
|
|
|
#
|
|
|
|
def add_options(opts, owner = nil, advanced = false, evasion = false)
|
|
|
|
return false if (opts == nil)
|
2007-03-09 06:12:28 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
if (opts.kind_of?(Array))
|
|
|
|
add_options_array(opts, owner, advanced, evasion)
|
2013-08-30 21:28:33 +00:00
|
|
|
else
|
2014-11-03 22:20:21 +00:00
|
|
|
add_options_hash(opts, owner, advanced, evasion)
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Add options from a hash of names.
|
|
|
|
#
|
|
|
|
def add_options_hash(opts, owner = nil, advanced = false, evasion = false)
|
|
|
|
opts.each_pair { |name, opt|
|
|
|
|
add_option(opt, name, owner, advanced, evasion)
|
|
|
|
}
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Add options from an array of option instances or arrays.
|
|
|
|
#
|
|
|
|
def add_options_array(opts, owner = nil, advanced = false, evasion = false)
|
|
|
|
opts.each { |opt|
|
|
|
|
add_option(opt, nil, owner, advanced, evasion)
|
|
|
|
}
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Adds an option.
|
|
|
|
#
|
|
|
|
def add_option(option, name = nil, owner = nil, advanced = false, evasion = false)
|
|
|
|
if (option.kind_of?(Array))
|
|
|
|
option = option.shift.new(name, option)
|
|
|
|
elsif (!option.kind_of?(OptBase))
|
|
|
|
raise ArgumentError,
|
|
|
|
"The option named #{name} did not come in a compatible format.",
|
|
|
|
caller
|
|
|
|
end
|
2005-07-18 23:32:34 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
option.advanced = advanced
|
|
|
|
option.evasion = evasion
|
|
|
|
option.owner = owner
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
self.store(option.name, option)
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
# Re-calculate the sorted list
|
|
|
|
self.sorted = self.sort
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Alias to add advanced options that sets the proper state flag.
|
|
|
|
#
|
|
|
|
def add_advanced_options(opts, owner = nil)
|
|
|
|
return false if (opts == nil)
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
add_options(opts, owner, true)
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Alias to add evasion options that sets the proper state flag.
|
|
|
|
#
|
|
|
|
def add_evasion_options(opts, owner = nil)
|
|
|
|
return false if (opts == nil)
|
2005-07-18 23:32:34 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
add_options(opts, owner, false, true)
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Make sures that each of the options has a value of a compatible
|
|
|
|
# format and that all the required options are set.
|
|
|
|
#
|
|
|
|
def validate(datastore)
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
each_pair { |name, option|
|
|
|
|
if (!option.valid?(datastore[name]))
|
|
|
|
errors << name
|
|
|
|
# If the option is valid, normalize its format to the correct type.
|
|
|
|
elsif ((val = option.normalize(datastore[name])) != nil)
|
|
|
|
# This *will* result in a module that previously used the
|
|
|
|
# global datastore to have its local datastore set, which
|
|
|
|
# means that changing the global datastore and re-running
|
|
|
|
# the same module will now use the newly-normalized local
|
|
|
|
# datastore value instead. This is mostly mitigated by
|
|
|
|
# forcing a clone through mod.replicant, but can break
|
|
|
|
# things in corner cases.
|
|
|
|
datastore[name] = val
|
|
|
|
end
|
|
|
|
}
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
if (errors.empty? == false)
|
|
|
|
raise OptionValidateError.new(errors),
|
|
|
|
"One or more options failed to validate", caller
|
|
|
|
end
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
return true
|
|
|
|
end
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Creates string of options that were used from the datastore in VAR=VAL
|
|
|
|
# format separated by commas.
|
|
|
|
#
|
|
|
|
def options_used_to_s(datastore)
|
|
|
|
used = ''
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
each_pair { |name, option|
|
|
|
|
next if (datastore[name] == nil)
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
used += ", " if (used.length > 0)
|
|
|
|
used += "#{name}=#{datastore[name]}"
|
|
|
|
}
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
return used
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Enumerates each option name
|
|
|
|
#
|
|
|
|
def each_option(&block)
|
|
|
|
each_pair(&block)
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Overrides the builtin 'each' operator to avoid the following exception on Ruby 1.9.2+
|
|
|
|
# "can't add a new key into hash during iteration"
|
|
|
|
#
|
|
|
|
def each(&block)
|
|
|
|
list = []
|
|
|
|
self.keys.sort.each do |sidx|
|
|
|
|
list << [sidx, self[sidx]]
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2014-11-03 22:20:21 +00:00
|
|
|
list.each(&block)
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# Merges the options in this container with another option container and
|
|
|
|
# returns the sorted results.
|
|
|
|
#
|
|
|
|
def merge_sort(other_container)
|
|
|
|
result = self.dup
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
other_container.each { |name, opt|
|
|
|
|
if (result.get(name) == nil)
|
|
|
|
result[name] = opt
|
|
|
|
end
|
|
|
|
}
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
result.sort
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
#
|
|
|
|
# The sorted array of options.
|
|
|
|
#
|
|
|
|
attr_reader :sorted
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
protected
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
attr_writer :sorted # :nodoc:
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2014-11-03 22:20:21 +00:00
|
|
|
end
|
2005-06-05 04:27:57 +00:00
|
|
|
|
2008-11-03 09:17:08 +00:00
|
|
|
end
|