Merge branch 'dmaloney-r7-feature/ssl/add_cipher_support' into rapid7

bug/bundler_fix
James Lee 2013-03-07 12:54:39 -06:00
commit f05431791f
7 changed files with 1058 additions and 5 deletions

View File

@ -88,6 +88,10 @@ require 'rex/compat'
# Platforms
require 'rex/platforms'
# SSLScan
require 'rex/sslscan/scanner'
require 'rex/sslscan/result'
# Overload the Kernel.sleep() function to be thread-safe
Kernel.class_eval("

View File

@ -140,10 +140,15 @@ class Rex::Socket::Parameters
self.ssl = false
end
if (hash['SSLVersion'] and hash['SSLVersion'].to_s =~ /^(SSL2|SSL3|TLS1)$/i)
supported_ssl_versions = ['SSL2', 'SSL23', 'TLS1', 'SSL3', :SSLv2, :SSLv3, :SSLv23, :TLSv1]
if (hash['SSLVersion'] and supported_ssl_versions.include? hash['SSLVersion'])
self.ssl_version = hash['SSLVersion']
end
if (hash['SSLCipher'])
self.ssl_cipher = hash['SSLCipher']
end
if (hash['SSLCert'] and ::File.file?(hash['SSLCert']))
begin
self.ssl_cert = ::File.read(hash['SSLCert'])
@ -338,6 +343,11 @@ class Rex::Socket::Parameters
#
attr_accessor :ssl_version
#
# 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"]
#
attr_accessor :ssl_cipher
#
# The SSL certificate, in pem format, stored as a string. See +SslTcpServer#make_ssl+
#
attr_accessor :ssl_cert

View File

@ -1,6 +1,5 @@
# -*- coding: binary -*-
require 'rex/socket'
###
#
# This class provides methods for interacting with an SSL TCP client
@ -60,11 +59,11 @@ begin
version = :SSLv3
if(params)
case params.ssl_version
when 'SSL2'
when 'SSL2', :SSLv2
version = :SSLv2
when 'SSL23'
when 'SSL23', :SSLv23
version = :SSLv23
when 'TLS1'
when 'TLS1', :TLSv1
version = :TLSv1
end
end
@ -81,6 +80,9 @@ begin
# VERIFY_PEER
self.sslctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
self.sslctx.options = OpenSSL::SSL::OP_ALL
if params.ssl_cipher
self.sslctx.ciphers = params.ssl_cipher
end
# Set the verification callback
self.sslctx.verify_callback = Proc.new do |valid, store|

200
lib/rex/sslscan/result.rb Normal file
View File

@ -0,0 +1,200 @@
require 'rex/socket'
require 'rex/ui/text/table'
module Rex::SSLScan
class Result
attr_accessor :openssl_sslv2
attr_reader :ciphers
attr_reader :supported_versions
def initialize()
@cert = nil
@ciphers = Set.new
@supported_versions = [:SSLv2, :SSLv3, :TLSv1]
end
def cert
@cert
end
def cert=(input)
unless input.kind_of? OpenSSL::X509::Certificate or input.nil?
raise ArgumentError, "Must be an X509 Cert!"
end
@cert = input
end
def sslv2
@ciphers.reject{|cipher| cipher[:version] != :SSLv2 }
end
def sslv3
@ciphers.reject{|cipher| cipher[:version] != :SSLv3 }
end
def tlsv1
@ciphers.reject{|cipher| cipher[:version] != :TLSv1 }
end
def weak_ciphers
accepted.reject{|cipher| cipher[:weak] == false }
end
def strong_ciphers
accepted.reject{|cipher| cipher[:weak] }
end
# Returns all accepted ciphers matching the supplied version
# @param version [Symbol, Array] The SSL Version to filter on
# @raise [ArgumentError] if the version supplied is invalid
# @return [Array] An array of accepted cipher details matching the supplied versions
def accepted(version = :all)
enum_ciphers(:accepted, version)
end
# Returns all rejected ciphers matching the supplied version
# @param version [Symbol, Array] The SSL Version to filter on
# @raise [ArgumentError] if the version supplied is invalid
# @return [Array] An array of rejected cipher details matching the supplied versions
def rejected(version = :all)
enum_ciphers(:rejected, version)
end
def each_accepted(version = :all)
accepted(version).each do |cipher_result|
yield cipher_result
end
end
def each_rejected(version = :all)
rejected(version).each do |cipher_result|
yield cipher_result
end
end
def supports_sslv2?
!(accepted(:SSLv2).empty?)
end
def supports_sslv3?
!(accepted(:SSLv3).empty?)
end
def supports_tlsv1?
!(accepted(:TLSv1).empty?)
end
def supports_ssl?
supports_sslv2? or supports_sslv3? or supports_tlsv1?
end
def supports_weak_ciphers?
!(weak_ciphers.empty?)
end
def standards_compliant?
if supports_ssl?
return false if supports_sslv2?
return false if supports_weak_ciphers?
end
true
end
# Adds the details of a cipher test to the Result object.
# @param version [Symbol] the SSL Version
# @param cipher [String] the SSL cipher
# @param key_length [Fixnum] the length of encryption key
# @param status [Symbol] :accepted or :rejected
def add_cipher(version, cipher, key_length, status)
unless @supported_versions.include? version
raise ArgumentError, "Must be a supported SSL Version"
end
unless OpenSSL::SSL::SSLContext.new(version).ciphers.flatten.include? cipher
raise ArgumentError, "Must be a valid SSL Cipher for #{version}!"
end
unless key_length.kind_of? Fixnum
raise ArgumentError, "Must supply a valid key length"
end
unless [:accepted, :rejected].include? status
raise ArgumentError, "Status must be either :accepted or :rejected"
end
strong_cipher_ctx = OpenSSL::SSL::SSLContext.new(version)
# OpenSSL Directive For Strong Ciphers
# See: http://www.rapid7.com/vulndb/lookup/ssl-weak-ciphers
strong_cipher_ctx.ciphers = "ALL:!aNULL:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM"
if strong_cipher_ctx.ciphers.flatten.include? cipher
weak = false
else
weak = true
end
cipher_details = {:version => version, :cipher => cipher, :key_length => key_length, :weak => weak, :status => status}
@ciphers << cipher_details
end
def to_s
unless supports_ssl?
return "Server does not appear to support SSL on this port!"
end
table = Rex::Ui::Text::Table.new(
'Header' => 'SSL Ciphers',
'Indent' => 1,
'Columns' => ['Status', 'Weak', 'SSL Version', 'Key Length', 'Cipher'],
'SortIndex' => -1
)
ciphers.each do |cipher|
if cipher[:weak]
weak = '*'
else
weak = ' '
end
table << [cipher[:status].to_s.capitalize, weak , cipher[:version], cipher[:key_length], cipher[:cipher]]
end
# Sort by SSL Version, then Key Length, and then Status
table.rows.sort_by!{|row| [row[0],row[2],row[3]]}
text = "#{table.to_s}"
if @cert
text << " \n\n #{@cert.to_text}"
end
if openssl_sslv2 == false
text << "\n\n *** WARNING: Your OS hates freedom! Your OpenSSL libs are compiled without SSLv2 support!"
end
text
end
protected
# @param state [Symbol] Either :accepted or :rejected
# @param version [Symbol, Array] The SSL Version to filter on (:SSLv2, :SSLv3, :TLSv1, :all)
# @return [Set] The Set of cipher results matching the filter criteria
def enum_ciphers(state, version = :all)
case version
when Symbol
case version
when :all
return @ciphers.select{|cipher| cipher[:status] == state}
when :SSLv2, :SSLv3, :TLSv1
return @ciphers.select{|cipher| cipher[:status] == state and cipher[:version] == version}
else
raise ArgumentError, "Invalid SSL Version Supplied: #{version}"
end
when Array
version = version.reject{|v| !(@supported_versions.include? v)}
if version.empty?
return @ciphers.select{|cipher| cipher[:status] == state}
else
return @ciphers.select{|cipher| cipher[:status] == state and version.include? cipher[:version]}
end
else
raise ArgumentError, "Was expecting Symbol or Array and got #{version.class}"
end
end
end
end

205
lib/rex/sslscan/scanner.rb Normal file
View File

@ -0,0 +1,205 @@
require 'rex/socket'
require 'rex/sslscan/result'
module Rex::SSLScan
class Scanner
attr_accessor :context
attr_accessor :host
attr_accessor :port
attr_accessor :timeout
attr_reader :supported_versions
attr_reader :sslv2
# Initializes the scanner object
# @param host [String] IP address or hostname to scan
# @param port [Fixnum] Port number to scan, default: 443
# @param timeout [Fixnum] Timeout for connections, in seconds. default: 5
# @raise [StandardError] Raised when the configuration is invalid
def initialize(host,port = 443,context = {},timeout=5)
@host = host
@port = port
@timeout = timeout
@context = context
if check_opensslv2 == true
@supported_versions = [:SSLv2, :SSLv3, :TLSv1]
@sslv2 = true
else
@supported_versions = [:SSLv3, :TLSv1]
@sslv2 = false
end
raise StandardError, "The scanner configuration is invalid" unless valid?
end
# Checks whether the scanner option has a valid configuration
# @return [Boolean] True or False, the configuration is valid.
def valid?
begin
@host = Rex::Socket.getaddress(@host, true)
rescue
return false
end
return false unless @port.kind_of? Fixnum
return false unless @port >= 0 and @port <= 65535
return false unless @timeout.kind_of? Fixnum
return true
end
# Initiate the Scan against the target. Will test each cipher one at a time.
# @return [Result] object containing the details of the scan
def scan
scan_result = Rex::SSLScan::Result.new
scan_result.openssl_sslv2 = sslv2
# If we can't get any SSL connection, then don't bother testing
# individual ciphers.
if test_ssl == :rejected and test_tls == :rejected
return scan_result
end
@supported_versions.each do |ssl_version|
sslctx = OpenSSL::SSL::SSLContext.new(ssl_version)
sslctx.ciphers.each do |cipher_name, ssl_ver, key_length, alg_length|
status = test_cipher(ssl_version, cipher_name)
scan_result.add_cipher(ssl_version, cipher_name, key_length, status)
if status == :accepted and scan_result.cert.nil?
scan_result.cert = get_cert(ssl_version, cipher_name)
end
end
end
scan_result
end
def test_ssl
begin
scan_client = Rex::Socket::Tcp.create(
'Context' => @context,
'PeerHost' => @host,
'PeerPort' => @port,
'SSL' => true,
'SSLVersion' => :SSLv23,
'Timeout' => @timeout
)
rescue ::Exception => e
return :rejected
ensure
if scan_client
scan_client.close
end
end
return :accepted
end
def test_tls
begin
scan_client = Rex::Socket::Tcp.create(
'Context' => @context,
'PeerHost' => @host,
'PeerPort' => @port,
'SSL' => true,
'SSLVersion' => :TLSv1,
'Timeout' => @timeout
)
rescue ::Exception => e
return :rejected
ensure
if scan_client
scan_client.close
end
end
return :accepted
end
# Tests the specified SSL Version and Cipher against the configured target
# @param ssl_version [Symbol] The SSL version to use (:SSLv2, :SSLv3, :TLSv1)
# @param cipher [String] The SSL Cipher to use
# @return [Symbol] Either :accepted or :rejected
def test_cipher(ssl_version, cipher)
validate_params(ssl_version,cipher)
begin
scan_client = Rex::Socket::Tcp.create(
'Context' => @context,
'PeerHost' => @host,
'PeerPort' => @port,
'SSL' => true,
'SSLVersion' => ssl_version,
'SSLCipher' => cipher,
'Timeout' => @timeout
)
rescue ::Exception => e
return :rejected
ensure
if scan_client
scan_client.close
end
end
return :accepted
end
# Retrieve the X509 Cert from the target service,
# @param ssl_version [Symbol] The SSL version to use (:SSLv2, :SSLv3, :TLSv1)
# @param cipher [String] The SSL Cipher to use
# @return [OpenSSL::X509::Certificate] if the certificate was retrieved
# @return [Nil] if the cert couldn't be retrieved
def get_cert(ssl_version, cipher)
validate_params(ssl_version,cipher)
begin
scan_client = Rex::Socket::Tcp.create(
'PeerHost' => @host,
'PeerPort' => @port,
'SSL' => true,
'SSLVersion' => ssl_version,
'SSLCipher' => cipher,
'Timeout' => @timeout
)
cert = scan_client.peer_cert
if cert.kind_of? OpenSSL::X509::Certificate
return cert
else
return nil
end
rescue ::Exception => e
return nil
ensure
if scan_client
scan_client.close
end
end
end
protected
# Validates that the SSL Version and Cipher are valid both seperately and
# together as part of an SSL Context.
# @param ssl_version [Symbol] The SSL version to use (:SSLv2, :SSLv3, :TLSv1)
# @param cipher [String] The SSL Cipher to use
# @raise [StandardError] If an invalid or unsupported SSL Version was supplied
# @raise [StandardError] If the cipher is not valid for that version of SSL
def validate_params(ssl_version, cipher)
raise StandardError, "The scanner configuration is invalid" unless valid?
unless @supported_versions.include? ssl_version
raise StandardError, "SSL Version must be one of: #{@supported_versions.to_s}"
end
if ssl_version == :SSLv2 and sslv2 == false
raise StandardError, "Your OS hates freedom! Your OpenSSL libs are compiled without SSLv2 support!"
else
unless OpenSSL::SSL::SSLContext.new(ssl_version).ciphers.flatten.include? cipher
raise StandardError, "Must be a valid SSL Cipher for #{ssl_version}!"
end
end
end
def check_opensslv2
begin
OpenSSL::SSL::SSLContext.new(:SSLv2)
rescue
return false
end
return true
end
end
end

View File

@ -0,0 +1,526 @@
require 'rex/sslscan/result'
describe Rex::SSLScan::Result do
subject{Rex::SSLScan::Result.new}
it { should respond_to :accepted }
it { should respond_to :cert }
it { should respond_to :ciphers }
it { should respond_to :rejected }
it { should respond_to :sslv2 }
it { should respond_to :sslv3 }
it { should respond_to :standards_compliant? }
it { should respond_to :strong_ciphers }
it { should respond_to :supports_ssl? }
it { should respond_to :supports_sslv2? }
it { should respond_to :supports_sslv3? }
it { should respond_to :supports_tlsv1? }
it { should respond_to :supports_weak_ciphers? }
it { should respond_to :tlsv1 }
it { should respond_to :weak_ciphers }
context "with no values set" do
it "should return nil for the cert" do
subject.cert.should == nil
end
it "should return an empty set for ciphers" do
subject.ciphers.empty?.should == true
end
it "should return an empty array for accepted" do
subject.accepted.should == []
end
it "should return an empty array for rejected" do
subject.rejected.should == []
end
it "should return an empty array for #sslv2" do
subject.sslv2.should == []
end
it "should return an empty array for #sslv3" do
subject.sslv3.should == []
end
it "should return an empty array for #tlsv1" do
subject.tlsv1.should == []
end
it "should return an empty array for #weak_ciphers" do
subject.weak_ciphers.should == []
end
it "should return an empty array for #strong_ciphers" do
subject.strong_ciphers.should == []
end
it "should return false for #supports_ssl?" do
subject.supports_ssl?.should == false
end
it "should return false for #supports_ssl?v2" do
subject.supports_sslv2?.should == false
end
it "should return false for #supports_sslv3?" do
subject.supports_sslv3?.should == false
end
it "should return false for #supports_tlsv1?" do
subject.supports_tlsv1?.should == false
end
it "should return false for #supports_weak_ciphers?" do
subject.supports_weak_ciphers?.should == false
end
it "should return true for #standards_compliant?" do
subject.standards_compliant?.should == true
end
end
context "setting the cert" do
it "should accept nil" do
subject.cert = nil
subject.cert.should == nil
end
it "should accept an X509 cert" do
cert = OpenSSL::X509::Certificate.new
subject.cert = cert
subject.cert.should == cert
end
it "should raise an exception for anything else" do
expect{subject.cert = "foo"}.to raise_error
end
end
context "adding a cipher result" do
context "should raise an exception if" do
it "given an invalid SSL version" do
expect{subject.add_cipher(:ssl3, 'AES256-SHA', 256, :accepted )}.to raise_error
end
it "given SSL version as a string" do
expect{subject.add_cipher('sslv3', 'AES256-SHA', 256, :accepted )}.to raise_error
end
it "given an invalid SSL cipher" do
expect{subject.add_cipher(:SSLv3, 'FOO256-SHA', 256, :accepted )}.to raise_error
end
it "given an unsupported cipher for the version" do
expect{subject.add_cipher(:SSLv3, 'DES-CBC3-MD5', 256, :accepted )}.to raise_error
end
it "given a non-number for key length" do
expect{subject.add_cipher(:SSLv3, 'AES256-SHA', "256", :accepted )}.to raise_error
end
it "given a decimal key length" do
expect{subject.add_cipher(:SSLv3, 'AES256-SHA', 25.6, :accepted )}.to raise_error
end
it "given an invalid status" do
expect{subject.add_cipher(:SSLv3, 'AES256-SHA', 256, :good )}.to raise_error
end
it "given status as a string" do
expect{subject.add_cipher(:SSLv3, 'AES256-SHA', 256, "accepted" )}.to raise_error
end
end
context "that was accepted" do
it "should add an SSLv2 cipher result to the SSLv2 Accepted array or generate an SSLv2 exception" do
begin
subject.add_cipher(:SSLv2, "DES-CBC3-MD5", 168, :accepted)
subject.accepted(:SSLv2).should include({
:version => :SSLv2,
:cipher=>"DES-CBC3-MD5",
:key_length=>168,
:weak=> false,
:status => :accepted})
rescue ArgumentError => e
e.message.should == "unknown SSL method `SSLv2'."
end
end
it "should add an SSLv3 cipher result to the SSLv3 Accepted array" do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :accepted)
subject.accepted(:SSLv3).should include({
:version => :SSLv3,
:cipher=>"AES256-SHA",
:key_length=>256,
:weak=> false,
:status => :accepted})
end
it "should add an TLSv1 cipher result to the TLSv1 Accepted array" do
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :accepted)
subject.accepted(:TLSv1).should include({
:version => :TLSv1,
:cipher=>"AES256-SHA",
:key_length=>256,
:weak=> false,
:status => :accepted})
end
it "should successfully add multiple entries in a row" do
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :accepted)
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :accepted)
subject.accepted(:SSLv3).should include({
:version => :SSLv3,
:cipher=>"AES256-SHA",
:key_length=>256,
:weak=> false,
:status => :accepted})
subject.accepted(:SSLv3).should include({
:version => :SSLv3,
:cipher=>"AES256-SHA",
:key_length=>256,
:weak=> false,
:status => :accepted})
end
it "should not add duplicate entries" do
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :accepted)
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :accepted)
subject.accepted(:SSLv3).count.should == 1
end
end
context "that was rejected" do
it "should add an SSLv2 cipher result to the SSLv2 Rejected array or generate an SSLv2 exception" do
begin
subject.add_cipher(:SSLv2, "DES-CBC3-MD5", 168, :rejected)
subject.rejected(:SSLv2).should include({
:version => :SSLv2,
:cipher=>"DES-CBC3-MD5",
:key_length=>168,
:weak=> false,
:status => :rejected})
rescue ArgumentError => e
e.message.should == "unknown SSL method `SSLv2'."
end
end
it "should add an SSLv3 cipher result to the SSLv3 Rejected array" do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :rejected)
subject.rejected(:SSLv3).should include({
:version => :SSLv3,
:cipher=>"AES256-SHA",
:key_length=>256,
:weak=> false,
:status => :rejected})
end
it "should add an TLSv1 cipher result to the TLSv1 Rejected array" do
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :rejected)
subject.rejected(:TLSv1).should include({
:version => :TLSv1,
:cipher=>"AES256-SHA",
:key_length=>256,
:weak=> false,
:status => :rejected})
end
it "should successfully add multiple entries in a row" do
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :rejected)
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :rejected)
subject.rejected(:SSLv3).should include({
:version => :SSLv3,
:cipher=>"AES256-SHA",
:key_length=>256,
:weak=> false,
:status => :rejected})
subject.rejected(:SSLv3).should include({
:version => :SSLv3,
:cipher=>"AES128-SHA",
:key_length=>128,
:weak=> false,
:status => :rejected})
end
it "should not add duplicate entries" do
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :rejected)
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :rejected)
subject.rejected(:SSLv3).count.should == 1
end
end
end
context "enumerating all accepted ciphers" do
before(:each) do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :accepted)
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :accepted)
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :accepted)
end
context "with no version selected" do
it "should return an array of cipher detail hashes" do
subject.each_accepted do |cipher_details|
cipher_details.should include(:version, :cipher, :key_length, :status, :weak)
end
end
it "should return all of the accepted cipher details" do
count = 0
subject.each_accepted do |cipher_details|
count = count+1
end
count.should == 3
end
end
context "when specifying one SSL version" do
it "should raise an exception if not given a symbol" do
expect{ subject.each_accepted('sslv2')}.to raise_error
end
it "should raise an exception if given an invalid SSL version" do
expect{ subject.each_accepted(:TLSv3)}.to raise_error
end
it "should return only ciphers matching the version" do
subject.each_accepted(:SSLv3) do |cipher_details|
cipher_details[:version].should == :SSLv3
end
end
end
context "when specifying multiple SSL Versions in an array" do
it "should return all versions if no valid versions were supplied" do
count = 0
subject.each_accepted([:TLSv3, :TLSv4]) do |cipher_details|
count = count+1
end
count.should == 3
end
it "should return only the ciphers for the specified version" do
subject.each_accepted([:SSLv3,:TLSv1]) do |cipher_details|
cipher_details[:version].should_not == :SSLv2
end
end
end
end
context "enumerating all rejected ciphers" do
before(:each) do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :rejected)
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :rejected)
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :rejected)
end
context "with no version selected" do
it "should return an array of cipher detail hashes" do
subject.each_rejected do |cipher_details|
cipher_details.should include(:version, :cipher, :key_length, :status, :weak)
end
end
it "should return all of the rejected cipher details" do
count = 0
subject.each_rejected do |cipher_details|
count = count+1
end
count.should == 3
end
end
context "when specifying one SSL version" do
it "should raise an exception if not given a symbol" do
expect{ subject.each_rejected('sslv2')}.to raise_error
end
it "should raise an exception if given an invalid SSL version" do
expect{ subject.each_rejected(:TLSv3)}.to raise_error
end
it "should return only ciphers matching the version" do
subject.each_rejected(:SSLv3) do |cipher_details|
cipher_details[:version].should == :SSLv3
end
end
end
context "when specifying multiple SSL Versions in an array" do
it "should return all versions if no valid versions were supplied" do
count = 0
subject.each_rejected([:TLSv3, :TLSv4]) do |cipher_details|
count = count+1
end
count.should == 3
end
it "should return only the ciphers for the specified version" do
subject.each_rejected([:SSLv3,:TLSv1]) do |cipher_details|
cipher_details[:version].should_not == :SSLv2
end
end
end
end
context "checking SSL support" do
context "for SSLv2" do
it "should return false if there are no accepted ciphers" do
subject.supports_sslv2?.should == false
end
it "should return true if there are accepted ciphers or raise an SSLv2 exception" do
begin
subject.add_cipher(:SSLv2, "DES-CBC3-MD5", 168, :accepted)
subject.supports_sslv2?.should == true
rescue ArgumentError => e
e.message.should == "unknown SSL method `SSLv2'."
end
end
end
context "for SSLv3" do
it "should return false if there are no accepted ciphers" do
subject.supports_sslv3?.should == false
end
it "should return true if there are accepted ciphers" do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :accepted)
subject.supports_sslv3?.should == true
end
end
context "for TLSv1" do
it "should return false if there are no accepted ciphers" do
subject.supports_tlsv1?.should == false
end
it "should return true if there are accepted ciphers" do
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :accepted)
subject.supports_tlsv1?.should == true
end
end
context "for SSL at large" do
it "should return false if there are no accepted ciphers" do
subject.supports_ssl?.should == false
end
it "should return true if there are accepted ciphers" do
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :accepted)
subject.supports_ssl?.should == true
end
end
end
context "checking for weak ciphers" do
context "when weak ciphers are supported" do
before(:each) do
subject.add_cipher(:SSLv3, "EXP-RC4-MD5", 40, :accepted)
subject.add_cipher(:SSLv3, "DES-CBC-SHA", 56, :accepted)
end
it "should return an array of weak ciphers from #weak_ciphers" do
weak = subject.weak_ciphers
weak.class.should == Array
weak.each do |cipher|
cipher[:weak].should == true
end
weak.count.should == 2
end
it "should return true from #supports_weak_ciphers" do
subject.supports_weak_ciphers?.should == true
end
end
context "when no weak ciphers are supported" do
before(:each) do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :accepted)
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :accepted)
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :accepted)
end
it "should return an empty array from #weak_ciphers" do
subject.weak_ciphers.should == []
end
it "should return false from #supports_weak_ciphers" do
subject.supports_weak_ciphers?.should == false
end
end
end
context "checking for standards compliance" do
it "should return true if there is no SSL support" do
subject.standards_compliant?.should == true
end
it "should return false if SSLv2 is supported or raise an SSLv2 exception" do
begin
subject.add_cipher(:SSLv2, "DES-CBC3-MD5", 168, :accepted)
subject.standards_compliant?.should == false
rescue ArgumentError => e
e.message.should == "unknown SSL method `SSLv2'."
end
end
it "should return false if weak ciphers are supported" do
subject.add_cipher(:SSLv3, "EXP-RC2-CBC-MD5", 40, :accepted)
subject.standards_compliant?.should == false
end
it "should return true if SSLv2 and Weak Ciphers are disabled" do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :accepted)
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :accepted)
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :accepted)
subject.standards_compliant?.should == true
end
end
context "when printing the results" do
context "when OpenSSL is compiled without SSLv2" do
before(:each) do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :accepted)
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :accepted)
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :accepted)
subject.openssl_sslv2 = false
end
it "should warn the user" do
subject.to_s.should include "*** WARNING: Your OS hates freedom! Your OpenSSL libs are compiled without SSLv2 support!"
end
end
context "when we have SSL results" do
before(:each) do
subject.add_cipher(:SSLv3, "AES256-SHA", 256, :accepted)
subject.add_cipher(:TLSv1, "AES256-SHA", 256, :accepted)
subject.add_cipher(:SSLv3, "AES128-SHA", 128, :accepted)
subject.add_cipher(:SSLv3, "EXP-RC2-CBC-MD5", 40, :accepted)
cert = OpenSSL::X509::Certificate.new
key = OpenSSL::PKey::RSA.new 2048
cert.version = 2 #
cert.serial = 1
cert.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby CA"
cert.issuer = cert.subject
cert.public_key = key.public_key
cert.not_before = Time.now
cert.not_after = cert.not_before + 2 * 365 * 24 * 60 * 60 # 2
subject.cert = cert
end
it "should contain the certificate" do
subject.to_s.should include "Issuer: DC=org, DC=ruby-lang, CN=Ruby CA"
subject.to_s.should include "Subject: DC=org, DC=ruby-lang, CN=Ruby CA"
end
it "should have a table with our SSL Cipher Results" do
subject.to_s.should include "Accepted * SSLv3 40 EXP-RC2-CBC-MD5"
subject.to_s.should include "Accepted SSLv3 128 AES128-SHA"
subject.to_s.should include "Accepted SSLv3 256 AES256-SHA"
subject.to_s.should include "Accepted TLSv1 256 AES256-SHA"
end
end
it "should return an appropriate message when SSL is not supported" do
subject.stub(:supports_ssl?).and_return(false)
subject.to_s.should == "Server does not appear to support SSL on this port!"
end
end
end

