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
|
|
|
|
require 'openssl'
|
|
|
|
|
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.
|
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
def self.create(hash)
|
|
|
|
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
|
2005-06-03 22:51:09 +00:00
|
|
|
|
2005-09-27 05:31:48 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
# Build the SSL connection
|
|
|
|
self.sslctx = OpenSSL::SSL::SSLContext.new
|
2005-09-27 05:31:48 +00:00
|
|
|
self.sslsock = OpenSSL::SSL::SSLSocket.new(self, self.sslctx)
|
2006-01-20 19:04:17 +00:00
|
|
|
# XXX - enabling this causes infinite recursion, so disable for now
|
|
|
|
# self.sslsock.sync_close = true
|
2005-06-03 22:51:09 +00:00
|
|
|
self.sslsock.connect
|
|
|
|
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 = {})
|
|
|
|
return sslsock.write(buf)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Reads data from the SSL socket.
|
|
|
|
#
|
2005-06-03 22:51:09 +00:00
|
|
|
def read(length = nil, opts = {})
|
|
|
|
length = 16384 unless length
|
|
|
|
|
|
|
|
begin
|
|
|
|
return sslsock.read(length)
|
|
|
|
rescue EOFError
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Closes the SSL socket.
|
|
|
|
#
|
2005-06-03 22:51:09 +00:00
|
|
|
def close
|
|
|
|
sslsock.close
|
2005-09-27 05:31:48 +00:00
|
|
|
super
|
2005-06-03 22:51:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :sslsock, :sslctx # :nodoc:
|
2005-06-03 22:51:09 +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
|
|
|
|
2005-06-03 22:51:09 +00:00
|
|
|
end
|