Class: Msf::Sessions::SshCommandShellBind::TcpClientChannel

Inherits:
Object
  • Object
show all
Includes:
Rex::Post::Channel::StreamAbstraction
Defined in:
lib/msf/base/sessions/ssh_command_shell_bind.rb

Overview

This is a Metasploit Framework channel object that wraps a Net::SSH native channel object.

Defined Under Namespace

Modules: SocketInterface

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Rex::Post::Channel::StreamAbstraction

#read

Constructor Details

#initialize(client, cid, ssh_channel, params) ⇒ TcpClientChannel

Create a new TcpClientChannel instance.

Parameters:

  • client (SshCommandShellBind)

    The command shell session that this channel instance belongs to.

  • cid (Integer)

    The channel ID.

  • ssh_channel (Net::SSH::Connection::Channel)

    The connected SSH channel.

  • params (Rex::Socket::Parameters)

    The parameters that were used to open the channel.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/msf/base/sessions/ssh_command_shell_bind.rb', line 55

def initialize(client, cid, ssh_channel, params)
  initialize_abstraction

  @client = client
  @cid = cid
  @ssh_channel = ssh_channel
  @params = params
  @mutex = Mutex.new

  ssh_channel.on_close do |_ch|
    dlog('ssh_channel#on_close closing the sock')
    close
  end

  ssh_channel.on_data do |_ch, data|
    # dlog("ssh_channel#on_data received #{data.length} bytes")
    rsock.syswrite(data)
  end

  ssh_channel.on_eof do |_ch|
    dlog('ssh_channel#on_eof shutting down the socket')
    rsock.shutdown(Socket::SHUT_WR)
  end

  lsock.extend(SocketInterface)
  lsock.channel = self

  rsock.extend(SocketInterface)
  rsock.channel = self

  lsock.extend(Rex::Socket::SslTcp) if params.ssl && !params.server

  # synchronize access so the socket isn't closed while initializing, this is particularly important for SSL
  lsock.synchronize_access { lsock.initsock(params) }
  rsock.synchronize_access { rsock.initsock(params) }

  client.add_channel(self)
end

Instance Attribute Details

#cidObject (readonly)

Returns the value of attribute cid.



138
139
140
# File 'lib/msf/base/sessions/ssh_command_shell_bind.rb', line 138

def cid
  @cid
end

#clientObject (readonly)

Returns the value of attribute client.



138
139
140
# File 'lib/msf/base/sessions/ssh_command_shell_bind.rb', line 138

def client
  @client
end

#paramsObject (readonly)

Returns the value of attribute params.



138
139
140
# File 'lib/msf/base/sessions/ssh_command_shell_bind.rb', line 138

def params
  @params
end

Instance Method Details

#closeObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/msf/base/sessions/ssh_command_shell_bind.rb', line 98

def close
  cid = @cid
  @mutex.synchronize do
    return if closed?

    @cid = nil
  end

  @client.remove_channel(cid)
  cleanup_abstraction
  @ssh_channel.close
end

#close_writeObject



111
112
113
114
115
116
117
# File 'lib/msf/base/sessions/ssh_command_shell_bind.rb', line 111

def close_write
  if closed?
    raise IOError, 'Channel has been closed.', caller
  end

  @ssh_channel.eof!
end

#closed?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/msf/base/sessions/ssh_command_shell_bind.rb', line 94

def closed?
  @cid.nil?
end

#write(buf, length = nil) ⇒ Object

Write buf to the channel, optionally truncating it to length bytes.

Parameters:

  • buf (String)

    The data to write to the channel.

  • length (Integer) (defaults to: nil)

    An optional length to truncate data to before sending it.



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/msf/base/sessions/ssh_command_shell_bind.rb', line 125

def write(buf, length = nil)
  if closed?
    raise IOError, 'Channel has been closed.', caller
  end

  if !length.nil? && buf.length >= length
    buf = buf[0..length]
  end

  @ssh_channel.send_data(buf)
  buf.length
end