Class: Rex::Proto::Ssh::Connection

Inherits:
HrrRbSsh::Connection show all
Includes:
AccessControlList
Defined in:
lib/rex/proto/ssh/connection.rb

Overview

Encapsulation of Connection constructor for Rex use Provides ACLs for port forwarding and client (io) access hooks

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AccessControlList

#deny=, #permit=, #permit?

Constructor Details

#initialize(io = nil, options = self.default_options, context = {}) ⇒ Rex::Proto::Ssh::Connection

Create new Connection from an IO and options set, pull trans and auth from options if present, create from options set otherwise.

Creates a default empty handler set for channel requests.

Parameters:

  • io (IO) (defaults to: nil)

    Socket, FD, or abstraction on which to build Connection

  • options (Hash) (defaults to: self.default_options)

    Options for constructing Connection components



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rex/proto/ssh/connection.rb', line 82

def initialize(io = nil, options = self.default_options, context = {})
  @context = context
  @logger = Logger.new self.class.name
  @server = options.delete(:ssh_server)
  @mode = options.delete(:ssh_mode) || HrrRbSsh::Mode::SERVER
  # Take a pre-built transport from the options or build one on the fly
  @transport = options.delete(:ssh_transport) || HrrRbSsh::Transport.new(
    io,
    @mode,
    options
  )
  # Take a pre-built authentication from the options or build one on the fly
  @authentication = options.delete(:ssh_authentication) ||
    HrrRbSsh::Authentication.new(@transport, @mode, options)
  @global_request_handler = GlobalRequestHandler.new(self)
  # Retain remaining options for later use
  @options = options

  @channels = Hash.new
  @username = nil
  @closed = nil
end

Instance Attribute Details

#authenticationObject

Returns the value of attribute authentication.



149
150
151
# File 'lib/rex/proto/ssh/connection.rb', line 149

def authentication
  @authentication
end

#channelsObject

Returns the value of attribute channels.



149
150
151
# File 'lib/rex/proto/ssh/connection.rb', line 149

def channels
  @channels
end

#contextObject (readonly)

Returns the value of attribute context.



150
151
152
# File 'lib/rex/proto/ssh/connection.rb', line 150

def context
  @context
end

#global_request_handlerObject

Returns the value of attribute global_request_handler.



149
150
151
# File 'lib/rex/proto/ssh/connection.rb', line 149

def global_request_handler
  @global_request_handler
end

#serverObject (readonly)

Returns the value of attribute server.



150
151
152
# File 'lib/rex/proto/ssh/connection.rb', line 150

def server
  @server
end

#transportObject

Returns the value of attribute transport.



149
150
151
# File 'lib/rex/proto/ssh/connection.rb', line 149

def transport
  @transport
end

Class Method Details

.default_optionsObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/rex/proto/ssh/connection.rb', line 62

def self.default_options
  noneauth = HrrRbSsh::Authentication::Authenticator.new { |context| true }
  return {
    'authentication_none_authenticator' => noneauth,
    'authentication_password_authenticator' => noneauth,
    'authentication_publickey_authenticator' => noneauth,
    'authentication_keyboard_interactive_authenticator' => noneauth,
    'local_version' => 'SSH-2.0-RexProtoSsh'
  }
end

Instance Method Details

#closeObject

Close the connection and underlying socket



144
145
146
147
# File 'lib/rex/proto/ssh/connection.rb', line 144

def close
  super
  @transport.io.close if @transport and !@transport.io.closed?
end

#open_channel_keys(ctype = 'session') ⇒ Array

Provide keys of explicitly not closed channels

Parameters:

  • ctype (String) (defaults to: 'session')

    Channel type to select, nil for all

Returns:

  • (Array)

    Array of integers indexing open channels



111
112
113
114
115
116
117
# File 'lib/rex/proto/ssh/connection.rb', line 111

def open_channel_keys(ctype = 'session')
  channels.keys.sort.select do |cn|
    channels[cn].closed? === false and (
      ctype.nil? or channels[cn].channel_type == ctype
    )
  end
end

#reader(fd = 0, cn = open_channel_keys.first) ⇒ IO

Provide IO from which to read remote-end inputs

Parameters:

  • fd (Integer) (defaults to: 0)

    Desired descriptor from which to read

  • cn (Integer) (defaults to: open_channel_keys.first)

    Desired channel from which to take fd

Returns:

  • (IO)

    File descriptor for reading



126
127
128
# File 'lib/rex/proto/ssh/connection.rb', line 126

def reader(fd = 0, cn = open_channel_keys.first)
  channels[cn].io[fd]
end

#writer(fd = 1, cn = open_channel_keys.first) ⇒ IO

Provide IO into which writes to the remote end can be sent

Parameters:

  • fd (Integer) (defaults to: 1)

    Desired descriptor to which to write

  • cn (Integer) (defaults to: open_channel_keys.first)

    Desired channel from which to take fd

Returns:

  • (IO)

    File descriptor for writing



137
138
139
# File 'lib/rex/proto/ssh/connection.rb', line 137

def writer(fd = 1, cn = open_channel_keys.first)
  channels[cn].io[fd]
end