2005-06-02 07:52:17 +00:00
|
|
|
require 'socket'
|
|
|
|
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
|
|
|
|
2005-06-03 04:51:51 +00:00
|
|
|
module Comm
|
|
|
|
end
|
|
|
|
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/socket/parameters'
|
2005-07-24 20:53:54 +00:00
|
|
|
require 'rex/socket/tcp'
|
|
|
|
require 'rex/socket/tcp_server'
|
2005-09-30 05:59:44 +00:00
|
|
|
|
|
|
|
require 'rex/socket/comm'
|
2005-07-24 20:53:54 +00:00
|
|
|
require 'rex/socket/comm/local'
|
2005-09-30 05:59:44 +00:00
|
|
|
|
2005-09-30 05:48:45 +00:00
|
|
|
require 'rex/socket/switch_board'
|
2005-10-27 04:14:02 +00:00
|
|
|
require 'rex/socket/subnet_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
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Serialization
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2006-06-08 21:02:58 +00:00
|
|
|
#
|
|
|
|
# Determine whether this is an IPv4 address
|
|
|
|
#
|
|
|
|
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
|
|
|
|
|
|
|
|
#
|
|
|
|
# Determine whether this is an IPv6 address
|
|
|
|
#
|
|
|
|
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
|
|
|
#
|
|
|
|
# Checks to see if the supplied address is a dotted quad.
|
|
|
|
# TODO: IPV6
|
|
|
|
#
|
|
|
|
def self.dotted_ip?(addr)
|
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
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
def self.getaddress(addr)
|
2006-06-27 23:00:49 +00:00
|
|
|
dotted_ip?(addr) ? addr : Resolv.getaddress(addr)
|
2006-06-15 20:53:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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)
|
2006-06-16 03:03:40 +00:00
|
|
|
dotted_ip?(host) ? [ host, host, 2, host.split('.').map{ |o| o.to_i }.pack('C*') ] : ::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)
|
2005-06-02 07:52:17 +00:00
|
|
|
ip = "0.0.0.0" unless ip
|
2006-06-15 20:53:20 +00:00
|
|
|
ip = Rex::Socket.getaddress(ip)
|
2006-06-08 21:02:58 +00:00
|
|
|
af = ip.match(/:/) ? ::Socket::AF_INET6 : ::Socket::AF_INET
|
2006-07-31 19:22:21 +00:00
|
|
|
|
2006-06-08 21:02:58 +00:00
|
|
|
if (af == ::Socket::AF_INET6)
|
2006-06-15 20:53:20 +00:00
|
|
|
data = [af, port.to_i, 0, self.gethostbyname(ip)[3], 0]
|
2006-06-08 21:02:58 +00:00
|
|
|
return data.pack('snNa16N')
|
|
|
|
end
|
|
|
|
|
2006-07-31 19:22:21 +00:00
|
|
|
# Mac OS X returns the wrong AF
|
|
|
|
if (true or af == ::Socket::AF_INET)
|
|
|
|
data = [ af, port.to_i ] + ip.split('.').collect { |o| o.to_i } + [ "" ]
|
|
|
|
return data.pack('snCCCCa8')
|
|
|
|
end
|
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)
|
2006-06-16 03:11:53 +00:00
|
|
|
up = saddr.unpack('snA*')
|
2005-06-04 06:19:42 +00:00
|
|
|
af = up.shift
|
|
|
|
port = up.shift
|
|
|
|
|
2006-06-08 21:02:58 +00:00
|
|
|
case af
|
|
|
|
when ::Socket::AF_INET6
|
2006-06-16 03:56:08 +00:00
|
|
|
return [ af, up.shift[0,16].unpack('H*').gsub(/(....)/){ |r| r << ':' }.sub(/:$/, ''), port ]
|
2006-07-31 19:22:21 +00:00
|
|
|
|
|
|
|
# Mac OS X returns the wrong AF
|
|
|
|
# when ::Socket::AF_INET
|
|
|
|
else
|
|
|
|
return [ af, up.shift[0, 4].unpack('C*').join('.'), port ]
|
2006-06-08 21:02:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
raise RuntimeError, "Invalid address family"
|
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.
|
2006-06-08 21:02:58 +00:00
|
|
|
# TODO: All this to work with IPV6 sockets
|
|
|
|
|
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
|
|
|
|
|
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)
|
2006-06-08 21:02:58 +00:00
|
|
|
ret = resolv_nbo(host).unpack('N*')
|
|
|
|
case ret.length
|
|
|
|
when 1
|
|
|
|
return ret[0]
|
|
|
|
when 4
|
|
|
|
val = 0
|
|
|
|
ret.each_index { |i| val += ( ret[i] << (96 - (i * 32)) ) }
|
|
|
|
return val
|
|
|
|
else
|
|
|
|
raise RuntimeError, "Invalid address format"
|
|
|
|
end
|
2005-09-30 05:43:06 +00:00
|
|
|
end
|
|
|
|
|
2005-10-27 04:14:02 +00:00
|
|
|
#
|
|
|
|
# Resolves a host to a dotted address.
|
|
|
|
#
|
|
|
|
def self.resolv_to_dotted(host)
|
2006-06-15 20:53:20 +00:00
|
|
|
Rex::Socket.getaddress(host)
|
2005-10-27 04:14:02 +00:00
|
|
|
end
|
|
|
|
|
2005-09-30 05:43:06 +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)
|
|
|
|
raw = resolv_nbo(netmask).unpack('N')[0]
|
|
|
|
|
|
|
|
0.upto(31) { |bit|
|
|
|
|
p = 2 ** bit
|
|
|
|
return (32 - bit) if ((raw & p) == p)
|
|
|
|
}
|
|
|
|
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Converts a bitmask (28) into a netmask (255.255.255.240)
|
|
|
|
#
|
|
|
|
def self.bit2netmask(bitmask)
|
|
|
|
[ (~((2 ** (32 - bitmask)) - 1)) & 0xffffffff ].pack('N').unpack('CCCC').join('.')
|
|
|
|
end
|
|
|
|
|
2005-11-24 00:08:09 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Utility class methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
def self.source_address(dest='1.2.3.4')
|
2006-01-30 21:57:24 +00:00
|
|
|
begin
|
|
|
|
return self.create_udp(
|
|
|
|
'PeerHost' => dest,
|
|
|
|
'PeerPort' => 31337
|
|
|
|
).getsockname[1]
|
|
|
|
rescue ::Exception
|
|
|
|
return '127.0.0.1'
|
|
|
|
end
|
2005-11-24 00:08:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
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 || {}
|
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
|
|
|
|
return 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
|
|
|
|
|
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
|
|
|
#
|
|
|
|
# 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:
|
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
|
|
|
|
SHUT_WR = ::Socket::SHUT_WR
|