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

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

Overview

The UDP Proxy allows for inspecting and manipulating UDP protocols.

Example

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

Ronin::Network::UDP::Proxy.start(:port => 1337, :server => ['4.2.2.1', 53]) do |proxy|
  hex = Hexdump::Dumper.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:

Constant Summary

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::Network::Proxy

Instance Method Details

- (Object) close_client_connection(connection) (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/network/udp/proxy.rb', line 167

def close_client_connection(connection)
  # no-op
end

- (Object) close_proxy (protected)

Closes the UDP proxy socket.

Since:

  • 0.5.0



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

def close_proxy
  @socket.close
end

- (Object) close_server_connection(connection) (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/network/udp/proxy.rb', line 177

def close_server_connection(connection)
  connection.close
end

- (Object) open

Opens the UDP Proxy.

Since:

  • 0.5.0



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

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

- (UDPSocket) open_server_connection (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/network/udp/proxy.rb', line 152

def open_server_connection
  socket = UDPSocket.new
  socket.connect(@server_host,@server_port)

  return socket
end

- (Object) poll

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/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

- (String, (String, Array)) recv(connection)

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/network/udp/proxy.rb', line 133

def recv(connection)
  case connection
  when Array
    socket, (host, port) = connection

    socket.recvfrom(@buffer_size)
  when UDPSocket
    connection.recvfrom(@buffer_size)
  end
end

- (Object) send(connection, data)

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/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