Path: blob/master/lib/msf/base/sessions/smb.rb
19591 views
# -*- coding: binary -*-12require 'rex/post/smb'34class Msf::Sessions::SMB5#6# This interface supports basic interaction.7#8include Msf::Session::Basic9include Msf::Sessions::Scriptable1011# @return [Rex::Post::SMB::Ui::Console] The interactive console12attr_accessor :console13# @return [RubySMB::Client] The SMB client14attr_accessor :client15# @return [Rex::Proto::SMB::SimpleClient]16attr_accessor :simple_client17attr_accessor :platform, :arch18attr_reader :framework1920# @param[Rex::IO::Stream] rstream21# @param [Hash] opts22# @option opts [RubySMB::Client] :client23def initialize(rstream, opts = {})24@client = opts.fetch(:client)25@simple_client = ::Rex::Proto::SMB::SimpleClient.new(client.dispatcher.tcp_socket, client: client, msf_session: self)26self.console = Rex::Post::SMB::Ui::Console.new(self)27super(rstream, opts)28end2930def bootstrap(datastore = {}, handler = nil)31session = self32session.init_ui(user_input, user_output)3334@info = "SMB #{datastore['USERNAME']} @ #{@peer_info}"35end3637def execute_file(full_path, args)38if File.extname(full_path) == '.rb'39Rex::Script::Shell.new(self, full_path).run(args)40else41console.load_resource(full_path)42end43end4445def process_autoruns(datastore)46['InitialAutoRunScript', 'AutoRunScript'].each do |key|47next if datastore[key].nil? || datastore[key].empty?4849args = Shellwords.shellwords(datastore[key])50print_status("Session ID #{sid} (#{tunnel_to_s}) processing #{key} '#{datastore[key]}'")51execute_script(args.shift, *args)52end53end5455def verify_connectivity56@client.dispatcher.tcp_socket.peerinfo57rescue Errno::ENOTCONN58self.kill59raise60end6162def type63self.class.type64end6566# Returns the type of session.67#68def self.type69'smb'70end7172def self.can_cleanup_files73false74end7576#77# Returns the session description.78#79def desc80'SMB'81end8283def address84@address ||= simple_client.peerhost85end8687def port88@port ||= simple_client.peerport89end9091##92# :category: Msf::Session::Interactive implementors93#94# Initializes the console's I/O handles.95#96def init_ui(input, output)97self.user_input = input98self.user_output = output99console.init_ui(input, output)100console.set_log_source(log_source)101102super103end104105##106# :category: Msf::Session::Interactive implementors107#108# Resets the console's I/O handles.109#110def reset_ui111console.unset_log_source112console.reset_ui113end114115def exit116console.stop117end118119##120# :category: Msf::Session::Interactive implementors121#122# Override the basic session interaction to use shell_read and123# shell_write instead of operating on rstream directly.124def _interact125framework.events.on_session_interact(self)126framework.history_manager.with_context(name: type.to_sym) do127_interact_stream128end129end130131##132# :category: Msf::Session::Interactive implementors133#134def _interact_stream135framework.events.on_session_interact(self)136137console.framework = framework138# Call the console interaction of the smb client and139# pass it a block that returns whether or not we should still be140# interacting. This will allow the shell to abort if interaction is141# canceled.142console.interact { interacting != true }143console.framework = nil144145# If the stop flag has been set, then that means the user exited. Raise146# the EOFError so we can drop this handle like a bad habit.147raise EOFError if (console.stopped? == true)148end149end150151152