Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/multi/manage/multi_post.rb
19591 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
include Msf::Post::File
8
9
def initialize(info = {})
10
super(
11
update_info(
12
info,
13
'Name' => 'Multi Manage Post Module Macro Execution',
14
'Description' => %q{
15
This module will execute a list of modules given in a macro file in the format
16
of <module> <opt=val,opt=val> against the select session checking for compatibility
17
of the module against the sessions and validation of the options provided.
18
},
19
'License' => MSF_LICENSE,
20
'Author' => [ '<carlos_perez[at]darkoperator.com>'],
21
'Platform' => %w[linux osx solaris unix win],
22
'SessionTypes' => [ 'meterpreter', 'shell' ],
23
'Notes' => {
24
'Stability' => [CRASH_SAFE],
25
'SideEffects' => [CONFIG_CHANGES],
26
'Reliability' => []
27
}
28
)
29
)
30
register_options(
31
[
32
OptString.new('MACRO', [true, 'File with Post Modules and Options to run in the session', nil])
33
]
34
)
35
end
36
37
def run
38
hostname = sysinfo.nil? ? cmd_exec('hostname') : sysinfo['Computer']
39
print_status("Running module against #{hostname} (#{session.session_host})")
40
41
macro = datastore['MACRO']
42
43
fail_with(Failure::BadConfig, 'Resource File does not exist!') unless ::File.exist?(macro)
44
45
entries = []
46
47
::File.open(macro, 'rb').each_line do |line|
48
# Empty line
49
next if line.strip.empty?
50
# Comment
51
next if line[0, 1] == '#'
52
53
entries << line.chomp
54
end
55
56
fail_with(Failure::BadConfig, 'Resource File was empty!') if entries.blank?
57
58
entries.each do |l|
59
values = l.split(' ')
60
post_mod = values[0]
61
if values.length == 2
62
mod_opts = values[1].split(',')
63
end
64
print_line("Loading #{post_mod}")
65
# Make sure we can handle post module names with or without post in the start
66
if post_mod =~ %r{^post/}
67
post_mod.gsub!(%r{^post/}, '')
68
end
69
m = framework.post.create(post_mod)
70
71
# Check if a post module was actually initiated
72
if m.nil?
73
print_error("Post module #{post_mod} could not be initialized!")
74
next
75
end
76
77
# Set the current session
78
s = datastore['SESSION']
79
80
if !m.session_compatible?(s.to_i)
81
print_error("Session #{s} is not compatible with #{post_mod}")
82
next
83
end
84
85
print_line("Running Against #{s}")
86
m.datastore['SESSION'] = s
87
if mod_opts
88
mod_opts.each do |o|
89
opt_pair = o.split('=', 2)
90
print_line("\tSetting Option #{opt_pair[0]} to #{opt_pair[1]}")
91
m.datastore[opt_pair[0]] = opt_pair[1]
92
end
93
end
94
m.options.validate(m.datastore)
95
m.run_simple(
96
'LocalInput' => user_input,
97
'LocalOutput' => user_output
98
)
99
end
100
end
101
end
102
103