-
Notifications
You must be signed in to change notification settings - Fork 54
/
detail.rb
executable file
·319 lines (274 loc) · 10.1 KB
/
detail.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# Details about an Msf::Module. Metadata that can be an array is stored in associations in modules under the
# {Mdm::Module} namespace.
class Mdm::Module::Detail < ActiveRecord::Base
self.table_name = 'module_details'
#
# CONSTANTS
#
# The directory for a given {#mtype} is a not always the pluralization of {#mtype}, so this maps the {#mtype} to the
# type directory that is used to generate the {#file} from the {#mtype} and {#refname}.
DIRECTORY_BY_TYPE = {
'auxiliary' => 'auxiliary',
'encoder' => 'encoders',
'exploit' => 'exploits',
'nop' => 'nops',
'payload' => 'payloads',
'post' => 'post'
}
# {#privileged} is Boolean so, valid values are just `true` and `false`, but since both the validation and
# factory need an array of valid values, this constant exists.
PRIVILEGES = [
false,
true
]
# Converts {#rank}, which is an Integer, to the name used for that rank.
RANK_BY_NAME = {
'Manual' => 0,
'Low' => 100,
'Average' => 200,
'Normal' => 300,
'Good' => 400,
'Great' => 500,
'Excellent' => 600
}
# Valid values for {#stance}.
STANCES = [
'aggressive',
'passive'
]
#
# Associations
#
# @!attribute [rw] actions
# Auxiliary actions to perform when this running this module.
#
# @return [ActiveRecord::Relation<Mdm::Module::Action>]
has_many :actions, :class_name => 'Mdm::Module::Action', :dependent => :destroy
# @!attribute [rw] archs
# Architectures supported by this module.
#
# @return [ActiveRecord::Relation<Mdm::Module::Arch>]
has_many :archs, :class_name => 'Mdm::Module::Arch', :dependent => :destroy
# @!attribute [rw] authors
# Authors (and their emails) of this module. Usually includes the original discoverer who wrote the
# proof-of-concept and then the people that ported the proof-of-concept to metasploit-framework.
#
# @return [ActiveRecord::Relation<Mdm::Module::Mixin>]
has_many :authors, :class_name => 'Mdm::Module::Author', :dependent => :destroy
# @!attribute [rw] matches
# Matches for this module
#
# @return [ActiveRecord::Relation<MetasploitDataModels::AutomaticExploitation::Match>]
has_many :matches,
:class_name => 'MetasploitDataModels::AutomaticExploitation::Match',
:primary_key => :fullname,
:foreign_key => :module_fullname,
:inverse_of => :module_detail
# @!attribute [rw] mixins
# Mixins used by this module.
#
# @return [ActiveRecord::Relation<Mdm::Module::Mixin>]
has_many :mixins, :class_name => 'Mdm::Module::Mixin', :dependent => :destroy
# @!attribute [rw] module_runs
# Records of times when this module has been used
#
# @return [ActiveRecord::Relation<MetasploitDataModels::ModuleRun>]
has_many :module_runs,
:class_name => 'MetasploitDataModels::ModuleRun',
:primary_key => :fullname,
:foreign_key => :module_fullname,
:inverse_of => :module_detail
# @!attribute [rw] platforms
# Platforms supported by this module.
#
# @return [ActiveRecord::Relation<Mdm::Module::Platform>]
has_many :platforms, :class_name => 'Mdm::Module::Platform', :dependent => :destroy
# @!attribute [rw] refs
# External references to the vulnerabilities this module exploits.
#
# @return [ActiveRecord::Relation<Mdm::Module::Ref>]
has_many :refs, :class_name => 'Mdm::Module::Ref', :dependent => :destroy
# @!attribute [rw] targets
# Names of targets with different configurations that can be exploited by this module.
#
# @return [ActiveRecord::Relation<Mdm::Module::Target>]
has_many :targets, :class_name => 'Mdm::Module::Target', :dependent => :destroy
#
# Attributes
#
# @!attribute [rw] default_action
# Name of the default action in {#actions}.
#
# @return [String] {Mdm::Module::Action#name}.
# @!attribute [rw] default_target
# Name of the default target in {#targets}.
#
# @return [String] {Mdm::Module::Target#name}.
# @!attribute [rw] description
# A long, paragraph description of what the module does.
#
# @return [String]
# @!attribute [rw] disclosure_date
# The date the vulnerability exploited by this module was disclosed to the public.
#
# @return [DateTime]
# @!attribute [rw] file
# The full path to the module file on-disk.
#
# @return [String]
# @!attribute [rw] fullname
# The full name of the module. The full name is "{#mtype}/{#refname}".
#
# @return [String]
# @!attribute [rw] license
# The name of the software license for the module's code.
#
# @return [String]
# @!attribute [rw] mtime
# The modification time of the module file on-disk.
#
# @return [DateTime]
# @!attribute [rw] mtype
# The type of the module.
#
# @return [String] key in {DIRECTORY_BY_TYPE}
# @!attribute [rw] name
# The human readable name of the module. It is unrelated to {#fullname} or {#refname} and is better thought of
# as a short summary of the {#description}.
#
# @return [String]
# @!attribute [rw] privileged
# Whether this module requires priveleged access to run.
#
# @return [Boolean]
# @!attribute [rw] rank
# The reliability of the module and likelyhood that the module won't knock over the service or host being exploited.
# Bigger values is better.
#
# @return [Integer]
# @!attribute [rw] ready
# Boolean indicating whether the metadata for the module has been updated from the on-disk module.
#
# @return [false] if the associations are still being updated.
# @return [true] if this detail and its associations are up-to-date.
# @!attribute [rw] refname
# The reference name of the module.
#
# @return [String]
# @!attribute [rw] stance
# Whether the module is active or passive. `nil` if the {#mtype module type} does not
# {#supports_stance? support stances}.
#
# @return ['active', 'passive', nil]
#
# Validations
#
validates :mtype,
:inclusion => {
:in => DIRECTORY_BY_TYPE.keys
}
validates :privileged,
:inclusion => {
:in => PRIVILEGES
}
validates :rank,
:inclusion => {
:in => RANK_BY_NAME.values
},
:numericality => {
:only_integer => true
}
validates :refname, :presence => true
validates :stance,
:inclusion => {
:if => :supports_stance?,
:in => STANCES
}
validates_associated :actions
validates_associated :archs
validates_associated :authors
validates_associated :mixins
validates_associated :platforms
validates_associated :refs
validates_associated :targets
# Adds an {Mdm::Module::Action} with the given {Mdm::Module::Action#name} to {#actions} and immediately saves it to
# the database.
#
# @param name [String] {Mdm::Module::Action#name}.
# @return [true] if save was successful.
# @return [false] if save was unsucessful.
def add_action(name)
self.actions.build(:name => name).save
end
# Adds an {Mdm::Module::Arch} with the given {Mdm::Module::Arch#name} to {#archs} and immediately saves it to the
# database.
#
# @param name [String] {Mdm::Module::Arch#name}.
# @return [true] if save was successful.
# @return [false] if save was unsuccessful.
def add_arch(name)
self.archs.build(:name => name).save
end
# Adds an {Mdm::Module::Author} with the given {Mdm::Module::Author#name} and {Mdm::Module::Author#email} to
# {#authors} and immediately saves it to the database.
#
# @param name [String] {Mdm::Module::Author#name}.
# @param email [String] {Mdm::Module::Author#email}.
# @return [true] if save was successful.
# @return [false] if save was unsuccessful.
def add_author(name, email=nil)
self.authors.build(:name => name, :email => email).save
end
# Adds an {Mdm::Module::Mixin} with the given {Mdm::Module::Mixin#name} to {#mixins} and immediately saves it to the
# database.
#
# @param name [String] {Mdm::Module::Mixin#name}.
# @return [true] if save was successful.
# @return [false] if save was unsuccessful.
def add_mixin(name)
self.mixins.build(:name => name).save
end
# Adds an {Mdm::Module::Platform} with the given {Mdm::Module::Platform#name} to {#platforms} and immediately saves it
# to the database.
#
# @param name [String] {Mdm::Module::Platform#name}.
# @return [true] if save was successful.
# @return [false] if save was unsuccessful.
def add_platform(name)
self.platforms.build(:name => name).save
end
# Adds an {Mdm::Module::Ref} with the given {Mdm::Module::Ref#name} to {#refs} and immediately saves it to the
# database.
#
# @param name [String] {Mdm::Module::Ref#name}.
# @return [true] if save was successful.
# @return [false] if save was unsuccessful.
def add_ref(name)
self.refs.build(:name => name).save
end
# Adds an {Mdm::Module::Target} with the given {Mdm::Module::Target#index} and {Mdm::Module::Target#name} to
# {#targets} and immediately saves it to the database.
#
# @param index [Integer] index of target among other {#targets}.
# @param name [String] {Mdm::Module::Target#name}.
# @return [true] if save was successful.
# @return [false] if save was unsuccessful.
def add_target(index, name)
self.targets.build(:index => index, :name => name).save
end
# Returns whether this module supports a {#stance}. Only modules with {#mtype} `'auxiliary'` and `'exploit'` support
# a non-nil {#stance}.
#
# @return [true] if {#mtype} is `'auxiliary'` or `'exploit'`
# @return [false] otherwise
# @see https://github.com/rapid7/metasploit-framework/blob/a6070f8584ad9e48918b18c7e765d85f549cb7fd/lib/msf/core/db_manager.rb#L423
# @see https://github.com/rapid7/metasploit-framework/blob/a6070f8584ad9e48918b18c7e765d85f549cb7fd/lib/msf/core/db_manager.rb#L436
def supports_stance?
supports_stance = false
if ['auxiliary', 'exploit'].include? mtype
supports_stance = true
end
supports_stance
end
Metasploit::Concern.run(self)
end