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.4KB

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