Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/smb/ui/console.rb
Views: 11705
# -*- coding: binary -*-12require 'English'3require 'rex/post/session_compatible_modules'45module Rex6module Post7module SMB8module Ui9###10#11# This class provides a shell driven interface to the RubySMB client API.12#13###14class Console1516include Rex::Ui::Text::DispatcherShell17include Rex::Post::SessionCompatibleModules1819# Dispatchers20require 'rex/post/smb/ui/console/command_dispatcher'21require 'rex/post/smb/ui/console/command_dispatcher/core'22require 'rex/post/smb/ui/console/command_dispatcher/shares'2324#25# Initialize the SMB console.26#27# @param [Msf::Sessions::SMB] session28def initialize(session)29if Rex::Compat.is_windows30super('smb')31else32super('%undSMB%clr', '>', Msf::Config.smb_session_history, nil, :smb)33end3435# The ruby smb client context36self.session = session37self.client = session.client38self.simple_client = session.simple_client3940# Queued commands array41self.commands = []4243# Point the input/output handles elsewhere44reset_ui4546enstack_dispatcher(Rex::Post::SMB::Ui::Console::CommandDispatcher::Core)47enstack_dispatcher(Rex::Post::SMB::Ui::Console::CommandDispatcher::Shares)48enstack_dispatcher(Msf::Ui::Console::CommandDispatcher::LocalFileSystem)4950# Set up logging to whatever logsink 'core' is using51if !$dispatcher['smb']52$dispatcher['smb'] = $dispatcher['core']53end54end5556#57# Called when someone wants to interact with the smb client. It's58# assumed that init_ui has been called prior.59#60def interact(&block)61# Run queued commands62commands.delete_if do |ent|63run_single(ent)64true65end6667# Run the interactive loop68run do |line|69# Run the command70run_single(line)7172# If a block was supplied, call it, otherwise return false73if block74block.call75else76false77end78end79end8081#82# Queues a command to be run when the interactive loop is entered.83#84def queue_cmd(cmd)85commands << cmd86end8788#89# Runs the specified command wrapper in something to catch meterpreter90# exceptions.91#92def run_command(dispatcher, method, arguments)93super94rescue Timeout::Error95log_error('Operation timed out.')96rescue Rex::InvalidDestination => e97log_error(e.message)98rescue ::Errno::EPIPE, ::OpenSSL::SSL::SSLError, ::IOError99session.kill100rescue ::RubySMB::Error::CommunicationError => e101log_error("Error running command #{method}: #{e.class} #{e}")102elog(e)103session.alive = false104rescue ::StandardError => e105log_error("Error running command #{method}: #{e.class} #{e}")106elog(e)107end108109# @param [Hash] opts110# @return [String]111def help_to_s(opts = {})112super + format_session_compatible_modules113end114115#116# Logs that an error occurred and persists the callstack.117#118def log_error(msg)119print_error(msg)120121elog(msg, 'smb')122123dlog("Call stack:\n#{$ERROR_POSITION.join("\n")}", 'smb')124end125126# @return [Msf::Sessions::SMB]127attr_reader :session128129# @return [RubySMB::Client]130attr_reader :client # :nodoc:131132# @return [Rex::Proto::SMB::SimpleClient]133attr_reader :simple_client134135# @return [RubySMB::SMB2::Tree]136attr_accessor :active_share137138# @return [String]139attr_accessor :cwd140141def format_prompt(val)142if active_share143share_name = active_share.share[/[^\\].*$/, 0]144cwd = self.cwd.blank? ? '' : "\\#{Rex::Ntpath.as_ntpath(self.cwd)}"145prompt = "#{share_name}#{cwd}"146else147prompt = session.address.to_s148end149150substitute_colors("%undSMB%clr (#{prompt}) > ", true)151end152153protected154155attr_writer :session, :client, :simple_client # :nodoc: # :nodoc:156attr_accessor :commands # :nodoc:157end158end159end160end161end162163164