Class: Rex::Socket::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/socket/parameters.rb

Overview

This class represents the set of parameters that are used to create a socket, whether it be a server or client socket.

Examples:

nsock = Rex::Socket::Tcp.create(
  'PeerHost'  =>  opts['RHOST'] || rhost,
  'PeerPort'  => (opts['RPORT'] || rport).to_i,
  'LocalHost' =>  opts['CHOST'] || chost || "0.0.0.0",
  'LocalPort' => (opts['CPORT'] || cport || 0).to_i,
  'SSL'       =>  dossl,
  'SSLVersion'=>  opts['SSLVersion'] || ssl_version,
  'Proxies'   => proxies,
  'Timeout'   => (opts['ConnectTimeout'] || connect_timeout || 10).to_i,
  'Context'   =>
    {
      'Msf'        => framework,
      'MsfExploit' => self,
    })

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Parameters

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

Parameters:

  • hash (Hash) (defaults to: {})

    a customizable set of options

Options Hash (hash):

  • 'PeerHost' (String)

    The remote host to connect to

  • 'PeerHostname' (String)

    The unresolved remote hostname, used to specify Server Name Indication (SNI)

  • 'SSLKeyLogFile' (String)

    The SSL key log file path, used for network capture decryption which is useful to decrypt TLS traffic in wireshark

  • '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

  • 'Interface' (String)

    The network interface name to bind the socket to (e.g. 'eth0'). Only honoured by the local Comm; raises Rex::BindFailed if combined with a proxy.



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
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rex/socket/parameters.rb', line 87

def initialize(hash = {})
  if (hash['PeerHost'])
    self.peerhost = hash['PeerHost']
  elsif (hash['PeerAddr'])
    self.peerhost = hash['PeerAddr']
  end

  if (hash['PeerHostname'])
    self.peerhostname = hash['PeerHostname']
  end

  if (hash['LocalHost'])
    self.localhost = hash['LocalHost']
  elsif (hash['LocalAddr'])
    self.localhost = hash['LocalAddr']
  end

  if (hash['PeerPort'])
    self.peerport = hash['PeerPort'].to_i
  end

  if (hash['LocalPort'])
    self.localport = hash['LocalPort'].to_i
  end

  if (hash['Bare'])
    self.bare = hash['Bare']
  end

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

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

  if (hash['SSLKeyLogFile'])
    self.sslkeylogfile = hash['SSLKeyLogFile']
  end

  self.ssl_version = hash.fetch('SSLVersion', nil)

  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['VHOST'])
    self.ssl_cn = hash['VHOST']
  end

  if (hash['SSLCommonName'])
    self.ssl_cn = hash['SSLCommonName']
  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['SSLClientCert'] and ::File.file?(hash['SSLClientCert']))
    begin
      self.ssl_client_cert = ::File.read(hash['SSLClientCert'])
    rescue ::Exception => e
      elog("Failed to read client cert: #{e.class}: #{e}", LogSource)
    end
  end

  if (hash['SSLClientKey'] and ::File.file?(hash['SSLClientKey']))
    begin
      self.ssl_client_key = ::File.read(hash['SSLClientKey'])
    rescue ::Exception => e
      elog("Failed to read client key: #{e.class}: #{e}", LogSource)
    end
  end

  if hash['Proxies']
    self.proxies = Rex::Socket::Proxies.parse(hash['Proxies'])
  end

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

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

  # 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 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
  end

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

  self.interface = hash['Interface'].to_s.strip if hash['Interface'] && !hash['Interface'].strip.empty?

  # Whether to force IPv6 addressing
  if hash['IPv6'].nil?
    # if IPv6 isn't specified and at least one host is an IPv6 address and the
    # other is either nil, a hostname or an IPv6 address, then use IPv6
    self.v6 = (Rex::Socket.is_ipv6?(self.localhost) || Rex::Socket.is_ipv6?(self.peerhost)) && \
      (self.localhost.nil? || !Rex::Socket.is_ipv4?(self.localhost)) && \
      (self.peerhost.nil? || !Rex::Socket.is_ipv4?(self.peerhost))
  else
    self.v6 = hash['IPv6']
  end
