Class: Ronin::Support::Network::UDP::Proxy

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

Overview

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

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!, #initialize, #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

This class inherits a constructor from Ronin::Support::Network::Proxy

Instance Method Details

#close_client_connection(connection) ⇒ Object (protected)

Note:

no-op

Closes a connection from the client to the proxy.

Parameters:

  • connection ((UDPSocket, (String, Integer)))

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

Since:

  • 0.5.0



167
168
169
# File 'lib/ronin/support/network/udp/proxy.rb', line 167

def close_client_connection(connection)
  # no-op
end

#close_proxyObject (protected)

Closes the UDP proxy socket.

Since:

  • 0.5.0



184
185
186
# File 'lib/ronin/support/network/udp/proxy.rb', line 184

def close_proxy
  @socket.close
end

#close_server_connection(connection) ⇒ Object (protected)

Closes the connection from the proxy to the server.

Parameters:

  • connection (UDPSocket)

    The UDPSocket from the proxy to the server.

Since:

  • 0.5.0



177
178
179
# File 'lib/ronin/support/network/udp/proxy.rb', line 177

def close_server_connection(connection)
  connection.close
end

#openObject

Opens the UDP Proxy.

Since:

  • 0.5.0



59
60
61
62
# File 'lib/ronin/support/network/udp/proxy.rb', line 59

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

#open_server_connectionUDPSocket (protected)

Creates a new connection from the proxy to the server.

Returns:

  • (UDPSocket)

    The new UDPSocket to the server.

Since:

  • 0.5.0



152
153
154
155
156
157
# 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

#pollObject

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

Since:

  • 0.5.0



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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) ⇒ String, (String, Array)

Receives data from a connection.

Parameters:

  • connection (UDPSocket, (UDPSocket, (String, Integer)))

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

Returns:

Since:

  • 0.5.0



133
134
135
136
137
138
139
140
141
142
# 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) ⇒ Object

Sends data to a connection.

Parameters:

  • connection (UDPSocket, (UDPSocket, (String, Integer)))

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

  • data (String)

    The data to be sent.

Since:

  • 0.5.0



110
111
112
113
114
115
116
117
118
119
# 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