Class: Ronin::Support::Network::TCP::Proxy

Inherits:
Proxy
  • Object
show all
Defined in:
lib/ronin/support/network/tcp/proxy.rb

Overview

The TCP Proxy allows for inspecting and manipulating TCP protocols.

Example

require 'ronin/support/network/tcp/proxy'
require 'hexdump'

Ronin::Support::Network::TCP::Proxy.start(port: 1337, server: ['www.wired.com', 80]) do |proxy|
  address = lambda { |socket|
    addrinfo = socket.peeraddr

   "#{addrinfo[3]}:#{addrinfo[1]}"
  }
  hex = Hexdump::Hexdump.new

  proxy.on_client_data do |client,server,data|
    puts "#{address[client]} -> #{proxy}"
    hex.dump(data)
  end

  proxy.on_client_connect do |client|
    puts "#{address[client]} -> #{proxy} [connected]"
  end

  proxy.on_client_disconnect do |client,server|
    puts "#{address[client]} <- #{proxy} [disconnected]"
  end

  proxy.on_server_data do |client,server,data|
    puts "#{address[client]} <- #{proxy}"
    hex.dump(data)
  end

  proxy.on_server_connect do |client,server|
    puts "#{address[client]} <- #{proxy} [connected]"
  end

  proxy.on_server_disconnect do |client,server|
    puts "#{address[client]} <- #{proxy} [disconnected]"
  end
end

Callbacks

In addition to the events supported by the Proxy base class, the TCP Proxy also supports the following callbacks.

client_connect

When a client connects to the proxy:

on_client_connect do |client|
  puts "[connected] #{client.remote_address.ip_address}:#{client.remote_addre
end

client_disconnect

When a client disconnects from the proxy:

on_client_disconnect do |client,server|
  puts "[disconnected] #{client.remote_address.ip_address}:#{client.remote_ad
end

server_connect

When the server accepts a connection from the proxy:

on_server_connect do |client,server|
  puts "[connected] #{proxy}"
end

server_disconnect

When the server closes a connection from the proxy.

on_server_disconnect do |client,server|
  puts "[disconnected] #{proxy}"
end

connect

Alias for #on_server_connect.

disconnect

Alias for #on_client_disconnect.

Since:

  • 0.5.0

Direct Known Subclasses

SSL::Proxy

Constant Summary

Constants inherited from Proxy

Proxy::DEFAULT_BUFFER_SIZE, Proxy::DEFAULT_HOST

Instance Attribute Summary

Attributes inherited from Proxy

#buffer_size, #connections, #host, #port, #server_host, #server_port

Instance Method Summary collapse

Methods inherited from Proxy

#callback, #client_connection_for, #client_connections, #client_data, #close, #close!, #close_connection, #close_connections, #ignore!, #inspect, #listen, #on_client_data, #on_data, #on_server_data, #reset!, #reset_connection, #server_connection_for, #server_connections, #server_data, start, #start, #stop, #stop!, #to_s

Constructor Details

#initialize(**kwargs) ⇒ Proxy

Creates a new TCP Proxy.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for Proxy#initialize.

See Also:

Since:

  • 0.5.0



127
128
129
130
131
132
133
134
135
136
# File 'lib/ronin/support/network/tcp/proxy.rb', line 127

def initialize(**kwargs)
  super(**kwargs) do |proxy|
    @callbacks[:client_connect]    = []
    @callbacks[:client_disconnect] = []
    @callbacks[:server_connect]    = []
    @callbacks[:server_disconnect] = []

    yield proxy if block_given?
  end
end

Instance Method Details

#accept_client_connectionTCPSocket (protected)

Accepts a new client connection.

Returns:

  • (TCPSocket)

    A new connection.

Since:

  • 0.6.0



334
335
336
# File 'lib/ronin/support/network/tcp/proxy.rb', line 334

def accept_client_connection
  @socket.accept
end

#client_connect(client_connection) ⇒ Object (protected)

Triggers the client_connect event.

