Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/lib/msf/core/opt_base.rb
Views: 15981
# -*- coding: binary -*-1require 'resolv'2require 'rex/socket'34module Msf56###7#8# The base class for all options.9#10###11class OptBase1213#14# Initializes a named option with the supplied attribute array.15# The array is composed of three values.16#17# attrs[0] = required (boolean type)18# attrs[1] = description (string)19# attrs[2] = default value20# attrs[3] = possible enum values21# attrs[4] = Regex to validate the option22#23# Attrs can also be specified explicitly via named parameters, or attrs can24# also be a string as standin for the required description field.25#26def initialize(in_name, attrs = [],27required: false, desc: nil, default: nil, conditions: [], enums: [], regex: nil, aliases: [], max_length: nil,28fallbacks: [])29self.name = in_name30self.advanced = false31self.evasion = false32self.aliases = aliases33self.max_length = max_length34self.conditions = conditions35self.fallbacks = fallbacks3637if attrs.is_a?(String) || attrs.length == 038self.required = required39self.desc = attrs.is_a?(String) ? attrs : desc40self.enums = [ *(enums) ].map { |x| x.to_s }41if default.nil? && enums.length > 042self.default = enums[0]43else44self.default = default45end46regex_temp = regex47else48if attrs[0].nil?49self.required = required50else51self.required = attrs[0]52end53self.desc = attrs[1] || desc54self.default = attrs[2] || default55self.enums = attrs[3] || enums56self.enums = [ *(self.enums) ].map { |x| x.to_s }57regex_temp = attrs[4] || regex58end5960unless max_length.nil?61self.desc += " Max parameter length: #{max_length} characters"62end6364if regex_temp65# convert to string66regex_temp = regex_temp.to_s if regex_temp.is_a? Regexp67# remove start and end character, they will be added later68regex_temp = regex_temp.sub(/^\^/, '').sub(/\$$/, '')69# Add start and end marker to match the whole regex70regex_temp = "^#{regex_temp}$"71begin72Regexp.compile(regex_temp)73self.regex = regex_temp74rescue RegexpError, TypeError => e75raise("Invalid Regex #{regex_temp}: #{e}")76end77end78end7980#81# Returns true if this is a required option.82#83def required?84required85end8687#88# Returns true if this is an advanced option.89#90def advanced?91advanced92end9394#95# Returns true if this is an evasion option.96#97def evasion?98evasion99end100101#102# Returns true if the supplied type is equivalent to this option's type.103#104def type?(in_type)105type == in_type106end107108#109# Returns true if this option can be validated on assignment110#111def validate_on_assignment?112true113end114115#116# If it's required and the value is nil or empty, then it's not valid.117#118def valid?(value, check_empty: true)119if check_empty && required?120# required variable not set121return false if (value.nil? || value.to_s.empty?)122end123if regex && !value.nil?124return !!value.match(regex)125end126true127end128129#130# Returns true if the value supplied is nil and it's required to be131# a valid value132#133def empty_required_value?(value)134required? && value.nil?135end136137#138# Normalizes the supplied value to conform with the type that the option is139# conveying.140#141def normalize(value)142value143end144145#146# Returns a string representing a user-friendly display of the chosen value147#148def display_value(value)149value.to_s150end151152#153# Returns true if the value supplied is longer then the max allowed length154#155def invalid_value_length?(value)156if !value.nil? && !max_length.nil?157value.length > max_length158end159end160161#162# The name of the option.163#164attr_reader :name165#166# Whether or not the option is required.167#168attr_reader :required169#170# The description of the option.171#172attr_reader :desc173#174# The default value of the option.175#176attr_reader :default177#178# Storing the name of the option.179#180attr_writer :name181#182# Whether or not this is an advanced option.183#184attr_accessor :advanced185#186# Whether or not this is an evasion option.187#188attr_accessor :evasion189#190# The module or entity that owns this option.191#192attr_accessor :owner193#194# The list of potential conditions195#196attr_accessor :conditions197#198# The list of potential valid values199#200attr_accessor :enums201#202# A optional regex to validate the option value203#204attr_accessor :regex205206#207# Array of aliases for this option for backward compatibility.208#209# This is useful in the scenario of an existing option being renamed210# to a new value. Either the current option name or list of aliases can211# be used in modules for retrieving datastore values, or by a user when212# setting datastore values manually.213#214# @return [Array<String>] the array of aliases215attr_accessor :aliases216217#218# Array of fallbacks for this option.219#220# This is useful in the scenario of wanting specialised option names such as221# {SMBUser}, but to also support gracefully checking a list of more generic fallbacks222# option names such as {Username}.223#224# @return [Array<String>] the array of fallbacks225attr_accessor :fallbacks226227#228# The max length of the input value229#230attr_accessor :max_length231232protected233234attr_writer :required, :desc, :default # :nodoc:235end236end237238239