2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/socket'
|
2005-06-04 06:19:42 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class provides methods for interacting with a UDP socket.
|
|
|
|
#
|
|
|
|
###
|
2005-09-27 05:31:48 +00:00
|
|
|
module Rex::Socket::Udp
|
|
|
|
|
|
|
|
include Rex::Socket
|
2005-06-04 06:19:42 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Factory
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-09-27 05:31:48 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Creates the client using the supplied hash.
|
2005-09-27 05:31:48 +00:00
|
|
|
#
|
|
|
|
def self.create(hash = {})
|
2010-02-09 16:45:46 +00:00
|
|
|
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
|
2005-09-27 05:31:48 +00:00
|
|
|
self.create_param(Rex::Socket::Parameters.from_hash(hash))
|
|
|
|
end
|
|
|
|
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
|
|
|
# Wrapper around the base socket class' creation method that automatically
|
2005-11-15 05:22:13 +00:00
|
|
|
# sets the parameter's protocol to UDP.
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
|
|
|
def self.create_param(param)
|
|
|
|
param.proto = 'udp'
|
2005-09-27 05:31:48 +00:00
|
|
|
Rex::Socket.create_param(param)
|
2005-06-04 06:19:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# UDP connected state methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Write the supplied datagram to the connected UDP socket.
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
|
|
|
def write(gram)
|
2008-11-11 05:11:40 +00:00
|
|
|
begin
|
|
|
|
return syswrite(gram)
|
2008-11-11 07:41:07 +00:00
|
|
|
rescue ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
|
2010-04-10 17:31:40 +00:00
|
|
|
return nil
|
|
|
|
end
|
2005-06-04 06:19:42 +00:00
|
|
|
end
|
|
|
|
|
2009-02-05 21:49:50 +00:00
|
|
|
alias put write
|
2005-11-26 11:16:36 +00:00
|
|
|
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Read a datagram from the UDP socket.
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
|
|
|
def read(length = 65535)
|
2006-01-27 05:33:08 +00:00
|
|
|
if length < 0
|
|
|
|
length = 65535
|
|
|
|
end
|
2005-09-27 05:31:48 +00:00
|
|
|
return sysread(length)
|
2005-06-04 06:19:42 +00:00
|
|
|
end
|
|
|
|
|
2005-11-26 11:16:36 +00:00
|
|
|
#
|
|
|
|
# Read a datagram from the UDP socket with a timeout
|
|
|
|
#
|
|
|
|
def timed_read(length = 65535, timeout=def_read_timeout)
|
|
|
|
begin
|
2010-05-20 20:42:17 +00:00
|
|
|
if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
|
2005-11-26 11:16:36 +00:00
|
|
|
(rv[0]) and (rv[0][0] == fd)
|
|
|
|
)
|
|
|
|
return read(length)
|
|
|
|
else
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
return ''
|
2010-04-10 17:31:40 +00:00
|
|
|
end
|
2005-11-26 11:16:36 +00:00
|
|
|
end
|
2010-04-10 17:31:40 +00:00
|
|
|
|
2005-06-04 06:19:42 +00:00
|
|
|
#alias send write
|
|
|
|
#alias recv read
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# UDP non-connected state methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Sends a datagram to the supplied host:port with optional flags.
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
|
|
|
def sendto(gram, peerhost, peerport, flags = 0)
|
2010-04-10 17:31:40 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
# Catch unconnected IPv6 sockets talking to IPv4 addresses
|
|
|
|
peer = Rex::Socket.resolv_nbo(peerhost)
|
|
|
|
if (peer.length == 4 and self.ipv == 6)
|
2011-12-10 14:16:01 +00:00
|
|
|
peerhost = Rex::Socket.getaddress(peerhost, true)
|
2010-05-21 05:01:14 +00:00
|
|
|
if peerhost[0,7].downcase != '::ffff:'
|
|
|
|
peerhost = '::ffff:' + peerhost
|
|
|
|
end
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
|
|
|
|
2008-11-11 05:11:40 +00:00
|
|
|
begin
|
|
|
|
send(gram, flags, Rex::Socket.to_sockaddr(peerhost, peerport))
|
2008-11-11 07:41:07 +00:00
|
|
|
rescue ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
|
2008-11-11 05:11:40 +00:00
|
|
|
return nil
|
|
|
|
end
|
2010-04-10 17:31:40 +00:00
|
|
|
|
2005-06-04 06:19:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Receives a datagram and returns the data and host:port of the requestor
|
2005-11-15 05:22:13 +00:00
|
|
|
# as [ data, host, port ].
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
2005-11-26 11:16:36 +00:00
|
|
|
def recvfrom(length = 65535, timeout=def_read_timeout)
|
2010-04-10 17:31:40 +00:00
|
|
|
|
2005-11-26 11:16:36 +00:00
|
|
|
begin
|
2010-05-20 20:42:17 +00:00
|
|
|
if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
|
2005-11-26 11:16:36 +00:00
|
|
|
(rv[0]) and (rv[0][0] == fd)
|
|
|
|
)
|
2010-04-10 17:31:40 +00:00
|
|
|
data, saddr = recvfrom_nonblock(length)
|
2005-11-26 11:16:36 +00:00
|
|
|
af, host, port = Rex::Socket.from_sockaddr(saddr)
|
2005-06-04 06:19:42 +00:00
|
|
|
|
2005-11-26 11:16:36 +00:00
|
|
|
return [ data, host, port ]
|
|
|
|
else
|
|
|
|
return [ '', nil, nil ]
|
|
|
|
end
|
2010-05-29 06:52:47 +00:00
|
|
|
rescue ::Timeout::Error
|
|
|
|
return [ '', nil, nil ]
|
|
|
|
rescue ::Interrupt
|
|
|
|
raise $!
|
|
|
|
rescue ::Exception
|
2005-11-26 11:16:36 +00:00
|
|
|
return [ '', nil, nil ]
|
|
|
|
end
|
2005-06-04 06:19:42 +00:00
|
|
|
end
|
2010-04-10 17:31:40 +00:00
|
|
|
|
2005-11-26 11:16:36 +00:00
|
|
|
#
|
|
|
|
# Calls recvfrom and only returns the data
|
|
|
|
#
|
|
|
|
def get(timeout=nil)
|
|
|
|
data, saddr, sport = recvfrom(65535, timeout)
|
|
|
|
return data
|
|
|
|
end
|
2010-04-10 17:31:40 +00:00
|
|
|
|
2005-11-26 11:16:36 +00:00
|
|
|
#
|
|
|
|
# The default number of seconds to wait for a read operation to timeout.
|
|
|
|
#
|
|
|
|
def def_read_timeout
|
|
|
|
10
|
2010-04-10 17:31:40 +00:00
|
|
|
end
|
2005-06-04 06:19:42 +00:00
|
|
|
|
2006-01-27 05:33:08 +00:00
|
|
|
def type?
|
|
|
|
return 'udp'
|
|
|
|
end
|
2005-12-13 06:08:40 +00:00
|
|
|
|
2008-11-11 05:11:40 +00:00
|
|
|
end
|
2010-04-10 17:31:40 +00:00
|
|
|
|