CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/core/exception.rb
Views: 1904
1
# -*- coding: binary -*-
2
require 'rex/exceptions'
3
module Msf
4
5
###
6
#
7
# Mixin that should be included in all exceptions that can be raised from the
8
# framework so that they can be universally caught. Framework exceptions
9
# automatically extended Rex exceptions
10
#
11
###
12
module Exception
13
include Rex::Exception
14
end
15
16
###
17
#
18
# This exception is raised when one or more options failed
19
# to pass data store validation. The list of option names
20
# can be obtained through the options attribute.
21
#
22
# A human readable error can be associated with each option,
23
# which can contain additional detail / context on why the
24
# option validation failed.
25
#
26
###
27
class OptionValidateError < ArgumentError
28
29
include Exception
30
31
def initialize(options = [], reasons: {}, message: nil)
32
if options.is_a?(Hash)
33
@options = options.keys
34
@reasons = options
35
else
36
@options = options
37
@reasons = reasons
38
end
39
@reasons = @reasons.transform_values { |value| Array(value) }
40
41
super(message || "The following options failed to validate: #{@options.join(', ')}.")
42
end
43
44
attr_reader :options, :reasons
45
end
46
47
###
48
#
49
# This exception is raised when something failed to validate properly.
50
#
51
###
52
class ValidationError < ArgumentError
53
include Exception
54
55
def initialize(msg="One or more requirements could not be validated.")
56
super(msg)
57
end
58
end
59
60
###
61
#
62
# This exception is raised when the module cache is invalidated. It is
63
# handled internally by the ModuleManager.
64
#
65
###
66
class ModuleCacheInvalidated < RuntimeError
67
end
68
69
##
70
#
71
# Encoding exceptions
72
#
73
##
74
75
###
76
#
77
# This exception is raised to indicate that an encoding error of some sort has
78
# occurred.
79
#
80
###
81
class EncodingError < RuntimeError
82
include Exception
83
84
def initialize(msg = "An encoding exception occurred.")
85
super(msg)
86
end
87
end
88
89
###
90
#
91
# Thrown when an encoder fails to find a viable encoding key.
92
#
93
###
94
class NoKeyError < EncodingError
95
def initialize(msg="A valid encoding key could not be found.")
96
super(msg)
97
end
98
end
99
100
###
101
#
102
# Thrown when an encoder fails to encode a buffer due to a bad character.
103
#
104
###
105
class BadcharError < EncodingError
106
def initialize(buf = nil, index = nil, stub_size = nil, char = nil)
107
@buf = buf
108
@index = index
109
@stub_size = stub_size
110
@char = char
111
end
112
113
def to_s
114
# Deal with elements of a String being an instance of String instead of
115
# Integer in ruby 1.9.
116
if (char.respond_to? :ord)
117
c = char.ord
118
else
119
c = char
120
end
121
if (c)
122
return "Encoding failed due to a bad character (index=#{index}, char=#{sprintf("0x%.2x", c)})"
123
else
124
return "Encoding failed due to a nil character"
125
end
126
end
127
128
attr_reader :buf, :index, :stub_size, :char
129
end
130
131
###
132
#
133
# This exception is raised when no encoders succeed to encode a buffer.
134
#
135
###
136
class NoEncodersSucceededError < EncodingError
137
def initialize(msg="No encoders encoded the buffer successfully.")
138
super(msg)
139
end
140
end
141
142
###
143
#
144
# Thrown when an encoder fails to generate a valid opcode sequence.
145
#
146
###
147
class BadGenerateError < EncodingError
148
def initialize(msg="A valid opcode permutation could not be found.")
149
super(msg)
150
end
151
end
152
153
##
154
#
155
# Exploit exceptions
156
#
157
##
158
159
###
160
#
161
# This exception is raised to indicate a general exploitation error.
162
#
163
###
164
module ExploitError
165
include Exception
166
167
def initialize(msg="An exploitation error occurred.")
168
super(msg)
169
end
170
end
171
172
###
173
#
174
# This exception is raised to indicate a general auxiliary error.
175
#
176
###
177
module AuxiliaryError
178
include Exception
179
180
def initialize(msg="An auxiliary error occurred.")
181
super(msg)
182
end
183
end
184
185
###
186
#
187
# This exception is raised if a target was not specified when attempting to
188
# exploit something.
189
#
190
###
191
class MissingTargetError < ArgumentError
192
include ExploitError
193
194
def initialize(msg="A target has not been selected.")
195
super(msg)
196
end
197
end
198
199
###
200
#
201
# This exception is raised if a payload was not specified when attempting to
202
# exploit something.
203
#
204
###
205
class MissingPayloadError < ArgumentError
206
include ExploitError
207
208
def initialize(msg="A payload has not been selected.")
209
super(msg)
210
end
211
end
212
213
###
214
#
215
# This exception is raised if a valid action was not specified when attempting to
216
# run an auxiliary module.
217
#
218
###
219
class MissingActionError < ArgumentError
220
include AuxiliaryError
221
attr_accessor :reason
222
223
def initialize(reason='')
224
@reason = reason
225
super("Invalid action: #{@reason}")
226
end
227
end
228
229
###
230
#
231
# This exception is raised if an incompatible payload was specified when
232
# attempting to exploit something.
233
#
234
###
235
class IncompatiblePayloadError < ArgumentError
236
include ExploitError
237
238
def initialize(pname = nil)
239
@pname = pname
240
super("#{pname} is not a compatible payload.")
241
end
242
243
#
244
# The name of the payload that was used.
245
#
246
attr_reader :pname
247
end
248
249
class NoCompatiblePayloadError < ArgumentError
250
include Exception
251
end
252
253
##
254
#
255
# NOP exceptions
256
#
257
##
258
259
###
260
#
261
# This exception is raised to indicate that a general NOP error occurred.
262
#
263
###
264
module NopError
265
include Exception
266
267
def initialize(msg="A NOP generator error occurred.")
268
super(msg)
269
end
270
end
271
272
###
273
#
274
# This exception is raised when no NOP generators succeed at generating a
275
# sled.
276
#
277
###
278
class NoNopsSucceededError < RuntimeError
279
include NopError
280
281
def initialize(msg="No NOP generators succeeded.")
282
super(msg)
283
end
284
end
285
286
##
287
#
288
# Plugin exceptions
289
#
290
##
291
292
class PluginLoadError < RuntimeError
293
include Exception
294
attr_accessor :reason
295
296
def initialize(reason='')
297
@reason = reason
298
super("This plugin failed to load: #{reason}")
299
end
300
end
301
302
##
303
#
304
# This exception is raised if a payload option string exceeds the maximum
305
# allowed size during the payload generation.
306
#
307
##
308
class PayloadItemSizeError < ArgumentError
309
include Exception
310
311
def initialize(item = nil, max_size = nil)
312
@item = item
313
@max_size = max_size
314
end
315
316
def to_s
317
"Option value: #{item.slice(0..30)} is too big (Current length: #{item.length}, Maximum length: #{max_size})."
318
end
319
320
attr_reader :item # The content of the payload option (for example a URL)
321
attr_reader :max_size # The maximum allowed size of the payload option
322
end
323
324
end
325
326