Parameters:

  • client_connection (TCPSocket)

    The new connection from a client to the proxy.

Since:

  • 0.5.0



381
382
383
384
385
# File 'lib/ronin/support/network/tcp/proxy.rb', line 381

def client_connect(client_connection)
  callback(:client_connect,client_connection) do
    server_connect(client_connection)
  end
end

#client_disconnect(client_connection, server_connection) ⇒ Object (protected)

Triggers the client_disconnect event.

Parameters:

  • client_connection (TCPSocket)

    The connection from a client to the proxy.

  • server_connection (TCPSocket)

    The connection from the proxy to the server.

Since:

  • 0.5.0



396
397
398
399
400
# File 'lib/ronin/support/network/tcp/proxy.rb', line 396

def client_disconnect(client_connection,server_connection)
  callback(:client_disconnect,client_connection,server_connection) do
    close_connection(client_connection,server_connection)
  end
end

#close_client_connection(socket) ⇒ Object (protected)

Closes a connection from the client.

Parameters:

  • socket (TCPSocket)

    The connection from the client.

Since:

  • 0.5.0



354
355
356
# File 'lib/ronin/support/network/tcp/proxy.rb', line 354

def close_client_connection(socket)
  socket.close
end

#close_proxyObject (protected)

Closes the TCP proxy.

Since:

  • 0.5.0



371
372
373
# File 'lib/ronin/support/network/tcp/proxy.rb', line 371

def close_proxy
  @socket.close
end

#close_server_connection(socket) ⇒ Object (protected)

Closes a connection to the server.

Parameters:

  • socket (TCPSocket)

    The connection to the server.

Since:

  • 0.5.0



364
365
366
# File 'lib/ronin/support/network/tcp/proxy.rb', line 364

def close_server_connection(socket)
  socket.close
end

#on_client_connect {|client| ... } ⇒ Object

Registers a callback for when a client connects.

Examples:

on_client_connect do |client|
  puts "[connected] #{client.remote_address.ip_address}:#{client.remote_address.ip_port}"
end

Yields:

  • (client)

    The block will be passed each newly connected client.

Yield Parameters:

  • client (TCPSocket)

    The connection from the client to the proxy.

Since:

  • 0.5.0



244
245
246
# File 'lib/ronin/support/network/tcp/proxy.rb', line 244

def on_client_connect(&block)
  @callbacks[:client_connect] << block
end

#on_client_disconnect {|client, server| ... } ⇒ Object Also known as: on_disconnect

Registers a callback for when a client disconnects.

Examples:

on_client_disconnect do |client,server|
  puts "[disconnected] #{client.remote_address.ip_address}:#{client.remote_address.ip_port}"
end

Yields:

  • (client, server)

    The block will be passed each disconnected client and their connection to the server.

Yield Parameters:

  • client (TCPSocket)

    The connection from the client to the proxy.

  • server (TCPSocket)

    The connection from the proxy to the server.

Since:

  • 0.5.0



268
269
270
# File 'lib/ronin/support/network/tcp/proxy.rb', line 268

def on_client_disconnect(&block)
  @callbacks[:client_disconnect] << block
end

#on_server_connect {|client, server| ... } ⇒ Object Also known as: on_connect

Registers a callback for when the server accepts a connection.

Examples:

on_server_connect do |client,server|
  puts "[connected] #{proxy}"
end

Yields:

  • (client, server)

    The block will be passed each connected client and their newly formed connection to the server.

Yield Parameters:

  • client (TCPSocket)

    The connection from the client to the proxy.

  • server (TCPSocket)

    The connection from the proxy to the server.

Since:

  • 0.5.0



294
295
296
# File 'lib/ronin/support/network/tcp/proxy.rb', line 294

def on_server_connect(&block)
  @callbacks[:server_connect] << block
end

#on_server_disconnect {|client, server| ... } ⇒ Object

Registers a callback for when the server closes a connection.

Examples:

on_server_disconnect do |client,server|
  puts "[disconnected] #{proxy}"
end

