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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. require 'colorize'
  2. require 'ipaddr'
  3. require 'timeout'
  4. require 'yaml'
  5. module CryptCheck
  6. MAX_ANALYSIS_DURATION = 120
  7. PARALLEL_ANALYSIS = 10
  8. class AnalysisFailure
  9. attr_reader :error
  10. def initialize(error)
  11. @error = error
  12. end
  13. def to_s
  14. @error.to_s
  15. end
  16. end
  17. class NoTLSAvailableServer
  18. attr_reader :server
  19. def initialize(server)
  20. @server = OpenStruct.new hostname: server
  21. end
  22. def grade
  23. 'X'
  24. end
  25. end
  26. autoload :Status, 'cryptcheck/status'
  27. autoload :Statused, 'cryptcheck/statused'
  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. ap s.status
  86. exit
  87. if grade
  88. g = grade.new s
  89. Logger.info { '' }
  90. g.display
  91. [key, g]
  92. else
  93. [key, s]
  94. end
  95. end
  96. rescue => e
  97. e = Tls::Server::TLSException.new "Too long analysis (max #{MAX_ANALYSIS_DURATION.humanize})" \
  98. if e.message == 'execution expired'
  99. raise unless e.is_a? Tls::Server::TLSException
  100. Logger.error e
  101. [key, AnalysisFailure.new(e)]
  102. end
  103. end.to_h
  104. end
  105. def self.analyze(host, port, server, grade, *args, **kargs)
  106. addresses = begin
  107. addresses host
  108. rescue ::SocketError => e
  109. Logger::error e
  110. key = [host, nil, port]
  111. error = AnalysisFailure.new "Unable to resolve #{host}"
  112. return { key => error }
  113. end
  114. analyze_addresses host, addresses, port, server, grade, *args, **kargs
  115. end
  116. def self.analyze_hosts(hosts, template, output, groups: nil, &block)
  117. results = {}
  118. semaphore = ::Mutex.new
  119. ::Parallel.each hosts, progress: 'Analysing', in_threads: PARALLEL_ANALYSIS, finish: lambda { |item, _, _| puts item[1] } do |description, host|
  120. #hosts.each do |description, host|
  121. result = block.call host.strip
  122. result = result.values.first
  123. result = NoTLSAvailableServer.new(host) if result.is_a? AnalysisFailure
  124. semaphore.synchronize do
  125. if results.include? description
  126. results[description] << result
  127. else
  128. results[description] = [result]
  129. end
  130. end
  131. end
  132. results = ::Hash[groups.collect { |g| [g, results[g]] }] if groups
  133. results.each do |d, _|
  134. results[d].sort! do |a, b|
  135. cmp = score(a) <=> score(b)
  136. if cmp == 0
  137. cmp = a.server.hostname <=> b.server.hostname
  138. end
  139. cmp
  140. end
  141. end
  142. ::File.write output, ::ERB.new(::File.read template).result(binding)
  143. end
  144. def self.analyze_file(input, template, output, &block)
  145. config = ::YAML.load_file input
  146. hosts = []
  147. groups = []
  148. config.each do |c|
  149. d, hs = c['description'], c['hostnames']
  150. groups << d
  151. hs.each { |host| hosts << [d, host] }
  152. end
  153. self.analyze_hosts hosts, template, output, groups: groups, &block
  154. end
  155. private
  156. SCORES = %w(A+ A B+ B C+ C D E F G M T X)
  157. def self.score(a)
  158. SCORES.index a.grade
  159. end
  160. end
  161. require 'cryptcheck/fixture'
  162. require 'cryptcheck/tls/fixture'