2005-06-02 07:52:17 +00:00
|
|
|
require 'socket'
|
2010-02-06 17:59:25 +00:00
|
|
|
require 'thread'
|
2005-06-02 07:52:17 +00:00
|
|
|
require 'resolv'
|
2005-09-30 05:59:44 +00:00
|
|
|
require 'rex/exceptions'
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Base class for all sockets.
|
|
|
|
#
|
|
|
|
###
|
2005-09-27 05:31:48 +00:00
|
|
|
module Socket
|
2005-06-02 07:52:17 +00:00
|
|
|
|
2011-05-12 20:03:55 +00:00
|
|
|
module Comm
|
|
|
|
end
|
|
|
|
|
|
|
|
require 'rex/socket/parameters'
|
|
|
|
require 'rex/socket/tcp'
|
|
|
|
require 'rex/socket/tcp_server'
|
|
|
|
|
|
|
|
require 'rex/socket/comm'
|
|
|
|
require 'rex/socket/comm/local'
|
|
|
|
|
|
|
|
require 'rex/socket/switch_board'
|
|
|
|
require 'rex/socket/subnet_walker'
|
|
|
|
require 'rex/socket/range_walker'
|
2005-06-03 04:51:51 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Factory methods
|
|
|
|
#
|
|
|
|
##
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Create a socket instance using the supplied parameter hash.
|
|
|
|
#
|
2005-06-04 06:19:42 +00:00
|
|
|
def self.create(opts = {})
|
2005-06-02 07:52:17 +00:00
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts))
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Create a socket using the supplied Rex::Socket::Parameter instance.
|
|
|
|
#
|
2005-06-02 07:52:17 +00:00
|
|
|
def self.create_param(param)
|
|
|
|
return param.comm.create(param)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Create a TCP socket using the supplied parameter hash.
|
|
|
|
#
|
2005-06-04 06:19:42 +00:00
|
|
|
def self.create_tcp(opts = {})
|
2005-06-03 04:51:51 +00:00
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts.merge('Proto' => 'tcp')))
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Create a TCP server socket using the supplied parameter hash.
|
|
|
|
#
|
|
|
|
def self.create_tcp_server(opts = {})
|
2005-06-03 07:13:15 +00:00
|
|
|
return create_tcp(opts.merge('Server' => true))
|
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Create a UDP socket using the supplied parameter hash.
|
|
|
|
#
|
2005-06-04 06:19:42 +00:00
|
|
|
def self.create_udp(opts = {})
|
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts.merge('Proto' => 'udp')))
|
|
|
|
end
|
|
|
|
|
2008-07-22 19:03:59 +00:00
|
|
|
#
|
|
|
|
# Create a IP socket using the supplied parameter hash.
|
|
|
|
#
|
|
|
|
def self.create_ip(opts = {})
|
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts.merge('Proto' => 'ip')))
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Serialization
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2007-04-30 00:11:49 +00:00
|
|
|
|
|
|
|
# Cache our IPv6 support flag
|
|
|
|
@@support_ipv6 = nil
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
#
|
|
|
|
# Determine whether we support IPv6
|
2009-12-08 18:32:26 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.support_ipv6?
|
2008-12-26 07:11:49 +00:00
|
|
|
return @@support_ipv6 if not @@support_ipv6.nil?
|
|
|
|
|
|
|
|
@@support_ipv6 = false
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-30 00:11:49 +00:00
|
|
|
if (::Socket.const_defined?('AF_INET6'))
|
|
|
|
begin
|
|
|
|
s = ::Socket.new(::Socket::AF_INET6, ::Socket::SOCK_DGRAM, ::Socket::IPPROTO_UDP)
|
|
|
|
s.close
|
2008-12-26 07:11:49 +00:00
|
|
|
@@support_ipv6 = true
|
2007-04-30 00:13:26 +00:00
|
|
|
rescue
|
2007-04-30 00:11:49 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2008-12-26 07:11:49 +00:00
|
|
|
return @@support_ipv6
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2006-06-08 21:02:58 +00:00
|
|
|
#
|
|
|
|
# Determine whether this is an IPv4 address
|
2009-12-08 18:32:26 +00:00
|
|
|
#
|
2006-06-08 21:02:58 +00:00
|
|
|
def self.is_ipv4?(addr)
|
2006-06-15 20:53:20 +00:00
|
|
|
res = Rex::Socket.getaddress(addr)
|
2006-06-08 21:02:58 +00:00
|
|
|
res.match(/:/) ? false : true
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2006-06-08 21:02:58 +00:00
|
|
|
#
|
|
|
|
# Determine whether this is an IPv6 address
|
2009-12-08 18:32:26 +00:00
|
|
|
#
|
2006-06-08 21:02:58 +00:00
|
|
|
def self.is_ipv6?(addr)
|
2006-06-15 20:53:20 +00:00
|
|
|
res = Rex::Socket.getaddress(addr)
|
2006-06-08 21:02:58 +00:00
|
|
|
res.match(/:/) ? true : false
|
|
|
|
end
|
|
|
|
|
2006-06-15 20:53:20 +00:00
|
|
|
#
|
2009-12-08 18:32:26 +00:00
|
|
|
# Checks to see if the supplied address is a dotted quad.
|
2006-06-15 20:53:20 +00:00
|
|
|
#
|
|
|
|
def self.dotted_ip?(addr)
|
2007-04-29 23:43:39 +00:00
|
|
|
# Assume anything with a colon is IPv6
|
|
|
|
return true if (support_ipv6? and addr =~ /:/)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
# Otherwise assume this is IPv4
|
2006-06-15 22:41:57 +00:00
|
|
|
(addr =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))$/) ? true : false
|
2006-06-15 20:53:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2011-01-15 04:40:21 +00:00
|
|
|
# Return true if +addr+ is within the ranges specified in RFC1918, or
|
|
|
|
# RFC5735/RFC3927
|
|
|
|
#
|
|
|
|
def self.is_internal?(addr)
|
|
|
|
if self.dotted_ip?(addr)
|
|
|
|
addr =~ /^(?:10\.|192\.168|172.(?:1[6-9]|2[0-9]|3[01])\.|169\.254)/
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2006-06-15 20:53:20 +00:00
|
|
|
# Wrapper for Resolv.getaddress that takes special care to see if the
|
|
|
|
# supplied address is already a dotted quad, for instance. This is
|
|
|
|
# necessary to prevent calls to gethostbyaddr (which occurs on windows).
|
2010-10-04 14:49:19 +00:00
|
|
|
# These calls can be quite slow. This also fixes an issue with the
|
|
|
|
# Resolv.getaddress() call being non-functional on Ruby 1.9.1 (Win32).
|
2006-06-15 20:53:20 +00:00
|
|
|
#
|
2010-10-20 01:27:00 +00:00
|
|
|
def self.getaddress(addr, accept_ipv6 = true)
|
2008-11-09 22:59:21 +00:00
|
|
|
begin
|
2010-10-20 01:27:00 +00:00
|
|
|
if dotted_ip?(addr)
|
|
|
|
return addr
|
|
|
|
end
|
2011-09-26 17:42:07 +00:00
|
|
|
|
2010-10-20 01:27:00 +00:00
|
|
|
res = ::Socket.gethostbyname(addr)
|
|
|
|
return nil if not res
|
2011-09-26 17:42:07 +00:00
|
|
|
|
2010-10-20 01:27:00 +00:00
|
|
|
# Shift the first three elements out
|
|
|
|
rname = res.shift
|
|
|
|
ralias = res.shift
|
|
|
|
rtype = res.shift
|
2011-09-26 17:42:07 +00:00
|
|
|
|
|
|
|
# Reject IPv6 addresses if we don't accept them
|
2010-10-20 01:27:00 +00:00
|
|
|
if not accept_ipv6
|
|
|
|
res.reject!{|nbo| nbo.length != 4}
|
|
|
|
end
|
2011-09-26 17:42:07 +00:00
|
|
|
|
2010-10-20 01:27:00 +00:00
|
|
|
# Make sure we have at least one name
|
2011-09-26 17:42:07 +00:00
|
|
|
return nil if res.length == 0
|
|
|
|
|
2010-10-20 01:27:00 +00:00
|
|
|
# Return the first address of the result
|
|
|
|
self.addr_ntoa( res[0] )
|
2008-11-09 22:59:21 +00:00
|
|
|
rescue ::ArgumentError # Win32 bug
|
|
|
|
nil
|
|
|
|
end
|
2006-06-15 20:53:20 +00:00
|
|
|
end
|
|
|
|
|
2011-10-09 01:07:51 +00:00
|
|
|
#
|
|
|
|
# Wrapper for Resolv.getaddress that takes special care to see if the
|
|
|
|
# supplied address is already a dotted quad, for instance. This is
|
|
|
|
# necessary to prevent calls to gethostbyaddr (which occurs on windows).
|
|
|
|
# These calls can be quite slow. This also fixes an issue with the
|
|
|
|
# Resolv.getaddress() call being non-functional on Ruby 1.9.1 (Win32).
|
|
|
|
#
|
|
|
|
def self.getaddresses(addr, accept_ipv6 = true)
|
|
|
|
begin
|
|
|
|
if dotted_ip?(addr)
|
|
|
|
return addr
|
|
|
|
end
|
|
|
|
|
|
|
|
res = ::Socket.gethostbyname(addr)
|
|
|
|
return nil if not res
|
|
|
|
|
|
|
|
# Shift the first three elements out
|
|
|
|
rname = res.shift
|
|
|
|
ralias = res.shift
|
|
|
|
rtype = res.shift
|
|
|
|
|
|
|
|
# Reject IPv6 addresses if we don't accept them
|
|
|
|
if not accept_ipv6
|
|
|
|
res.reject!{|nbo| nbo.length != 4}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make sure we have at least one name
|
|
|
|
return nil if res.length == 0
|
|
|
|
|
|
|
|
# Return an array of all addresses
|
|
|
|
res.map{ |addr| self.addr_ntoa(addr) }
|
|
|
|
rescue ::ArgumentError # Win32 bug
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-15 20:53:20 +00:00
|
|
|
#
|
|
|
|
# Wrapper for Socket.gethostbyname which takes into account whether or not
|
|
|
|
# an IP address is supplied. If it is, then reverse DNS resolution does
|
|
|
|
# not occur. This is done in order to prevent delays, such as would occur
|
|
|
|
# on Windows.
|
|
|
|
#
|
|
|
|
def self.gethostbyname(host)
|
2007-04-29 23:43:39 +00:00
|
|
|
if (dotted_ip?(host))
|
|
|
|
if (is_ipv4?(host))
|
|
|
|
return [ host, host, 2, host.split('.').map{ |c| c.to_i }.pack("C4") ]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
::Socket.gethostbyname(host)
|
2006-06-15 20:53:20 +00:00
|
|
|
end
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
#
|
|
|
|
# Create a sockaddr structure using the supplied IP address, port, and
|
|
|
|
# address family
|
|
|
|
#
|
2006-06-08 21:02:58 +00:00
|
|
|
def self.to_sockaddr(ip, port)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-05-01 04:47:53 +00:00
|
|
|
if (ip == '::ffff:0.0.0.0')
|
|
|
|
ip = support_ipv6?() ? '::' : '0.0.0.0'
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ::Socket.pack_sockaddr_in(port, ip)
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
|
|
|
# Returns the address family, host, and port of the supplied sockaddr as
|
|
|
|
# [ af, host, port ]
|
|
|
|
#
|
|
|
|
def self.from_sockaddr(saddr)
|
2007-03-22 22:15:26 +00:00
|
|
|
port, host = ::Socket::unpack_sockaddr_in(saddr)
|
2007-04-29 23:43:39 +00:00
|
|
|
af = ::Socket::AF_INET
|
|
|
|
if (support_ipv6?() and is_ipv6?(host))
|
|
|
|
af = ::Socket::AF_INET6
|
|
|
|
end
|
2007-03-22 22:15:26 +00:00
|
|
|
return [ af, host, port ]
|
2005-06-04 06:19:42 +00:00
|
|
|
end
|
|
|
|
|
2005-07-08 14:53:12 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Resolves a host to raw network-byte order.
|
2007-04-29 23:43:39 +00:00
|
|
|
#
|
2005-07-08 14:53:12 +00:00
|
|
|
def self.resolv_nbo(host)
|
2006-06-15 20:53:20 +00:00
|
|
|
self.gethostbyname(Rex::Socket.getaddress(host))[3]
|
2005-07-08 14:53:12 +00:00
|
|
|
end
|
|
|
|
|
2011-10-09 01:07:51 +00:00
|
|
|
#
|
|
|
|
# Resolves a host to raw network-byte order.
|
|
|
|
#
|
|
|
|
def self.resolv_nbo_list(host)
|
|
|
|
Rex::Socket.getaddresses(host).map{|addr| self.gethostbyname(addr)[3] }
|
|
|
|
end
|
|
|
|
|
2005-10-27 04:14:02 +00:00
|
|
|
#
|
|
|
|
# Resolves a host to a network-byte order ruby integer.
|
|
|
|
#
|
2005-09-30 05:43:06 +00:00
|
|
|
def self.resolv_nbo_i(host)
|
2007-04-29 23:43:39 +00:00
|
|
|
addr_ntoi(resolv_nbo(host))
|
2005-09-30 05:43:06 +00:00
|
|
|
end
|
|
|
|
|
2011-10-09 01:07:51 +00:00
|
|
|
#
|
|
|
|
# Resolves a host to a list of network-byte order ruby integers.
|
|
|
|
#
|
|
|
|
def self.resolv_nbo_i_list(host)
|
|
|
|
resolv_nbo_list(host).map{|addr| addr_ntoi(addr) }
|
|
|
|
end
|
|
|
|
|
2011-09-26 17:42:07 +00:00
|
|
|
#
|
2010-07-03 01:27:24 +00:00
|
|
|
# Converts an ASCII IP address to a CIDR mask. Returns
|
|
|
|
# nil if it's not convertable.
|
2011-09-26 17:42:07 +00:00
|
|
|
#
|
2010-07-03 01:27:24 +00:00
|
|
|
def self.addr_atoc(mask)
|
2010-07-02 18:42:56 +00:00
|
|
|
mask_i = resolv_nbo_i(mask)
|
|
|
|
cidr = nil
|
|
|
|
0.upto(32) do |i|
|
|
|
|
if ((1 << i)-1) << (32-i) == mask_i
|
|
|
|
cidr = i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return cidr
|
|
|
|
end
|
|
|
|
|
2010-07-03 01:27:24 +00:00
|
|
|
#
|
|
|
|
# Resolves a CIDR bitmask into a dotted-quad. Returns
|
|
|
|
# nil if it's not convertable.
|
|
|
|
#
|
|
|
|
def self.addr_ctoa(cidr)
|
|
|
|
return nil unless (0..32) === cidr.to_i
|
|
|
|
addr_itoa(((1 << cidr)-1) << 32-cidr)
|
|
|
|
end
|
|
|
|
|
2005-10-27 04:14:02 +00:00
|
|
|
#
|
|
|
|
# Resolves a host to a dotted address.
|
|
|
|
#
|
|
|
|
def self.resolv_to_dotted(host)
|
2007-04-29 23:43:39 +00:00
|
|
|
addr_ntoa(addr_aton(host))
|
2005-10-27 04:14:02 +00:00
|
|
|
end
|
|
|
|
|
2005-09-30 05:43:06 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# Converts a ascii address into an integer
|
2005-09-30 05:43:06 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.addr_atoi(addr)
|
|
|
|
resolv_nbo_i(addr)
|
|
|
|
end
|
2005-09-30 05:43:06 +00:00
|
|
|
|
2011-10-09 01:07:51 +00:00
|
|
|
#
|
|
|
|
# Converts a ascii address into a list of addresses
|
|
|
|
#
|
|
|
|
def self.addr_atoi_list(addr)
|
|
|
|
resolv_nbo_i_list(addr)
|
|
|
|
end
|
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
#
|
|
|
|
# Converts an integer address into ascii
|
|
|
|
#
|
|
|
|
def self.addr_itoa(addr, v6=false)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
nboa = addr_iton(addr, v6)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
|
|
|
# IPv4
|
2007-04-29 23:43:39 +00:00
|
|
|
if (addr < 0x100000000 and not v6)
|
|
|
|
nboa.unpack('C4').join('.')
|
|
|
|
# IPv6
|
|
|
|
else
|
|
|
|
nboa.unpack('n8').map{ |c| "%.4x" % c }.join(":")
|
2009-12-08 18:32:26 +00:00
|
|
|
end
|
2005-09-30 05:43:06 +00:00
|
|
|
end
|
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# Converts a ascii address to network byte order
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.addr_aton(addr)
|
|
|
|
resolv_nbo(addr)
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# Converts a network byte order address to ascii
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.addr_ntoa(addr)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
|
|
|
# IPv4
|
2007-04-29 23:43:39 +00:00
|
|
|
if (addr.length == 4)
|
2008-07-23 17:39:02 +00:00
|
|
|
return addr.unpack('C4').join('.')
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
# IPv6
|
|
|
|
if (addr.length == 16)
|
|
|
|
return addr.unpack('n8').map{ |c| "%.4x" % c }.join(":")
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
|
|
|
raise RuntimeError, "Invalid address format"
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2007-04-29 23:43:39 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Converts a network byte order address to an integer
|
|
|
|
#
|
|
|
|
def self.addr_ntoi(addr)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
bits = addr.unpack("N*")
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
if (bits.length == 1)
|
|
|
|
return bits[0]
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
if (bits.length == 4)
|
|
|
|
val = 0
|
|
|
|
bits.each_index { |i| val += ( bits[i] << (96 - (i * 32)) ) }
|
|
|
|
return val
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
raise RuntimeError, "Invalid address format"
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Converts an integer into a network byte order address
|
|
|
|
#
|
|
|
|
def self.addr_iton(addr, v6=false)
|
|
|
|
if(addr < 0x100000000 and not v6)
|
|
|
|
return [addr].pack('N')
|
|
|
|
else
|
|
|
|
w = []
|
|
|
|
w[0] = (addr >> 96) & 0xffffffff
|
|
|
|
w[1] = (addr >> 64) & 0xffffffff
|
|
|
|
w[2] = (addr >> 32) & 0xffffffff
|
|
|
|
w[3] = addr & 0xffffffff
|
2009-12-08 18:32:26 +00:00
|
|
|
return w.pack('N4')
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
|
|
|
# Converts a CIDR subnet into an array (base, bcast)
|
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.cidr_crack(cidr, v6=false)
|
2006-08-12 08:31:38 +00:00
|
|
|
tmp = cidr.split('/')
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2008-08-23 07:05:38 +00:00
|
|
|
tst,scope = tmp[0].split("%",2)
|
|
|
|
scope = "%" + scope if scope
|
|
|
|
scope ||= ""
|
|
|
|
|
|
|
|
addr = addr_atoi(tst)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
bits = 32
|
|
|
|
mask = 0
|
|
|
|
use6 = false
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-05-01 04:54:18 +00:00
|
|
|
if (addr > 0xffffffff or v6 or cidr =~ /:/)
|
2007-04-29 23:43:39 +00:00
|
|
|
use6 = true
|
|
|
|
bits = 128
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
mask = (2 ** bits) - (2 ** (bits - tmp[1].to_i))
|
2006-08-12 08:31:38 +00:00
|
|
|
base = addr & mask
|
2007-04-29 23:43:39 +00:00
|
|
|
|
|
|
|
stop = base + (2 ** (bits - tmp[1].to_i)) - 1
|
2009-12-08 18:32:26 +00:00
|
|
|
return [self.addr_itoa(base, use6) + scope, self.addr_itoa(stop, use6) + scope]
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2007-04-29 23:43:39 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Converts a netmask (255.255.255.240) into a bitmask (28). This is the
|
|
|
|
# lame kid way of doing it.
|
|
|
|
#
|
|
|
|
def self.net2bitmask(netmask)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
nmask = resolv_nbo(netmask)
|
|
|
|
imask = addr_ntoi(nmask)
|
|
|
|
bits = 32
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
if (imask > 0xffffffff)
|
|
|
|
bits = 128
|
|
|
|
end
|
|
|
|
|
|
|
|
0.upto(bits-1) do |bit|
|
|
|
|
p = 2 ** bit
|
|
|
|
return (bits - bit) if ((imask & p) == p)
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
0
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-09-30 05:43:06 +00:00
|
|
|
#
|
|
|
|
# Converts a bitmask (28) into a netmask (255.255.255.240)
|
2007-04-29 23:43:39 +00:00
|
|
|
# TODO: IPv6 (use is ambiguous right now)
|
2005-09-30 05:43:06 +00:00
|
|
|
#
|
|
|
|
def self.bit2netmask(bitmask)
|
|
|
|
[ (~((2 ** (32 - bitmask)) - 1)) & 0xffffffff ].pack('N').unpack('CCCC').join('.')
|
|
|
|
end
|
|
|
|
|
2010-03-28 23:02:28 +00:00
|
|
|
|
|
|
|
def self.portspec_crack(pspec)
|
|
|
|
portspec_to_portlist(pspec)
|
|
|
|
end
|
|
|
|
|
2008-12-20 20:34:05 +00:00
|
|
|
#
|
|
|
|
# Converts a port specification like "80,21-23,443" into a sorted,
|
|
|
|
# unique array of valid port numbers like [21,22,23,80,443]
|
|
|
|
#
|
2010-03-28 23:02:28 +00:00
|
|
|
def self.portspec_to_portlist(pspec)
|
2008-12-20 20:34:05 +00:00
|
|
|
ports = []
|
|
|
|
|
|
|
|
# Build ports array from port specification
|
|
|
|
pspec.split(/,/).each do |item|
|
|
|
|
start, stop = item.split(/-/).map { |p| p.to_i }
|
|
|
|
|
|
|
|
start ||= 0
|
|
|
|
stop ||= item.match(/-/) ? 65535 : start
|
|
|
|
|
|
|
|
start, stop = stop, start if stop < start
|
|
|
|
|
|
|
|
start.upto(stop) { |p| ports << p }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sort, and remove dups and invalid ports
|
2010-03-28 23:02:28 +00:00
|
|
|
ports.sort.uniq.delete_if { |p| p < 1 or p > 65535 }
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Converts a port list like [1,2,3,4,5,100] into a
|
|
|
|
# range specification like "1-5,100"
|
|
|
|
#
|
|
|
|
def self.portlist_to_portspec(parr)
|
|
|
|
ranges = []
|
|
|
|
range = []
|
|
|
|
lastp = nil
|
|
|
|
|
|
|
|
parr.uniq.sort{|a,b| a<=>b}.map{|a| a.to_i}.each do |n|
|
|
|
|
next if (n < 1 or n > 65535)
|
|
|
|
if not lastp
|
|
|
|
range = [n]
|
|
|
|
lastp = n
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if lastp == n - 1
|
|
|
|
range << n
|
|
|
|
else
|
|
|
|
ranges << range
|
|
|
|
range = [n]
|
|
|
|
end
|
|
|
|
lastp = n
|
|
|
|
end
|
|
|
|
|
|
|
|
ranges << range
|
|
|
|
ranges.delete(nil)
|
|
|
|
ranges.uniq.map{|x| x.length == 1 ? "#{x[0]}" : "#{x[0]}-#{x[-1]}"}.join(",")
|
2008-12-20 20:34:05 +00:00
|
|
|
end
|
|
|
|
|
2005-11-24 00:08:09 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Utility class methods
|
|
|
|
#
|
|
|
|
##
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2010-12-20 22:13:49 +00:00
|
|
|
#
|
2011-09-26 17:42:07 +00:00
|
|
|
# This method does NOT send any traffic to the destination, instead, it uses a
|
2010-12-20 22:13:49 +00:00
|
|
|
# "bound" UDP socket to determine what source address we would use to
|
|
|
|
# communicate with the specified destination. The destination defaults to
|
|
|
|
# Google's DNS server to make the standard behavior determine which IP
|
|
|
|
# we would use to communicate with the internet.
|
|
|
|
#
|
|
|
|
def self.source_address(dest='8.8.8.8', comm = ::Rex::Socket::Comm::Local)
|
2006-01-30 21:57:24 +00:00
|
|
|
begin
|
2008-07-21 03:32:51 +00:00
|
|
|
s = self.create_udp(
|
2006-01-30 21:57:24 +00:00
|
|
|
'PeerHost' => dest,
|
2010-10-04 02:11:22 +00:00
|
|
|
'PeerPort' => 31337,
|
|
|
|
'Comm' => comm
|
2008-07-21 03:32:51 +00:00
|
|
|
)
|
|
|
|
r = s.getsockname[1]
|
|
|
|
s.close
|
2011-09-26 17:42:07 +00:00
|
|
|
|
2010-12-27 16:43:53 +00:00
|
|
|
# Trim off the trailing interface ID for link-local IPv6
|
|
|
|
return r.split('%').first
|
2006-01-30 21:57:24 +00:00
|
|
|
rescue ::Exception
|
|
|
|
return '127.0.0.1'
|
|
|
|
end
|
2010-02-09 16:45:46 +00:00
|
|
|
end
|
2011-09-26 17:42:07 +00:00
|
|
|
|
2010-12-27 16:43:53 +00:00
|
|
|
#
|
|
|
|
# Identifies the link-local address of a given interface (if IPv6 is enabled)
|
|
|
|
#
|
|
|
|
def self.ipv6_link_address(intf)
|
|
|
|
r = source_address("FF02::1%#{intf}")
|
|
|
|
return if not (r and r =~ /^fe80/i)
|
|
|
|
r
|
|
|
|
end
|
2011-09-26 17:42:07 +00:00
|
|
|
|
2010-12-27 16:43:53 +00:00
|
|
|
#
|
|
|
|
# Identifies the mac address of a given interface (if IPv6 is enabled)
|
2011-09-26 17:42:07 +00:00
|
|
|
#
|
2010-12-27 16:43:53 +00:00
|
|
|
def self.ipv6_mac(intf)
|
|
|
|
r = ipv6_link_address(intf)
|
|
|
|
return if not r
|
|
|
|
raw = addr_aton(r)[-8, 8]
|
|
|
|
(raw[0,3] + raw[5,3]).unpack("C*").map{|c| "%.2x" % c}.join(":")
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2010-02-09 16:45:46 +00:00
|
|
|
#
|
|
|
|
# Create a TCP socket pair.
|
|
|
|
#
|
2010-03-28 23:02:28 +00:00
|
|
|
# sf: This create a socket pair using native ruby sockets and will work
|
2010-02-06 17:59:25 +00:00
|
|
|
# on Windows where ::Socket.pair is not implemented.
|
|
|
|
# Note: OpenSSL requires native ruby sockets for its io.
|
2010-02-09 16:45:46 +00:00
|
|
|
#
|
2010-10-08 04:55:06 +00:00
|
|
|
# Note: Even though sub-threads are smashing the parent threads local, there
|
|
|
|
# is no concurrent use of the same locals and this is safe.
|
2010-02-09 16:45:46 +00:00
|
|
|
def self.tcp_socket_pair
|
2010-02-06 17:59:25 +00:00
|
|
|
lsock = nil
|
|
|
|
rsock = nil
|
2010-03-28 23:02:28 +00:00
|
|
|
laddr = '127.0.0.1'
|
2010-02-06 17:59:25 +00:00
|
|
|
lport = 0
|
|
|
|
threads = []
|
|
|
|
mutex = ::Mutex.new
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2010-11-12 06:19:49 +00:00
|
|
|
threads << Rex::ThreadFactory.spawn('TcpSocketPair', false) {
|
2010-02-06 17:59:25 +00:00
|
|
|
server = nil
|
|
|
|
mutex.synchronize {
|
2010-11-12 06:19:49 +00:00
|
|
|
threads << Rex::ThreadFactory.spawn('TcpSocketPairClient', false) {
|
2010-02-06 17:59:25 +00:00
|
|
|
mutex.synchronize {
|
|
|
|
rsock = ::TCPSocket.new( laddr, lport )
|
|
|
|
}
|
|
|
|
}
|
2010-12-02 23:49:02 +00:00
|
|
|
server = ::TCPServer.new(laddr, 0)
|
|
|
|
if (server.getsockname =~ /127\.0\.0\.1:/)
|
|
|
|
# JRuby ridiculousness
|
|
|
|
caddr, lport = server.getsockname.split(":")
|
|
|
|
caddr = caddr[1,caddr.length]
|
|
|
|
lport = lport.to_i
|
|
|
|
else
|
|
|
|
# Sane implementations where Socket#getsockname returns a
|
|
|
|
# sockaddr
|
|
|
|
lport, caddr = ::Socket.unpack_sockaddr_in( server.getsockname )
|
|
|
|
end
|
2010-02-06 17:59:25 +00:00
|
|
|
}
|
|
|
|
lsock, saddr = server.accept
|
|
|
|
server.close
|
|
|
|
}
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2010-02-06 17:59:25 +00:00
|
|
|
threads.each { |t| t.join }
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2010-02-06 17:59:25 +00:00
|
|
|
return [lsock, rsock]
|
2007-01-21 20:21:58 +00:00
|
|
|
end
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2010-02-09 16:45:46 +00:00
|
|
|
#
|
|
|
|
# Create a UDP socket pair using native ruby UDP sockets.
|
|
|
|
#
|
|
|
|
def self.udp_socket_pair
|
2010-03-28 23:02:28 +00:00
|
|
|
laddr = '127.0.0.1'
|
|
|
|
|
2010-02-09 16:45:46 +00:00
|
|
|
lsock = ::UDPSocket.new
|
|
|
|
lsock.bind( laddr, 0 )
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2010-02-09 16:45:46 +00:00
|
|
|
rsock = ::UDPSocket.new
|
|
|
|
rsock.bind( laddr, 0 )
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2010-02-09 16:45:46 +00:00
|
|
|
rsock.connect( *lsock.addr.values_at(3,1) )
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2010-02-09 16:45:46 +00:00
|
|
|
lsock.connect( *rsock.addr.values_at(3,1) )
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2010-02-09 16:45:46 +00:00
|
|
|
return [lsock, rsock]
|
|
|
|
end
|
2010-03-28 23:02:28 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Class initialization
|
|
|
|
#
|
|
|
|
##
|
2005-06-03 07:37:15 +00:00
|
|
|
|
2005-09-27 05:31:48 +00:00
|
|
|
#
|
|
|
|
# Initialize general socket parameters.
|
|
|
|
#
|
|
|
|
def initsock(params = nil)
|
2005-06-03 07:37:15 +00:00
|
|
|
if (params)
|
|
|
|
self.peerhost = params.peerhost
|
|
|
|
self.peerport = params.peerport
|
|
|
|
self.localhost = params.localhost
|
|
|
|
self.localport = params.localport
|
2005-11-24 18:50:33 +00:00
|
|
|
self.context = params.context || {}
|
2007-04-29 23:43:39 +00:00
|
|
|
self.ipv = params.v6 ? 6 : 4
|
2005-06-03 07:37:15 +00:00
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
2005-06-03 07:13:15 +00:00
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
# By default, all sockets are themselves selectable file descriptors.
|
2005-06-03 07:13:15 +00:00
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
def fd
|
|
|
|
self
|
2005-06-03 07:13:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
# Returns local connection information.
|
2005-06-03 07:13:15 +00:00
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
def getsockname
|
2009-12-08 18:32:26 +00:00
|
|
|
Socket.from_sockaddr(super)
|
2005-06-03 07:13:15 +00:00
|
|
|
end
|
|
|
|
|
2005-07-16 08:12:58 +00:00
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
# Wrapper around getsockname
|
2005-07-16 08:12:58 +00:00
|
|
|
#
|
|
|
|
def getlocalname
|
2005-09-27 05:31:48 +00:00
|
|
|
getsockname
|
2005-07-16 08:12:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Return peer connection information.
|
|
|
|
#
|
|
|
|
def getpeername
|
2005-09-27 05:31:48 +00:00
|
|
|
return Socket.from_sockaddr(super)
|
2005-07-16 08:12:58 +00:00
|
|
|
end
|
|
|
|
|
2006-08-15 04:07:25 +00:00
|
|
|
#
|
|
|
|
# Returns a string that indicates the type of the socket, such as 'tcp'.
|
|
|
|
#
|
|
|
|
def type?
|
|
|
|
raise NotImplementedError, "Socket type is not supported."
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The peer host of the connected socket.
|
|
|
|
#
|
|
|
|
attr_reader :peerhost
|
|
|
|
#
|
|
|
|
# The peer port of the connected socket.
|
|
|
|
#
|
|
|
|
attr_reader :peerport
|
|
|
|
#
|
|
|
|
# The local host of the connected socket.
|
|
|
|
#
|
|
|
|
attr_reader :localhost
|
|
|
|
#
|
|
|
|
# The local port of the connected socket.
|
|
|
|
#
|
|
|
|
attr_reader :localport
|
2005-11-24 18:50:33 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# The IP version of the socket
|
|
|
|
#
|
|
|
|
attr_reader :ipv
|
|
|
|
#
|
2005-11-24 18:50:33 +00:00
|
|
|
# Contextual information that describes the source and other
|
|
|
|
# instance-specific attributes. This comes from the param.context
|
|
|
|
# attribute.
|
|
|
|
#
|
|
|
|
attr_reader :context
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_writer :peerhost, :peerport, :localhost, :localport # :nodoc:
|
2005-11-24 18:50:33 +00:00
|
|
|
attr_writer :context # :nodoc:
|
2007-04-29 23:43:39 +00:00
|
|
|
attr_writer :ipv # :nodoc:
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2005-09-29 20:18:24 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Globalized socket constants
|
|
|
|
#
|
|
|
|
SHUT_RDWR = ::Socket::SHUT_RDWR
|
|
|
|
SHUT_RD = ::Socket::SHUT_RD
|
2008-11-09 22:59:21 +00:00
|
|
|
SHUT_WR = ::Socket::SHUT_WR
|
2009-12-08 18:32:26 +00:00
|
|
|
|