class Ronin::Support::Network::UDP::Proxy

The UDP Proxy allows for inspecting and manipulating UDP protocols.

## Example

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

Ronin::Support::Network::UDP::Proxy.start(:port => 1337, :server => ['4.2.2.1', 53]) do |proxy|
  hex = Hexdump::Hexdump.new

  proxy.on_client_data do |(client,(host,port)),server,data|
    puts "#{host}:#{port} -> #{proxy}"
    hex.dump(data)
  end

  proxy.on_server_data do |(client,(host,port)),server,data|
    puts "#{host}:#{port} <- #{proxy}"
    hex.dump(data)
  end

end

@since 0.5.0

Public Instance Methods

open() click to toggle source

Opens the UDP Proxy.

@api public

# File lib/ronin/support/network/udp/proxy.rb, line 59
def open
  @socket = UDPSocket.new
  @socket.bind(@host,@port)
end
poll() click to toggle source

Polls the connections for data/errors and the proxy socket for new client connections.

@api public

# File lib/ronin/support/network/udp/proxy.rb, line 70
def poll
  sockets = [@socket] + server_connections

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

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

    close_connection(client_socket,server_socket)
  end

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

    server_data(client_socket,server_socket,data)
  end

  if readable.include?(@socket)
    data, addrinfo = recv(@socket)

    client_socket = [@socket, [addrinfo[3], addrinfo[1]]]
    server_socket = (@connections[client_socket] ||= open_server_connection)

    client_data(client_socket,server_socket,data)
  end
end
recv(connection) click to toggle source

Receives data from a connection.

@param [UDPSocket, (UDPSocket, (String, Integer))] connection

The connection from the proxy to the server, or the proxy socket
and the address of a client.

@return [String, (String, Array)]

The data received.

@api public

# File lib/ronin/support/network/udp/proxy.rb, line 133
def recv(connection)
  case connection
  when Array
    socket, _host_and_port = connection

    socket.recvfrom(@buffer_size)
  when UDPSocket
    connection.recvfrom(@buffer_size)
  end
end
send(connection,data) click to toggle source

Sends data to a connection.

@param [UDPSocket, (UDPSocket, (String, Integer))] connection

The connection from the proxy to the server, or the proxy socket
and host/port of the client.

@param [String] data

The data to be sent.

@api public

# File lib/ronin/support/network/udp/proxy.rb, line 110
def send(connection,data)
  case connection
  when Array
    socket, (host, port) = connection

    socket.send(data,0,host,port)
  when UDPSocket
    connection.send(data,0)
  end
end

Protected Instance Methods

close_client_connection(connection) click to toggle source

Closes a connection from the client to the proxy.

@param [(UDPSocket, (String, Integer))] connection

The UDP Proxy socket and the host/port of the client.

@note no-op

# File lib/ronin/support/network/udp/proxy.rb, line 167
def close_client_connection(connection)
  # no-op
end
close_proxy() click to toggle source

Closes the UDP proxy socket.

# File lib/ronin/support/network/udp/proxy.rb, line 184
def close_proxy
  @socket.close
end
close_server_connection(connection) click to toggle source

Closes the connection from the proxy to the server.

@param [UDPSocket] connection

The UDPSocket from the proxy to the server.
# File lib/ronin/support/network/udp/proxy.rb, line 177
def close_server_connection(connection)
  connection.close
end
open_server_connection() click to toggle source

Creates a new connection from the proxy to the server.

@return [UDPSocket]

The new UDPSocket to the server.
# File lib/ronin/support/network/udp/proxy.rb, line 152
def open_server_connection
  socket = UDPSocket.new
  socket.connect(@server_host,@server_port)

  return socket
end