Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/ui/console/command_dispatcher/common.rb
Views: 11784
# -*- coding: binary -*-12require 'rexml/document'34module Msf5module Ui6module Console7module CommandDispatcher89# These are functions that are used in two or more command dispatchers.1011module Common1213# Parse +arg+ into a {Rex::Socket::RangeWalker} and append the result into +host_ranges+14#15# @note This modifies +host_ranges+ in place16#17# @param arg [String] The thing to turn into a RangeWalker18# @param host_ranges [Array] The array of ranges to append19# @param required [Boolean] Whether an empty +arg+ should be an error20# @return [Boolean] true if parsing was successful or false otherwise21def arg_host_range(arg, host_ranges, required=false)22if (!arg and required)23print_error("Missing required host argument")24return false25end26begin27rw = Rex::Socket::RangeWalker.new(arg)28rescue29print_error("Invalid host parameter, #{arg}.")30return false31end3233if rw.valid?34host_ranges << rw35else36print_error("Invalid host parameter, #{arg}.")37return false38end39return true40end4142#43# Parse +arg+ into an array of ports and append the result into +port_ranges+44#45# Returns true if parsing was successful or nil otherwise.46#47# NOTE: This modifies +port_ranges+48#49def arg_port_range(arg, port_ranges, required=false)50if (!arg and required)51print_error("Argument required for -p")52return53end54begin55port_ranges << Rex::Socket.portspec_to_portlist(arg)56rescue57print_error("Invalid port parameter, #{arg}.")58return59end60return true61end6263#64# Set RHOSTS in the +active_module+'s (or global if none) datastore from an array of addresses65#66# This stores all the addresses to a temporary file and utilizes the67# <pre>file:/tmp/filename</pre> syntax to confer the addrs. +rhosts+68# should be an Array. NOTE: the temporary file is *not* deleted69# automatically.70#71def set_rhosts_from_addrs(rhosts)72if rhosts.empty?73print_status("The list is empty, cowardly refusing to set RHOSTS")74return75end76if active_module77mydatastore = active_module.datastore78else79# if there is no module in use set the list to the global variable80mydatastore = self.framework.datastore81end8283if rhosts.length > 584# Lots of hosts makes 'show options' wrap which is difficult to85# read, store to a temp file86rhosts_file = Rex::Quickfile.new("msf-db-rhosts-")87mydatastore['RHOSTS'] = 'file:'+rhosts_file.path88# create the output file and assign it to the RHOSTS variable89rhosts_file.write(rhosts.join("\n")+"\n")90rhosts_file.close91else92# For short lists, just set it directly93mydatastore['RHOSTS'] = rhosts.join(" ")94end9596print_line "RHOSTS => #{mydatastore['RHOSTS']}"97print_line98end99100def show_options(mod) # :nodoc:101mod_opt = Serializer::ReadableText.dump_options(mod, ' ')102print("\nModule options (#{mod.fullname}):\n\n#{mod_opt}\n") if (mod_opt and mod_opt.length > 0)103104# If it's an exploit and a payload is defined, create it and105# display the payload's options106if ((mod.exploit? or mod.evasion? ) and mod.datastore['PAYLOAD'])107p = framework.payloads.create(mod.datastore['PAYLOAD'])108109if (!p)110print_error("Invalid payload defined: #{mod.datastore['PAYLOAD']}\n")111return112end113114p.share_datastore(mod.datastore)115116if (p)117p_opt = Serializer::ReadableText.dump_options(p, ' ')118print("\nPayload options (#{mod.datastore['PAYLOAD']}):\n\n#{p_opt}\n") if (p_opt and p_opt.length > 0)119print(" **DisablePayloadHandler: True (no handler will be created!)**\n\n") if mod.datastore['DisablePayloadHandler'].to_s == 'true'120end121end122123# Print the selected target124if (mod.exploit? and mod.target)125mod_targ = Serializer::ReadableText.dump_exploit_target(mod, ' ')126print("\nExploit target:\n\n#{mod_targ}\n") if (mod_targ and mod_targ.length > 0)127elsif mod.evasion? and mod.target128mod_targ = Serializer::ReadableText.dump_evasion_target(mod, ' ')129print("\nEvasion target:\n\n#{mod_targ}\n") if (mod_targ and mod_targ.length > 0)130end131132# Print the selected action133if mod.kind_of?(Msf::Module::HasActions) && mod.action134mod_action = Serializer::ReadableText.dump_module_action(mod, ' ')135print("\n#{mod.type.capitalize} action:\n\n#{mod_action}\n") if (mod_action and mod_action.length > 0)136end137138print("\nView the full module info with the #{Msf::Ui::Tip.highlight('info')}, or #{Msf::Ui::Tip.highlight('info -d')} command.\n\n")139140# Uncomment this line if u want target like msf2 format141#print("\nTarget: #{mod.target.name}\n\n")142end143144# This is for the "use" and "set" commands145def index_from_list(list, index, &block)146return unless list.kind_of?(Array) && index147148begin149idx = Integer(index)150rescue ArgumentError151return152end153154# Don't support negative indices155return if idx < 0156157yield list[idx]158end159160# Trims starting `.`, `./` `/`, `+path_head+/`, & `/+path_head+/` from +path+. Also trims trailing `.+extension+`161# from +path+, and any possible combination of misspellings of +extension+.162#163# @param path [String] The path to be trimmed164# @param path_head [String] The top-level directory that should be removed from the path165# @param extensions [Array] File extensions to be trimmed from +path+. `.` is automatically included. Defaults to ['rb', 'py', 'go'].166# @return [String] Altered +path+. Will return unaltered +path+ if regex constructed with +path_head+ & +path+ is not detected167def trim_path(path, path_head, extensions: ['rb', 'py', 'go'])168#Builds capture groups for all supported file extensions169regex_extension = ''170extensions.each do |ext|171regex_extension << "([#{ext}])+|"172end173regex_extension.delete_suffix!('|')174175regexp = %r{176(177^\.? # Dot at beginning of path178/? # Slash at beginning of path179(#{path_head}/)? # top level directory (slash prepending directory name is optional)180)181182| # OR183184(185\.(#{regex_extension})$ # any possible file extension at end of path186)187188| # OR189190(191\.$ # trailing dot192)193}ix194195path.gsub(regexp, '')196end197198end199200end201end202end203end204205206