2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2006-04-24 18:49:00 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
require 'rex/socket/tcp_server'
|
|
|
|
require 'rex/io/stream_server'
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class provides methods for interacting with an SSL wrapped TCP server. It
|
|
|
|
# implements the StreamServer IO interface.
|
|
|
|
#
|
|
|
|
###
|
2008-11-12 22:47:21 +00:00
|
|
|
module Rex::Socket::SslTcpServer
|
|
|
|
|
|
|
|
@@loaded_openssl = false
|
2009-11-09 00:13:25 +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
|
2009-11-09 00:13:25 +00:00
|
|
|
|
2006-04-24 18:49:00 +00:00
|
|
|
include Rex::Socket::TcpServer
|
2009-11-09 00:13:25 +00:00
|
|
|
|
2006-04-24 18:49:00 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Factory
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2010-02-06 17:59:25 +00:00
|
|
|
def self.create(hash = {})
|
|
|
|
hash['Proto'] = 'tcp'
|
|
|
|
hash['Server'] = true
|
|
|
|
hash['SSL'] = true
|
2009-11-09 00:13:25 +00:00
|
|
|
self.create_param(Rex::Socket::Parameters.from_hash(hash))
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Wrapper around the base class' creation method that automatically sets
|
|
|
|
# the parameter's protocol to TCP and sets the server flag to true.
|
|
|
|
#
|
|
|
|
def self.create_param(param)
|
|
|
|
param.proto = 'tcp'
|
|
|
|
param.server = true
|
|
|
|
param.ssl = true
|
|
|
|
Rex::Socket.create_param(param)
|
|
|
|
end
|
|
|
|
|
2006-04-24 18:49:00 +00:00
|
|
|
def initsock(params = nil)
|
2008-11-12 22:47:21 +00:00
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
2011-05-20 23:12:35 +00:00
|
|
|
self.sslctx = makessl(params.ssl_cert)
|
2006-04-24 18:49:00 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2013-03-18 20:17:07 +00:00
|
|
|
# (see TcpServer#accept)
|
2006-04-24 18:49:00 +00:00
|
|
|
def accept(opts = {})
|
|
|
|
sock = super()
|
2009-11-09 00:13:25 +00:00
|
|
|
return if not sock
|
2006-04-24 18:49:00 +00:00
|
|
|
|
2008-03-22 06:34:52 +00:00
|
|
|
begin
|
2009-11-09 00:13:25 +00:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock, self.sslctx)
|
2011-04-30 07:59:33 +00:00
|
|
|
|
2011-07-29 23:58:05 +00:00
|
|
|
if not allow_nonblock?(ssl)
|
2011-04-30 07:59:33 +00:00
|
|
|
ssl.accept
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
ssl.accept_nonblock
|
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)
|
|
|
|
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)
|
|
|
|
IO::select( [ ssl ], 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, [ ssl ], 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
|
2011-07-29 03:34:15 +00:00
|
|
|
|
2009-11-09 00:13:25 +00:00
|
|
|
sock.extend(Rex::Socket::SslTcp)
|
|
|
|
sock.sslsock = ssl
|
|
|
|
sock.sslctx = self.sslctx
|
2011-04-30 07:59:33 +00:00
|
|
|
|
2009-11-09 00:13:25 +00:00
|
|
|
return sock
|
|
|
|
|
2008-03-22 06:34:52 +00:00
|
|
|
rescue ::OpenSSL::SSL::SSLError
|
|
|
|
sock.close
|
|
|
|
nil
|
|
|
|
end
|
2006-04-24 18:49:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-07-29 03:34:15 +00:00
|
|
|
#
|
2011-05-20 23:12:35 +00:00
|
|
|
# Create a new ssl context. If +ssl_cert+ is not given, generates a new
|
|
|
|
# key and a leaf certificate with random values.
|
|
|
|
#
|
2013-03-18 20:17:07 +00:00
|
|
|
# @return [::OpenSSL::SSL::SSLContext]
|
2011-05-20 23:12:35 +00:00
|
|
|
def makessl(ssl_cert=nil)
|
|
|
|
|
|
|
|
if ssl_cert
|
|
|
|
cert = OpenSSL::X509::Certificate.new(ssl_cert)
|
|
|
|
key = OpenSSL::PKey::RSA.new(ssl_cert)
|
|
|
|
else
|
|
|
|
key = OpenSSL::PKey::RSA.new(1024){ }
|
|
|
|
cert = OpenSSL::X509::Certificate.new
|
|
|
|
cert.version = 2
|
|
|
|
cert.serial = rand(0xFFFFFFFF)
|
|
|
|
# name = OpenSSL::X509::Name.new([["C","JP"],["O","TEST"],["CN","localhost"]])
|
|
|
|
subject = OpenSSL::X509::Name.new([
|
|
|
|
["C","US"],
|
|
|
|
['ST', Rex::Text.rand_state()],
|
|
|
|
["L", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
|
|
|
["O", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
|
|
|
["CN", Rex::Text.rand_hostname],
|
|
|
|
])
|
|
|
|
issuer = OpenSSL::X509::Name.new([
|
|
|
|
["C","US"],
|
|
|
|
['ST', Rex::Text.rand_state()],
|
|
|
|
["L", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
|
|
|
["O", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
|
|
|
["CN", Rex::Text.rand_hostname],
|
|
|
|
])
|
|
|
|
|
|
|
|
cert.subject = subject
|
|
|
|
cert.issuer = issuer
|
|
|
|
cert.not_before = Time.now - (3600 * 365)
|
|
|
|
cert.not_after = Time.now + (3600 * 365)
|
|
|
|
cert.public_key = key.public_key
|
|
|
|
ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
|
|
|
|
cert.extensions = [
|
|
|
|
ef.create_extension("basicConstraints","CA:FALSE"),
|
|
|
|
ef.create_extension("subjectKeyIdentifier","hash"),
|
|
|
|
ef.create_extension("extendedKeyUsage","serverAuth"),
|
|
|
|
ef.create_extension("keyUsage","keyEncipherment,dataEncipherment,digitalSignature")
|
|
|
|
]
|
|
|
|
ef.issuer_certificate = cert
|
|
|
|
cert.add_extension ef.create_extension("authorityKeyIdentifier", "keyid:always,issuer:always")
|
|
|
|
cert.sign(key, OpenSSL::Digest::SHA1.new)
|
|
|
|
end
|
2009-11-09 00:13:25 +00:00
|
|
|
|
2006-04-24 18:49:00 +00:00
|
|
|
ctx = OpenSSL::SSL::SSLContext.new()
|
|
|
|
ctx.key = key
|
|
|
|
ctx.cert = cert
|
|
|
|
|
2009-11-09 00:13:25 +00:00
|
|
|
ctx.session_id_context = Rex::Text.rand_text(16)
|
2006-04-24 18:49:00 +00:00
|
|
|
|
|
|
|
return ctx
|
|
|
|
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
|
|
|
#
|
2011-07-29 23:58:05 +00:00
|
|
|
def allow_nonblock?(sock=self.sock)
|
|
|
|
avail = sock.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
|
|
|
|
|
2006-04-24 18:49:00 +00:00
|
|
|
attr_accessor :sslctx
|
2008-11-12 22:47:21 +00:00
|
|
|
end
|
2009-11-09 00:13:25 +00:00
|
|
|
|