end

Instance Attribute Details

#bareObject



410
411
412
# File 'lib/rex/socket/parameters.rb', line 410

def bare
  @comm || false
end

#commObject



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/rex/socket/parameters.rb', line 368

def comm
  return @comm unless @comm.nil?

  best_comm = nil
  # If no comm was explicitly specified, try to use the comm that is best fit
  # to handle the provided host based on the current routing table.
  if proxies?
    best_comm = Rex::Socket::Comm::Local
  elsif server && localhost
    best_comm = Rex::Socket::SwitchBoard.best_comm(localhost)
  elsif peerhost
    best_comm =  Rex::Socket::SwitchBoard.best_comm(peerhost)
  end

  best_comm || Rex::Socket::Comm::Local
end

#contextObject



388
389
390
# File 'lib/rex/socket/parameters.rb', line 388

def context
  @context || {}
end

#interfaceString?

The network interface name to bind the socket to (e.g. ‘eth0’). nil means no binding.

Returns:

  • (String, nil)


496
497
498
# File 'lib/rex/socket/parameters.rb', line 496

def interface
  @interface
end

#localhostObject Also known as: localaddr



332
333
334
335
336
337
338
339
340
# File 'lib/rex/socket/parameters.rb', line 332

def localhost
  return @localhost if @localhost

  if @v6 || (@peerhost && Rex::Socket.is_ipv6?(@peerhost))
    '::'
  else
    '0.0.0.0'
  end
end

#localportObject



345
346
347
# File 'lib/rex/socket/parameters.rb', line 345

def localport
  @localport || 0
end

#peerhostString Also known as: peeraddr

The remote host information, equivalent to the PeerHost parameter hash key.

Returns:

  • (String)


309
310
311
# File 'lib/rex/socket/parameters.rb', line 309

def peerhost
  @peerhost
end

#peerhostnameString

The remote hostname information, equivalent to the PeerHostname parameter hash key.

Returns:

  • (String)


314
315
316
# File 'lib/rex/socket/parameters.rb', line 314

def peerhostname
  @peerhostname
end

#peerportObject



324
325
326
# File 'lib/rex/socket/parameters.rb', line 324

def peerport
  @peerport || 0
end

#protoObject



353
354
355
# File 'lib/rex/socket/parameters.rb', line 353

def proto
  @proto || 'tcp'
end

#proxiesArray

List of proxies to use

Returns:

  • (Array)


492
493
494
# File 'lib/rex/socket/parameters.rb', line 492

def proxies
  @proxies
end

#retriesObject



395
396
397
# File 'lib/rex/socket/parameters.rb', line 395

def retries
  @retries || 0
end

#serverObject



361
362
363
# File 'lib/rex/socket/parameters.rb', line 361

def server
  @server || false
end

#sslObject



417
418
419
# File 'lib/rex/socket/parameters.rb', line 417

def ssl
  @ssl || false
end

#ssl_certString

The SSL certificate, in pem format, stored as a string. See SslTcpServer#makessl

Returns:

  • (String)


462
463
464
# File 'lib/rex/socket/parameters.rb', line 462

def ssl_cert
  @ssl_cert
end

#ssl_cipherString, Array

What specific SSL Cipher(s) to use, may be a string containing the cipher name or an array of strings containing cipher names e.g.

“DHE-RSA-AES256-SHA”, “DHE-DSS-AES256-SHA”

Returns:

  • (String, Array)


453
454
455
# File 'lib/rex/socket/parameters.rb', line 453

def ssl_cipher
  @ssl_cipher
end

#ssl_client_certObject

The client SSL certificate



471
472
473
# File 'lib/rex/socket/parameters.rb', line 471

def ssl_client_cert
  @ssl_client_cert
end

#ssl_client_keyObject

The client SSL key



476
477
478
# File 'lib/rex/socket/parameters.rb', line 476

def ssl_client_key
  @ssl_client_key
end

#ssl_cnString

Which Common Name to use for certificate

Returns:

  • (String)


457
458
459
# File 'lib/rex/socket/parameters.rb', line 457

def ssl_cn
  @ssl_cn
end

