2005-07-18 04:07:56 +00:00
|
|
|
require 'msf/base'
|
|
|
|
require 'rex/post/meterpreter'
|
|
|
|
|
|
|
|
module Msf
|
|
|
|
module Sessions
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class represents a session compatible interface to a meterpreter server
|
|
|
|
# instance running on a remote machine. It provides the means of interacting
|
|
|
|
# with the server instance both at an API level as well as at a console level.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Meterpreter < Rex::Post::Meterpreter::Client
|
|
|
|
|
|
|
|
#
|
|
|
|
# The meterpreter session is interactive
|
|
|
|
#
|
|
|
|
include Msf::Session
|
|
|
|
include Msf::Session::Interactive
|
2005-09-30 05:59:44 +00:00
|
|
|
include Msf::Session::Comm
|
2005-07-18 04:07:56 +00:00
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Initializes a meterpreter session instance using the supplied rstream
|
|
|
|
# that is to be used as the client's connection to the server.
|
|
|
|
#
|
2005-07-18 04:07:56 +00:00
|
|
|
def initialize(rstream)
|
|
|
|
super
|
|
|
|
|
2005-07-18 05:59:27 +00:00
|
|
|
#
|
|
|
|
# Initialize the meterpreter client
|
|
|
|
#
|
|
|
|
self.init_meterpreter(rstream)
|
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
|
|
|
# Create the console instance
|
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
self.console = Rex::Post::Meterpreter::Ui::Console.new(self)
|
2005-07-18 04:07:56 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Returns the session type as being 'meterpreter'.
|
|
|
|
#
|
2005-07-19 14:33:25 +00:00
|
|
|
def self.type
|
|
|
|
"meterpreter"
|
|
|
|
end
|
2009-08-01 03:50:40 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the exact type of meterpreter
|
|
|
|
#
|
|
|
|
def self.platform
|
|
|
|
"generic/generic"
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the extension for binary files
|
|
|
|
#
|
|
|
|
def self.binary_suffix
|
|
|
|
"generic"
|
|
|
|
end
|
2005-07-19 14:33:25 +00:00
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Msf::Session overrides
|
|
|
|
#
|
|
|
|
##
|
2005-11-15 15:11:43 +00:00
|
|
|
|
2006-09-17 23:12:46 +00:00
|
|
|
#
|
|
|
|
# Cleans up the meterpreter client session.
|
|
|
|
#
|
|
|
|
def cleanup
|
|
|
|
cleanup_meterpreter
|
2006-09-19 03:15:25 +00:00
|
|
|
|
|
|
|
super
|
2006-09-17 23:12:46 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Returns the session description.
|
|
|
|
#
|
2005-07-18 04:07:56 +00:00
|
|
|
def desc
|
|
|
|
"Meterpreter"
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the class method.
|
|
|
|
#
|
2005-07-18 04:07:56 +00:00
|
|
|
def type
|
2005-07-19 14:33:25 +00:00
|
|
|
self.class.type
|
2005-07-18 04:07:56 +00:00
|
|
|
end
|
2009-08-01 04:12:19 +00:00
|
|
|
|
|
|
|
def platform
|
|
|
|
self.class.platform
|
|
|
|
end
|
|
|
|
|
|
|
|
def binary_suffix
|
|
|
|
self.class.binary_suffix
|
|
|
|
end
|
2005-07-18 04:07:56 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Msf::Session::Interactive implementors
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Initializes the console's I/O handles.
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
|
|
|
def init_ui(input, output)
|
2005-07-18 05:13:21 +00:00
|
|
|
console.init_ui(input, output)
|
2005-10-02 03:21:26 +00:00
|
|
|
console.set_log_source(log_source)
|
2005-12-07 03:40:09 +00:00
|
|
|
|
|
|
|
super
|
2005-07-18 04:07:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Resets the console's I/O handles.
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
|
|
|
def reset_ui
|
2005-10-02 03:21:26 +00:00
|
|
|
console.unset_log_source
|
2005-07-18 04:07:56 +00:00
|
|
|
console.reset_ui
|
|
|
|
end
|
|
|
|
|
2005-11-19 15:09:41 +00:00
|
|
|
#
|
|
|
|
# Run the supplied command as if it came from suer input.
|
|
|
|
#
|
|
|
|
def queue_cmd(cmd)
|
|
|
|
console.queue_cmd(cmd)
|
|
|
|
end
|
|
|
|
|
2006-08-15 04:07:25 +00:00
|
|
|
#
|
|
|
|
# Explicitly runs a command.
|
|
|
|
#
|
|
|
|
def run_cmd(cmd)
|
|
|
|
console.run_single(cmd)
|
|
|
|
end
|
|
|
|
|
2006-09-19 03:15:25 +00:00
|
|
|
ScriptBase = Msf::Config.script_directory + Msf::Config::FileSep + "meterpreter"
|
|
|
|
UserScriptBase = Msf::Config.user_script_directory + Msf::Config::FileSep + "meterpreter"
|
|
|
|
|
|
|
|
#
|
|
|
|
# Executes the supplie script.
|
|
|
|
#
|
|
|
|
def execute_script(script, in_binding)
|
|
|
|
# Find the full file path of the specified argument
|
|
|
|
check_paths =
|
|
|
|
[
|
|
|
|
script,
|
|
|
|
ScriptBase + Msf::Config::FileSep + "#{script}",
|
|
|
|
ScriptBase + Msf::Config::FileSep + "#{script}.rb",
|
|
|
|
UserScriptBase + Msf::Config::FileSep + "#{script}",
|
|
|
|
UserScriptBase + Msf::Config::FileSep + "#{script}.rb"
|
|
|
|
]
|
|
|
|
|
|
|
|
full_path = nil
|
|
|
|
|
|
|
|
# Scan all of the path combinations
|
|
|
|
check_paths.each { |path|
|
|
|
|
if ::File.exists?(path)
|
|
|
|
full_path = path
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# No path found? Weak.
|
|
|
|
if full_path.nil?
|
|
|
|
print_error("The specified script could not be found: #{script}")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
execute_file(full_path, in_binding)
|
|
|
|
end
|
|
|
|
|
2006-08-15 04:07:25 +00:00
|
|
|
#
|
|
|
|
# Load the stdapi extension.
|
|
|
|
#
|
|
|
|
def load_stdapi()
|
|
|
|
original = console.disable_output
|
|
|
|
|
|
|
|
console.disable_output = true
|
|
|
|
console.run_single('use stdapi')
|
|
|
|
console.disable_output = original
|
|
|
|
end
|
|
|
|
|
2008-03-02 23:07:33 +00:00
|
|
|
#
|
|
|
|
# Load the priv extension.
|
|
|
|
#
|
|
|
|
def load_priv()
|
|
|
|
original = console.disable_output
|
|
|
|
|
|
|
|
console.disable_output = true
|
|
|
|
console.run_single('use priv')
|
|
|
|
console.disable_output = original
|
|
|
|
end
|
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Interacts with the meterpreter client at a user interface level.
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
|
|
|
def _interact
|
2009-12-22 18:52:48 +00:00
|
|
|
framework.events.on_session_interact(self)
|
2005-07-18 04:07:56 +00:00
|
|
|
# Call the console interaction subsystem of the meterpreter client and
|
|
|
|
# pass it a block that returns whether or not we should still be
|
|
|
|
# interacting. This will allow the shell to abort if interaction is
|
|
|
|
# canceled.
|
2005-12-07 03:40:09 +00:00
|
|
|
console.interact { self.interacting != true }
|
2005-07-18 05:13:21 +00:00
|
|
|
|
|
|
|
# If the stop flag has been set, then that means the user exited. Raise
|
|
|
|
# the EOFError so we can drop this bitch like a bad habit.
|
|
|
|
raise EOFError if (console.stopped? == true)
|
2005-07-18 04:07:56 +00:00
|
|
|
end
|
|
|
|
|
2005-09-30 05:59:44 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Msf::Session::Comm implementors
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates a connection based on the supplied parameters and returns it to
|
|
|
|
# the caller. The connection is created relative to the remote machine on
|
|
|
|
# which the meterpreter server instance is running.
|
|
|
|
#
|
|
|
|
def create(param)
|
2005-11-15 20:33:55 +00:00
|
|
|
sock = nil
|
|
|
|
|
|
|
|
# Notify handlers before we create the socket
|
|
|
|
notify_before_socket_create(self, param)
|
|
|
|
|
2005-09-30 05:59:44 +00:00
|
|
|
case param.proto
|
|
|
|
when 'tcp'
|
2005-11-15 20:33:55 +00:00
|
|
|
sock = net.socket.create(param)
|
2005-09-30 05:59:44 +00:00
|
|
|
else
|
|
|
|
raise Rex::UnsupportedProtocol.new(param.proto), caller
|
|
|
|
end
|
2005-11-15 20:33:55 +00:00
|
|
|
|
|
|
|
# Notify now that we've created the socket
|
|
|
|
notify_socket_created(self, sock, param)
|
|
|
|
|
2008-06-16 05:39:36 +00:00
|
|
|
# Return the socket to the caller
|
2005-11-15 20:33:55 +00:00
|
|
|
sock
|
2005-09-30 05:59:44 +00:00
|
|
|
end
|
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
protected
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
attr_accessor :rstream, :console # :nodoc:
|
2005-07-18 04:07:56 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-08-01 03:50:40 +00:00
|
|
|
end
|