2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2011-01-19 16:01:12 +00:00
|
|
|
|
|
|
|
module Msf::Session
|
|
|
|
|
|
|
|
module Scriptable
|
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
2011-01-19 16:01:12 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
module ClassMethods
|
|
|
|
#
|
|
|
|
# If the +script+ exists, return its path. Otherwise return nil
|
|
|
|
#
|
|
|
|
def find_script_path(script)
|
|
|
|
# Find the full file path of the specified argument
|
|
|
|
check_paths =
|
|
|
|
[
|
|
|
|
script,
|
|
|
|
::File.join(script_base, "#{script}"),
|
|
|
|
::File.join(script_base, "#{script}.rb"),
|
|
|
|
::File.join(user_script_base, "#{script}"),
|
|
|
|
::File.join(user_script_base, "#{script}.rb")
|
|
|
|
]
|
2011-01-19 16:01:12 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
full_path = nil
|
2011-01-19 16:01:12 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Scan all of the path combinations
|
|
|
|
check_paths.each { |path|
|
|
|
|
if ::File.exists?(path)
|
|
|
|
full_path = path
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
2011-01-19 16:01:12 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
full_path
|
|
|
|
end
|
|
|
|
def script_base
|
|
|
|
::File.join(Msf::Config.script_directory, self.type)
|
|
|
|
end
|
|
|
|
def user_script_base
|
|
|
|
::File.join(Msf::Config.user_script_directory, self.type)
|
|
|
|
end
|
2011-01-19 16:01:12 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2011-01-19 16:01:12 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Override
|
|
|
|
#
|
|
|
|
def execute_file
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2011-01-19 16:01:12 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
2014-05-08 21:04:18 +00:00
|
|
|
# Executes the supplied script, Post module, or local Exploit module with
|
|
|
|
# arguments +args+
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Will search the script path.
|
|
|
|
#
|
|
|
|
def execute_script(script_name, *args)
|
|
|
|
mod = framework.modules.create(script_name)
|
2014-05-06 06:21:11 +00:00
|
|
|
if mod
|
2013-08-30 21:28:33 +00:00
|
|
|
# Don't report module run events here as it will be taken care of
|
|
|
|
# in +Post.run_simple+
|
|
|
|
opts = { 'SESSION' => self.sid }
|
|
|
|
args.each do |arg|
|
|
|
|
k,v = arg.split("=", 2)
|
2014-06-17 22:40:50 +00:00
|
|
|
# case doesn't matter in datastore, but it does in hashes, let's normalize
|
|
|
|
opts[k.downcase] = v
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2014-05-06 20:33:51 +00:00
|
|
|
if mod.type == "post"
|
2014-05-06 06:21:11 +00:00
|
|
|
mod.run_simple(
|
|
|
|
# Run with whatever the default stance is for now. At some
|
|
|
|
# point in the future, we'll probably want a way to force a
|
|
|
|
# module to run in the background
|
|
|
|
#'RunAsJob' => true,
|
|
|
|
'LocalInput' => self.user_input,
|
|
|
|
'LocalOutput' => self.user_output,
|
|
|
|
'Options' => opts
|
|
|
|
)
|
2014-05-06 20:33:51 +00:00
|
|
|
elsif mod.type == "exploit"
|
2014-05-06 06:21:11 +00:00
|
|
|
# well it must be a local, we're not currently supporting anything else
|
2014-05-07 16:29:36 +00:00
|
|
|
if mod.exploit_type == "local"
|
2014-05-06 06:21:11 +00:00
|
|
|
# get a copy of the session exploit's datastore if we can
|
|
|
|
original_exploit_datastore = self.exploit.datastore || {}
|
2014-05-08 21:04:18 +00:00
|
|
|
copy_of_orig_exploit_datastore = original_exploit_datastore.clone
|
2014-06-17 22:40:50 +00:00
|
|
|
# convert datastore opts to a hash to normalize casing issues
|
|
|
|
local_exploit_opts = {}
|
2014-07-09 22:05:35 +00:00
|
|
|
copy_of_orig_exploit_datastore.each do |k,v|
|
2014-06-17 22:40:50 +00:00
|
|
|
local_exploit_opts[k.downcase] = v
|
|
|
|
end
|
2014-05-06 06:21:11 +00:00
|
|
|
# we don't want to inherit a couple things, like AutoRunScript's
|
2014-05-09 20:57:36 +00:00
|
|
|
to_neuter = %w{AutoRunScript InitialAutoRunScript LPORT TARGET}
|
2014-05-08 21:04:18 +00:00
|
|
|
to_neuter.each do |setting|
|
2014-06-17 22:40:50 +00:00
|
|
|
local_exploit_opts.delete(setting.downcase)
|
2014-05-08 21:04:18 +00:00
|
|
|
end
|
2014-05-06 06:21:11 +00:00
|
|
|
|
2014-05-08 21:04:18 +00:00
|
|
|
# merge in any opts that were passed in, defaulting all other settings
|
|
|
|
# to the values from the datastore (of the exploit) that spawned the
|
|
|
|
# session
|
2014-06-17 22:40:50 +00:00
|
|
|
local_exploit_opts = local_exploit_opts.merge(opts)
|
2014-05-06 06:21:11 +00:00
|
|
|
|
2014-09-28 08:03:55 +00:00
|
|
|
new_session = mod.exploit_simple(
|
|
|
|
'Payload' => local_exploit_opts.delete('payload'),
|
|
|
|
'Target' => local_exploit_opts.delete('target'),
|
|
|
|
'LocalInput' => self.user_input,
|
|
|
|
'LocalOutput' => self.user_output,
|
|
|
|
'Options' => local_exploit_opts
|
|
|
|
)
|
2014-05-06 06:21:11 +00:00
|
|
|
|
|
|
|
end # end if local
|
|
|
|
end # end if exploit
|
|
|
|
|
2014-05-06 20:33:51 +00:00
|
|
|
else
|
2013-08-30 21:28:33 +00:00
|
|
|
full_path = self.class.find_script_path(script_name)
|
2011-01-19 16:01:12 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# No path found? Weak.
|
|
|
|
if full_path.nil?
|
2014-07-09 22:25:39 +00:00
|
|
|
print_error("The specified script could not be found: #{script_name}")
|
2013-08-30 21:28:33 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
framework.events.on_session_script_run(self, full_path)
|
|
|
|
execute_file(full_path, args)
|
2014-05-06 20:33:51 +00:00
|
|
|
end
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2011-01-19 16:01:12 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|