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.

Yields:

  • (banner)

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

Yield Parameters:

  • banner (String)

    The grabbed banner.


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

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

Yields:

  • (socket)

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

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

See Also:


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

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

  socket = UDPSocket.new
  socket.bind(local_host,local_port) if (local_host && local_port)
  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.

Yields:

  • (socket)

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

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.


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

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

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
84
85
86
# 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 junk data, to elicit an error message
        socket.syswrite("\0" * 64)

        # test if we've received any data
        socket.sysread(1)
      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

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.

Since:

  • 0.5.0


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

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

Since:

  • 0.4.0


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

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)

Yields:

  • (server)

See Also:


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

def udp_server(port=nil,host=nil)
  port   = port.to_i
  host   = (host || '0.0.0.0').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

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.

Since:

  • 0.5.0


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

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

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.


343
344
345
346
347
# File 'lib/ronin/network/udp/udp.rb', line 343

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.

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.


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

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.


445
446
447
# File 'lib/ronin/network/udp/udp.rb', line 445

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