Yields:

  • (client, server)

    The block will be passed the each client connection and the recently disconnected server connection.

Yield Parameters:

  • client (TCPSocket)

    The connection from the client to the proxy.

  • server (TCPSocket)

    The connection from the proxy to the server.

Since:

  • 0.5.0



320
321
322
# File 'lib/ronin/support/network/tcp/proxy.rb', line 320

def on_server_disconnect(&block)
  @callbacks[:server_disconnect] << block
end

#openObject

Opens the proxy.

Since:

  • 0.5.0



143
144
145
# File 'lib/ronin/support/network/tcp/proxy.rb', line 143

def open
  @socket = TCPServer.new(@host,@port)
end

#open_server_connectionTCPSocket (protected)

Creates a new connection to the server.

Returns:

  • (TCPSocket)

    A new connection.

Since:

  • 0.5.0



344
345
346
# File 'lib/ronin/support/network/tcp/proxy.rb', line 344

def open_server_connection
  TCPSocket.new(@server_host,@server_port)
end

#pollObject

Polls the connections for data.

Since:

  • 0.5.0



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ronin/support/network/tcp/proxy.rb', line 152

def poll
  sockets = [@socket] + client_connections + server_connections

  readable, _writtable, errors = IO.select(sockets,nil,sockets)

  (errors & client_connections).each do |client_socket|
    server_socket = server_connection_for(client_socket)

    client_disconnect(client_socket,server_socket)
  end

  (errors & server_connections).each do |server_socket|
    client_socket = client_connection_for(server_socket)

    server_disconnect(client_socket,server_socket)
  end

  (readable & client_connections).each do |client_socket|
    server_socket = server_connection_for(client_socket)
    data          = recv(client_socket)

    unless data.empty?
      client_data(client_socket,server_socket,data)
    else
      client_disconnect(client_socket,server_socket)
    end
  end

  (readable & server_connections).each do |server_socket|
    client_socket = client_connection_for(server_socket)
    data          = recv(server_socket)

    unless data.empty?
      server_data(client_socket,server_socket,data)
    else
      server_disconnect(client_socket,server_socket)
    end
  end

  if readable.include?(@socket)
    if (client_socket = accept_client_connection)
      client_connect(client_socket)
    end
  end
end

#recv(connection) ⇒ String?

Receives data from a connection.

Parameters:

  • connection (TCPSocket)

    The TCP connection to receive data from.

Returns:

  • (String, nil)

    The received data.

Since:

  • 0.5.0



224
225
226
# File 'lib/ronin/support/network/tcp/proxy.rb', line 224

def recv(connection)
  connection.recv(@buffer_size)
end

#send(connection, data) ⇒ Object

Sends data to a connection.

Parameters:

  • connection (TCPSocket)

    A TCP connection to write data to.

  • data (String)

    The data to write.

Since:

  • 0.5.0



209
210
211
# File 'lib/ronin/support/network/tcp/proxy.rb', line 209

def send(connection,data)
  connection.send(data,0)
end

#server_connect(client_connection) ⇒ Object (protected)

Triggers the server_connect event.

Parameters:

  • client_connection (TCPSocket)

    The connection from a client to the proxy.

Since:

  • 0.5.0



408
409
410
411
412
413
414
# File 'lib/ronin/support/network/tcp/proxy.rb', line 408

def server_connect(client_connection)
  server_connection = open_server_connection

  callback(:server_connect,client_connection,server_connection) do
    @connections[client_connection] = server_connection
  end
end

#server_disconnect(client_connection, server_connection) ⇒ Object (protected)

Triggers the server_disconnect event.

Parameters:

  • client_connection (TCPSocket)

    The connection from a client to the proxy.

  • server_connection (TCPSocket)

    The connection from the proxy to the server.

Since:

  • 0.5.0



425
426
427
428
429
# File 'lib/ronin/support/network/tcp/proxy.rb', line 425

def server_disconnect(client_connection,server_connection)
  callback(:server_disconnect,client_connection,server_connection) do
    close_connection(client_connection)
  end
end