Method: Rex::Socket::Parameters#initialize

Defined in:
lib/rex/socket/parameters.rb

#initialize(hash) ⇒ Parameters

Initializes the attributes from the supplied hash. The following hash keys can be specified.

Options Hash (hash):

  • 'PeerHost' (String)

    The remote host to connect to

  • 'PeerAddr' (String) — default: alias for 'PeerHost'
  • 'PeerPort' (Fixnum)

    The remote port to connect to

  • 'LocalHost' (String)

    The local host to communicate from, if any

  • 'LocalPort' (String)

    The local port to communicate from, if any

  • 'Bool' (Bool)

    Create a bare socket

  • 'Server' (Bool)

    Whether or not this should be a server

  • 'SSL' (Bool)

    Whether or not SSL should be used

  • 'SSLContext' (OpenSSL::SSL::SSLContext)

    Use a pregenerated SSL Context

  • 'SSLVersion' (String)

    Specify Auto, SSL2, SSL3, or TLS1 (Auto is default)

  • 'SSLCert' (String)

    A file containing an SSL certificate (for server sockets)

  • 'SSLCipher' (String)
  • 'SSLCompression' (Bool)

    enable SSL-level compression where available

  • 'SSLVerifyMode' (String)

    SSL certificate verification mechanism. One of 'NONE' (default), 'CLIENT_ONCE', 'FAIL_IF_NO_PEER_CERT ', 'PEER'

  • 'Proxies' (String)

    List of proxies to use.

  • 'Proto' (String)

    The underlying protocol to use.

  • 'IPv6' (String)

    Force the use of IPv6.

  • 'Comm' (String)

    The underlying Comm object to use to create the socket for this parameter set.

  • 'Context' (Hash)

    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' (String)

    The number of times a connection should be retried.

  • 'Timeout' (Fixnum)

    The number of seconds before a connection should time out



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rex/socket/parameters.rb', line 80

def initialize(hash)
  if (hash['PeerHost'])
    self.peerhost = hash['PeerHost']
  elsif (hash['PeerAddr'])
    self.peerhost = hash['PeerAddr']
  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'])
    self.bare = hash['Bare']
  else
    self.bare = false
  end

  if (hash['SSL'] and hash['SSL'].to_s =~ /^(t|y|1)/i)
    self.ssl = true
  else
    self.ssl = false
  end

  if hash['SSLContext']
    self.sslctx = hash['SSLContext']
  end

  supported_ssl_versions = ['Auto', 'SSL2', 'SSL23', 'TLS1', 'SSL3', :Auto, :SSLv2, :SSLv3, :SSLv23, :TLSv1]
  if (hash['SSLVersion'] and supported_ssl_versions.include? hash['SSLVersion'])
    self.ssl_version = hash['SSLVersion']
  end

  supported_ssl_verifiers = %W{CLIENT_ONCE FAIL_IF_NO_PEER_CERT NONE PEER}
  if (hash['SSLVerifyMode'] and supported_ssl_verifiers.include? hash['SSLVerifyMode'])
    self.ssl_verify_mode = hash['SSLVerifyMode']
  end

  if hash['SSLCompression']
    self.ssl_compression = hash['SSLCompression']
  end

  if (hash['SSLCipher'])
    self.ssl_cipher = hash['SSLCipher']
  end

  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

  if hash['Proxies']
    self.proxies = hash['Proxies'].split('-').map{|a| a.strip}.map{|a| a.split(':').map{|b| b.strip}}
  end

  # The protocol this socket will be using
  if (hash['Proto'])
    self.proto = hash['Proto'].downcase
  else
    self.proto = 'tcp'
  end

  # Whether or not the socket should be a server
  self.server    = hash['Server'] || false

  # The communication subsystem to use to create the socket
  self.comm      = hash['Comm']

  # The context that was passed in, if any.
  self.context   = hash['Context'] || {}

  # 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.
  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
  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)

  # If we are a UDP server, turn off the server flag as it was only set when
  # creating the UDP socket in order to avail of the switch board above.
  if( self.server and self.proto == 'udp' )
    self.server = false
  end

  # The number of connection retries to make (client only)
  if hash['Retries']
    self.retries = hash['Retries'].to_i
  else
    self.retries = 0
  end

  # The number of seconds before a connect attempt times out (client only)
  if hash['Timeout']
    self.timeout = hash['Timeout'].to_i
  else
    self.timeout = 5
  end

  # Whether to force IPv6 addressing
  self.v6        = hash['IPv6'] || false
end