Path: blob/master/modules/post/multi/manage/multi_post.rb
19591 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Multi Manage Post Module Macro Execution',13'Description' => %q{14This module will execute a list of modules given in a macro file in the format15of <module> <opt=val,opt=val> against the select session checking for compatibility16of the module against the sessions and validation of the options provided.17},18'License' => MSF_LICENSE,19'Author' => [ '<carlos_perez[at]darkoperator.com>'],20'Platform' => %w[linux osx solaris unix win],21'SessionTypes' => [ 'meterpreter', 'shell' ],22'Notes' => {23'Stability' => [CRASH_SAFE],24'SideEffects' => [CONFIG_CHANGES],25'Reliability' => []26}27)28)29register_options(30[31OptString.new('MACRO', [true, 'File with Post Modules and Options to run in the session', nil])32]33)34end3536def run37hostname = sysinfo.nil? ? cmd_exec('hostname') : sysinfo['Computer']38print_status("Running module against #{hostname} (#{session.session_host})")3940macro = datastore['MACRO']4142fail_with(Failure::BadConfig, 'Resource File does not exist!') unless ::File.exist?(macro)4344entries = []4546::File.open(macro, 'rb').each_line do |line|47# Empty line48next if line.strip.empty?49# Comment50next if line[0, 1] == '#'5152entries << line.chomp53end5455fail_with(Failure::BadConfig, 'Resource File was empty!') if entries.blank?5657entries.each do |l|58values = l.split(' ')59post_mod = values[0]60if values.length == 261mod_opts = values[1].split(',')62end63print_line("Loading #{post_mod}")64# Make sure we can handle post module names with or without post in the start65if post_mod =~ %r{^post/}66post_mod.gsub!(%r{^post/}, '')67end68m = framework.post.create(post_mod)6970# Check if a post module was actually initiated71if m.nil?72print_error("Post module #{post_mod} could not be initialized!")73next74end7576# Set the current session77s = datastore['SESSION']7879if !m.session_compatible?(s.to_i)80print_error("Session #{s} is not compatible with #{post_mod}")81next82end8384print_line("Running Against #{s}")85m.datastore['SESSION'] = s86if mod_opts87mod_opts.each do |o|88opt_pair = o.split('=', 2)89print_line("\tSetting Option #{opt_pair[0]} to #{opt_pair[1]}")90m.datastore[opt_pair[0]] = opt_pair[1]91end92end93m.options.validate(m.datastore)94m.run_simple(95'LocalInput' => user_input,96'LocalOutput' => user_output97)98end99end100end101102103