Techioz Blog

Ruby を使用して、Web サーバーがサポートする暗号スイートを確認するにはどうすればよいですか?

概要

使用されている TLS 暗号スイートをターゲット ホストに対して検証する自動チェッカーを Ruby で作成したいと考えています。組み込みライブラリを使用しても外部 gem を使用しても構いませんが、今のところ何も見つかりません。

サポートされている暗号スイートの簡単なリストを取得したいと考えています。

解決策

最初の回答は質問を読み間違えたため不正解でした。良い出発点ではないにしても、これで希望どおりのことができると思います。これについてはクリーンアップやリファクタリングは行っていません。

require 'openssl'
require 'socket'

# it doesn't matter that this would give a 301, this isn't HTTP
hostname = 'google.co.uk'
port = 443

# we cannot directly get to the SSL socket using net/http because
# it is already gone when we get the response back and it doesn't
# remember it, so we have to interact with OpenSSL directly like you
# would with the CLI version: `openssl s_client -cipher "TLSv1.2" -host google.co.uk -port 443`

# keep track of which ones work
success = []

# get a list of our ciphers we know about
ciphers = OpenSSL::Cipher.ciphers

# try each one
ciphers.each do |cipher|
  puts "Trying: #{cipher} ..."

  begin
    context = OpenSSL::SSL::SSLContext.new
    context.ciphers = [cipher.upcase]
    socket = TCPSocket.new(hostname, port)
    ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, context)
    ssl_socket.connect

    puts "Negotiated Cipher: #{ssl_socket.cipher[0]}"
    puts "Remote Ciphers:"
    puts ssl_socket.ssl_version
    ssl_socket.close

    # if we get this far, it worked
    success << cipher.upcase
  rescue OpenSSL::SSL::SSLError => e
    # do nothing
  end
end


puts "All the ones that worked:"
puts success

答えを知っているホストと照合してみます。