You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cryptcheck.rb 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. require 'colorize'
  2. require 'ipaddr'
  3. require 'timeout'
  4. require 'yaml'
  5. require 'cryptcheck/tls/fixture'
  6. module CryptCheck
  7. MAX_ANALYSIS_DURATION = 120
  8. PARALLEL_ANALYSIS = 10
  9. class AnalysisFailure
  10. attr_reader :error
  11. def initialize(error)
  12. @error = error
  13. end
  14. def to_s
  15. @error.to_s
  16. end
  17. end
  18. class NoTLSAvailableServer
  19. attr_reader :server
  20. def initialize(server)
  21. @server = OpenStruct.new hostname: server
  22. end
  23. def grade
  24. 'X'
  25. end
  26. end
  27. autoload :Status, 'cryptcheck/status'
  28. autoload :Logger, 'cryptcheck/logger'
  29. autoload :Tls, 'cryptcheck/tls'
  30. module Tls
  31. autoload :Cipher, 'cryptcheck/tls/cipher'
  32. autoload :Server, 'cryptcheck/tls/server'
  33. autoload :TcpServer, 'cryptcheck/tls/server'
  34. autoload :UdpServer, 'cryptcheck/tls/server'
  35. autoload :Grade, 'cryptcheck/tls/grade'
  36. autoload :Https, 'cryptcheck/tls/https'
  37. module Https
  38. autoload :Server, 'cryptcheck/tls/https/server'
  39. autoload :Grade, 'cryptcheck/tls/https/grade'
  40. end
  41. autoload :Xmpp, 'cryptcheck/tls/xmpp'
  42. module Xmpp
  43. autoload :Server, 'cryptcheck/tls/xmpp/server'
  44. autoload :Grade, 'cryptcheck/tls/xmpp/grade'
  45. end
  46. autoload :Smtp, 'cryptcheck/tls/smtp'
  47. module Smtp
  48. autoload :Server, 'cryptcheck/tls/smtp/server'
  49. autoload :Grade, 'cryptcheck/tls/smtp/grade'
  50. end
  51. end
  52. autoload :Ssh, 'cryptcheck/ssh'
  53. module Ssh
  54. autoload :Packet, 'cryptcheck/ssh/packet'
  55. autoload :Server, 'cryptcheck/ssh/server'
  56. autoload :SshNotSupportedServer, 'cryptcheck/ssh/server'
  57. autoload :Grade, 'cryptcheck/ssh/grade'
  58. end
  59. private
  60. def self.addresses(host)
  61. begin
  62. ip = IPAddr.new host
  63. return [[ip.family, ip.to_s, nil]]
  64. rescue IPAddr::InvalidAddressError
  65. end
  66. ::Addrinfo.getaddrinfo(host, nil, nil, :STREAM)
  67. .collect { |a| [a.afamily, a.ip_address, host] }
  68. end
  69. def self.analyze_addresses(host, addresses, port, server, grade, *args, **kargs)
  70. first = true
  71. addresses.collect do |family, ip|
  72. first ? (first = false) : Logger.info { '' }
  73. key = [host, ip, port]
  74. a = [host, family, ip, port, *args]
  75. begin
  76. ::Timeout::timeout MAX_ANALYSIS_DURATION do
  77. s = if kargs.empty?
  78. server.new *a
  79. else
  80. server.new *a, **kargs
  81. end
  82. if grade
  83. g = grade.new s
  84. Logger.info { '' }
  85. g.display
  86. [key, g]
  87. else
  88. [key, s]
  89. end
  90. end
  91. rescue => e
  92. e = Tls::Server::TLSException.new "Too long analysis (max #{MAX_ANALYSIS_DURATION.humanize})" \
  93. if e.message == 'execution expired'
  94. raise unless e.is_a? Tls::Server::TLSException
  95. Logger.error e
  96. [key, AnalysisFailure.new(e)]
  97. end
  98. end.to_h
  99. end
  100. def self.analyze(host, port, server, grade, *args, **kargs)
  101. addresses = begin
  102. addresses host
  103. rescue ::SocketError => e
  104. Logger::error e
  105. key = [host, nil, port]
  106. error = AnalysisFailure.new "Unable to resolve #{host}"
  107. return { key => error }
  108. end
  109. analyze_addresses host, addresses, port, server, grade, *args, **kargs
  110. end
  111. def self.analyze_hosts(hosts, template, output, groups: nil, &block)
  112. results = {}
  113. semaphore = ::Mutex.new
  114. ::Parallel.each hosts, progress: 'Analysing', in_threads: PARALLEL_ANALYSIS, finish: lambda { |item, _, _| puts item[1] } do |description, host|
  115. #hosts.each do |description, host|
  116. result = block.call host.strip
  117. result = result.values.first
  118. result = NoTLSAvailableServer.new(host) if result.is_a? AnalysisFailure
  119. semaphore.synchronize do
  120. if results.include? description
  121. results[description] << result
  122. else
  123. results[description] = [result]
  124. end
  125. end
  126. end
  127. results = ::Hash[groups.collect { |g| [g, results[g]] }] if groups
  128. results.each do |d, _|
  129. results[d].sort! do |a, b|
  130. cmp = score(a) <=> score(b)
  131. if cmp == 0
  132. cmp = a.server.hostname <=> b.server.hostname
  133. end
  134. cmp
  135. end
  136. end
  137. ::File.write output, ::ERB.new(::File.read template).result(binding)
  138. end
  139. def self.analyze_file(input, template, output, &block)
  140. config = ::YAML.load_file input
  141. hosts = []
  142. groups = []
  143. config.each do |c|
  144. d, hs = c['description'], c['hostnames']
  145. groups << d
  146. hs.each { |host| hosts << [d, host] }
  147. end
  148. self.analyze_hosts hosts, template, output, groups: groups, &block
  149. end
  150. private
  151. SCORES = %w(A+ A B+ B C+ C D E F G M T X)
  152. def self.score(a)
  153. SCORES.index a.grade
  154. end
  155. end