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

Included in:
Mixin
Defined in:
lib/ronin/support/network/udp/mixin.rb

Overview

Provides helper methods for using the UDP protocol.

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#udp_banner(host, port, **kwargs) {|banner| ... } ⇒ String

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 #udp_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.

See Also:

Since:

  • 1.0.0



227
228
229
# File 'lib/ronin/support/network/udp/mixin.rb', line 227

def udp_banner(host,port,**kwargs,&block)
  UDP.banner(host,port,**kwargs,&block)
end

#udp_connect(host, port, **kwargs) {|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.

  • kwargs (Hash)

    a customizable set of options

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



112
113
114
# File 'lib/ronin/support/network/udp/mixin.rb', line 112

def udp_connect(host,port,**kwargs,&block)
  UDP.connect(host,port,**kwargs,&block)
end

#udp_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 #udp_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.

See Also:

Since:

  • 1.0.0



151
152
153
# File 'lib/ronin/support/network/udp/mixin.rb', line 151

def udp_connect_and_send(data,host,port,**kwargs,&block)
  UDP.connect_and_send(data,host,port,**kwargs,&block)
end

#udp_open?(host, port, **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.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #udp_connect.

Options Hash (**kwargs):

  • :timeout (Integer) — default: 5

    The maximum time to attempt connecting.

  • :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.

See Also:

Since:

  • 0.5.0



68
69
70
# File 'lib/ronin/support/network/udp/mixin.rb', line 68

def udp_open?(host,port,**kwargs)
  UDP.open?(host,port,**kwargs)
end

#udp_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 #udp_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)

See Also:

Since:

  • 0.5.0



382
383
384
# File 'lib/ronin/support/network/udp/mixin.rb', line 382

def udp_recv(**kwargs,&block)
  UDP.recv(**kwargs,&block)
end

#udp_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 #udp_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.

See Also:

Since:

  • 0.4.0



191
192
193
# File 'lib/ronin/support/network/udp/mixin.rb', line 191

def udp_send(data,host,port,**kwargs)
  UDP.send(data,host,port,**kwargs)
end

#udp_server(**kwargs, &block) ⇒ UDPServer

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

Examples:

udp_server(port: 1337)

Parameters:

Options Hash (**kwargs):

  • :port (Integer, nil) — default: 0

    The local port to listen on.

  • :host (String, nil)

    The host to bind to.

Returns:

  • (UDPServer)

    The new UDP server.

See Also:

Since:

  • 1.0.0



255
256
257
# File 'lib/ronin/support/network/udp/mixin.rb', line 255

def udp_server(**kwargs,&block)
  UDP.server(**kwargs,&block)
end

#udp_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 #udp_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)

See Also:

Since:

  • 0.5.0



336
337
338
# File 'lib/ronin/support/network/udp/mixin.rb', line 336

def udp_server_loop(**kwargs,&block)
  UDP.server_loop(**kwargs,&block)
end

#udp_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 #udp_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)

See Also:

Since:

  • 1.0.0



290
291
292
# File 'lib/ronin/support/network/udp/mixin.rb', line 290

def udp_server_session(**kwargs,&block)
  UDP.server_session(**kwargs,&block)
end