class Ronin::Support::Network::TCP::Proxy
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 {Network::Proxy 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
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
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
Public Class Methods
Source
# 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
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Network::Proxy#initialize}.
@see Network::Proxy#initialize
Ronin::Support::Network::Proxy::new
Public Instance Methods
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 244 def on_client_connect(&block) @callbacks[:client_connect] << block end
Registers a callback for when a client connects.
@yield [client]
The block will be passed each newly connected client.
@yieldparam [TCPSocket] client
The connection from the client to the proxy.
@example
on_client_connect do |client| puts "[connected] #{client.remote_address.ip_address}:#{client.remote_address.ip_port}" end
@api public
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 268 def on_client_disconnect(&block) @callbacks[:client_disconnect] << block end
Registers a callback for when a client disconnects.
@yield [client, server]
The block will be passed each disconnected client and their connection to the server.
@yieldparam [TCPSocket] client
The connection from the client to the proxy.
@yieldparam [TCPSocket] server
The connection from the proxy to the server.
@example
on_client_disconnect do |client,server| puts "[disconnected] #{client.remote_address.ip_address}:#{client.remote_address.ip_port}" end
@api public
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 294 def on_server_connect(&block) @callbacks[:server_connect] << block end
Registers a callback for when the server accepts a connection.
@yield [client, server]
The block will be passed each connected client and their newly formed connection to the server.
@yieldparam [TCPSocket] client
The connection from the client to the proxy.
@yieldparam [TCPSocket] server
The connection from the proxy to the server.
@example
on_server_connect do |client,server| puts "[connected] #{proxy}" end
@api public
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 320 def on_server_disconnect(&block) @callbacks[:server_disconnect] << block end
Registers a callback for when the server closes a connection.
@yield [client, server]
The block will be passed the each client connection and the recently disconnected server connection.
@yieldparam [TCPSocket] client
The connection from the client to the proxy.
@yieldparam [TCPSocket] server
The connection from the proxy to the server.
@example
on_server_disconnect do |client,server| puts "[disconnected] #{proxy}" end
@api public
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 143 def open @socket = TCPServer.new(@host,@port) end
Opens the proxy.
@api public
Source
# 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
Polls the connections for data.
@api public
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 224 def recv(connection) connection.recv(@buffer_size) end
Receives data from a connection.
@param [TCPSocket] connection
The TCP connection to receive data from.
@return [String, nil]
The received data.
@api public
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 209 def send(connection,data) connection.send(data,0) end
Sends data to a connection.
@param [TCPSocket] connection
A TCP connection to write data to.
@param [String] data
The data to write.
@api public
Protected Instance Methods
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 334 def accept_client_connection @socket.accept end
Accepts a new client connection.
@return [TCPSocket]
A new connection.
@since 0.6.0
Source
# 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
Triggers the ‘client_connect` event.
@param [TCPSocket] client_connection
The new connection from a client to the proxy.
Source
# 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
Triggers the ‘client_disconnect` event.
@param [TCPSocket] client_connection
The connection from a client to the proxy.
@param [TCPSocket] server_connection
The connection from the proxy to the server.
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 354 def close_client_connection(socket) socket.close end
Closes a connection from the client.
@param [TCPSocket] socket
The connection from the client.
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 371 def close_proxy @socket.close end
Closes the TCP
proxy.
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 364 def close_server_connection(socket) socket.close end
Closes a connection to the server.
@param [TCPSocket] socket
The connection to the server.
Source
# File lib/ronin/support/network/tcp/proxy.rb, line 344 def open_server_connection TCPSocket.new(@server_host,@server_port) end
Creates a new connection to the server.
@return [TCPSocket]
A new connection.
Source
# 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
Triggers the ‘server_connect` event.
@param [TCPSocket] client_connection
The connection from a client to the proxy.
Source
# 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
Triggers the ‘server_disconnect` event.
@param [TCPSocket] client_connection
The connection from a client to the proxy.
@param [TCPSocket] server_connection
The connection from the proxy to the server.