Module: Rex::Socket::Udp

Includes:
Rex::Socket
Included in:
Post::Meterpreter::Extensions::Stdapi::Net::SocketSubsystem::UdpChannel
Defined in:
lib/rex/socket/udp.rb

Overview

This class provides methods for interacting with a UDP socket.

Defined Under Namespace

Classes: UnitTest

Instance Attribute Summary

Attributes included from Rex::Socket

#context, #ipv, #localhost, #localport, #peerhost, #peerport

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rex::Socket

addr_atoc, addr_atoi, addr_aton, addr_ctoa, addr_itoa, addr_iton, addr_ntoa, addr_ntoi, bit2netmask, cidr_crack, create_ip, create_tcp, create_tcp_server, create_udp, dotted_ip?, #fd, from_sockaddr, getaddress, gethostbyname, #getlocalname, #getpeername, #getsockname, #initsock, ipv6_link_address, ipv6_mac, is_internal?, is_ipv4?, is_ipv6?, net2bitmask, portlist_to_portspec, portspec_crack, portspec_to_portlist, resolv_nbo, resolv_nbo_i, resolv_to_dotted, source_address, support_ipv6?, tcp_socket_pair, to_sockaddr, udp_socket_pair

Class Method Details

.create(hash = {}) ⇒ Object

Creates the client using the supplied hash.



21
22
23
24
25
26
27
28
29
30
# File 'lib/rex/socket/udp.rb', line 21

def self.create(hash = {})
	hash['Proto'] = 'udp'
	# If we have are to bind to a LocalHost we must be a Server to avail of pivoting.
	# Rex::Socket::Parameters will subsequently turn off the sever flag after the correct
	# comm has been chosen.
	if( hash['LocalHost'] )
		hash['Server'] = true
	end
	self.create_param(Rex::Socket::Parameters.from_hash(hash))
end

.create_param(param) ⇒ Object

Wrapper around the base socket class’ creation method that automatically sets the parameter’s protocol to UDP.



36
37
38
39
# File 'lib/rex/socket/udp.rb', line 36

def self.create_param(param)
	param.proto = 'udp'
	Rex::Socket.create_param(param)
end

Instance Method Details

#def_read_timeoutObject

The default number of seconds to wait for a read operation to timeout.



155
156
157
# File 'lib/rex/socket/udp.rb', line 155

def def_read_timeout
	10
end

#get(timeout = nil) ⇒ Object

Calls recvfrom and only returns the data



147
148
149
150
# File 'lib/rex/socket/udp.rb', line 147

def get(timeout=nil)
	data, saddr, sport = recvfrom(65535, timeout)
	return data
end

#read(length = 65535) ⇒ Object

Read a datagram from the UDP socket.



63
64
65
66
67
68
# File 'lib/rex/socket/udp.rb', line 63

def read(length = 65535)
	if length < 0
		length = 65535
	end
	return sysread(length)
end

#recvfrom(length = 65535, timeout = def_read_timeout) ⇒ Object

Receives a datagram and returns the data and host:port of the requestor as [ data, host, port ].



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

def recvfrom(length = 65535, timeout=def_read_timeout)

	begin
		if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
		    (rv[0]) and (rv[0][0] == fd)
		   )
				data, saddr    = recvfrom_nonblock(length)
				af, host, port = Rex::Socket.from_sockaddr(saddr)

				return [ data, host, port ]
		else
			return [ '', nil, nil ]
		end
	rescue ::Timeout::Error
		return [ '', nil, nil ]
	rescue ::Interrupt
		raise $!
	rescue ::Exception
		return [ '', nil, nil ]
	end
end

#sendto(gram, peerhost, peerport, flags = 0) ⇒ Object

Sends a datagram to the supplied host:port with optional flags.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rex/socket/udp.rb', line 99

def sendto(gram, peerhost, peerport, flags = 0)

	# Catch unconnected IPv6 sockets talking to IPv4 addresses
	peer = Rex::Socket.resolv_nbo(peerhost)
	if (peer.length == 4 and self.ipv == 6)
		peerhost = Rex::Socket.getaddress(peerhost)
		if peerhost[0,7].downcase != '::ffff:'
			peerhost = '::ffff:' + peerhost
		end
	end

	begin
		send(gram, flags, Rex::Socket.to_sockaddr(peerhost, peerport))
	rescue  ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
		return nil
	end

end

#timed_read(length = 65535, timeout = def_read_timeout) ⇒ Object

Read a datagram from the UDP socket with a timeout



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rex/socket/udp.rb', line 73

def timed_read(length = 65535, timeout=def_read_timeout)
	begin
		if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
		    (rv[0]) and (rv[0][0] == fd)
		   )
				return read(length)
		else
			return ''
		end
	rescue Exception
		return ''
	end
end

#type?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/rex/socket/udp.rb', line 159

def type?
	return 'udp'
end

#write(gram) ⇒ Object Also known as: put

Write the supplied datagram to the connected UDP socket.



50
51
52
53
54
55
56
# File 'lib/rex/socket/udp.rb', line 50

def write(gram)
	begin
		return syswrite(gram)
	rescue  ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
		return nil
	end
end