CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/support/acceptance/target.rb
Views: 11780
1
module Acceptance
2
###
3
# Stores the data for a target. These credentials can be used to create a sesion, or run a module against
4
###
5
class Target
6
attr_reader :session_module, :type, :datastore
7
8
def initialize(options)
9
@type = options.fetch(:type)
10
@session_module = options.fetch(:session_module)
11
@datastore = options.fetch(:datastore)
12
end
13
14
def [](k)
15
options[k]
16
end
17
18
# @param [Hash] default_global_datastore
19
# @return [String] The setg commands for setting the global datastore
20
def setg_commands(default_global_datastore: {})
21
commands = []
22
# Ensure the global framework datastore is always clear
23
commands << "irb -e '(self.respond_to?(:framework) ? framework : self).datastore.user_defined.clear'"
24
# Call setg
25
global_datastore = default_global_datastore.merge(@datastore[:global])
26
global_datastore.each do |key, value|
27
commands << "setg #{key} #{value}"
28
end
29
commands.join("\n")
30
end
31
32
# @param [Hash] default_module_datastore
33
# @return [String] The datastore options string
34
def datastore_options(default_module_datastore: {})
35
module_datastore = default_module_datastore.merge(@datastore[:module])
36
module_options = module_datastore.map do |key, value|
37
"#{key}=#{value}"
38
end
39
40
module_options.join(' ')
41
end
42
43
# @param [Hash] default_module_datastore
44
# @return [String] The command which can be used on msfconsole to generate the payload
45
def run_command(default_module_datastore: {})
46
"run #{datastore_options(default_module_datastore: default_module_datastore)}"
47
end
48
49
# @param [Hash] default_global_datastore
50
# @param [Hash] default_module_datastore
51
# @return [String] A human readable representation of the payload configuration object
52
def as_readable_text(default_global_datastore: {}, default_module_datastore: {})
53
<<~EOF
54
## Session module
55
use #{session_module}
56
57
## Set global datastore
58
#{setg_commands(default_global_datastore: default_global_datastore)}
59
60
## Run command
61
#{run_command(default_module_datastore: default_module_datastore)}
62
EOF
63
end
64
end
65
end
66
67