2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-05-21 17:57:00 +00:00
|
|
|
require 'resolv'
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core'
|
2011-05-12 20:03:55 +00:00
|
|
|
require 'rex/socket'
|
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
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Initializes a named option with the supplied attribute array.
|
|
|
|
# The array is composed of three values.
|
|
|
|
#
|
|
|
|
# attrs[0] = required (boolean type)
|
|
|
|
# attrs[1] = description (string)
|
|
|
|
# attrs[2] = default value
|
2006-01-16 23:30:59 +00:00
|
|
|
# attrs[3] = possible enum values
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
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
|
2006-01-05 03:57:12 +00:00
|
|
|
self.evasion = false
|
2005-05-21 18:27:24 +00:00
|
|
|
self.required = attrs[0] || false
|
2005-06-05 08:38:24 +00:00
|
|
|
self.desc = attrs[1]
|
|
|
|
self.default = attrs[2]
|
2006-01-16 23:32:51 +00:00
|
|
|
self.enums = [ *(attrs[3]) ].map { |x| x.to_s }
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Returns true if this is a required option.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def required?
|
|
|
|
return required
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Returns true if this is an advanced option.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def advanced?
|
|
|
|
return advanced
|
|
|
|
end
|
|
|
|
|
2006-01-05 03:57:12 +00:00
|
|
|
#
|
|
|
|
# Returns true if this is an evasion option.
|
|
|
|
#
|
|
|
|
def evasion?
|
|
|
|
return evasion
|
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Returns true if the supplied type is equivalent to this option's type.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def type?(in_type)
|
|
|
|
return (type == in_type)
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
2005-05-21 18:27:24 +00:00
|
|
|
# If it's required and the value is nil or empty, then it's not valid.
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
|
|
|
return (required? and (value == nil or value.to_s.empty?)) ? false : true
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2007-03-09 06:12:28 +00:00
|
|
|
#
|
|
|
|
# Returns true if the value supplied is nil and it's required to be
|
|
|
|
# a valid value
|
|
|
|
#
|
|
|
|
def empty_required_value?(value)
|
|
|
|
return (required? and value.nil?)
|
|
|
|
end
|
|
|
|
|
2005-11-15 21:50:10 +00:00
|
|
|
#
|
|
|
|
# Normalizes the supplied value to conform with the type that the option is
|
|
|
|
# conveying.
|
|
|
|
#
|
|
|
|
def normalize(value)
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2012-01-23 18:25:52 +00:00
|
|
|
#
|
|
|
|
# Returns a string representing a user-friendly display of the chosen value
|
|
|
|
#
|
|
|
|
def display_value(value)
|
|
|
|
value.to_s
|
|
|
|
end
|
2012-05-24 22:28:20 +00:00
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# The name of the option.
|
|
|
|
#
|
|
|
|
attr_reader :name
|
|
|
|
#
|
|
|
|
# Whether or not the option is required.
|
|
|
|
#
|
|
|
|
attr_reader :required
|
|
|
|
#
|
|
|
|
# The description of the option.
|
|
|
|
#
|
|
|
|
attr_reader :desc
|
|
|
|
#
|
|
|
|
# The default value of the option.
|
|
|
|
#
|
|
|
|
attr_reader :default
|
|
|
|
#
|
|
|
|
# Storing the name of the option.
|
|
|
|
#
|
2005-05-21 18:27:24 +00:00
|
|
|
attr_writer :name
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Whether or not this is an advanced option.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
attr_accessor :advanced
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
2006-01-05 03:57:12 +00:00
|
|
|
# Whether or not this is an evasion option.
|
|
|
|
#
|
|
|
|
attr_accessor :evasion
|
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# The module or entity that owns this option.
|
|
|
|
#
|
2005-06-05 23:45:58 +00:00
|
|
|
attr_accessor :owner
|
2006-01-16 23:30:59 +00:00
|
|
|
#
|
|
|
|
# The list of potential valid values
|
|
|
|
#
|
2009-11-03 18:09:05 +00:00
|
|
|
attr_accessor :enums
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
attr_writer :required, :desc, :default # :nodoc:
|
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
|
2011-06-30 06:52:52 +00:00
|
|
|
# OptPath - Path name on disk or an Object ID
|
2005-07-18 23:32:34 +00:00
|
|
|
# OptInt - An integer value
|
2006-01-16 23:30:59 +00:00
|
|
|
# OptEnum - Select from a set of valid values
|
2006-08-12 08:31:38 +00:00
|
|
|
# OptAddressRange - A subnet or range of addresses
|
2010-12-27 17:46:42 +00:00
|
|
|
# OptSession - A session identifier
|
2012-01-23 18:25:52 +00:00
|
|
|
# OptRegexp - Valid Ruby regular expression
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Mult-byte character string option.
|
|
|
|
#
|
|
|
|
###
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptString < OptBase
|
2009-11-03 18:09:05 +00:00
|
|
|
def type
|
|
|
|
return 'string'
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
def normalize(value)
|
|
|
|
if (value =~ /^file:(.*)/)
|
|
|
|
path = $1
|
|
|
|
begin
|
2007-04-24 06:37:23 +00:00
|
|
|
value = File.read(path)
|
2007-04-24 06:27:39 +00:00
|
|
|
rescue ::Errno::ENOENT, ::Errno::EISDIR
|
|
|
|
value = nil
|
|
|
|
end
|
|
|
|
end
|
2007-04-24 13:47:46 +00:00
|
|
|
value
|
2007-04-24 06:27:39 +00:00
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
def valid?(value=self.value)
|
|
|
|
value = normalize(value)
|
2009-11-03 18:09:05 +00:00
|
|
|
return false if empty_required_value?(value)
|
2007-04-24 13:47:46 +00:00
|
|
|
return super
|
2007-04-24 06:27:39 +00:00
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Raw, arbitrary data option.
|
|
|
|
#
|
|
|
|
###
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptRaw < OptBase
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return 'raw'
|
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
def normalize(value)
|
|
|
|
if (value =~ /^file:(.*)/)
|
|
|
|
path = $1
|
|
|
|
begin
|
2007-04-24 06:37:23 +00:00
|
|
|
value = File.read(path)
|
2007-04-24 06:27:39 +00:00
|
|
|
rescue ::Errno::ENOENT, ::Errno::EISDIR
|
|
|
|
value = nil
|
|
|
|
end
|
|
|
|
end
|
2007-04-24 13:47:46 +00:00
|
|
|
value
|
2007-04-24 06:27:39 +00:00
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
def valid?(value=self.value)
|
|
|
|
value = normalize(value)
|
2009-11-03 18:09:05 +00:00
|
|
|
return false if empty_required_value?(value)
|
2007-04-24 13:47:46 +00:00
|
|
|
return super
|
2009-11-03 18:09:05 +00:00
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Boolean option.
|
|
|
|
#
|
|
|
|
###
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptBool < OptBase
|
2005-11-15 21:50:10 +00:00
|
|
|
|
|
|
|
TrueRegex = /^(y|yes|t|1|true)$/i
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return 'bool'
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
2007-03-09 06:12:28 +00:00
|
|
|
return false if empty_required_value?(value)
|
|
|
|
|
2009-11-03 18:09:05 +00:00
|
|
|
if ((value != nil and
|
2005-11-17 23:03:53 +00:00
|
|
|
(value.to_s.empty? == false) and
|
|
|
|
(value.to_s.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
|
|
|
|
|
2005-11-15 21:50:10 +00:00
|
|
|
def normalize(value)
|
2005-11-18 00:26:19 +00:00
|
|
|
if(value.nil? or value.to_s.match(TrueRegex).nil?)
|
2005-11-15 21:50:10 +00:00
|
|
|
false
|
2005-11-15 23:02:17 +00:00
|
|
|
else
|
|
|
|
true
|
2005-11-15 21:50:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-01 21:52:30 +00:00
|
|
|
def is_true?(value)
|
|
|
|
return normalize(value)
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2010-07-01 21:52:30 +00:00
|
|
|
def is_false?(value)
|
|
|
|
return !is_true?(value)
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
2005-07-11 02:03:48 +00:00
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2006-01-16 23:30:59 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Enum option.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class OptEnum < OptBase
|
|
|
|
|
|
|
|
def type
|
|
|
|
return 'enum'
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?(value=self.value)
|
2007-03-09 06:12:28 +00:00
|
|
|
return false if empty_required_value?(value)
|
|
|
|
|
2006-01-16 23:30:59 +00:00
|
|
|
(value and self.enums.include?(value.to_s))
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize(value=self.value)
|
|
|
|
return nil if not self.valid?(value)
|
|
|
|
return value.to_s
|
|
|
|
end
|
2006-01-25 19:15:09 +00:00
|
|
|
|
|
|
|
def desc=(value)
|
|
|
|
self.desc_string = value
|
|
|
|
|
|
|
|
self.desc
|
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2006-01-25 19:15:09 +00:00
|
|
|
def desc
|
|
|
|
if self.enums
|
|
|
|
str = self.enums.join(', ')
|
|
|
|
end
|
|
|
|
"#{self.desc_string || ''} (accepted: #{str})"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
attr_accessor :desc_string # :nodoc:
|
|
|
|
|
2006-01-16 23:30:59 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Network port option.
|
|
|
|
#
|
|
|
|
###
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptPort < OptBase
|
2009-11-03 18:09:05 +00:00
|
|
|
def type
|
|
|
|
return 'port'
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2013-01-12 00:34:27 +00:00
|
|
|
def normalize(value)
|
|
|
|
value.to_i
|
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
2007-03-09 06:12:28 +00:00
|
|
|
return false if empty_required_value?(value)
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
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-11-15 15:11:43 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Network address option.
|
|
|
|
#
|
|
|
|
###
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptAddress < OptBase
|
2009-11-03 18:09:05 +00:00
|
|
|
def type
|
|
|
|
return 'address'
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
2007-03-09 06:12:28 +00:00
|
|
|
return false if empty_required_value?(value)
|
|
|
|
|
2005-05-21 18:27:24 +00:00
|
|
|
if (value != nil and value.empty? == false)
|
2005-05-21 17:57:00 +00:00
|
|
|
begin
|
2011-12-10 14:16:47 +00:00
|
|
|
::Rex::Socket.getaddress(value, true)
|
2005-05-21 17:57:00 +00:00
|
|
|
rescue
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Network address range option.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class OptAddressRange < OptBase
|
2009-11-03 18:09:05 +00:00
|
|
|
def type
|
|
|
|
return 'addressrange'
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def normalize(value)
|
2007-04-24 06:27:39 +00:00
|
|
|
if (value =~ /^file:(.*)/)
|
|
|
|
path = $1
|
2009-12-04 19:37:14 +00:00
|
|
|
return false if not File.exists?(path) or File.directory?(path)
|
|
|
|
return File.readlines(path).map{ |s| s.strip}.join(" ")
|
2010-11-19 18:31:14 +00:00
|
|
|
elsif (value =~ /^rand:(.*)/)
|
|
|
|
count = $1.to_i
|
|
|
|
return false if count < 1
|
|
|
|
ret = ''
|
|
|
|
count.times {
|
|
|
|
ret << " " if not ret.empty?
|
2011-12-10 19:26:47 +00:00
|
|
|
ret << [ rand(0x100000000) ].pack("N").unpack("C*").map{|x| x.to_s }.join(".")
|
2010-11-19 18:31:14 +00:00
|
|
|
}
|
|
|
|
return ret
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2009-12-04 19:37:14 +00:00
|
|
|
return value
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
def valid?(value)
|
2007-03-09 06:12:28 +00:00
|
|
|
return false if empty_required_value?(value)
|
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
if (value != nil and value.empty? == false)
|
2009-12-04 19:37:14 +00:00
|
|
|
walker = Rex::Socket::RangeWalker.new(normalize(value))
|
|
|
|
if (not walker or not walker.valid?)
|
2006-08-12 08:31:38 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# File system path option.
|
|
|
|
#
|
|
|
|
###
|
2005-05-21 18:27:24 +00:00
|
|
|
class OptPath < OptBase
|
2009-11-03 18:09:05 +00:00
|
|
|
def type
|
|
|
|
return 'path'
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2011-06-30 06:52:52 +00:00
|
|
|
# Generally, 'value' should be a file that exists.
|
2005-05-21 18:27:24 +00:00
|
|
|
def valid?(value)
|
2007-03-09 06:12:28 +00:00
|
|
|
return false if empty_required_value?(value)
|
2011-06-30 06:52:52 +00:00
|
|
|
if value and !value.empty?
|
|
|
|
if value =~ /^memory:\s*([0-9]+)/i
|
|
|
|
return false unless check_memory_location($1)
|
|
|
|
else
|
|
|
|
unless File.exists?(value)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
return super
|
|
|
|
end
|
2011-06-30 06:52:52 +00:00
|
|
|
|
|
|
|
# The AuthBrute mixin can take a memory address as well --
|
|
|
|
# currently, no other OptFile can make use of these objects.
|
|
|
|
# TODO: Implement memory:xxx to be more generally useful so
|
|
|
|
# the validator on OptFile isn't lying for non-AuthBrute.
|
|
|
|
def check_memory_location(id)
|
|
|
|
return false unless self.class.const_defined?(:ObjectSpace)
|
|
|
|
obj = ObjectSpace._id2ref(id.to_i) rescue nil
|
|
|
|
return false unless obj.respond_to? :acts_as_file?
|
2011-11-20 01:10:08 +00:00
|
|
|
return false unless obj.acts_as_file? # redundant?
|
2011-06-30 06:52:52 +00:00
|
|
|
return !!obj
|
|
|
|
end
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Integer option.
|
|
|
|
#
|
|
|
|
###
|
2005-09-15 07:16:03 +00:00
|
|
|
class OptInt < OptBase
|
2009-11-03 18:09:05 +00:00
|
|
|
def type
|
|
|
|
return 'integer'
|
2005-07-18 23:32:34 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 21:50:10 +00:00
|
|
|
def normalize(value)
|
2007-04-05 05:00:55 +00:00
|
|
|
if (value.to_s.match(/^0x[a-fA-F\d]+$/))
|
|
|
|
value.to_i(16)
|
|
|
|
else
|
|
|
|
value.to_i
|
|
|
|
end
|
2005-11-15 21:50:10 +00:00
|
|
|
end
|
|
|
|
|
2005-07-18 23:32:34 +00:00
|
|
|
def valid?(value)
|
2013-01-14 21:47:58 +00:00
|
|
|
return super if !required? and value.to_s.empty?
|
2007-03-09 06:12:28 +00:00
|
|
|
return false if empty_required_value?(value)
|
|
|
|
|
2013-01-14 20:18:56 +00:00
|
|
|
if value and not value.to_s.match(/^0x[0-9a-fA-F]+$|^-?\d+$/)
|
2005-07-18 23:32:34 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-23 18:25:52 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Regexp option
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class OptRegexp < OptBase
|
|
|
|
def type
|
|
|
|
return 'regexp'
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?(value)
|
|
|
|
unless super
|
|
|
|
return false
|
|
|
|
end
|
2012-03-26 23:06:43 +00:00
|
|
|
return true if (not required? and value.nil?)
|
2012-01-23 18:25:52 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
Regexp.compile(value)
|
|
|
|
|
|
|
|
return true
|
2013-01-14 20:18:56 +00:00
|
|
|
rescue RegexpError
|
2012-01-23 18:25:52 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize(value)
|
2012-03-26 23:06:43 +00:00
|
|
|
return nil if value.nil?
|
2012-01-23 18:25:52 +00:00
|
|
|
return Regexp.compile(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_value(value)
|
|
|
|
if value.kind_of?(Regexp)
|
|
|
|
return value.source
|
|
|
|
elsif value.kind_of?(String)
|
|
|
|
return display_value(normalize(value))
|
|
|
|
end
|
|
|
|
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
2005-07-18 23:32:34 +00:00
|
|
|
|
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
|
2009-11-03 18:09:05 +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-11-15 15:11:43 +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-11-15 15:11:43 +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,
|
2006-01-05 03:57:12 +00:00
|
|
|
# excluding advanced (and evasions).
|
2005-07-10 00:49:12 +00:00
|
|
|
#
|
|
|
|
def has_options?
|
|
|
|
each_option { |name, opt|
|
|
|
|
return true if (opt.advanced? == false)
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2005-07-10 00:49:12 +00:00
|
|
|
}
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2005-07-10 00:49:12 +00:00
|
|
|
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
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2006-01-05 03:57:12 +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)
|
|
|
|
}
|
2005-07-10 00:49:12 +00:00
|
|
|
|
2006-01-05 03:57:12 +00:00
|
|
|
return false
|
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2005-07-11 05:21:19 +00:00
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Removes an option.
|
2005-07-11 05:21:19 +00:00
|
|
|
#
|
|
|
|
def remove_option(name)
|
|
|
|
delete(name)
|
2005-07-11 20:32:56 +00:00
|
|
|
sorted.each_with_index { |e, idx|
|
2010-11-05 21:16:33 +00:00
|
|
|
sorted[idx] = nil if (e[0] == name)
|
2005-07-11 20:32:56 +00:00
|
|
|
}
|
2010-11-05 21:16:33 +00:00
|
|
|
sorted.delete(nil)
|
2005-07-11 05:21:19 +00:00
|
|
|
end
|
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Adds one or more options.
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2006-01-05 03:57:12 +00:00
|
|
|
def add_options(opts, owner = nil, advanced = false, evasion = 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))
|
2006-01-05 03:57:12 +00:00
|
|
|
add_options_array(opts, owner, advanced, evasion)
|
2005-06-05 04:27:57 +00:00
|
|
|
else
|
2006-01-05 03:57:12 +00:00
|
|
|
add_options_hash(opts, owner, advanced, evasion)
|
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
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Add options from a hash of names.
|
2005-06-05 04:27:57 +00:00
|
|
|
#
|
2006-01-05 03:57:12 +00:00
|
|
|
def add_options_hash(opts, owner = nil, advanced = false, evasion = false)
|
2005-06-05 04:27:57 +00:00
|
|
|
opts.each_pair { |name, opt|
|
2006-01-05 03:57:12 +00:00
|
|
|
add_option(opt, name, owner, advanced, evasion)
|
2005-06-05 04:27:57 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Add options from an array of option instances or arrays.
|
2005-06-05 04:27:57 +00:00
|
|
|
#
|
2006-01-05 03:57:12 +00:00
|
|
|
def add_options_array(opts, owner = nil, advanced = false, evasion = false)
|
2005-06-05 04:27:57 +00:00
|
|
|
opts.each { |opt|
|
2006-01-05 03:57:12 +00:00
|
|
|
add_option(opt, nil, owner, advanced, evasion)
|
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
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Adds an option.
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2006-01-05 03:57:12 +00:00
|
|
|
def add_option(option, name = nil, owner = nil, advanced = false, evasion = 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))
|
2009-11-03 18:09:05 +00:00
|
|
|
raise ArgumentError,
|
|
|
|
"The option named #{name} did not come in a compatible format.",
|
2005-06-05 04:27:57 +00:00
|
|
|
caller
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2005-06-05 04:27:57 +00:00
|
|
|
option.advanced = advanced
|
2006-01-05 03:57:12 +00:00
|
|
|
option.evasion = evasion
|
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-11-15 15:11:43 +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
|
|
|
|
|
2006-01-05 03:57:12 +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)
|
|
|
|
|
|
|
|
add_options(opts, owner, false, true)
|
|
|
|
end
|
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
2009-11-03 18:09:05 +00:00
|
|
|
# Make sures that each of the options has a value of a compatible
|
2005-11-15 15:11:43 +00:00
|
|
|
# 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 = []
|
|
|
|
|
2009-11-03 18:09:05 +00:00
|
|
|
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
|
2005-11-15 21:50:10 +00:00
|
|
|
# If the option is valid, normalize its format to the correct type.
|
|
|
|
elsif ((val = option.normalize(datastore[name])) != nil)
|
2012-05-24 22:28:20 +00:00
|
|
|
# This *will* result in a module that previously used the
|
2012-03-07 13:37:02 +00:00
|
|
|
# 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
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
}
|
2009-11-03 18:09:05 +00:00
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
if (errors.empty? == false)
|
2009-11-03 18:09:05 +00:00
|
|
|
raise OptionValidateError.new(errors),
|
2005-05-21 17:57:00 +00:00
|
|
|
"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
|
|
|
|
|
2011-04-30 04:59:27 +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]]
|
|
|
|
end
|
|
|
|
list.each(&block)
|
|
|
|
end
|
|
|
|
|
2005-11-24 20:41:56 +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
|
|
|
|
|
|
|
|
other_container.each { |name, opt|
|
|
|
|
if (result.get(name) == nil)
|
|
|
|
result[name] = opt
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
result.sort
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# The sorted array of options.
|
|
|
|
#
|
2005-07-11 15:34:31 +00:00
|
|
|
attr_reader :sorted
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
attr_writer :sorted # :nodoc:
|
2005-07-11 15:34:31 +00:00
|
|
|
|
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
|
|
|
|
|
2009-11-03 18:09:05 +00:00
|
|
|
@@builtin_opts =
|
2005-06-05 04:27:57 +00:00
|
|
|
{
|
2005-11-11 01:22:03 +00:00
|
|
|
'RHOST' => [ OptAddress, 'nil', true, '"The target address"' ],
|
|
|
|
'RPORT' => [ OptPort, 'nil', true, '"The target port"' ],
|
2010-06-02 16:23:46 +00:00
|
|
|
'LHOST' => [ OptAddress, 'nil', true, '"The listen address"' ],
|
|
|
|
'LPORT' => [ OptPort, 'nil', true, '"The listen port"' ],
|
2005-11-11 01:22:03 +00:00
|
|
|
'CPORT' => [ OptPort, 'nil', false, '"The local client port"' ],
|
2007-02-18 07:02:47 +00:00
|
|
|
'CHOST' => [ OptAddress, 'nil', false, '"The local client address"' ],
|
2009-11-03 18:09:05 +00:00
|
|
|
'Proxies' => [ OptString, 'nil', 'false', '"Use a proxy chain"']
|
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
|
|
|
|
|
2009-11-03 18:09:05 +00:00
|
|
|
#
|
2005-06-05 04:27:57 +00:00
|
|
|
# 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
|
|
|
|
|
2008-11-03 09:17:08 +00:00
|
|
|
end
|
2009-11-03 18:09:05 +00:00
|
|
|
|