2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/socket'
|
2005-06-03 22:51:09 +00:00
|
|
|
###
|
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
# This class provides methods for interacting with an SSL TCP client
|
|
|
|
# connection.
|
2005-06-03 22:51:09 +00:00
|
|
|
#
|
|
|
|
###
|
2005-09-27 05:31:48 +00:00
|
|
|
module Rex::Socket::SslTcp
|
|
|
|
|
2005-11-03 04:43:19 +00:00
|
|
|
begin
|
2008-11-12 22:47:21 +00:00
|
|
|
@@loaded_openssl = false
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2008-11-12 22:47:21 +00:00
|
|
|
begin
|
|
|
|
require 'openssl'
|
|
|
|
@@loaded_openssl = true
|
2011-04-30 07:59:33 +00:00
|
|
|
require 'openssl/nonblock'
|
2008-11-12 22:47:21 +00:00
|
|
|
rescue ::Exception
|
|
|
|
end
|
2011-04-30 07:59:33 +00:00
|
|
|
|
2005-11-03 04:43:19 +00:00
|
|
|
|
2005-09-27 05:31:48 +00:00
|
|
|
include Rex::Socket::Tcp
|
2005-06-03 22:51:09 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Factory
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Creates an SSL TCP instance.
|
|
|
|
#
|
2010-02-06 17:59:25 +00:00
|
|
|
def self.create(hash = {})
|
2008-11-12 22:47:21 +00:00
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
2010-02-06 17:59:25 +00:00
|
|
|
hash['SSL'] = true
|
2005-09-27 05:31:48 +00:00
|
|
|
self.create_param(Rex::Socket::Parameters.from_hash(hash))
|
|
|
|
end
|
|
|
|
|
2005-06-03 22:51:09 +00:00
|
|
|
#
|
|
|
|
# Set the SSL flag to true and call the base class's create_param routine.
|
|
|
|
#
|
|
|
|
def self.create_param(param)
|
2005-09-27 05:31:48 +00:00
|
|
|
param.ssl = true
|
|
|
|
Rex::Socket::Tcp.create_param(param)
|
2005-06-03 22:51:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Class initialization
|
|
|
|
#
|
|
|
|
##
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Initializes the SSL socket.
|
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
def initsock(params = nil)
|
2005-06-03 22:51:09 +00:00
|
|
|
super
|
|
|
|
|
2009-10-26 19:48:15 +00:00
|
|
|
version = :SSLv3
|
|
|
|
if(params)
|
|
|
|
case params.ssl_version
|
2013-02-20 21:33:24 +00:00
|
|
|
when 'SSL2', :SSLv2
|
2009-10-26 19:48:15 +00:00
|
|
|
version = :SSLv2
|
2013-02-20 21:33:24 +00:00
|
|
|
when 'SSL23', :SSLv23
|
2011-07-29 03:34:15 +00:00
|
|
|
version = :SSLv23
|
2013-02-20 21:33:24 +00:00
|
|
|
when 'TLS1', :TLSv1
|
2013-02-09 06:40:58 +00:00
|
|
|
version = :TLSv1
|
2009-10-26 19:48:15 +00:00
|
|
|
end
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2005-06-03 22:51:09 +00:00
|
|
|
# Build the SSL connection
|
2009-10-26 19:48:15 +00:00
|
|
|
self.sslctx = OpenSSL::SSL::SSLContext.new(version)
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2007-05-03 20:01:29 +00:00
|
|
|
# Configure the SSL context
|
|
|
|
# TODO: Allow the user to specify the verify mode and callback
|
|
|
|
# Valid modes:
|
|
|
|
# VERIFY_CLIENT_ONCE
|
2011-07-29 03:34:15 +00:00
|
|
|
# VERIFY_FAIL_IF_NO_PEER_CERT
|
2007-05-03 20:01:29 +00:00
|
|
|
# VERIFY_NONE
|
|
|
|
# VERIFY_PEER
|
|
|
|
self.sslctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
|
|
|
self.sslctx.options = OpenSSL::SSL::OP_ALL
|
2013-02-06 22:57:47 +00:00
|
|
|
if params.ssl_cipher
|
|
|
|
self.sslctx.ciphers = params.ssl_cipher
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2007-05-03 20:01:29 +00:00
|
|
|
# Set the verification callback
|
|
|
|
self.sslctx.verify_callback = Proc.new do |valid, store|
|
|
|
|
self.peer_verified = valid
|
|
|
|
true
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2007-05-03 20:01:29 +00:00
|
|
|
# Tie the context to a socket
|
2005-09-27 05:31:48 +00:00
|
|
|
self.sslsock = OpenSSL::SSL::SSLSocket.new(self, self.sslctx)
|
2011-04-30 07:59:33 +00:00
|
|
|
|
2006-01-20 19:04:17 +00:00
|
|
|
# XXX - enabling this causes infinite recursion, so disable for now
|
|
|
|
# self.sslsock.sync_close = true
|
2011-05-02 17:25:26 +00:00
|
|
|
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2009-12-21 22:48:40 +00:00
|
|
|
# Force a negotiation timeout
|
|
|
|
begin
|
2011-07-29 03:34:15 +00:00
|
|
|
Timeout.timeout(params.timeout) do
|
|
|
|
if not allow_nonblock?
|
2011-04-30 07:59:33 +00:00
|
|
|
self.sslsock.connect
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
self.sslsock.connect_nonblock
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-02 17:25:26 +00:00
|
|
|
# Ruby 1.8.7 and 1.9.0/1.9.1 uses a standard Errno
|
|
|
|
rescue ::Errno::EAGAIN, ::Errno::EWOULDBLOCK
|
|
|
|
IO::select(nil, nil, nil, 0.10)
|
2011-07-29 03:34:15 +00:00
|
|
|
retry
|
|
|
|
|
|
|
|
# Ruby 1.9.2+ uses IO::WaitReadable/IO::WaitWritable
|
2011-05-02 17:25:26 +00:00
|
|
|
rescue ::Exception => e
|
|
|
|
if ::IO.const_defined?('WaitReadable') and e.kind_of?(::IO::WaitReadable)
|
|
|
|
IO::select( [ self.sslsock ], nil, nil, 0.10 )
|
|
|
|
retry
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
|
|
|
if ::IO.const_defined?('WaitWritable') and e.kind_of?(::IO::WaitWritable)
|
2011-05-02 17:25:26 +00:00
|
|
|
IO::select( nil, [ self.sslsock ], nil, 0.10 )
|
|
|
|
retry
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-02 17:25:26 +00:00
|
|
|
raise e
|
2011-04-30 07:59:33 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-21 22:48:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rescue ::Timeout::Error
|
|
|
|
raise Rex::ConnectionTimeout.new(params.peerhost, params.peerport)
|
|
|
|
end
|
2005-06-03 22:51:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Stream mixin implementations
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Writes data over the SSL socket.
|
|
|
|
#
|
2005-06-03 22:51:09 +00:00
|
|
|
def write(buf, opts = {})
|
2011-07-29 03:34:15 +00:00
|
|
|
return sslsock.write(buf) if not allow_nonblock?
|
2011-04-30 07:59:33 +00:00
|
|
|
|
|
|
|
total_sent = 0
|
|
|
|
total_length = buf.length
|
2011-05-03 03:59:02 +00:00
|
|
|
block_size = 16384
|
|
|
|
retry_time = 0.5
|
2011-04-30 07:59:33 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
while( total_sent < total_length )
|
2011-05-02 17:25:26 +00:00
|
|
|
s = Rex::ThreadSafe.select( nil, [ self.sslsock ], nil, 0.25 )
|
2011-04-30 07:59:33 +00:00
|
|
|
if( s == nil || s[0] == nil )
|
|
|
|
next
|
|
|
|
end
|
|
|
|
data = buf[total_sent, block_size]
|
|
|
|
sent = sslsock.write_nonblock( data )
|
|
|
|
if sent > 0
|
|
|
|
total_sent += sent
|
|
|
|
end
|
|
|
|
end
|
2011-05-02 17:25:26 +00:00
|
|
|
|
|
|
|
rescue ::IOError, ::Errno::EPIPE
|
2011-05-03 03:59:02 +00:00
|
|
|
return nil
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-02 17:25:26 +00:00
|
|
|
# Ruby 1.8.7 and 1.9.0/1.9.1 uses a standard Errno
|
|
|
|
rescue ::Errno::EAGAIN, ::Errno::EWOULDBLOCK
|
2011-04-30 07:59:33 +00:00
|
|
|
# Sleep for a half a second, or until we can write again
|
2011-05-03 03:59:02 +00:00
|
|
|
Rex::ThreadSafe.select( nil, [ self.sslsock ], nil, retry_time )
|
2011-04-30 07:59:33 +00:00
|
|
|
# Decrement the block size to handle full sendQs better
|
|
|
|
block_size = 1024
|
|
|
|
# Try to write the data again
|
|
|
|
retry
|
2011-07-29 03:34:15 +00:00
|
|
|
|
|
|
|
# Ruby 1.9.2+ uses IO::WaitReadable/IO::WaitWritable
|
2011-05-02 17:25:26 +00:00
|
|
|
rescue ::Exception => e
|
|
|
|
if ::IO.const_defined?('WaitReadable') and e.kind_of?(::IO::WaitReadable)
|
2011-05-03 03:59:02 +00:00
|
|
|
IO::select( [ self.sslsock ], nil, nil, retry_time )
|
2011-05-02 17:25:26 +00:00
|
|
|
retry
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
|
|
|
if ::IO.const_defined?('WaitWritable') and e.kind_of?(::IO::WaitWritable)
|
2011-05-03 03:59:02 +00:00
|
|
|
IO::select( nil, [ self.sslsock ], nil, retry_time )
|
2011-05-02 17:25:26 +00:00
|
|
|
retry
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-03 03:59:02 +00:00
|
|
|
# Another form of SSL error, this is always fatal
|
|
|
|
if e.kind_of?(::OpenSSL::SSL::SSLError)
|
|
|
|
return nil
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-03 03:59:02 +00:00
|
|
|
# Bubble the event up to the caller otherwise
|
2011-05-02 17:25:26 +00:00
|
|
|
raise e
|
2011-04-30 07:59:33 +00:00
|
|
|
end
|
2011-05-02 17:25:26 +00:00
|
|
|
|
2011-04-30 07:59:33 +00:00
|
|
|
total_sent
|
2005-06-03 22:51:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Reads data from the SSL socket.
|
|
|
|
#
|
2011-07-29 03:34:15 +00:00
|
|
|
def read(length = nil, opts = {})
|
|
|
|
if not allow_nonblock?
|
2011-04-30 07:59:33 +00:00
|
|
|
length = 16384 unless length
|
|
|
|
begin
|
|
|
|
return sslsock.sysread(length)
|
2011-05-03 03:59:02 +00:00
|
|
|
rescue ::IOError, ::Errno::EPIPE, ::OpenSSL::SSL::SSLError
|
|
|
|
return nil
|
2011-04-30 07:59:33 +00:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-02 17:25:26 +00:00
|
|
|
|
2005-06-03 22:51:09 +00:00
|
|
|
begin
|
2011-07-29 03:34:15 +00:00
|
|
|
while true
|
|
|
|
s = Rex::ThreadSafe.select( [ self.sslsock ], nil, nil, 0.10 )
|
2011-04-30 10:24:52 +00:00
|
|
|
if( s == nil || s[0] == nil )
|
|
|
|
next
|
2011-07-29 03:34:15 +00:00
|
|
|
end
|
|
|
|
return sslsock.read_nonblock( length )
|
2011-04-30 10:24:52 +00:00
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-04-30 07:59:33 +00:00
|
|
|
rescue ::IOError, ::Errno::EPIPE
|
2011-05-03 03:59:02 +00:00
|
|
|
return nil
|
2011-05-02 17:25:26 +00:00
|
|
|
|
2011-07-29 03:34:15 +00:00
|
|
|
# Ruby 1.8.7 and 1.9.0/1.9.1 uses a standard Errno
|
2011-05-02 17:25:26 +00:00
|
|
|
rescue ::Errno::EAGAIN, ::Errno::EWOULDBLOCK
|
|
|
|
# Sleep for a tenth a second, or until we can read again
|
|
|
|
Rex::ThreadSafe.select( [ self.sslsock ], nil, nil, 0.10 )
|
|
|
|
# Decrement the block size to handle full sendQs better
|
|
|
|
block_size = 1024
|
|
|
|
# Try to write the data again
|
|
|
|
retry
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-02 17:25:26 +00:00
|
|
|
# Ruby 1.9.2+ uses IO::WaitReadable/IO::WaitWritable
|
|
|
|
rescue ::Exception => e
|
|
|
|
if ::IO.const_defined?('WaitReadable') and e.kind_of?(::IO::WaitReadable)
|
|
|
|
IO::select( [ self.sslsock ], nil, nil, 0.5 )
|
|
|
|
retry
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
|
|
|
if ::IO.const_defined?('WaitWritable') and e.kind_of?(::IO::WaitWritable)
|
2011-05-02 17:25:26 +00:00
|
|
|
IO::select( nil, [ self.sslsock ], nil, 0.5 )
|
|
|
|
retry
|
|
|
|
end
|
2011-05-03 03:59:02 +00:00
|
|
|
|
|
|
|
# Another form of SSL error, this is always fatal
|
|
|
|
if e.kind_of?(::OpenSSL::SSL::SSLError)
|
|
|
|
return nil
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-02 17:25:26 +00:00
|
|
|
raise e
|
2005-06-03 22:51:09 +00:00
|
|
|
end
|
2011-05-02 17:25:26 +00:00
|
|
|
|
2005-06-03 22:51:09 +00:00
|
|
|
end
|
|
|
|
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Closes the SSL socket.
|
|
|
|
#
|
2005-06-03 22:51:09 +00:00
|
|
|
def close
|
2011-05-03 03:59:02 +00:00
|
|
|
sslsock.close rescue nil
|
2005-09-27 05:31:48 +00:00
|
|
|
super
|
2005-06-03 22:51:09 +00:00
|
|
|
end
|
|
|
|
|
2011-07-29 03:34:15 +00:00
|
|
|
#
|
2007-05-03 20:01:29 +00:00
|
|
|
# Ignore shutdown requests
|
|
|
|
#
|
|
|
|
def shutdown(how=0)
|
|
|
|
# Calling shutdown() on an SSL socket can lead to bad things
|
|
|
|
# Cause of http://metasploit.com/dev/trac/ticket/102
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2007-05-03 20:01:29 +00:00
|
|
|
#
|
|
|
|
# Access to peer cert
|
|
|
|
#
|
|
|
|
def peer_cert
|
|
|
|
sslsock.peer_cert if sslsock
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2007-05-03 20:01:29 +00:00
|
|
|
#
|
|
|
|
# Access to peer cert chain
|
|
|
|
#
|
|
|
|
def peer_cert_chain
|
|
|
|
sslsock.peer_cert_chain if sslsock
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2007-05-03 20:01:29 +00:00
|
|
|
#
|
|
|
|
# Access to the current cipher
|
|
|
|
#
|
|
|
|
def cipher
|
|
|
|
sslsock.cipher if sslsock
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2011-05-03 03:59:02 +00:00
|
|
|
#
|
|
|
|
# Prevent a sysread from the bare socket
|
|
|
|
#
|
|
|
|
def sysread(*args)
|
|
|
|
raise RuntimeError, "Invalid sysread() call on SSL socket"
|
|
|
|
end
|
2007-05-03 20:01:29 +00:00
|
|
|
|
2011-05-03 03:59:02 +00:00
|
|
|
#
|
|
|
|
# Prevent a sysread from the bare socket
|
|
|
|
#
|
|
|
|
def syswrite(*args)
|
|
|
|
raise RuntimeError, "Invalid syswrite() call on SSL socket"
|
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# This flag determines whether to use the non-blocking openssl
|
|
|
|
# API calls when they are available. This is still buggy on
|
2011-07-29 19:10:20 +00:00
|
|
|
# Linux/Mac OS X, but is required on Windows
|
2011-07-29 03:34:15 +00:00
|
|
|
#
|
|
|
|
def allow_nonblock?
|
2011-07-29 19:26:43 +00:00
|
|
|
avail = self.sslsock.respond_to?(:accept_nonblock)
|
2011-07-29 19:10:20 +00:00
|
|
|
if avail and Rex::Compat.is_windows
|
2011-07-29 03:34:15 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2007-05-03 20:01:29 +00:00
|
|
|
attr_reader :peer_verified # :nodoc:
|
2008-08-23 05:12:43 +00:00
|
|
|
attr_accessor :sslsock, :sslctx # :nodoc:
|
2011-04-30 07:59:33 +00:00
|
|
|
|
2005-06-03 22:51:09 +00:00
|
|
|
protected
|
|
|
|
|
2007-05-03 20:01:29 +00:00
|
|
|
attr_writer :peer_verified # :nodoc:
|
2005-06-03 22:51:09 +00:00
|
|
|
|
2011-04-30 07:59:33 +00:00
|
|
|
|
2005-11-03 04:43:19 +00:00
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2006-01-27 05:33:08 +00:00
|
|
|
def type?
|
|
|
|
return 'tcp-ssl'
|
|
|
|
end
|
2005-12-13 06:08:40 +00:00
|
|
|
|
2008-11-11 05:11:40 +00:00
|
|
|
end
|
2011-07-29 03:34:15 +00:00
|
|
|
|