Class: SSHScan::PolicyManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_scan/policy_manager.rb

Overview

Policy management methods, compliance checking and recommendations.

Instance Method Summary collapse

Constructor Details

#initialize(result, policy) ⇒ PolicyManager

Returns a new instance of PolicyManager.



6
7
8
9
# File 'lib/ssh_scan/policy_manager.rb', line 6

def initialize(result, policy)
  @policy = policy
  @result = result
end

Instance Method Details

#compliance_resultsObject



213
214
215
216
217
218
219
220
# File 'lib/ssh_scan/policy_manager.rb', line 213

def compliance_results
  {
    "policy" => @policy.name,
    "compliant" => compliant?,
    "recommendations" => recommendations,
    "references" => @policy.references,
  }
end

#compliant?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ssh_scan/policy_manager.rb', line 142

def compliant?
  out_of_policy_encryption.empty? &&
  out_of_policy_macs.empty? &&
  out_of_policy_kex.empty? &&
  out_of_policy_compression.empty? &&
  missing_policy_encryption.empty? &&
  missing_policy_macs.empty? &&
  missing_policy_kex.empty? &&
  missing_policy_compression.empty? &&
  out_of_policy_auth_methods.empty? &&
  !out_of_policy_ssh_version
end

#missing_policy_compressionObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ssh_scan/policy_manager.rb', line 100

def missing_policy_compression
  return [] if @policy.compression.empty?
  target_compressions =
    @result.compression_algorithms_server_to_client |
    @result.compression_algorithms_client_to_server
  outliers = []

  @policy.compression.each do |compression|
    if SSHScan.make_attributes(target_compressions).include?(SSHScan::Attribute.new(compression)) == false
      outliers << compression
    end
  end
  return outliers
end

#missing_policy_encryptionObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ssh_scan/policy_manager.rb', line 23

def missing_policy_encryption
  return [] if @policy.encryption.empty?
  target_encryption =
    @result.encryption_algorithms_client_to_server |
    @result.encryption_algorithms_server_to_client
  outliers = []
  @policy.encryption.each do |encryption|
    if SSHScan.make_attributes(target_encryption).include?(SSHScan::Attribute.new(encryption)) == false
      outliers << encryption
    end
  end
  return outliers
end

#missing_policy_kexObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ssh_scan/policy_manager.rb', line 74

def missing_policy_kex
  return [] if @policy.kex.empty?
  target_kex = @result.key_algorithms
  outliers = []

  @policy.kex.each do |kex|
    if SSHScan.make_attributes(target_kex).include?(SSHScan::Attribute.new(kex)) == false
      outliers << kex
    end
  end
  return outliers
end

#missing_policy_macsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ssh_scan/policy_manager.rb', line 49

def missing_policy_macs
  return [] if @policy.macs.empty?
  target_macs =
    @result.mac_algorithms_server_to_client |
    @result.mac_algorithms_client_to_server
  outliers = []

  @policy.macs.each do |mac|
    if SSHScan.make_attributes(target_macs).include?(SSHScan::Attribute.new(mac)) == false
      outliers << mac
    end
  end
  return outliers
end

#out_of_policy_auth_methodsObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ssh_scan/policy_manager.rb', line 115

def out_of_policy_auth_methods
  return [] if @policy.auth_methods.empty?
  return [] if @result.auth_methods.empty?
  target_auth_methods = @result.auth_methods
  outliers = []

  if not @policy.auth_methods.empty?
    target_auth_methods.each do |auth_method|
      if not @policy.auth_methods.include?(auth_method)
        outliers << auth_method
      end
    end
  end
  return outliers
end

#out_of_policy_compressionObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ssh_scan/policy_manager.rb', line 87

def out_of_policy_compression
  return [] if @policy.compression.empty?
  target_compressions =
    @result.compression_algorithms_server_to_client |
    @result.compression_algorithms_client_to_server
  outliers = []
  target_compressions.each do |target_compression|
    outliers << target_compression unless
      @policy.compression_attributes.include?(SSHScan::Attribute.new(target_compression))
  end
  return outliers
end

#out_of_policy_encryptionObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ssh_scan/policy_manager.rb', line 11

def out_of_policy_encryption
  return [] if @policy.encryption.empty?
  target_encryption =
    @result.encryption_algorithms_client_to_server |
    @result.encryption_algorithms_server_to_client
  outliers = []
  target_encryption.each do |target_enc|
    outliers << target_enc unless @policy.encryption_attributes.include?(SSHScan::Attribute.new(target_enc))
  end
  return outliers
end

#out_of_policy_kexObject



64
65
66
67
68
69
70
71
72
# File 'lib/ssh_scan/policy_manager.rb', line 64

def out_of_policy_kex
  return [] if @policy.kex.empty?
  target_kexs = @result.key_algorithms
  outliers = []
  target_kexs.each do |target_kex|
    outliers << target_kex unless @policy.kex_attributes.include?(SSHScan::Attribute.new(target_kex))
  end
  return outliers
end

#out_of_policy_macsObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ssh_scan/policy_manager.rb', line 37

def out_of_policy_macs
  return [] if @policy.macs.empty?
  target_macs =
    @result.mac_algorithms_server_to_client |
    @result.mac_algorithms_client_to_server
  outliers = []
  target_macs.each do |target_mac|
    outliers << target_mac unless @policy.mac_attributes.include?(SSHScan::Attribute.new(target_mac))
  end
  return outliers
end

#out_of_policy_ssh_versionObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/ssh_scan/policy_manager.rb', line 131

def out_of_policy_ssh_version
  return false if @policy.ssh_version.nil?
  target_ssh_version = @result.ssh_version
  if @policy.ssh_version
    if target_ssh_version < @policy.ssh_version
      return true
    end
  end
  return false
end

#recommendationsObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ssh_scan/policy_manager.rb', line 155

def recommendations
  recommendations = []

  # Add these items to be compliant
  if missing_policy_kex.any?
    recommendations << "Add these key exchange algorithms: \
#{missing_policy_kex.join(",")}"
  end

  if missing_policy_macs.any?
    recommendations << "Add these MAC algorithms: \
#{missing_policy_macs.join(",")}"
  end

  if missing_policy_encryption.any?
    recommendations << "Add these encryption ciphers: \
#{missing_policy_encryption.join(",")}"
  end

  if missing_policy_compression.any?
    recommendations << "Add these compression algorithms: \
#{missing_policy_compression.join(",")}"
  end

  # Remove these items to be compliant
  if out_of_policy_kex.any?
    recommendations << "Remove these key exchange algorithms: \
#{out_of_policy_kex.join(", ")}"
  end

  if out_of_policy_macs.any?
    recommendations << "Remove these MAC algorithms: \
#{out_of_policy_macs.join(", ")}"
  end

  if out_of_policy_encryption.any?
    recommendations << "Remove these encryption ciphers: \
#{out_of_policy_encryption.join(", ")}"
  end

  if out_of_policy_compression.any?
    recommendations << "Remove these compression algorithms: \
#{out_of_policy_compression.join(", ")}"
  end

  if out_of_policy_auth_methods.any?
    recommendations << "Remove these authentication methods: \
#{out_of_policy_auth_methods.join(", ")}"
  end

  # Update these items to be compliant
  if out_of_policy_ssh_version
    recommendations << "Update your ssh version to: #{@policy.ssh_version}"
  end

  return recommendations
end