Module: Ronin::Network::UDP

Included in:
Net, Mixins::UDP, Support
Defined in:
lib/ronin/network/udp/udp.rb,
lib/ronin/network/udp/proxy.rb

Overview

Provides helper methods for using the UDP protocol.

Defined Under Namespace

Classes: Proxy

Instance Method Summary collapse

Instance Method Details

#udp_banner(host, port, local_host = nil, local_port = nil) {|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.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

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



279
280
281
282
283
284
285
286
287
288
# File 'lib/ronin/network/udp/udp.rb', line 279

def udp_banner(host,port,local_host=nil,local_port=nil)
  banner = nil

  udp_session(host,port,local_host,local_port) do |socket|
    banner = socket.readline
  end

  yield banner if block_given?
  return banner
end

#udp_connect(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ UDPSocket

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

Examples:

udp_connect('www.hackety.org',80)
# => UDPSocket
udp_connect('www.wired.com',80) do |socket|
  puts socket.readlines
end

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (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:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ronin/network/udp/udp.rb', line 122

def udp_connect(host,port,local_host=nil,local_port=nil)
  host = host.to_s
  port = port.to_i

  socket = UDPSocket.new

  if local_host || local_port
    local_host = local_host.to_s
    local_port = local_port.to_i

    socket.bind(local_host,local_port)
  end

  socket.connect(host,port)

  yield socket if block_given?
  return socket
end

#udp_connect_and_send(data, host, port, local_host = nil, local_port = nil) {|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.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

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



171
172
173
174
175
176
177
# File 'lib/ronin/network/udp/udp.rb', line 171

def udp_connect_and_send(data,host,port,local_host=nil,local_port=nil)
  socket = udp_connect(host,port,local_host,local_port)
  socket.write(data)

  yield socket if block_given?
  return socket
end

#udp_open?(host, port, local_host = nil, local_port = nil, timeout = nil) ⇒ 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,nil,nil,5)
# => nil

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

  • timeout (Integer) (defaults to: nil)

    (5) The maximum time to attempt connecting.

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ronin/network/udp/udp.rb', line 63

def udp_open?(host,port,local_host=nil,local_port=nil,timeout=nil)
  timeout ||= 5

  begin
    Timeout.timeout(timeout) do
      udp_session(host,port,local_host,local_port) 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
end

#udp_recv(port = nil, host = nil) {|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(1337) do |server,(host,port),mesg|
  server.send('hello',host,port)
end

Parameters:

  • port (Integer) (defaults to: nil)

    (nil) The port the UDPServer will listen on.

  • host (String) (defaults to: nil)

    (nil) The optional host the UDPServer will 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



435
436
437
438
439
440
441
# File 'lib/ronin/network/udp/udp.rb', line 435

def udp_recv(port=nil,host=nil)
  udp_server_session(port,host) do |server|
    mesg, addrinfo = server.recvfrom(4096)

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

#udp_send(data, host, port, local_host = nil, local_port = nil) ⇒ 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.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Returns:

  • (true)

    The data was successfully sent.

Since:

  • 0.4.0



245
246
247
248
249
250
251
# File 'lib/ronin/network/udp/udp.rb', line 245

def udp_send(data,host,port,local_host=nil,local_port=nil)
  udp_session(host,port,local_host,local_port) do |socket|
    socket.write(data)
  end

  return true
end

#udp_server(port = nil, host = nil) {|server| ... } ⇒ UDPServer

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

Examples:

udp_server(1337)

Parameters:

  • port (Integer) (defaults to: nil)

    (nil) The local port to listen on.

  • host (String) (defaults to: nil)

    (nil) The host to bind to.

Yields:

  • (server)

Returns:

  • (UDPServer)

    The new UDP server.

See Also:



309
310
311
312
313
314
315
316
317
318
# File 'lib/ronin/network/udp/udp.rb', line 309

def udp_server(port=nil,host=nil)
  port   = port.to_i
  host   = host.to_s

  server = UDPSocket.new
  server.bind(host,port)

  yield server if block_given?
  return server
end

#udp_server_loop(port = nil, host = nil) {|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(1337) do |server,(host,port),mesg|
  server.send('hello',host,port)
end

Parameters:

  • port (Integer) (defaults to: nil)

    (nil) The port the UDPServer will listen on.

  • host (String) (defaults to: nil)

    (nil) The optional host the UDPServer will 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



388
389
390
391
392
393
394
395
396
# File 'lib/ronin/network/udp/udp.rb', line 388

def udp_server_loop(port=nil,host=nil)
  udp_server_session(port,host) do |server|
    loop do
      mesg, addrinfo = server.recvfrom(4096)

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

#udp_server_session(port = nil, host = nil) {|server| ... } ⇒ nil

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

Examples:

udp_server_session(1337) do |server|
  data, sender = server.recvfrom(1024)
end

Parameters:

  • port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

  • host (String) (defaults to: nil)

    (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)


345
346
347
348
349
# File 'lib/ronin/network/udp/udp.rb', line 345

def udp_server_session(port=nil,host=nil,&block)
  server = udp_server(port,host,&block)
  server.close()
  return nil
end

#udp_session(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ nil

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

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket. After the block has returned, the socket will then be closed.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

Returns:

  • (nil)


206
207
208
209
210
211
212
# File 'lib/ronin/network/udp/udp.rb', line 206

def udp_session(host,port,local_host=nil,local_port=nil)
  socket = udp_connect(host,port,local_host,local_port)

  yield socket if block_given?
  socket.close
  return nil
end

#udp_single_server(port = nil, host = nil) ⇒ Object

Deprecated.

Deprecated as of 0.5.0. Use #udp_recv instead.



447
448
449
# File 'lib/ronin/network/udp/udp.rb', line 447

def udp_single_server(port=nil,host=nil)
  udp_recv(port,host)
end