From f4013b02e14991154f8df10a11c800ab544359aa Mon Sep 17 00:00:00 2001 From: darkbushido Date: Mon, 5 Jun 2017 14:19:18 -0500 Subject: [PATCH 1/2] renaming db_common to common this moves the following methods into common arg_host_range ( used in creds and db ) arg_port_range ( used in creds and db ) set_rhosts_from_addrs ( used in creds and db ) show_options ( used in jobs and modules ) --- lib/msf/ui/console/command_dispatcher.rb | 2 +- .../ui/console/command_dispatcher/common.rb | 147 ++++++++++++++++++ .../ui/console/command_dispatcher/creds.rb | 39 +---- lib/msf/ui/console/command_dispatcher/db.rb | 52 +------ .../console/command_dispatcher/db_common.rb | 57 ------- lib/msf/ui/console/command_dispatcher/jobs.rb | 1 + .../ui/console/command_dispatcher/modules.rb | 39 +---- 7 files changed, 155 insertions(+), 182 deletions(-) create mode 100644 lib/msf/ui/console/command_dispatcher/common.rb delete mode 100644 lib/msf/ui/console/command_dispatcher/db_common.rb diff --git a/lib/msf/ui/console/command_dispatcher.rb b/lib/msf/ui/console/command_dispatcher.rb index 32fea80cf8..6f4d05180d 100644 --- a/lib/msf/ui/console/command_dispatcher.rb +++ b/lib/msf/ui/console/command_dispatcher.rb @@ -1,5 +1,5 @@ # -*- coding: binary -*- - +require 'msf/ui/console/command_dispatcher/common' module Msf module Ui module Console diff --git a/lib/msf/ui/console/command_dispatcher/common.rb b/lib/msf/ui/console/command_dispatcher/common.rb new file mode 100644 index 0000000000..f9a5cc9201 --- /dev/null +++ b/lib/msf/ui/console/command_dispatcher/common.rb @@ -0,0 +1,147 @@ +# -*- coding: binary -*- + +require 'rexml/document' +require 'rex/parser/nmap_xml' +require 'msf/core/db_export' + +module Msf +module Ui +module Console +module CommandDispatcher + + # These are functions that are used in two or more command dispatchers. + +module Common + + # Parse +arg+ into a {Rex::Socket::RangeWalker} and append the result into +host_ranges+ + # + # @note This modifies +host_ranges+ in place + # + # @param arg [String] The thing to turn into a RangeWalker + # @param host_ranges [Array] The array of ranges to append + # @param required [Boolean] Whether an empty +arg+ should be an error + # @return [Boolean] true if parsing was successful or false otherwise + def arg_host_range(arg, host_ranges, required=false) + if (!arg and required) + print_error("Missing required host argument") + return false + end + begin + rw = Rex::Socket::RangeWalker.new(arg) + rescue + print_error("Invalid host parameter, #{arg}.") + return false + end + + if rw.valid? + host_ranges << rw + else + print_error("Invalid host parameter, #{arg}.") + return false + end + return true + end + + # + # Parse +arg+ into an array of ports and append the result into +port_ranges+ + # + # Returns true if parsing was successful or nil otherwise. + # + # NOTE: This modifies +port_ranges+ + # + def arg_port_range(arg, port_ranges, required=false) + if (!arg and required) + print_error("Argument required for -p") + return + end + begin + port_ranges << Rex::Socket.portspec_to_portlist(arg) + rescue + print_error("Invalid port parameter, #{arg}.") + return + end + return true + end + + # + # Set RHOSTS in the +active_module+'s (or global if none) datastore from an array of addresses + # + # This stores all the addresses to a temporary file and utilizes the + #
file:/tmp/filename
syntax to confer the addrs. +rhosts+ + # should be an Array. NOTE: the temporary file is *not* deleted + # automatically. + # + def set_rhosts_from_addrs(rhosts) + if rhosts.empty? + print_status("The list is empty, cowardly refusing to set RHOSTS") + return + end + if active_module + mydatastore = active_module.datastore + else + # if there is no module in use set the list to the global variable + mydatastore = self.framework.datastore + end + + if rhosts.length > 5 + # Lots of hosts makes 'show options' wrap which is difficult to + # read, store to a temp file + rhosts_file = Rex::Quickfile.new("msf-db-rhosts-") + mydatastore['RHOSTS'] = 'file:'+rhosts_file.path + # create the output file and assign it to the RHOSTS variable + rhosts_file.write(rhosts.join("\n")+"\n") + rhosts_file.close + else + # For short lists, just set it directly + mydatastore['RHOSTS'] = rhosts.join(" ") + end + + print_line "RHOSTS => #{mydatastore['RHOSTS']}" + print_line + end + + def show_options(mod) # :nodoc: + mod_opt = Serializer::ReadableText.dump_options(mod, ' ') + print("\nModule options (#{mod.fullname}):\n\n#{mod_opt}\n") if (mod_opt and mod_opt.length > 0) + + # If it's an exploit and a payload is defined, create it and + # display the payload's options + if (mod.exploit? and mod.datastore['PAYLOAD']) + p = framework.payloads.create(mod.datastore['PAYLOAD']) + + if (!p) + print_error("Invalid payload defined: #{mod.datastore['PAYLOAD']}\n") + return + end + + p.share_datastore(mod.datastore) + + if (p) + p_opt = Serializer::ReadableText.dump_options(p, ' ') + print("\nPayload options (#{mod.datastore['PAYLOAD']}):\n\n#{p_opt}\n") if (p_opt and p_opt.length > 0) + end + end + + # Print the selected target + if (mod.exploit? and mod.target) + mod_targ = Serializer::ReadableText.dump_exploit_target(mod, ' ') + print("\nExploit target:\n\n#{mod_targ}\n") if (mod_targ and mod_targ.length > 0) + end + + # Print the selected action + if mod.kind_of?(Msf::Module::HasActions) && mod.action + mod_action = Serializer::ReadableText.dump_module_action(mod, ' ') + print("\n#{mod.type.capitalize} action:\n\n#{mod_action}\n") if (mod_action and mod_action.length > 0) + end + + # Uncomment this line if u want target like msf2 format + #print("\nTarget: #{mod.target.name}\n\n") + end + + +end + +end +end +end +end diff --git a/lib/msf/ui/console/command_dispatcher/creds.rb b/lib/msf/ui/console/command_dispatcher/creds.rb index 0eba702caa..b715afd55b 100644 --- a/lib/msf/ui/console/command_dispatcher/creds.rb +++ b/lib/msf/ui/console/command_dispatcher/creds.rb @@ -3,7 +3,6 @@ require 'rexml/document' require 'rex/parser/nmap_xml' require 'msf/core/db_export' -require 'msf/ui/console/command_dispatcher/db_common' module Msf module Ui @@ -15,7 +14,7 @@ class Creds include Msf::Ui::Console::CommandDispatcher include Metasploit::Credential::Creation - include Msf::Ui::Console::CommandDispatcher::DbCommon + include Msf::Ui::Console::CommandDispatcher::Common # # The dispatcher's name. @@ -53,39 +52,6 @@ class Creds true end - # - # Miscellaneous option helpers - # - - # Parse +arg+ into a {Rex::Socket::RangeWalker} and append the result into +host_ranges+ - # - # @note This modifies +host_ranges+ in place - # - # @param arg [String] The thing to turn into a RangeWalker - # @param host_ranges [Array] The array of ranges to append - # @param required [Boolean] Whether an empty +arg+ should be an error - # @return [Boolean] true if parsing was successful or false otherwise - def arg_host_range(arg, host_ranges, required=false) - if (!arg and required) - print_error("Missing required host argument") - return false - end - begin - rw = Rex::Socket::RangeWalker.new(arg) - rescue - print_error("Invalid host parameter, #{arg}.") - return false - end - - if rw.valid? - host_ranges << rw - else - print_error("Invalid host parameter, #{arg}.") - return false - end - return true - end - # # Can return return active or all, on a certain host or range, on a # certain port or range, and/or on a service name. @@ -118,6 +84,9 @@ class Creds # TODO: this needs to be cleaned up to use the new syntax # def cmd_creds_help + require 'pry' + binding.pry + print_line print_line "With no sub-command, list credentials. If an address range is" print_line "given, show only credentials with logins on hosts within that" diff --git a/lib/msf/ui/console/command_dispatcher/db.rb b/lib/msf/ui/console/command_dispatcher/db.rb index 960345afa9..64aa114fd0 100644 --- a/lib/msf/ui/console/command_dispatcher/db.rb +++ b/lib/msf/ui/console/command_dispatcher/db.rb @@ -3,7 +3,6 @@ require 'rexml/document' require 'rex/parser/nmap_xml' require 'msf/core/db_export' -require 'msf/ui/console/command_dispatcher/db_common' module Msf module Ui @@ -15,7 +14,7 @@ class Db require 'tempfile' include Msf::Ui::Console::CommandDispatcher - include Msf::Ui::Console::CommandDispatcher::DbCommon + include Msf::Ui::Console::CommandDispatcher::Common # # The dispatcher's name. @@ -1809,55 +1808,6 @@ class Db # Miscellaneous option helpers # - # Parse +arg+ into a {Rex::Socket::RangeWalker} and append the result into +host_ranges+ - # - # @note This modifies +host_ranges+ in place - # - # @param arg [String] The thing to turn into a RangeWalker - # @param host_ranges [Array] The array of ranges to append - # @param required [Boolean] Whether an empty +arg+ should be an error - # @return [Boolean] true if parsing was successful or false otherwise - def arg_host_range(arg, host_ranges, required=false) - if (!arg and required) - print_error("Missing required host argument") - return false - end - begin - rw = Rex::Socket::RangeWalker.new(arg) - rescue - print_error("Invalid host parameter, #{arg}.") - return false - end - - if rw.valid? - host_ranges << rw - else - print_error("Invalid host parameter, #{arg}.") - return false - end - return true - end - - # - # Parse +arg+ into an array of ports and append the result into +port_ranges+ - # - # Returns true if parsing was successful or nil otherwise. - # - # NOTE: This modifies +port_ranges+ - # - def arg_port_range(arg, port_ranges, required=false) - if (!arg and required) - print_error("Argument required for -p") - return - end - begin - port_ranges << Rex::Socket.portspec_to_portlist(arg) - rescue - print_error("Invalid port parameter, #{arg}.") - return - end - return true - end # # Takes +host_ranges+, an Array of RangeWalkers, and chunks it up into diff --git a/lib/msf/ui/console/command_dispatcher/db_common.rb b/lib/msf/ui/console/command_dispatcher/db_common.rb deleted file mode 100644 index 68f789d646..0000000000 --- a/lib/msf/ui/console/command_dispatcher/db_common.rb +++ /dev/null @@ -1,57 +0,0 @@ -# -*- coding: binary -*- - -require 'rexml/document' -require 'rex/parser/nmap_xml' -require 'msf/core/db_export' - -module Msf -module Ui -module Console -module CommandDispatcher - -module DbCommon - - # - # Set RHOSTS in the +active_module+'s (or global if none) datastore from an array of addresses - # - # This stores all the addresses to a temporary file and utilizes the - #
file:/tmp/filename
syntax to confer the addrs. +rhosts+ - # should be an Array. NOTE: the temporary file is *not* deleted - # automatically. - # - def set_rhosts_from_addrs(rhosts) - if rhosts.empty? - print_status("The list is empty, cowardly refusing to set RHOSTS") - return - end - if active_module - mydatastore = active_module.datastore - else - # if there is no module in use set the list to the global variable - mydatastore = self.framework.datastore - end - - if rhosts.length > 5 - # Lots of hosts makes 'show options' wrap which is difficult to - # read, store to a temp file - rhosts_file = Rex::Quickfile.new("msf-db-rhosts-") - mydatastore['RHOSTS'] = 'file:'+rhosts_file.path - # create the output file and assign it to the RHOSTS variable - rhosts_file.write(rhosts.join("\n")+"\n") - rhosts_file.close - else - # For short lists, just set it directly - mydatastore['RHOSTS'] = rhosts.join(" ") - end - - print_line "RHOSTS => #{mydatastore['RHOSTS']}" - print_line - end - - -end - -end -end -end -end diff --git a/lib/msf/ui/console/command_dispatcher/jobs.rb b/lib/msf/ui/console/command_dispatcher/jobs.rb index ac184be4e6..6ddb848068 100644 --- a/lib/msf/ui/console/command_dispatcher/jobs.rb +++ b/lib/msf/ui/console/command_dispatcher/jobs.rb @@ -16,6 +16,7 @@ module Msf # class Jobs include Msf::Ui::Console::CommandDispatcher + include Msf::Ui::Console::CommandDispatcher::Common @@handler_opts = Rex::Parser::Arguments.new( "-h" => [ false, "Help Banner"], diff --git a/lib/msf/ui/console/command_dispatcher/modules.rb b/lib/msf/ui/console/command_dispatcher/modules.rb index 09ac5b9233..43a890a686 100644 --- a/lib/msf/ui/console/command_dispatcher/modules.rb +++ b/lib/msf/ui/console/command_dispatcher/modules.rb @@ -13,6 +13,7 @@ module Msf class Modules include Msf::Ui::Console::CommandDispatcher + include Msf::Ui::Console::CommandDispatcher::Common # Constant for a retry timeout on using modules before they're loaded CMD_USE_TIMEOUT = 3 @@ -997,44 +998,6 @@ module Msf show_module_set("Post", framework.post, regex, minrank, opts) end - def show_options(mod) # :nodoc: - mod_opt = Serializer::ReadableText.dump_options(mod, ' ') - print("\nModule options (#{mod.fullname}):\n\n#{mod_opt}\n") if (mod_opt and mod_opt.length > 0) - - # If it's an exploit and a payload is defined, create it and - # display the payload's options - if (mod.exploit? and mod.datastore['PAYLOAD']) - p = framework.payloads.create(mod.datastore['PAYLOAD']) - - if (!p) - print_error("Invalid payload defined: #{mod.datastore['PAYLOAD']}\n") - return - end - - p.share_datastore(mod.datastore) - - if (p) - p_opt = Serializer::ReadableText.dump_options(p, ' ') - print("\nPayload options (#{mod.datastore['PAYLOAD']}):\n\n#{p_opt}\n") if (p_opt and p_opt.length > 0) - end - end - - # Print the selected target - if (mod.exploit? and mod.target) - mod_targ = Serializer::ReadableText.dump_exploit_target(mod, ' ') - print("\nExploit target:\n\n#{mod_targ}\n") if (mod_targ and mod_targ.length > 0) - end - - # Print the selected action - if mod.kind_of?(Msf::Module::HasActions) && mod.action - mod_action = Serializer::ReadableText.dump_module_action(mod, ' ') - print("\n#{mod.type.capitalize} action:\n\n#{mod_action}\n") if (mod_action and mod_action.length > 0) - end - - # Uncomment this line if u want target like msf2 format - #print("\nTarget: #{mod.target.name}\n\n") - end - def show_missing(mod) # :nodoc: mod_opt = Serializer::ReadableText.dump_options(mod, ' ', true) print("\nModule options (#{mod.fullname}):\n\n#{mod_opt}\n") if (mod_opt and mod_opt.length > 0) From 596924552e00caaf48d64a54bb7f4691cb43c3dd Mon Sep 17 00:00:00 2001 From: William Vu Date: Wed, 7 Jun 2017 03:19:30 -0500 Subject: [PATCH 2/2] Fix literal \n in jobs -i Regression from #4063. --- lib/msf/ui/console/command_dispatcher/jobs.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/msf/ui/console/command_dispatcher/jobs.rb b/lib/msf/ui/console/command_dispatcher/jobs.rb index 6ddb848068..cfb2d54bea 100644 --- a/lib/msf/ui/console/command_dispatcher/jobs.rb +++ b/lib/msf/ui/console/command_dispatcher/jobs.rb @@ -165,7 +165,7 @@ module Msf job = framework.jobs[job_id.to_s] mod = job.ctx[0] - output = '\n' + output = "\n" output += "Name: #{mod.name}" output += ", started at #{job.start_time}" if job.start_time print_line(output)