#ssl_compressionBool

Enables SSL/TLS-level compression

Returns:

  • (Bool)


466
467
468
# File 'lib/rex/socket/parameters.rb', line 466

def ssl_compression
  @ssl_compression
end

#ssl_verify_modeObject

SSL certificate verification mode for SSL context



480
481
482
# File 'lib/rex/socket/parameters.rb', line 480

def ssl_verify_mode
  @ssl_verify_mode
end

#ssl_versionString, Symbol

What version of SSL to use (Auto, SSL2, SSL3, SSL23, TLS1)

Returns:

  • (String, Symbol)


427
428
429
# File 'lib/rex/socket/parameters.rb', line 427

def ssl_version
  @ssl_version
end

#sslctxOpenSSL::SSL::SSLContext

Pre configured SSL Context to use

Returns:

  • (OpenSSL::SSL::SSLContext)


423
424
425
# File 'lib/rex/socket/parameters.rb', line 423

def sslctx
  @sslctx
end

#sslkeylogfileString

The SSL key log file path, equivalent to the sslkeylogfile parameter hash key.

Returns:

  • (String)


319
320
321
# File 'lib/rex/socket/parameters.rb', line 319

def sslkeylogfile
  @sslkeylogfile
end

#timeoutObject



402
403
404
# File 'lib/rex/socket/parameters.rb', line 402

def timeout
  @timeout || 5
end

#v6Object



486
487
488
# File 'lib/rex/socket/parameters.rb', line 486

def v6
  @v6 || false
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the Parameters class using the supplied hash.



38
39
40
# File 'lib/rex/socket/parameters.rb', line 38

def self.from_hash(hash)
  return self.new(hash)
end

Instance Method Details

#bare?Boolean

Returns true if the socket is a bare socket that does not inherit from any extended Rex classes.

Returns:

  • (Boolean)


282
283
284
# File 'lib/rex/socket/parameters.rb', line 282

def bare?
  return (bare == true)
end

#client?Boolean

Returns true if this represents parameters for a client.

Returns:

  • (Boolean)


253
254
255
# File 'lib/rex/socket/parameters.rb', line 253

def client?
  return (server == false)
end

#ip?Boolean

Returns true if the protocol for the parameters is IP.

Returns:

  • (Boolean)


274
275
276
# File 'lib/rex/socket/parameters.rb', line 274

def ip?
  return (proto == 'ip')
end

#merge(other) ⇒ Object



223
224
225
# File 'lib/rex/socket/parameters.rb', line 223

def merge(other)
  self.dup.merge!(other)
end

#merge!(other) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/rex/socket/parameters.rb', line 227

def merge!(other)
  other = self.class.new(other) if other.is_a? Hash

  other.instance_variables.each do |name|
    value = other.instance_variable_get(name)
    instance_variable_set(name, value) unless value.nil?
  end
  self
end

#proxies?Boolean

Returns:

  • (Boolean)


498
499
500
# File 'lib/rex/socket/parameters.rb', line 498

def proxies?
  proxies && !proxies.empty?
end

#server?Boolean

Returns true if this represents parameters for a server.

Returns:

  • (Boolean)


246
247
248
# File 'lib/rex/socket/parameters.rb', line 246

def server?
  return (server == true)
end

#ssl?Boolean

Returns true if SSL has been requested.

Returns:

  • (Boolean)


289
290
291
# File 'lib/rex/socket/parameters.rb', line 289

def ssl?
  return ssl
end

#tcp?Boolean

Returns true if the protocol for the parameters is TCP.

Returns:

  • (Boolean)


260
261
262
# File 'lib/rex/socket/parameters.rb', line 260

def tcp?
  return (proto == 'tcp')
end

#udp?Boolean

Returns true if the protocol for the parameters is UDP.

Returns:

  • (Boolean)


267
268
269
# File 'lib/rex/socket/parameters.rb', line 267

def udp?
  return (proto == 'udp')
end

#v6?Boolean

Returns true if IPv6 has been enabled

Returns:

  • (Boolean)


296
297
298
# File 'lib/rex/socket/parameters.rb', line 296

def v6?
  return v6
end