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

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

Overview

The TCP Proxy allows for inspecting and manipulating TCP protocols.

Example

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

Ronin::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::Dumper.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

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(options = {}) ⇒ Proxy

Creates a new TCP Proxy.

See Also:

Since:

  • 0.5.0


124
125
126
127
128
129
130
131
132
133
# File 'lib/ronin/network/tcp/proxy.rb', line 124

def initialize(options={})
  super(options) do |proxy|
    @callbacks[:client_connect]    = []
    @callbacks[:client_disconnect] = []
    @callbacks[:server_connect]    = []
    @callbacks[:server_disconnect] = []

    yield proxy if block_given?
  end
end

Instance Method Details

#client_connect(client_connection) ⇒ Object (protected)

Triggers the client_connect event.

Parameters:

  • client_connection (connection)

    The new connection from a client to the proxy.

Since:

  • 0.5.0


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

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 (connection)

    The connection from a client to the proxy.

  • server_connection (connection)

    The connection from the proxy to the server.

Since:

  • 0.5.0


379
380
381
382
383
# File 'lib/ronin/network/tcp/proxy.rb', line 379

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


337
338
339
# File 'lib/ronin/network/tcp/proxy.rb', line 337

def close_client_connection(socket)
  socket.close
end

#close_proxyObject (protected)

Closes the TCP proxy.

Since:

  • 0.5.0


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

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


347
348
349
# File 'lib/ronin/network/tcp/proxy.rb', line 347

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


239
240
241
# File 'lib/ronin/network/tcp/proxy.rb', line 239

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


263
264
265
# File 'lib/ronin/network/tcp/proxy.rb', line 263

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


289
290
291
# File 'lib/ronin/network/tcp/proxy.rb', line 289

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


315
316
317
# File 'lib/ronin/network/tcp/proxy.rb', line 315

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

#openObject

Opens the proxy.

Since:

  • 0.5.0


140
141
142
# File 'lib/ronin/network/tcp/proxy.rb', line 140

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


327
328
329
# File 'lib/ronin/network/tcp/proxy.rb', line 327

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

#pollObject

Polls the connections for data.

Since:

  • 0.5.0


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

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)
    client_connect(@socket.accept)
  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


219
220
221
# File 'lib/ronin/network/tcp/proxy.rb', line 219

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


204
205
206
# File 'lib/ronin/network/tcp/proxy.rb', line 204

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

#server_connect(client_connection) ⇒ Object (protected)

Triggers the server_connect event.

Parameters:

  • client_connection (connection)

    The connection from a client to the proxy.

Since:

  • 0.5.0


391
392
393
394
395
396
397
# File 'lib/ronin/network/tcp/proxy.rb', line 391

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 (connection)

    The connection from a client to the proxy.

  • server_connection (connection)

    The connection from the proxy to the server.

Since:

  • 0.5.0


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

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