Module: Ronin::Support::Network::UDP

Defined in:
lib/ronin/support/network/udp.rb,
lib/ronin/support/network/udp/mixin.rb,
lib/ronin/support/network/udp/proxy.rb

Overview

Since:

  • 1.0.0

Defined Under Namespace

Modules: Mixin Classes: Proxy

Class Method Summary collapse

Class Method Details

Reads the banner from the service running on the given host and port.

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for connect.

Options Hash (**kwargs):

  • :bind_host (String, nil)

    The local host to bind to.

  • :bind_port (Integer, nil)

    The local port to bind to.

Yields:

  • (banner)

    If a block is given, it will be passed the grabbed banner.

Yield Parameters:

  • banner (String)

    The grabbed banner.

Returns:

  • (String)

    The grabbed banner.

Since:

  • 1.0.0



263
264
265
266
267
268
269
270
271
272
# File 'lib/ronin/support/network/udp.rb', line 263

def self.banner(host,port,**kwargs)
  banner = nil

  connect(host,port,**kwargs) do |socket|
    banner = socket.readline
  end

  yield banner if block_given?
  return banner
end

.connect(host, port, bind_host: nil, bind_port: nil) {|socket| ... } ⇒ UDPSocket?

Creates a new UDPSocket object connected to a given host and port.

Examples:

UDP.connect('8.8.8.8',53)
# => #<UDPSocket:fd 5, AF_INET, 192.168.122.165, 48313>
UDP.connect('8.8.8.8',53) do |socket|
  # ...
end

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • bind_host (String, nil) (defaults to: nil)

    The local host to bind to.

  • bind_port (Integer, nil) (defaults to: nil)

    The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket. Once the block returns the socket will be closed.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDP socket.

Returns:

  • (UDPSocket, nil)

    The newly created UDP socket object. If a block is given a nil will be returned.

See Also:

Since:

  • 1.0.0



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ronin/support/network/udp.rb', line 127

def self.connect(host,port, bind_host: nil, bind_port: nil)
  host = DNS::IDN.to_ascii(host)
  port = port.to_i

  socket = UDPSocket.new

  if bind_host || bind_port
    socket.bind(bind_host.to_s,bind_port.to_i)
  end

  socket.connect(host,port)

  if block_given?
    begin
      yield socket
    ensure
      socket.close
    end
  else
    return socket
  end
end

.connect_and_send(data, host, port, **kwargs) {|socket| ... } ⇒ UDPSocket

Creates a new UDPSocket object, connected to a given host and port. The given data will then be written to the newly created UDPSocket.

Parameters:

  • data (String)

    The data to send through the connection.

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for connect.

Options Hash (**kwargs):

  • :bind_host (String, nil)

    The local host to bind to.

  • :bind_port (Integer, nil)

    The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

Returns:

  • (UDPSocket)

    The newly created UDPSocket object.

Since:

  • 1.0.0



183
184
185
186
187
188
189
# File 'lib/ronin/support/network/udp.rb', line 183

def self.connect_and_send(data,host,port,**kwargs)
  socket = connect(host,port,**kwargs)
  socket.write(data)

  yield socket if block_given?
  return socket
end

.open?(host, port, timeout: 5, **kwargs) ⇒ Boolean?

Tests whether a remote UDP port is open.

Examples:

UDP.open?('4.2.2.1',53)
# => true

Using a timeout:

UDP.open?('example.com',1111, timeout: 5)
# => nil

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • timeout (Integer) (defaults to: 5)

    (5) The maximum time to attempt connecting.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for connect.

Options Hash (**kwargs):

  • :bind_host (String, nil)

    The local host to bind to.

  • :bind_port (Integer, nil)

    The local port to bind to.

Returns:

  • (Boolean, nil)

    Specifies whether the remote UDP port is open. If no data or ICMP error were received, nil will be returned.

Since:

  • 0.5.0



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ronin/support/network/udp.rb', line 69

def self.open?(host,port, timeout: 5, **kwargs)
  Timeout.timeout(timeout) do
    connect(host,port,**kwargs) do |socket|
      # send an empty UDP packet, just like nmap
      socket.syswrite('')

      # send an empty UDP packet again, to elicit an
      # Errno::ECONNREFUSED
      socket.syswrite('')
    end
  end

  return true
rescue Timeout::Error
  return nil
rescue SocketError, SystemCallError
  return false
end

.recv(**kwargs) {|server, (client_host, client_port), mesg| ... } ⇒ nil

Creates a new UDPServer listening on a given host and port, accepts only one message from a client.

Examples:

