Class: Ronin::Support::Network::TCP::Proxy
- 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.
Direct Known Subclasses
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
-
#accept_client_connection ⇒ TCPSocket
protected
Accepts a new client connection.
-
#client_connect(client_connection) ⇒ Object
protected
Triggers the
client_connect
event. -
#client_disconnect(client_connection, server_connection) ⇒ Object
protected
Triggers the
client_disconnect
event. -
#close_client_connection(socket) ⇒ Object
protected
Closes a connection from the client.
-
#close_proxy ⇒ Object
protected
Closes the TCP proxy.
-
#close_server_connection(socket) ⇒ Object
protected
Closes a connection to the server.
-
#initialize(**kwargs) ⇒ Proxy
constructor
Creates a new TCP Proxy.
-
#on_client_connect {|client| ... } ⇒ Object
Registers a callback for when a client connects.
-
#on_client_disconnect {|client, server| ... } ⇒ Object
(also: #on_disconnect)
Registers a callback for when a client disconnects.
-
#on_server_connect {|client, server| ... } ⇒ Object
(also: #on_connect)
Registers a callback for when the server accepts a connection.
-
#on_server_disconnect {|client, server| ... } ⇒ Object
Registers a callback for when the server closes a connection.
-
#open ⇒ Object
Opens the proxy.
-
#open_server_connection ⇒ TCPSocket
protected
Creates a new connection to the server.
-
#poll ⇒ Object
Polls the connections for data.
-
#recv(connection) ⇒ String?
Receives data from a connection.
-
#send(connection, data) ⇒ Object
Sends data to a connection.
-
#server_connect(client_connection) ⇒ Object
protected
Triggers the
server_connect
event. -
#server_disconnect(client_connection, server_connection) ⇒ Object
protected
Triggers the
server_disconnect
event.
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.
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_connection ⇒ TCPSocket (protected)
Accepts a new client connection.
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.
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.
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.
354 355 356 |
# File 'lib/ronin/support/network/tcp/proxy.rb', line 354 def close_client_connection(socket) socket.close end |
#close_proxy ⇒ Object (protected)
Closes the TCP proxy.
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.
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.
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.
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.
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.
320 321 322 |
# File 'lib/ronin/support/network/tcp/proxy.rb', line 320 def on_server_disconnect(&block) @callbacks[:server_disconnect] << block end |
#open ⇒ Object
Opens the proxy.
143 144 145 |
# File 'lib/ronin/support/network/tcp/proxy.rb', line 143 def open @socket = TCPServer.new(@host,@port) end |
#open_server_connection ⇒ TCPSocket (protected)
Creates a new connection to the server.
344 345 346 |
# File 'lib/ronin/support/network/tcp/proxy.rb', line 344 def open_server_connection TCPSocket.new(@server_host,@server_port) end |
#poll ⇒ Object
Polls the connections for data.
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.
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.
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.
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.
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 |