2005-05-21 17:57:00 +00:00
|
|
|
require 'resolv'
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core'
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2005-05-21 18:27:24 +00:00
|
|
|
# The base class for all options.
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
###
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
def initialize(in_name, attrs = [])
|
2005-05-21 18:27:24 +00:00
|
|
|
self.name = in_name
|
|
|
|
self.advanced = false
|
|
|
|
self.required = attrs[0] || false
|
2005-06-05 08:38:24 +00:00
|
|
|
self.desc = attrs[1]
|
|
|
|
self.default = attrs[2]
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def required?
|
|
|
|
return required
|
|
|
|
end
|
|
|
|
|
|
|
|
def advanced?
|
|
|
|
return advanced
|
|
|
|
end
|
|
|
|
|
|
|
|
def type?(in_type)
|
|
|
|
return (type == in_type)
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
# If it's required and the value is nil or empty, then it's not valid.
|
|
|
|
def valid?(value)
|
|
|
|
return (required? and (value == nil or value.to_s.empty?)) ? false : true
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
def to_s
|
|
|
|
return value.to_s
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
attr_reader :name, :required, :desc, :default
|
|
|
|
attr_writer :name
|
2005-05-21 17:57:00 +00:00
|
|
|
attr_accessor :advanced
|
2005-06-05 23:45:58 +00:00
|
|
|
attr_accessor :owner
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
attr_writer :required, :desc, :default
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2005-05-21 18:27:24 +00:00
|
|
|
# Core option types. The core supported option types are:
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
# 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
|
2005-07-18 23:32:34 +00:00
|
|
|
# OptInt - An integer value
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptString < OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return 'string'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptRaw < OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return 'raw'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptBool < OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return 'bool'
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
|
|
|
if ((value != nil and value.empty? == false) and
|
2005-09-24 19:17:07 +00:00
|
|
|
(value.match(/^(y|yes|n|no|t|f|0|1|true|false)$/i) == nil))
|
2005-05-21 17:57:00 +00:00
|
|
|
return false
|
|
|
|
end
|
2005-09-24 19:17:07 +00:00
|
|
|
|
|
|
|
true
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_true?
|
2005-09-24 19:17:07 +00:00
|
|
|
return (value.match(/^(y|yes|t|1|true)$/i) != nil) ? true : false
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_false?
|
|
|
|
return !is_true?
|
|
|
|
end
|
2005-07-11 02:03:48 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
return is_true?.to_s
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptPort < OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return 'port'
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
|
|
|
if ((value != nil and value.to_s.empty? == false) and
|
2005-07-17 08:03:54 +00:00
|
|
|
((value.to_s.match(/^\d+$/) == nil or value.to_i < 0 or value.to_i > 65535)))
|
2005-05-21 17:57:00 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptAddress < OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return 'address'
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
|
|
|
if (value != nil and value.empty? == false)
|
2005-05-21 17:57:00 +00:00
|
|
|
begin
|
|
|
|
Resolv.getaddress(value)
|
|
|
|
rescue
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptPath < OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return 'path'
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
|
|
|
if ((value != nil and value.empty? == false) and
|
2005-05-21 17:57:00 +00:00
|
|
|
(File.exists?(value) == false))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-09-15 07:16:03 +00:00
|
|
|
class OptInt < OptBase
|
2005-07-18 23:32:34 +00:00
|
|
|
def type
|
|
|
|
return 'integer'
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?(value)
|
|
|
|
if (value.to_s.match(/^\d+$/) == nil)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
###
|
|
|
|
#
|
2005-05-21 18:27:24 +00:00
|
|
|
# The options purpose in life is to associate named options
|
2005-05-21 17:57:00 +00:00
|
|
|
# with arbitrary values at the most simplistic level. Each
|
2005-05-21 18:27:24 +00:00
|
|
|
# module contains a OptionContainer that is used to hold the
|
2005-05-21 17:57:00 +00:00
|
|
|
# various options that the module depends on. Example of options
|
2005-05-21 18:27:24 +00:00
|
|
|
# that are stored in the OptionContainer are rhost and rport for
|
2005-05-21 17:57:00 +00:00
|
|
|
# payloads or exploits that need to connect to a host and
|
|
|
|
# port, for instance.
|
|
|
|
#
|
|
|
|
###
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptionContainer < Hash
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2005-07-10 00:49:12 +00:00
|
|
|
#
|
2005-05-21 18:27:24 +00:00
|
|
|
# Merges in the supplied options and converts them to a OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
# as necessary.
|
2005-07-10 00:49:12 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def initialize(opts = {})
|
2005-07-11 15:34:31 +00:00
|
|
|
self.sorted = []
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
add_options(opts)
|
|
|
|
end
|
|
|
|
|
2005-07-10 00:49:12 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
# Return the value associated with the supplied name
|
2005-07-10 00:49:12 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def [](name)
|
|
|
|
return get(name)
|
|
|
|
end
|
|
|
|
|
2005-07-10 00:49:12 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
# Return the option associated with the supplied name
|
2005-07-10 00:49:12 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def get(name)
|
2005-06-05 05:42:14 +00:00
|
|
|
begin
|
|
|
|
return fetch(name)
|
|
|
|
rescue
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-07-10 00:49:12 +00:00
|
|
|
#
|
|
|
|
# Returns whether or not the container has any options,
|
|
|
|
# excluding advanced.
|
|
|
|
#
|
|
|
|
def has_options?
|
|
|
|
each_option { |name, opt|
|
|
|
|
return true if (opt.advanced? == false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns whether or not the container has any advanced
|
|
|
|
# options.
|
|
|
|
#
|
|
|
|
def has_advanced_options?
|
|
|
|
each_option { |name, opt|
|
|
|
|
return true if (opt.advanced? == true)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2005-07-11 05:21:19 +00:00
|
|
|
#
|
|
|
|
# Removes an option
|
|
|
|
#
|
|
|
|
def remove_option(name)
|
|
|
|
delete(name)
|
2005-07-11 20:32:56 +00:00
|
|
|
sorted.each_with_index { |e, idx|
|
|
|
|
sorted.delete_at(idx) if (e[0] == name)
|
|
|
|
}
|
2005-07-11 05:21:19 +00:00
|
|
|
end
|
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
# Adds one or more options
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2005-06-05 23:45:58 +00:00
|
|
|
def add_options(opts, owner = nil, advanced = false)
|
2005-05-21 17:57:00 +00:00
|
|
|
return false if (opts == nil)
|
|
|
|
|
2005-06-05 04:27:57 +00:00
|
|
|
if (opts.kind_of?(Array))
|
2005-06-05 23:45:58 +00:00
|
|
|
add_options_array(opts, owner, advanced)
|
2005-06-05 04:27:57 +00:00
|
|
|
else
|
2005-06-05 23:45:58 +00:00
|
|
|
add_options_hash(opts, owner, advanced)
|
2005-06-05 04:27:57 +00:00
|
|
|
end
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2005-06-05 04:27:57 +00:00
|
|
|
#
|
|
|
|
# Add options from a hash of names
|
|
|
|
#
|
2005-06-05 23:45:58 +00:00
|
|
|
def add_options_hash(opts, owner = nil, advanced = false)
|
2005-06-05 04:27:57 +00:00
|
|
|
opts.each_pair { |name, opt|
|
2005-06-05 23:45:58 +00:00
|
|
|
add_option(opt, name, owner, advanced)
|
2005-06-05 04:27:57 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Add options from an array of option instances or arrays
|
|
|
|
#
|
2005-06-05 23:45:58 +00:00
|
|
|
def add_options_array(opts, owner = nil, advanced = false)
|
2005-06-05 04:27:57 +00:00
|
|
|
opts.each { |opt|
|
2005-06-05 23:45:58 +00:00
|
|
|
add_option(opt, nil, owner, advanced)
|
2005-06-05 04:27:57 +00:00
|
|
|
}
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
|
|
|
# Adds an option
|
|
|
|
#
|
2005-06-05 23:45:58 +00:00
|
|
|
def add_option(option, name = nil, owner = nil, advanced = false)
|
2005-06-05 04:27:57 +00:00
|
|
|
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-05-21 17:57:00 +00:00
|
|
|
|
2005-06-05 04:27:57 +00:00
|
|
|
option.advanced = advanced
|
2005-06-05 23:45:58 +00:00
|
|
|
option.owner = owner
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2005-06-05 04:27:57 +00:00
|
|
|
self.store(option.name, option)
|
2005-07-11 15:34:31 +00:00
|
|
|
|
|
|
|
# Re-calculate the sorted list
|
|
|
|
self.sorted = self.sort
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
# Alias to add advanced options that sets the proper state flag
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2005-06-05 23:45:58 +00:00
|
|
|
def add_advanced_options(opts, owner = nil)
|
|
|
|
return false if (opts == nil)
|
|
|
|
|
|
|
|
add_options(opts, owner, true)
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
# Make sures that each of the options has a value of a compatible
|
|
|
|
# format and that all the required options are set
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2005-05-21 18:27:24 +00:00
|
|
|
def validate(datastore)
|
2005-05-21 17:57:00 +00:00
|
|
|
errors = []
|
|
|
|
|
|
|
|
each_pair { |name, option|
|
2005-05-21 18:27:24 +00:00
|
|
|
if (!option.valid?(datastore[name]))
|
2005-05-21 17:57:00 +00:00
|
|
|
errors << name
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.empty? == false)
|
|
|
|
raise OptionValidateError.new(errors),
|
|
|
|
"One or more options failed to validate", caller
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2005-07-14 06:34:58 +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 = ''
|
|
|
|
|
|
|
|
each_pair { |name, option|
|
|
|
|
next if (datastore[name] == nil)
|
|
|
|
|
|
|
|
used += ", " if (used.length > 0)
|
|
|
|
used += "#{name}=#{datastore[name]}"
|
|
|
|
}
|
|
|
|
|
|
|
|
return used
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
# Enumerates each option name
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def each_option(&block)
|
|
|
|
each_pair(&block)
|
|
|
|
end
|
|
|
|
|
2005-07-11 15:34:31 +00:00
|
|
|
attr_reader :sorted
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
attr_writer :sorted
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-06-05 04:27:57 +00:00
|
|
|
#
|
|
|
|
# Builtin framework options with shortcut methods
|
|
|
|
#
|
|
|
|
module Opt
|
|
|
|
|
|
|
|
@@builtin_opts =
|
|
|
|
{
|
2005-06-05 08:38:24 +00:00
|
|
|
'RHOST' => [ OptAddress, 'nil', true, '"The target address."' ],
|
|
|
|
'RPORT' => [ OptPort, 'nil', true, '"The target port."' ],
|
|
|
|
'LHOST' => [ OptAddress, 'nil', true, '"The local address."' ],
|
|
|
|
'LPORT' => [ OptPort, 'nil', true, '"The local port."' ],
|
|
|
|
'CPORT' => [ OptPort, 'nil', false, '"The local client port."' ],
|
|
|
|
'SSL' => [ OptBool, 'false', false, '"Use SSL."' ],
|
2005-06-05 04:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Build the builtin_xyz methods on the fly using the type information for each
|
|
|
|
# of the builtin framework options, such as RHOST.
|
|
|
|
#
|
|
|
|
class <<self
|
|
|
|
@@builtin_opts.each_pair { |opt, info|
|
|
|
|
eval(
|
|
|
|
"
|
|
|
|
def builtin_#{opt.downcase}(default = #{info[1]}, required = #{info[2]}, desc = #{info[3]})
|
|
|
|
#{info[0]}.new('#{opt}', [ required, desc, default ])
|
|
|
|
end
|
|
|
|
|
|
|
|
alias #{opt} builtin_#{opt.downcase}
|
|
|
|
")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Define the constant versions of the options which are merely redirections to
|
|
|
|
# the class methods.
|
|
|
|
#
|
|
|
|
@@builtin_opts.each_pair { |opt, info|
|
|
|
|
eval("#{opt} = Msf::Opt::builtin_#{opt.downcase}")
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|