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/lib/msf/base/simple/framework.rb
Views: 11784
1
# -*- coding: binary -*-
2
require 'msf/core/constants'
3
module Msf
4
module Simple
5
6
###
7
#
8
# This class wraps the framework-core supplied Framework class and adds some
9
# helper methods for analyzing statistics as well as other potentially useful
10
# information that is directly necessary to drive the framework-core.
11
#
12
###
13
module Framework
14
include Msf::Simple::Framework::ModulePaths
15
16
###
17
#
18
# Extends the framework.plugins class instance to automatically check in
19
# the framework plugin's directory.
20
#
21
###
22
module PluginManager
23
24
#
25
# Loads the supplied plugin by checking to see if it exists in the
26
# framework default plugin path as necessary.
27
#
28
def load(path, opts = {})
29
def_path = Msf::Config.plugin_directory + File::SEPARATOR + path
30
31
if (File.exist?(def_path) or File.exist?(def_path + ".rb"))
32
super(def_path, opts)
33
else
34
super
35
end
36
end
37
38
end
39
40
#
41
# We extend modules when we're created, and we do it by registering a
42
# general event subscriber.
43
#
44
include GeneralEventSubscriber
45
46
#
47
# Simplifies module instances when they're created.
48
#
49
def on_module_created(instance)
50
Msf::Simple::Framework.simplify_module(instance, load_saved_config: true)
51
end
52
53
ModuleSimplifiers =
54
{
55
Msf::MODULE_ENCODER => Msf::Simple::Encoder,
56
Msf::MODULE_EXPLOIT => Msf::Simple::Exploit,
57
Msf::MODULE_NOP => Msf::Simple::Nop,
58
Msf::MODULE_PAYLOAD => Msf::Simple::Payload,
59
Msf::MODULE_AUX => Msf::Simple::Auxiliary,
60
Msf::MODULE_POST => Msf::Simple::Post,
61
Msf::MODULE_EVASION => Msf::Simple::Evasion
62
}
63
64
# Create a simplified instance of the framework. This routine takes a hash
65
# of parameters as an argument. This hash can contain:
66
#
67
# @param opts [Hash{String => Object}]
68
# @option opts (see simplify)
69
# @return [Msf::Simple::Framework]
70
def self.create(opts = {})
71
framework = Msf::Framework.new(opts)
72
return simplify(framework, opts)
73
end
74
75
# @note If `opts['ConfigDirectory']` is set, then `Msf::Config::Defaults['ConfigDirectory']` will be updated to
76
# `opts['ConfigDirectory']`.
77
#
78
# Extends a framework object that may already exist.
79
#
80
# @param framework [Msf::Framework, Msf::Simple::Framework] framework to simplify
81
# @param opts [Hash{String => Object}]
82
# @option opts [#call] 'OnCreateProc' Proc to call after {#init_simplified}. Will be passed `framework`.
83
# @option opts [String] 'ConfigDirectory' Directory where configuration is saved. The `~/.msf4` directory.
84
# @option opts [Boolean] 'DisableLogging' (false) `true` to disable `Msf::Logging.init`
85
# @option opts [String] 'Logger' (Flatfile) Will default to logging to `~/.msf4`.
86
# @option opts [Boolean] 'DeferModuleLoads' (false) `true` to disable `framework.init_module_paths`.
87
# @return [Msf::Simple::Framework] `framework`
88
def self.simplify(framework, opts)
89
90
# If the framework instance has not already been extended, do it now.
91
if (framework.kind_of?(Msf::Simple::Framework) == false)
92
framework.extend(Msf::Simple::Framework)
93
framework.plugins.extend(Msf::Simple::Framework::PluginManager)
94
end
95
96
# Initialize the simplified framework
97
framework.init_simplified()
98
99
# Call the creation procedure if one was supplied
100
if (opts['OnCreateProc'])
101
opts['OnCreateProc'].call(framework)
102
end
103
104
# Change to a different configuration path if requested
105
if opts['ConfigDirectory']
106
Msf::Config::Defaults['ConfigDirectory'] = opts['ConfigDirectory']
107
end
108
109
# Initialize configuration and logging
110
Msf::Config.init
111
unless opts['DisableLogging']
112
log_sink_name = opts['Logger']
113
Msf::Logging.init(log_sink_name)
114
end
115
116
# Load the configuration
117
framework.load_config
118
119
# Register the framework as its own general event subscriber in this
120
# instance
121
framework.events.add_general_subscriber(framework)
122
123
framework.init_module_paths(defer_module_loads: opts['DeferModuleLoads'])
124
125
return framework
126
end
127
128
#
129
# Simplifies a module instance if the type is supported by extending it
130
# with the simplified module interface.
131
#
132
def self.simplify_module(instance, load_saved_config: false)
133
if ((ModuleSimplifiers[instance.type]) and
134
(instance.class.include?(ModuleSimplifiers[instance.type]) == false))
135
instance.extend(ModuleSimplifiers[instance.type])
136
137
instance.init_simplified(load_saved_config)
138
end
139
end
140
141
142
##
143
#
144
# Simplified interface
145
#
146
##
147
148
#
149
# Initializes the simplified interface.
150
#
151
def init_simplified
152
self.stats = Statistics.new(self)
153
end
154
155
#
156
# Loads configuration, populates the root datastore, etc.
157
#
158
def load_config
159
self.datastore.from_file(Msf::Config.config_file, 'framework/core')
160
end
161
162
#
163
# Saves the module's datastore to the file
164
#
165
def save_config
166
self.datastore.to_file(Msf::Config.config_file, 'framework/core')
167
end
168
169
#
170
# Statistics.
171
#
172
attr_reader :stats
173
174
175
#
176
# Boolean indicating whether the cache is initialized yet
177
#
178
attr_reader :cache_initialized
179
180
#
181
# Thread of the running rebuild operation
182
#
183
attr_reader :cache_thread
184
attr_writer :cache_initialized # :nodoc:
185
attr_writer :cache_thread # :nodoc:
186
187
188
protected
189
190
attr_writer :stats # :nodoc:
191
192
end
193
194
end
195
end
196
197