UDP.recv(port: 1337) do |server,(host,port),mesg|
  server.send('hello',host,port)
end

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional arguments for server.

Options Hash (**kwargs):

  • :port (Integer, nil)

    The local port to bind to.

  • :host (String, nil)

    The host to bind to.

Yields:

  • (server, (client_host, client_port), mesg)

    The given block will be passed the client host/port and the received message.

Yield Parameters:

  • server (UDPServer)

    The UDPServer.

  • client_host (String)

    The source host of the message.

  • client_port (Integer)

    The source port of the message.

  • mesg (String)

    The received message.

Returns:

  • (nil)

Since:

  • 0.5.0



426
427
428
429
430
431
432
# File 'lib/ronin/support/network/udp.rb', line 426

def self.recv(**kwargs)
  server_session(**kwargs) do |server|
    mesg, addrinfo = server.recvfrom(4096)

    yield server, [addrinfo[3], addrinfo[1]], mesg if block_given?
  end
end

.send(data, host, port, **kwargs) ⇒ true

Connects to a specified host and port, sends the given data and then closes the connection.

Examples:

buffer = "GET /" + ('A' * 4096) + "\n\r"
UDP.send(buffer,'victim.com',80)
# => true

Parameters:

  • data (String)

    The data to send through the connection.

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for connect.

Options Hash (**kwargs):

  • :bind_host (String, nil)

    The local host to bind to.

  • :bind_port (Integer, nil)

    The local port to bind to.

Returns:

  • (true)

    The data was successfully sent.

Since:

  • 0.4.0



225
226
227
228
229
230
231
# File 'lib/ronin/support/network/udp.rb', line 225

def self.send(data,host,port,**kwargs)
  connect(host,port,**kwargs) do |socket|
    socket.write(data)
  end

  return true
end

.server(port: nil, host: nil) {|server| ... } ⇒ UDPServer

Creates a new UDPServer listening on a given host and port.

Examples:

server = UDP.server(port: 1337)

Parameters:

  • port (Integer, nil) (defaults to: nil)

    The local port to listen on.

  • host (String, nil) (defaults to: nil)

    The host to bind to.

Yields:

Returns:

  • (UDPServer)

    The new UDP server.

See Also:

Since:

  • 1.0.0



293
294
295
296
297
298
299
# File 'lib/ronin/support/network/udp.rb', line 293

def self.server(port: nil, host: nil)
  server = UDPSocket.new
  server.bind(host.to_s,port.to_i)

  yield server if block_given?
  return server
end

.server_loop(**kwargs) {|server, (client_host, client_port), mesg| ... } ⇒ nil

Creates a new UDPServer listening on a given host and port, accepting messages from clients in a loop.

Examples:

UDP.server_loop(port: 1337) do |server,(host,port),mesg|
  server.send('hello',host,port)
end

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional arguments for server.

Options Hash (**kwargs):

  • :port (Integer, nil)

    The local port to bind to.

  • :host (String, nil)

    The host to bind to.

Yields:

  • (server, (client_host, client_port), mesg)

    The given block will be passed the client host/port and the received message.

Yield Parameters:

  • server (UDPServer)

    The UDPServer.

  • client_host (String)

    The source host of the message.

  • client_port (Integer)

    The source port of the message.

  • mesg (String)

    The received message.

Returns:

  • (nil)

Since:

  • 0.5.0



376
377
378
379
380
381
382
383
384
# File 'lib/ronin/support/network/udp.rb', line 376

def self.server_loop(**kwargs)
  server_session(**kwargs) do |server|
    loop do
      mesg, addrinfo = server.recvfrom(4096)

      yield server, [addrinfo[3], addrinfo[1]], mesg if block_given?
    end
  end
end

.server_session(**kwargs) {|server| ... } ⇒ nil

Creates a new temporary UDPServer listening on a given host and port.

Examples:

UDP.server_session(port: 1337) do |server|
  data, sender = server.recvfrom(1024)
end

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional arguments for server.

Options Hash (**kwargs):

  • :port (Integer, nil)

    The local port to bind to.

  • :host (String, nil)

    The host to bind to.

Yields:

  • (server)

    The block which will be called after the server has been created. After the block has finished, the server will be closed.

Yield Parameters:

  • server (UDPServer)

    The newly created UDP server.

Returns:

  • (nil)

Since:

  • 1.0.0



330
331
332
333
334
# File 'lib/ronin/support/network/udp.rb', line 330

def self.server_session(**kwargs,&block)
  server = server(**kwargs,&block)
  server.close
  return nil
end