2013-02-07 18:42:18 +00:00
|
|
|
|
|
|
|
require 'rex/socket'
|
2013-02-10 01:06:17 +00:00
|
|
|
require 'rex/ui/text/table'
|
2013-02-07 18:42:18 +00:00
|
|
|
|
|
|
|
module Rex::SSLScan
|
|
|
|
class Result
|
|
|
|
|
2013-03-05 05:43:18 +00:00
|
|
|
attr_accessor :openssl_sslv2
|
2013-03-04 23:39:28 +00:00
|
|
|
|
2013-02-08 17:33:36 +00:00
|
|
|
attr_reader :ciphers
|
2013-02-07 18:42:18 +00:00
|
|
|
attr_reader :supported_versions
|
|
|
|
|
|
|
|
def initialize()
|
|
|
|
@cert = nil
|
2013-03-07 17:11:48 +00:00
|
|
|
@ciphers = Set.new
|
2013-02-07 20:56:26 +00:00
|
|
|
@supported_versions = [:SSLv2, :SSLv3, :TLSv1]
|
2013-02-07 18:42:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cert
|
|
|
|
@cert
|
|
|
|
end
|
|
|
|
|
|
|
|
def cert=(input)
|
|
|
|
unless input.kind_of? OpenSSL::X509::Certificate or input.nil?
|
2013-03-07 17:35:34 +00:00
|
|
|
raise ArgumentError, "Must be an X509 Cert!"
|
2013-02-07 18:42:18 +00:00
|
|
|
end
|
|
|
|
@cert = input
|
|
|
|
end
|
|
|
|
|
2013-02-08 17:33:36 +00:00
|
|
|
def sslv2
|
|
|
|
@ciphers.reject{|cipher| cipher[:version] != :SSLv2 }
|
|
|
|
end
|
|
|
|
|
2013-02-08 19:25:49 +00:00
|
|
|
def sslv3
|
|
|
|
@ciphers.reject{|cipher| cipher[:version] != :SSLv3 }
|
|
|
|
end
|
2013-02-07 20:56:26 +00:00
|
|
|
|
2013-02-08 19:25:49 +00:00
|
|
|
def tlsv1
|
|
|
|
@ciphers.reject{|cipher| cipher[:version] != :TLSv1 }
|
|
|
|
end
|
2013-02-07 20:56:26 +00:00
|
|
|
|
2013-02-08 19:25:49 +00:00
|
|
|
def weak_ciphers
|
2013-02-26 20:13:17 +00:00
|
|
|
accepted.reject{|cipher| cipher[:weak] == false }
|
2013-02-08 19:25:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def strong_ciphers
|
2013-02-26 20:13:17 +00:00
|
|
|
accepted.reject{|cipher| cipher[:weak] }
|
2013-02-07 20:56:26 +00:00
|
|
|
end
|
|
|
|
|
2013-02-21 00:47:20 +00:00
|
|
|
# Returns all accepted ciphers matching the supplied version
|
2013-03-07 17:52:58 +00:00
|
|
|
# @param version [Symbol, Array] The SSL Version to filter on
|
2013-02-21 00:47:20 +00:00
|
|
|
# @raise [ArgumentError] if the version supplied is invalid
|
|
|
|
# @return [Array] An array of accepted cipher details matching the supplied versions
|
2013-02-08 17:33:36 +00:00
|
|
|
def accepted(version = :all)
|
2013-03-07 18:45:01 +00:00
|
|
|
enum_ciphers(:accepted, version)
|
2013-02-07 18:42:18 +00:00
|
|
|
end
|
|
|
|
|
2013-02-21 00:47:20 +00:00
|
|
|
# Returns all rejected ciphers matching the supplied version
|
2013-03-07 17:52:58 +00:00
|
|
|
# @param version [Symbol, Array] The SSL Version to filter on
|
2013-02-21 00:47:20 +00:00
|
|
|
# @raise [ArgumentError] if the version supplied is invalid
|
|
|
|
# @return [Array] An array of rejected cipher details matching the supplied versions
|
2013-02-08 17:33:36 +00:00
|
|
|
def rejected(version = :all)
|
2013-03-07 18:45:01 +00:00
|
|
|
enum_ciphers(:rejected, version)
|
2013-02-08 17:33:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def each_accepted(version = :all)
|
|
|
|
accepted(version).each do |cipher_result|
|
2013-02-07 21:51:07 +00:00
|
|
|
yield cipher_result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-08 17:33:36 +00:00
|
|
|
def each_rejected(version = :all)
|
|
|
|
rejected(version).each do |cipher_result|
|
2013-02-07 21:51:07 +00:00
|
|
|
yield cipher_result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def supports_sslv2?
|
2013-02-08 17:33:36 +00:00
|
|
|
!(accepted(:SSLv2).empty?)
|
2013-02-07 21:51:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def supports_sslv3?
|
2013-02-08 17:33:36 +00:00
|
|
|
!(accepted(:SSLv3).empty?)
|
2013-02-07 21:51:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def supports_tlsv1?
|
2013-02-08 17:33:36 +00:00
|
|
|
!(accepted(:TLSv1).empty?)
|
2013-02-07 21:51:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def supports_ssl?
|
|
|
|
supports_sslv2? or supports_sslv3? or supports_tlsv1?
|
|
|
|
end
|
2013-02-08 19:25:49 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-02-21 00:47:20 +00:00
|
|
|
# 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
|
2013-02-08 19:25:49 +00:00
|
|
|
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
|
2013-03-05 01:25:20 +00:00
|
|
|
raise ArgumentError, "Status must be either :accepted or :rejected"
|
2013-02-08 19:25:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
strong_cipher_ctx = OpenSSL::SSL::SSLContext.new(version)
|
2013-03-05 00:42:08 +00:00
|
|
|
# OpenSSL Directive For Strong Ciphers
|
|
|
|
# See: http://www.rapid7.com/vulndb/lookup/ssl-weak-ciphers
|
2013-02-08 19:25:49 +00:00
|
|
|
strong_cipher_ctx.ciphers = "ALL:!aNULL:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM"
|
2013-03-07 17:35:34 +00:00
|
|
|
|
2013-02-08 19:25:49 +00:00
|
|
|
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
|
2013-02-10 01:06:17 +00:00
|
|
|
|
|
|
|
def to_s
|
2013-03-07 17:35:34 +00:00
|
|
|
unless supports_ssl?
|
2013-02-10 01:06:17 +00:00
|
|
|
return "Server does not appear to support SSL on this port!"
|
|
|
|
end
|
|
|
|
table = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => 'SSL Ciphers',
|
|
|
|
'Indent' => 1,
|
2013-02-11 23:08:14 +00:00
|
|
|
'Columns' => ['Status', 'Weak', 'SSL Version', 'Key Length', 'Cipher'],
|
|
|
|
'SortIndex' => -1
|
2013-02-10 01:06:17 +00:00
|
|
|
)
|
|
|
|
ciphers.each do |cipher|
|
|
|
|
if cipher[:weak]
|
|
|
|
weak = '*'
|
|
|
|
else
|
|
|
|
weak = ' '
|
|
|
|
end
|
2013-02-11 23:08:14 +00:00
|
|
|
table << [cipher[:status].to_s.capitalize, weak , cipher[:version], cipher[:key_length], cipher[:cipher]]
|
2013-02-10 01:06:17 +00:00
|
|
|
end
|
2013-02-11 23:08:14 +00:00
|
|
|
|
|
|
|
# Sort by SSL Version, then Key Length, and then Status
|
|
|
|
table.rows.sort_by!{|row| [row[0],row[2],row[3]]}
|
2013-02-10 01:06:17 +00:00
|
|
|
text = "#{table.to_s}"
|
|
|
|
if @cert
|
2013-03-07 18:45:01 +00:00
|
|
|
text << " \n\n #{@cert.to_text}"
|
2013-02-10 01:06:17 +00:00
|
|
|
end
|
2013-03-05 05:43:18 +00:00
|
|
|
if openssl_sslv2 == false
|
|
|
|
text << "\n\n *** WARNING: Your OS hates freedom! Your OpenSSL libs are compiled without SSLv2 support!"
|
|
|
|
end
|
2013-02-10 01:06:17 +00:00
|
|
|
text
|
2013-03-07 18:45:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2013-03-07 17:35:34 +00:00
|
|
|
|
2013-03-07 18:50:43 +00:00
|
|
|
# @param state [Symbol] Either :accepted or :rejected
|
|
|
|
# @param version [Symbol, Array] The SSL Version to filter on (:SSLv2, :SSLv3, :TLSv1, :all)
|
2013-03-07 18:45:01 +00:00
|
|
|
# @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
|
2013-02-07 18:42:18 +00:00
|
|
|
end
|