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

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