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.

183 lines
4.2KB

  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. def score
  27. 0
  28. end
  29. end
  30. autoload :Logger, 'cryptcheck/logger'
  31. autoload :Tls, 'cryptcheck/tls'
  32. module Tls
  33. autoload :Cipher, 'cryptcheck/tls/cipher'
  34. autoload :Server, 'cryptcheck/tls/server'
  35. autoload :TcpServer, 'cryptcheck/tls/server'
  36. autoload :UdpServer, 'cryptcheck/tls/server'
  37. autoload :Grade, 'cryptcheck/tls/grade'
  38. autoload :Https, 'cryptcheck/tls/https'
  39. module Https
  40. autoload :Server, 'cryptcheck/tls/https/server'
  41. autoload :Grade, 'cryptcheck/tls/https/grade'
  42. end
  43. autoload :Xmpp, 'cryptcheck/tls/xmpp'
  44. module Xmpp
  45. autoload :Server, 'cryptcheck/tls/xmpp/server'
  46. autoload :Grade, 'cryptcheck/tls/xmpp/grade'
  47. end
  48. autoload :Smtp, 'cryptcheck/tls/smtp'
  49. module Smtp
  50. autoload :Server, 'cryptcheck/tls/smtp/server'
  51. autoload :Grade, 'cryptcheck/tls/smtp/grade'
  52. end
  53. end
  54. autoload :Ssh, 'cryptcheck/ssh'
  55. module Ssh
  56. autoload :Packet, 'cryptcheck/ssh/packet'
  57. autoload :Server, 'cryptcheck/ssh/server'
  58. autoload :SshNotSupportedServer, 'cryptcheck/ssh/server'
  59. autoload :Grade, 'cryptcheck/ssh/grade'
  60. end
  61. private
  62. def self.addresses(host)
  63. begin
  64. ip = IPAddr.new host
  65. return [[ip.family, ip.to_s, nil]]
  66. rescue IPAddr::InvalidAddressError
  67. end
  68. ::Addrinfo.getaddrinfo(host, nil, nil, :STREAM)
  69. .collect { |a| [a.afamily, a.ip_address, host] }
  70. end
  71. def self.analyze_addresses(host, addresses, port, server, grade, *args, **kargs)
  72. first = true
  73. addresses.collect do |family, ip|
  74. first ? (first = false) : Logger.info { '' }
  75. key = [host, ip, port]
  76. a = [host, family, ip, port, *args]
  77. begin
  78. ::Timeout::timeout MAX_ANALYSIS_DURATION do
  79. s = if kargs.empty?
  80. server.new *a
  81. else
  82. server.new *a, **kargs
  83. end
  84. if grade
  85. g = grade.new s
  86. Logger.info { '' }
  87. g.display
  88. [key, g]
  89. else
  90. [key, s]
  91. end
  92. end
  93. rescue => e
  94. e = "Too long analysis (max #{MAX_ANALYSIS_DURATION.humanize})" if e.message == 'execution expired'
  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 = b.score <=> a.score
  133. if cmp == 0
  134. cmp = a.server.hostname <=> b.server.hostname
  135. end
  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 A- B C D E F T M X)
  155. def self.score(a)
  156. SCORES.index a.grade
  157. end
  158. end