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/evasion.rb
Views: 11784
1
# -*- coding: binary -*-
2
3
module Msf
4
module Simple
5
6
module Evasion
7
8
include Module
9
10
def self.run_simple(oevasion, opts, &block)
11
evasion = oevasion.replicant
12
# Trap and print errors here (makes them UI-independent)
13
begin
14
# Clone the module to prevent changes to the original instance
15
16
Msf::Simple::Framework.simplify_module(evasion)
17
yield(evasion) if block_given?
18
19
# Import options from the OptionStr or Option hash.
20
evasion._import_extra_options(opts)
21
22
# Make sure parameters are valid.
23
if (opts['Payload'] == nil)
24
raise MissingPayloadError.new, 'A payload has not been selected.', caller
25
end
26
27
# Verify the options
28
evasion.options.validate(evasion.datastore)
29
30
# Start it up
31
driver = EvasionDriver.new(evasion.framework)
32
33
# Initialize the driver instance
34
driver.evasion = evasion
35
driver.payload = evasion.framework.payloads.create(opts['Payload'])
36
37
# Was the payload valid?
38
if (driver.payload == nil)
39
raise MissingPayloadError,
40
"You specified an invalid payload: #{opts['Payload']}", caller
41
end
42
43
# Use the supplied encoder, if any. If one was not specified, then
44
# nil will be assigned causing the evasion to default to picking the
45
# best encoder.
46
evasion.datastore['ENCODER'] = opts['Encoder'] if opts['Encoder']
47
48
# Use the supplied NOP generator, if any. If one was not specified, then
49
# nil will be assigned causing the evasion to default to picking a
50
# compatible NOP generator.
51
evasion.datastore['NOP'] = opts['Nop'] if opts['Nop']
52
53
# Force the payload to share the evasion's datastore
54
driver.payload.share_datastore(driver.evasion.datastore)
55
56
# Verify the payload options
57
driver.payload.options.validate(driver.payload.datastore)
58
59
# Set the target and then work some magic to derive index
60
evasion.datastore['TARGET'] = opts['Target'] if opts['Target']
61
target_idx = evasion.target_index
62
63
if (target_idx == nil or target_idx < 0)
64
raise MissingTargetError,
65
"You must select a target.", caller
66
end
67
68
driver.target_idx = target_idx
69
70
# Set the payload and evasion's subscriber values
71
if ! opts['Quiet']
72
driver.evasion.init_ui(opts['LocalInput'] || evasion.user_input, opts['LocalOutput'] || evasion.user_output)
73
driver.payload.init_ui(opts['LocalInput'] || evasion.user_input, opts['LocalOutput'] || evasion.user_output)
74
else
75
driver.evasion.init_ui(nil, nil)
76
driver.payload.init_ui(nil, nil)
77
end
78
79
if (opts['RunAsJob'])
80
driver.use_job = true
81
end
82
83
# Let's rock this party
84
driver.run
85
86
# Save the job identifier this evasion is running as
87
evasion.job_id = driver.job_id
88
89
# Propagate this back to the caller for console mgmt
90
oevasion.job_id = evasion.job_id
91
rescue ::Interrupt
92
evasion.error = $!
93
raise $!
94
rescue ::Msf::OptionValidateError => e
95
evasion.error = e
96
::Msf::Ui::Formatter::OptionValidateError.print_error(evasion, e)
97
rescue ::Exception => e
98
evasion.error = e
99
evasion.print_error("evasion failed: #{e}")
100
elog("Evasion failed (#{evasion.refname})", error: e)
101
end
102
103
nil
104
end
105
106
def run_simple(opts, &block)
107
Msf::Simple::Evasion.run_simple(self, opts, &block)
108
end
109
110
end
111
112
end
113
end
114
115
116