Skip to content

Commit

Permalink
feat: persistence mixin draft
Browse files Browse the repository at this point in the history
  • Loading branch information
dledda-r7 committed Jan 29, 2025
1 parent cda0881 commit 74acdf2
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions lib/msf/core/exploit/local/persistence.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,61 @@
# -*- coding: binary -*-

module Msf
# This module provides methods for persisting on a target system. Mainly initialization
# options.
module Exploit::Local::Persistence
def initialize(info = {})
@persistence_service = Rex::Sync::Event.new(auto_reset=false)
super(
update_info(
info,
'DefaultOptions' => {
# leaving this commented out, we don't want a wfs delay so that the module
# will run forever.
# 'WfsDelay' => 25 * 60 * 60, # 25hrs
'AllowNoCleanup' => true # don't delete our persistence after we get a shell
},
'DefaultOptions' => {},
# https://github.com/rapid7/metasploit-framework/pull/19676#discussion_r1907594308
'Stance' => Msf::Exploit::Stance::Passive
# 'Passive' => true # XXX when set, ignores wfsdelay and immediately exists after last command
'Stance' => Msf::Exploit::Stance::Passive,
'Passive' => true,
'Actions' => [
[ 'INSTALL', { 'Description' => 'Install the persistence' } ],
[ 'CLEANUP', { 'Description' => 'Cleanup the persistence' } ]
],
'DefaultAction' => 'INSTALL'
)
)

register_advanced_options(
[
OptString.new('WritableDir', [true, 'A directory where we can write files', ''])
OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp/']),
OptBool.new('CleanUpPersistence', [true, 'Remove the installed persistence at the end of the module', false])
]
)
end

def exploit

case action.name.upcase
when 'INSTALL'
run_as_background = !datastore['DisablePayloadHandler']
print_warning('Payload handler is disabled, the persistence will be installed only.') unless run_as_background

# Call the install_persistence function
# must be declared inside the persistence module
install_persistence

@persistence_service.wait if run_as_background

cleanup_persistence if datastore['CleanUpPersistence']

when 'CLEANUP'

# call cleanup_persistence
# must be declared inside the persistence module
cleanup_persistence
end
end

def install_persistence
# to be overloaded by the module
end

def cleanup
# this is done by the action
end
end
end
end

0 comments on commit 74acdf2

Please sign in to comment.