Class: Rex::Post::Meterpreter::Extensions::Stdapi::Net::SocketSubsystem::TcpServerChannel

Inherits:
Channel
  • Object
show all
Includes:
IO::StreamServer
Defined in:
lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb

Constant Summary collapse

@@server_channels =

This is a class variable to store all pending client tcp connections which have not been passed off via a call to the respective server tcp channels accept method. The dictionary key is the tcp server channel instance and the values held are an array of pending tcp client channels connected to the tcp server channel.

{}

Instance Attribute Summary

Attributes inherited from Channel

#cid, #client, #cls, #flags, #params, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Channel

_close, #_close, #_read, #_write, #cleanup, #close, #close_read, #close_write, #closed?, create, #dio_close_handler, #dio_handler, #dio_map, #dio_read_handler, #dio_write_handler, finalize, #flag?, #interactive, #read, #synchronous?, #write

Methods included from InboundPacketHandler

#request_handler, #response_handler

Constructor Details

#initialize(client, cid, type, flags, packet, sock_params: nil) ⇒ TcpServerChannel

Simply initialize this instance.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb', line 84

def initialize(client, cid, type, flags, packet, sock_params: nil)
  super(client, cid, type, flags, packet)

  # add this instance to the class variables dictionary of tcp server channels
  @@server_channels[self] ||= ::Queue.new

  unless sock_params.nil?
    @params = sock_params.merge(Socket.parameters_from_response(packet))
    if sock_params.ssl
      extend(Rex::Socket::SslTcpServer)
      initsock(sock_params)
    end
  end
end

Class Method Details

.clsObject



63
64
65
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb', line 63

def self.cls
  CHANNEL_CLASS_STREAM
end

.open(client, params) ⇒ Channel

Open a new tcp server channel on the remote end.

Returns:



71
72
73
74
75
76
77
78
79
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb', line 71

def self.open(client, params)
  Channel.create(client, 'stdapi_net_tcp_server', self, CHANNEL_FLAG_SYNCHRONOUS,
    [
      {'type'  => TLV_TYPE_LOCAL_HOST, 'value' => params.localhost},
      {'type'  => TLV_TYPE_LOCAL_PORT, 'value' => params.localport}
    ],
    sock_params: params
  )
end

.request_handler(client, packet) ⇒ Object

This is the request handler which is registered to the respective meterpreter instance via Rex::Post::Meterpreter::Extensions::Stdapi::Net::Socket. All incoming requests from the meterpreter for a COMMAND_ID_STDAPI_NET_TCP_CHANNEL_OPEN will be processed here. We create a new TcpClientChannel for each request received and store it in the respective tcp server channels list of new pending client channels. These new tcp client channels are passed off via a call the tcp server channels accept() method.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb', line 35

def self.request_handler(client, packet)
  return false unless packet.method == COMMAND_ID_STDAPI_NET_TCP_CHANNEL_OPEN

  cid = packet.get_tlv_value(TLV_TYPE_CHANNEL_ID)
  pid = packet.get_tlv_value(TLV_TYPE_CHANNEL_PARENTID)

  return false if cid.nil? || pid.nil?

  server_channel = client.find_channel(pid)

  return false if server_channel.nil?

  params = Rex::Socket::Parameters.from_hash(
    {
      'Proto'     => 'tcp',
      'Comm'      => server_channel.client
    }
  )

  client_channel = TcpClientChannel.new(client, cid, TcpClientChannel, CHANNEL_FLAG_SYNCHRONOUS, packet, sock_params: params)
  ilog("enqueueing new TCP client with channel id #{cid}")

  @@server_channels[server_channel] ||= ::Queue.new
  @@server_channels[server_channel].enq(client_channel)

  true
end

Instance Method Details

#_accept(nonblock: false, timeout: nil) ⇒ Object (protected)

Uses Queue#deq(timeout:) instead of ::Timeout.timeout to avoid GVL contention causing missed connections in Ruby 3.x



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb', line 124

def _accept(nonblock: false, timeout: nil)
  result = nil

  begin
    if nonblock
      channel = @@server_channels[self].deq(true)
    elsif timeout
      channel = @@server_channels[self].deq(timeout: timeout)
    else
      channel = @@server_channels[self].deq
    end

    if channel
      result = channel.lsock
    end

    if result != nil && !result.kind_of?(Rex::Socket::Tcp)
      result.extend(Rex::Socket::Tcp)
    end
  rescue ThreadError
    # No clients in queue (nonblock mode)
  end

  result
end

#accept(opts = {}) ⇒ Object

Accept a new tcp client connection form this tcp server channel. This method will block indefinitely if no timeout is specified.



111
112
113
114
115
116
117
118
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb', line 111

def accept(opts = {})
  timeout = opts['Timeout']
  if (timeout.nil? || timeout <= 0)
    timeout = nil
  end

  _accept(timeout: timeout)
end

#accept_nonblockObject

Accept a new tcp client connection form this tcp server channel. This method does not block and returns nil if no new client connection is available.



103
104
105
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb', line 103

def accept_nonblock
  _accept(nonblock: true)
end