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
|
|
|
|
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
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2009-11-09 00:13:25 +00:00
|
|
|
def self.create(hash)
|
|
|
|
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
|
2006-04-24 18:49:00 +00:00
|
|
|
self.sslctx = makessl()
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
ssl.accept
|
|
|
|
sock.extend(Rex::Socket::SslTcp)
|
|
|
|
sock.sslsock = ssl
|
|
|
|
sock.sslctx = self.sslctx
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def makessl
|
2009-11-09 00:13:25 +00:00
|
|
|
key = OpenSSL::PKey::RSA.new(1024){ }
|
|
|
|
|
2006-04-24 18:49:00 +00:00
|
|
|
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([
|
2009-11-09 00:13:25 +00:00
|
|
|
["C","US"],
|
|
|
|
['ST', Rex::Text.rand_state()],
|
2006-04-24 18:49:00 +00:00
|
|
|
["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([
|
2009-11-09 00:13:25 +00:00
|
|
|
["C","US"],
|
|
|
|
['ST', Rex::Text.rand_state()],
|
2006-04-24 18:49:00 +00:00
|
|
|
["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
|
2009-11-09 00:13:25 +00:00
|
|
|
cert.not_before = Time.now - (3600 * 365)
|
|
|
|
cert.not_after = Time.now + (3600 * 365)
|
2006-04-24 18:49:00 +00:00
|
|
|
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)
|
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
|
|
|
|
|
|
|
|
attr_accessor :sslctx
|
2008-11-12 22:47:21 +00:00
|
|
|
end
|
2009-11-09 00:13:25 +00:00
|
|
|
|