Class: Rex::Post::Meterpreter::Extensions::Stdapi::Net::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb

Overview

This class provides an interface to interacting with sockets on the remote machine. It allows callers to open TCP, UDP, and other arbitrary socket-based connections as channels that can then be interacted with through the established meterpreter connection.

Constant Summary collapse

TLV_PARAM_MAP =
{
  TLV_TYPE_CONNECT_RETRIES => 'Retries',
  TLV_TYPE_LOCAL_HOST      => 'LocalHost',
  TLV_TYPE_LOCAL_PORT      => 'LocalPort',
  TLV_TYPE_PEER_HOST       => 'PeerHost',
  TLV_TYPE_PEER_PORT       => 'PeerPort'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Socket

Initialize the socket subsystem and start monitoring sockets as they come in.



45
46
47
48
49
50
51
52
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb', line 45

def initialize(client)
  self.client = client

  # register the inbound handler for the tcp server channel (allowing us to
  # receive new client connections to a tcp server channel)
  client.register_inbound_handler(Rex::Post::Meterpreter::Extensions::Stdapi::Net::SocketSubsystem::TcpServerChannel)

end

Instance Attribute Details

#clientObject (protected)

:nodoc:



162
163
164
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb', line 162

def client
  @client
end

Class Method Details

.parameters_from_response(response) ⇒ Object

Process a response packet and extract TLVs that are relevant for updating socket parameters.



65
66
67
68
69
70
71
72
73
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb', line 65

def self.parameters_from_response(response)
  params = {}
  TLV_PARAM_MAP.each do |tlv_type, param_key|
    value = response.get_tlv_value(tlv_type)
    next if value.nil?
    params[param_key] = value
  end
  Rex::Socket::Parameters.from_hash(params)
end

Instance Method Details

#create(params) ⇒ Object

Creates an arbitrary client socket channel using the information supplied in the socket parameters instance. The params argument is expected to be of type Rex::Socket::Parameters.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb', line 86

def create(params)
  res = nil

  if params.tcp?
    if params.server?
      res = create_tcp_server_channel(params)
    else
      res = create_tcp_client_channel(params)
    end
  elsif params.udp?
    res = create_udp_channel(params)
  end

  return res
end

#create_tcp_client_channel(params) ⇒ Object

Creates a TCP client channel.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb', line 122

def create_tcp_client_channel(params)
  begin
    channel = SocketSubsystem::TcpClientChannel.open(client, params)
    if channel != nil
      return channel.lsock
    end
    return nil
  rescue ::Rex::Post::Meterpreter::RequestError => e
    case e.code
    when 10000 .. 10100
      raise ::Rex::ConnectionError.new
    end
    raise e
  end
end

#create_tcp_server_channel(params) ⇒ Object

Create a TCP server channel.



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb', line 105

def create_tcp_server_channel(params)
  begin
    return SocketSubsystem::TcpServerChannel.open(client, params)
  rescue ::Rex::Post::Meterpreter::RequestError => e
    case e.code
    when 10048
      raise ::Rex::AddressInUse.new(params.localhost, params.localport)
    when 10000 .. 10100
      raise ::Rex::ConnectionError.new
    end
    raise e
  end
end

#create_udp_channel(params) ⇒ Object

Creates a UDP channel.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb', line 141

def create_udp_channel(params)
  begin
    channel = SocketSubsystem::UdpChannel.open(client, params)
    if channel != nil
      return channel.lsock
    end
    return nil
  rescue ::Rex::Post::Meterpreter::RequestError => e
    case e.code
    when 10048
      raise ::Rex::AddressInUse.new(params.localhost, params.localport)
    when 10000 .. 10100
      raise ::Rex::ConnectionError.new
    end
    raise e
  end
end

#shutdownObject

Deregister the inbound handler for the tcp server channel



57
58
59
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb', line 57

def shutdown
  client.deregister_inbound_handler(Rex::Post::Meterpreter::Extensions::Stdapi::Net::SocketSubsystem::TcpServerChannel)
end