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

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