2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/socket'
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class represents the set of parameters that are used to create
|
|
|
|
# a socket, whether it be a server or client socket.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Rex::Socket::Parameters
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Factory
|
|
|
|
#
|
|
|
|
##
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Creates an instance of the Parameters class using the supplied hash.
|
|
|
|
#
|
2005-06-03 04:51:51 +00:00
|
|
|
def self.from_hash(hash)
|
|
|
|
return self.new(hash)
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Constructor
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-11-15 21:25:23 +00:00
|
|
|
# Initializes the attributes from the supplied hash. The following hash
|
|
|
|
# keys can be specified.
|
|
|
|
#
|
|
|
|
# PeerHost / PeerAddr
|
|
|
|
#
|
|
|
|
# The remote host to connect to.
|
|
|
|
#
|
|
|
|
# PeerPort
|
|
|
|
#
|
|
|
|
# The remote port to connect to.
|
|
|
|
#
|
|
|
|
# LocalHost / LocalAddr
|
|
|
|
#
|
|
|
|
# The local host to communicate from, if any.
|
|
|
|
#
|
|
|
|
# LocalPort
|
|
|
|
#
|
|
|
|
# The local port to communicate from, if any.
|
|
|
|
#
|
|
|
|
# Bare
|
|
|
|
#
|
|
|
|
# Create a bare socket.
|
|
|
|
#
|
|
|
|
# Server
|
|
|
|
#
|
|
|
|
# Whether or not this should be a server.
|
|
|
|
#
|
|
|
|
# SSL
|
|
|
|
#
|
|
|
|
# Whether or not SSL should be used.
|
|
|
|
#
|
2009-10-26 19:48:15 +00:00
|
|
|
# SSLVersion
|
|
|
|
#
|
|
|
|
# Specify SSL2, SSL3, or TLS1 (SSL3 is default)
|
2012-05-24 23:10:26 +00:00
|
|
|
#
|
2011-05-20 23:12:35 +00:00
|
|
|
# SSLCert
|
|
|
|
#
|
|
|
|
# A file containing an SSL certificate (for server sockets)
|
|
|
|
#
|
2008-12-19 09:33:16 +00:00
|
|
|
# Proxies
|
|
|
|
#
|
|
|
|
# List of proxies to use.
|
|
|
|
#
|
|
|
|
# Proto
|
|
|
|
#
|
|
|
|
# The underlying protocol to use.
|
|
|
|
#
|
|
|
|
# IPv6
|
|
|
|
#
|
|
|
|
# Force the use of IPv6.
|
|
|
|
#
|
2005-11-15 21:25:23 +00:00
|
|
|
# Comm
|
|
|
|
#
|
|
|
|
# The underlying Comm class to use to create the socket for this parameter
|
|
|
|
# set.
|
|
|
|
#
|
|
|
|
# Context
|
|
|
|
#
|
|
|
|
# A context hash that can allow users of this parameter class instance to
|
|
|
|
# determine who is responsible for requesting that a socket be created.
|
|
|
|
#
|
|
|
|
# Retries
|
|
|
|
#
|
2008-12-29 07:47:37 +00:00
|
|
|
# The number of times a connection should be retried.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2007-04-24 06:27:39 +00:00
|
|
|
# Timeout
|
|
|
|
#
|
|
|
|
# The number of seconds before a connection should time out
|
|
|
|
#
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
def initialize(hash)
|
|
|
|
if (hash['PeerHost'])
|
|
|
|
self.peerhost = hash['PeerHost']
|
|
|
|
elsif (hash['PeerAddr'])
|
2008-12-19 06:50:04 +00:00
|
|
|
self.peerhost = hash['PeerAddr']
|
2005-06-02 07:52:17 +00:00
|
|
|
else
|
|
|
|
self.peerhost = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if (hash['LocalHost'])
|
|
|
|
self.localhost = hash['LocalHost']
|
|
|
|
elsif (hash['LocalAddr'])
|
|
|
|
self.localhost = hash['LocalAddr']
|
|
|
|
else
|
|
|
|
self.localhost = '0.0.0.0'
|
|
|
|
end
|
|
|
|
|
|
|
|
if (hash['PeerPort'])
|
|
|
|
self.peerport = hash['PeerPort'].to_i
|
|
|
|
else
|
|
|
|
self.peerport = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if (hash['LocalPort'])
|
|
|
|
self.localport = hash['LocalPort'].to_i
|
|
|
|
else
|
|
|
|
self.localport = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if (hash['Bare'])
|
2005-06-03 22:51:09 +00:00
|
|
|
self.bare = hash['Bare']
|
2005-06-02 07:52:17 +00:00
|
|
|
else
|
|
|
|
self.bare = false
|
|
|
|
end
|
|
|
|
|
2005-11-18 00:26:19 +00:00
|
|
|
if (hash['SSL'] and hash['SSL'].to_s =~ /^(t|y|1)/i)
|
|
|
|
self.ssl = true
|
2005-06-03 22:51:09 +00:00
|
|
|
else
|
|
|
|
self.ssl = false
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2009-10-26 19:48:15 +00:00
|
|
|
if (hash['SSLVersion'] and hash['SSLVersion'].to_s =~ /^(SSL2|SSL3|TLS1)$/i)
|
|
|
|
self.ssl_version = hash['SSLVersion']
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-05-20 23:12:35 +00:00
|
|
|
if (hash['SSLCert'] and ::File.file?(hash['SSLCert']))
|
|
|
|
begin
|
|
|
|
self.ssl_cert = ::File.read(hash['SSLCert'])
|
|
|
|
rescue ::Exception => e
|
|
|
|
elog("Failed to read cert: #{e.class}: #{e}", LogSource)
|
|
|
|
end
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
|
|
|
if hash['Proxies']
|
2006-01-27 05:33:08 +00:00
|
|
|
self.proxies = hash['Proxies'].split('-').map{|a| a.strip}.map{|a| a.split(':').map{|b| b.strip}}
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
# The protocol this socket will be using
|
2005-06-03 04:51:51 +00:00
|
|
|
if (hash['Proto'])
|
|
|
|
self.proto = hash['Proto'].downcase
|
|
|
|
else
|
|
|
|
self.proto = 'tcp'
|
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
# Whether or not the socket should be a server
|
|
|
|
self.server = hash['Server'] || false
|
|
|
|
|
|
|
|
# The communication subsystem to use to create the socket
|
2005-09-30 05:48:45 +00:00
|
|
|
self.comm = hash['Comm']
|
|
|
|
|
2005-11-15 21:25:23 +00:00
|
|
|
# The context that was passed in, if any.
|
|
|
|
self.context = hash['Context'] || {}
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-30 05:48:45 +00:00
|
|
|
# If no comm was supplied, try to use the comm that is best fit to
|
|
|
|
# handle the provided host based on the current routing table.
|
2010-02-06 17:59:25 +00:00
|
|
|
if( self.server )
|
|
|
|
if (self.comm == nil and self.localhost)
|
|
|
|
self.comm = Rex::Socket::SwitchBoard.best_comm(self.localhost)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if (self.comm == nil and self.peerhost)
|
|
|
|
self.comm = Rex::Socket::SwitchBoard.best_comm(self.peerhost)
|
|
|
|
end
|
2005-09-30 05:48:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If we still haven't found a comm, we default to the local comm.
|
|
|
|
self.comm = Rex::Socket::Comm::Local if (self.comm == nil)
|
2012-05-24 23:10:26 +00:00
|
|
|
|
|
|
|
# If we are a UDP server, turn off the server flag as it was only set when
|
2010-02-09 16:45:46 +00:00
|
|
|
# creating the UDP socket in order to avail of the switch board above.
|
|
|
|
if( self.server and self.proto == 'udp' )
|
|
|
|
self.server = false
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
# The number of connection retries to make (client only)
|
2008-12-29 05:04:49 +00:00
|
|
|
if hash['Retries']
|
|
|
|
self.retries = hash['Retries'].to_i
|
|
|
|
else
|
|
|
|
self.retries = 0
|
|
|
|
end
|
2007-04-24 06:27:39 +00:00
|
|
|
|
|
|
|
# The number of seconds before a connect attempt times out (client only)
|
2008-12-29 05:04:49 +00:00
|
|
|
if hash['Timeout']
|
|
|
|
self.timeout = hash['Timeout'].to_i
|
|
|
|
else
|
|
|
|
self.timeout = 5
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
# Whether to force IPv6 addressing
|
|
|
|
self.v6 = hash['IPv6'] || false
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Conditionals
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns true if this represents parameters for a server.
|
|
|
|
#
|
2005-06-02 07:52:17 +00:00
|
|
|
def server?
|
|
|
|
return (server == true)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns true if this represents parameters for a client.
|
|
|
|
#
|
2005-06-02 07:52:17 +00:00
|
|
|
def client?
|
|
|
|
return (server == false)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns true if the protocol for the parameters is TCP.
|
|
|
|
#
|
2005-06-02 07:52:17 +00:00
|
|
|
def tcp?
|
2005-06-03 04:51:51 +00:00
|
|
|
return (proto == 'tcp')
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns true if the protocol for the parameters is UDP.
|
|
|
|
#
|
2005-06-02 07:52:17 +00:00
|
|
|
def udp?
|
2005-06-03 04:51:51 +00:00
|
|
|
return (proto == 'udp')
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
2008-07-22 19:03:59 +00:00
|
|
|
#
|
|
|
|
# Returns true if the protocol for the parameters is IP.
|
|
|
|
#
|
|
|
|
def ip?
|
|
|
|
return (proto == 'ip')
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns true if the socket is a bare socket that does not inherit from
|
|
|
|
# any extended Rex classes.
|
|
|
|
#
|
2005-06-02 07:52:17 +00:00
|
|
|
def bare?
|
|
|
|
return (bare == true)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns true if SSL has been requested.
|
|
|
|
#
|
2005-06-03 22:51:09 +00:00
|
|
|
def ssl?
|
|
|
|
return ssl
|
|
|
|
end
|
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
#
|
|
|
|
# Returns true if IPv6 has been enabled
|
|
|
|
#
|
|
|
|
def v6?
|
|
|
|
return v6
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Attributes
|
|
|
|
#
|
|
|
|
##
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# The remote host information, equivalent to the PeerHost parameter hash
|
|
|
|
# key.
|
|
|
|
#
|
|
|
|
attr_accessor :peerhost
|
|
|
|
#
|
|
|
|
# The remote port. Equivalent to the PeerPort parameter hash key.
|
|
|
|
#
|
|
|
|
attr_accessor :peerport
|
|
|
|
#
|
|
|
|
# The local host. Equivalent to the LocalHost parameter hash key.
|
|
|
|
#
|
|
|
|
attr_accessor :localhost
|
|
|
|
#
|
|
|
|
# The local port. Equivalent to the LocalPort parameter hash key.
|
|
|
|
#
|
|
|
|
attr_accessor :localport
|
|
|
|
#
|
|
|
|
# The protocol to to use, such as TCP. Equivalent to the Proto parameter
|
|
|
|
# hash key.
|
|
|
|
#
|
|
|
|
attr_accessor :proto
|
|
|
|
#
|
|
|
|
# Whether or not this is a server. Equivalent to the Server parameter hash
|
|
|
|
# key.
|
|
|
|
#
|
|
|
|
attr_accessor :server
|
|
|
|
#
|
|
|
|
# The Comm class that should be used to create the underlying socket.
|
|
|
|
#
|
|
|
|
attr_accessor :comm
|
|
|
|
#
|
2005-11-15 21:25:23 +00:00
|
|
|
# The context hash that was passed in to the structure.
|
|
|
|
#
|
|
|
|
attr_accessor :context
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# The number of attempts that should be made.
|
|
|
|
#
|
|
|
|
attr_accessor :retries
|
|
|
|
#
|
2007-04-24 06:27:39 +00:00
|
|
|
# The number of seconds before a connection attempt should time out.
|
|
|
|
#
|
|
|
|
attr_accessor :timeout
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Whether or not this is a bare (non-extended) socket instance that should
|
|
|
|
# be created.
|
|
|
|
#
|
|
|
|
attr_accessor :bare
|
|
|
|
#
|
|
|
|
# Whether or not SSL should be used to wrap the connection.
|
|
|
|
#
|
|
|
|
attr_accessor :ssl
|
2007-04-29 23:43:39 +00:00
|
|
|
#
|
2009-10-26 19:48:15 +00:00
|
|
|
# What version of SSL to use (SSL2, SSL3, TLS1)
|
|
|
|
#
|
|
|
|
attr_accessor :ssl_version
|
|
|
|
#
|
2011-05-20 23:12:35 +00:00
|
|
|
# The SSL certificate, in pem format, stored as a string. See +SslTcpServer#make_ssl+
|
|
|
|
#
|
|
|
|
attr_accessor :ssl_cert
|
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# Whether we should use IPv6
|
|
|
|
#
|
|
|
|
attr_accessor :v6
|
2005-06-02 07:52:17 +00:00
|
|
|
|
2005-12-18 02:19:21 +00:00
|
|
|
|
2006-01-27 05:33:08 +00:00
|
|
|
attr_accessor :proxies
|
2005-12-18 02:19:21 +00:00
|
|
|
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Synonyms
|
|
|
|
#
|
|
|
|
##
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
alias peeraddr peerhost
|
|
|
|
alias localaddr localhost
|
|
|
|
|
2008-12-19 06:50:04 +00:00
|
|
|
end
|