View File

@ -0,0 +1,106 @@
require 'rex/sslscan/scanner'
require 'rex/thread_factory'
require 'rex/text'
require 'rex/compat'
describe Rex::SSLScan::Scanner do
subject{Rex::SSLScan::Scanner.new("google.com", 443)}
it { should respond_to :host }
it { should respond_to :port }
it { should respond_to :timeout }
it { should respond_to :valid? }
context "when validating the scanner config" do
it "should return true when given a valid config" do
subject.valid?.should == true
end
it "should return false if given an invalid host" do
subject.host = nil
subject.valid?.should == false
end
it "should return false if given an invalid port" do
subject.port = nil
subject.valid?.should == false
end
it "should return false if given an invalid timeout" do
subject.timeout = nil
subject.valid?.should == false
end
end
context "when testing a single cipher" do
context "an exception should be raised if" do
it "has an invalid scanner configuration" do
subject.host =nil
expect{ subject.test_cipher(:SSLv2, "AES128-SHA")}.to raise_error
end
it "is given an invalid SSL version" do
expect{ subject.test_cipher(:SSLv5, "AES128-SHA")}.to raise_error
end
it "is given an invalid cipher" do
expect{ subject.test_cipher(:SSLv2, "FOO128-SHA")}.to raise_error
end
it "is given an invalid cipher for the SSL Version" do
expect{ subject.test_cipher(:SSLv3, 'DES-CBC3-MD5')}.to raise_error
end
end
context ":rejected should be returned if" do
it "scans a server that doesn't support the supplied SSL version" do
subject.test_cipher(:SSLv3, "DES-CBC-SHA").should == :rejected
end
it "scans a server that doesn't support the cipher" do
subject.test_cipher(:SSLv3, "DHE-DSS-AES256-SHA").should == :rejected
end
end
context ":accepted should be returned if" do
it "scans a server that accepts the given cipher" do
subject.test_cipher(:SSLv3, "AES256-SHA").should == :accepted
end
end
end
context "when retrieving the cert" do
it "should return nil if it can't connect" do
subject.get_cert(:SSLv3, "DES-CBC-SHA").should == nil
end
it "should return an X509 cert if it can connect" do
subject.get_cert(:SSLv3, "AES256-SHA").class.should == OpenSSL::X509::Certificate
end
end
context "when scanning https://google.com" do
it "should return a Result object" do
result = subject.scan
result.class.should == Rex::SSLScan::Result
end
context "if SSLv2 is not available locally" do
before(:each) do
subject.stub(:check_opensslv2).and_return(false)
subject.send(:initialize, 'google.com', 443)
end
it "should mark SSLv2 as unsupported" do
subject.supported_versions.should_not include :SSLv2
subject.sslv2.should == false
end
it "should not test any SSLv2 ciphers" do
res = subject.scan
res.sslv2.should == []
end
end
end
end