Skip to content

Commit

Permalink
Fix edgecase in Meterpreter job persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 committed Mar 23, 2024
1 parent 298e03b commit 9b4fb8e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
12 changes: 8 additions & 4 deletions lib/msf/base/simple/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ module Module
def _import_extra_options(opts)
# If options were supplied, import them into the payload's
# datastore
if (opts['Options'])
self.datastore.import_options_from_hash(opts['Options'])
elsif (opts['OptionStr'])
self.datastore.import_options_from_s(opts['OptionStr'])
if (value = opts['Options'])
if value.is_a?(String)
self.datastore.import_options_from_s(value)
else
self.datastore.import_options_from_hash(value)
end
elsif (value = opts['OptionStr'])
self.datastore.import_options_from_s(value)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/msf/core/opt_meterpreter_debug_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def self.parse_logging_options(value)
result = {}
errors = []

return result if value.nil?
value = value.to_s.strip
return result if value.empty?

value = value.strip
# Match 'rpath:./file', 'rpath:/file', and drive letters e.g. 'rpath:C:/file'
rpath_regex = %r{^rpath:((\.?/\p{ASCII}+)|(\p{ASCII}:/\p{ASCII}+))}i

Expand Down
2 changes: 1 addition & 1 deletion lib/msf/ui/console/command_dispatcher/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def add_persist_job(job_id)

payload_opts = {
'Payload' => payload.refname,
'Options' => payload.datastore,
'Options' => payload.datastore.to_h,
'RunAsJob' => true
}

Expand Down
18 changes: 16 additions & 2 deletions lib/msf/ui/console/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,23 @@ def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {
if restore_handlers
print_status("Starting persistent handler(s)...")

restore_handlers.each do |handler_opts|
restore_handlers.each.with_index do |handler_opts, index|
handler = framework.modules.create(handler_opts['mod_name'])
handler.exploit_simple(handler_opts['mod_options'])
handler.init_ui(self.input, self.output)
replicant_handler = nil
handler.exploit_simple(handler_opts['mod_options']) do |yielded_replicant_handler|
replicant_handler = yielded_replicant_handler
end

if replicant_handler.nil? || replicant_handler.error
print_status("Failed to start persistent payload handler ##{index} (#{handler_opts['mod_name']})")
next
end

if replicant_handler.error.nil?
job_id = handler.job_id
print_status "Persistent payload handler started as Job #{job_id}"
end
end
end

Expand Down
18 changes: 17 additions & 1 deletion spec/lib/msf/core/opt_meterpreter_debug_logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
{ value: 'rpath:C:/log.txt', normalized: 'rpath:C:/log.txt' },
{ value: 'rpath:/tmp/log.txt', normalized: 'rpath:/tmp/log.txt' },
{ value: 'rpath:./log.log', normalized: 'rpath:./log.log' },
{ value: ' rpath:./log.log ', normalized: ' rpath:./log.log ' }
{ value: ' rpath:./log.log ', normalized: ' rpath:./log.log ' },
{ value: '', normalized: '' },
{ value: ' ', normalized: ' ' }
]
invalid_values = [
{ value: 'rpath', normalized: 'rpath' },
Expand All @@ -17,4 +19,18 @@
]

it_behaves_like 'an option', valid_values, invalid_values, 'meterpreterdebuglogging'

describe '.parse_logging_options' do
[
{ value: nil, expected: {} },
{ value: '', expected: {} },
{ value: ' ', expected: {} },
{ value: 'rpath:./file', expected: { rpath: './file' } },
{ value: ' rpath:C:/file ', expected: { rpath: 'C:/file' } },
].each do |test|
it "parses #{test[:value]} as #{test[:expected]}" do
expect(described_class.parse_logging_options(test[:value])).to eq test[:expected]
end
end
end
end

0 comments on commit 9b4fb8e

Please sign in to comment.