Path: blob/master/lib/msf/ui/console/command_dispatcher/common.rb
36909 views
# -*- 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@rhosts_file_cleanup_proc ||= at_exit do85@temp_rhosts_files.each do |path|86File.delete(path)87rescue => e88elog(e)89end90end91# Lots of hosts makes 'show options' wrap which is difficult to92# read, store to a temp file93rhosts_file = Rex::Quickfile.create("msf-db-rhosts-")94@temp_rhosts_files ||= []95@temp_rhosts_files << rhosts_file.path96mydatastore['RHOSTS'] = 'file:'+rhosts_file.path97# create the output file and assign it to the RHOSTS variable98rhosts_file.write(rhosts.join("\n")+"\n")99rhosts_file.close100else101# For short lists, just set it directly102mydatastore['RHOSTS'] = rhosts.join(" ")103end104105print_line "RHOSTS => #{mydatastore['RHOSTS']}"106print_line107end108109def show_options(mod) # :nodoc:110mod_opt = Serializer::ReadableText.dump_options(mod, ' ')111print("\nModule options (#{mod.fullname}):\n\n#{mod_opt}\n") if (mod_opt and mod_opt.length > 0)112113# If it's an exploit and a payload is defined, create it and114# display the payload's options115if ((mod.exploit? or mod.evasion? ) and mod.datastore['PAYLOAD'])116p = framework.payloads.create(mod.datastore['PAYLOAD'])117118if (!p)119print_error("Invalid payload defined: #{mod.datastore['PAYLOAD']}\n")120return121end122123p.share_datastore(mod.datastore)124125if (p)126p_opt = Serializer::ReadableText.dump_options(p, ' ')127print("\nPayload options (#{mod.datastore['PAYLOAD']}):\n\n#{p_opt}\n") if (p_opt and p_opt.length > 0)128print(" **DisablePayloadHandler: True (no handler will be created!)**\n\n") if mod.datastore['DisablePayloadHandler'].to_s == 'true'129end130end131132# Print the selected target133if (mod.exploit? and mod.target)134mod_targ = Serializer::ReadableText.dump_exploit_target(mod, ' ')135print("\nExploit target:\n\n#{mod_targ}\n") if (mod_targ and mod_targ.length > 0)136elsif mod.evasion? and mod.target137mod_targ = Serializer::ReadableText.dump_evasion_target(mod, ' ')138print("\nEvasion target:\n\n#{mod_targ}\n") if (mod_targ and mod_targ.length > 0)139end140141# Print the selected action142if mod.kind_of?(Msf::Module::HasActions) && mod.action143mod_action = Serializer::ReadableText.dump_module_action(mod, ' ')144print("\n#{mod.type.capitalize} action:\n\n#{mod_action}\n") if (mod_action and mod_action.length > 0)145end146147print("\nView the full module info with the #{Msf::Ui::Tip.highlight('info')}, or #{Msf::Ui::Tip.highlight('info -d')} command.\n\n")148149# Uncomment this line if u want target like msf2 format150#print("\nTarget: #{mod.target.name}\n\n")151end152153# This is for the "use" and "set" commands154def index_from_list(list, index, &block)155return unless list.kind_of?(Array) && index156157begin158idx = Integer(index)159rescue ArgumentError160return161end162163# Don't support negative indices164return if idx < 0165166yield list[idx]167end168169# Trims starting `.`, `./` `/`, `+path_head+/`, & `/+path_head+/` from +path+. Also trims trailing `.+extension+`170# from +path+, and any possible combination of misspellings of +extension+.171#172# @param path [String] The path to be trimmed173# @param path_head [String] The top-level directory that should be removed from the path174# @param extensions [Array] File extensions to be trimmed from +path+. `.` is automatically included. Defaults to ['rb', 'py', 'go'].175# @return [String] Altered +path+. Will return unaltered +path+ if regex constructed with +path_head+ & +path+ is not detected176def trim_path(path, path_head, extensions: ['rb', 'py', 'go'])177#Builds capture groups for all supported file extensions178regex_extension = ''179extensions.each do |ext|180regex_extension << "([#{ext}])+|"181end182regex_extension.delete_suffix!('|')183184regexp = %r{185(186^\.? # Dot at beginning of path187/? # Slash at beginning of path188(#{path_head}/)? # top level directory (slash prepending directory name is optional)189)190191| # OR192193(194\.(#{regex_extension})$ # any possible file extension at end of path195)196197| # OR198199(200\.$ # trailing dot201)202}ix203204path.gsub(regexp, '')205end206207end208209end210end211end212end213214215