Class: Rex::Proto::Proxy::Socks5::ServerClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/proxy/socks5/server_client.rb

Overview

A client connected to the SOCKS5 server.

Constant Summary collapse

AUTH_NONE =
0
AUTH_GSSAPI =
1
AUTH_CREDS =
2
AUTH_NO_ACCEPTABLE_METHODS =
255
AUTH_PROTOCOL_VERSION =
1
AUTH_RESULT_SUCCESS =
0
AUTH_RESULT_FAILURE =
1
COMMAND_CONNECT =
1
COMMAND_BIND =
2
COMMAND_UDP_ASSOCIATE =
3
REPLY_SUCCEEDED =
0
REPLY_GENERAL_FAILURE =
1
REPLY_NOT_ALLOWED =
2
REPLY_NET_UNREACHABLE =
3
REPLY_HOST_UNREACHABLE =
4
REPLY_CONNECTION_REFUSED =
5
REPLY_TTL_EXPIRED =
6
REPLY_CMD_NOT_SUPPORTED =
7
REPLY_ADDRESS_TYPE_NOT_SUPPORTED =
8
HOST =
1
PORT =
2

Instance Method Summary collapse

Constructor Details

#initialize(server, sock, opts = {}) ⇒ ServerClient

Create a new client connected to the server.



104
105
106
107
108
109
110
111
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 104

def initialize(server, sock, opts={})
  @server        = server
  @lsock         = sock
  @opts          = opts
  @rsock         = nil
  @client_thread = nil
  @mutex         = ::Mutex.new
end

Instance Method Details

#handle_authenticationObject



137
138
139
140
141
142
143
144
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 137

def handle_authentication
  request = AuthRequestPacket.read(@lsock.get_once)
  if @opts['ServerUsername'].nil? && @opts['ServerPassword'].nil?
    handle_authentication_none(request)
  else
    handle_authentication_creds(request)
  end
end

#handle_authentication_creds(request) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 146

def handle_authentication_creds(request)
  unless request.supported_methods.include? AUTH_CREDS
    raise "Invalid SOCKS5 request packet received (no supported authentication methods)."
  end
  response = AuthResponsePacket.new
  response.chosen_method = AUTH_CREDS
  @lsock.put(response.to_binary_s)

  version = @lsock.read(1)
  raise "Invalid SOCKS5 authentication packet received." unless version.unpack('C').first == 0x01

  username_length = @lsock.read(1).unpack('C').first
  username        = @lsock.read(username_length)

  password_length = @lsock.read(1).unpack('C').first
  password        = @lsock.read(password_length)

  #  +-----+--------+
  #  | VER | STATUS |
  #  +-----+--------+  VERSION: 0x01
  #  | 1   | 1      |  STATUS:  0x00=SUCCESS, otherwise FAILURE
  #  +-----+--------+
  if username == @opts['ServerUsername'] && password == @opts['ServerPassword']
    raw = [ AUTH_PROTOCOL_VERSION, AUTH_RESULT_SUCCESS ].pack ('CC')
    ilog("SOCKS5: Successfully authenticated")
    @lsock.put(raw)
  else
    raw = [ AUTH_PROTOCOL_VERSION, AUTH_RESULT_FAILURE ].pack ('CC')
    @lsock.put(raw)
    raise "Invalid SOCKS5 credentials provided"
  end
end

#handle_authentication_none(request) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 179

def handle_authentication_none(request)
  unless request.supported_methods.include? AUTH_NONE
    raise "Invalid SOCKS5 request packet received (no supported authentication methods)."
  end
  response = AuthResponsePacket.new
  response.chosen_method = AUTH_NONE
  @lsock.put(response.to_binary_s)
end

#handle_commandObject



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 188

def handle_command
  request = RequestPacket.read(@lsock.get_once)
  response = nil
  case request.command
    when COMMAND_BIND
      response = handle_command_bind(request)
    when COMMAND_CONNECT
      response = handle_command_connect(request)
    when COMMAND_UDP_ASSOCIATE
      response = handle_command_udp_associate(request)
  end
  @lsock.put(response.to_binary_s) unless response.nil?
end

#handle_command_bind(request) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 202

def handle_command_bind(request)
  # create a server socket for this request
  params = {
    'LocalHost' => request.address_type == Address::ADDRESS_TYPE_IPV6 ? '::' : '0.0.0.0',
    'LocalPort' => 0,
  }
  params['Context'] = @server.opts['Context'] if @server.opts.has_key?('Context')
  bsock = Rex::Socket::TcpServer.create(params)

  # send back the bind success to the client
  response              = ResponsePacket.new
  response.command      = REPLY_SUCCEEDED
  response.address      = bsock.getlocalname[HOST]
  response.port         = bsock.getlocalname[PORT]
  @lsock.put(response.to_binary_s)

  # accept a client connection (2 minute timeout as per the socks4a spec)
  begin
    ::Timeout.timeout(120) do
      @rsock = bsock.accept
    end
  rescue ::Timeout::Error
    raise "Timeout reached on accept request."
  end

  # close the listening socket
  bsock.close

  setup_tcp_relay
  response              = ResponsePacket.new
  response.command      = REPLY_SUCCEEDED
  response.address      = @rsock.peerhost
  response.port         = @rsock.peerport
  response
end

#handle_command_connect(request) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 238

def handle_command_connect(request)
  # perform the connection request
  params = {
    'PeerHost' => request.address,
    'PeerPort' => request.port,
  }
  params['Context'] = @server.opts['Context'] if @server.opts.has_key?('Context')
  @rsock = Rex::Socket::Tcp.create(params)

  setup_tcp_relay
  response              = ResponsePacket.new
  response.command      = REPLY_SUCCEEDED
  response.address      = @rsock.getlocalname[HOST].split('-')[-1]
  response.port         = @rsock.getlocalname[PORT]
  response
end

#handle_command_udp_associate(request) ⇒ Object



255
256
257
258
259
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 255

def handle_command_udp_associate(request)
  response              = ResponsePacket.new
  response.command      = REPLY_CMD_NOT_SUPPORTED
  response
end

#setup_tcp_relayObject

Setup the TcpRelay between lsock and rsock.



264
265
266
267
268
269
270
271
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 264

def setup_tcp_relay
  # setup the two way relay for full duplex io
  @lsock.extend(TcpRelay)
  @rsock.extend(TcpRelay)
  # start the socket relays...
  @lsock.relay(self, @rsock)
  @rsock.relay(self, @lsock)
end

#startObject

Start handling the client connection.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 115

def start
  # create a thread to handle this client request so as to not block the socks5 server
  @client_thread = Rex::ThreadFactory.spawn("SOCKS5ProxyClient", false) do
    begin
      @server.add_client(self)
      # get the initial client request packet
      handle_authentication

      # handle the request
      handle_command
    rescue => exception
      # respond with a general failure to the client
      response         = ResponsePacket.new
      response.command = REPLY_GENERAL_FAILURE
      @lsock.put(response.to_binary_s)

      wlog("Client.start - #{$!}")
      self.stop
    end
  end
end

#stopObject

Stop handling the client connection.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 276

def stop
  @mutex.synchronize do
    unless @closed
      begin
        @lsock.close if @lsock
      rescue
      end

      begin
        @rsock.close if @rsock
      rescue
      end

      @client_thread.kill if @client_thread and @client_thread.alive?
      @server.remove_client(self)
      @closed = true
    end
  end
end