Skip to content

Commit a2d1eaa

Browse files
Use different algorithms depending on the ssh version
1 parent b882cdd commit a2d1eaa

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ override['ssh-hardening']['ssh']['server']['listen_to'] = node['ipaddress']
4646
* `['ssh-hardening']['ssh']['client']['remote_hosts']` - `[]` - one or more hosts, to which ssh-client can connect to.
4747
* `['ssh-hardening']['ssh']['client']['password_authentication']` - `false`. Set to `true` if password authentication should be enabled.
4848
* `['ssh-hardening']['ssh']['client']['roaming']` - `false`. Set to `true` if experimental client roaming should be enabled. This is known to cause potential issues with secrets being disclosed to malicious servers and defaults to being disabled.
49+
* `['ssh-hardening']['ssh']['server']['host_key_files']` - `nil` to calculate best hostkey configuration based on server version, otherwise specify an array with file paths (e.g. `/etc/ssh/ssh_host_rsa_key`)
4950
* `['ssh-hardening']['ssh']['server']['dh_min_prime_size']` - `2048` - Minimal acceptable prime length in bits in `/etc/ssh/moduli`. Primes below this number will get removed. (See [this](https://entropux.net/article/openssh-moduli/) for more information and background)
5051
* `['ssh-hardening']['ssh']['server']['dh_build_primes']` - `false` - If own primes should be built. This rebuild happens only once and takes a lot of time (~ 1.5 - 2h on the modern hardware for 4096 length).
5152
* `['ssh-hardening']['ssh']['server']['dh_build_primes_size']` - `4096` - Prime length which should be generated. This option is only valid if `dh_build_primes` is enabled.

attributes/default.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
default['ssh-hardening']['ssh']['server']['dh_min_prime_size'] = 2048
7474
default['ssh-hardening']['ssh']['server']['dh_build_primes'] = false
7575
default['ssh-hardening']['ssh']['server']['dh_build_primes_size'] = 4096
76-
default['ssh-hardening']['ssh']['server']['host_key_files'] = ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_ecdsa_key']
76+
default['ssh-hardening']['ssh']['server']['host_key_files'] = nil
7777
default['ssh-hardening']['ssh']['server']['client_alive_interval'] = 600 # 10min
7878
default['ssh-hardening']['ssh']['server']['client_alive_count'] = 3 # ~> 3 x interval
7979
default['ssh-hardening']['ssh']['server']['allow_root_with_key'] = false

libraries/devsec_ssh.rb

+14
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ class Ssh # rubocop:disable Metrics/ClassLength
7070
5.3 => 'yes',
7171
5.9 => 'sandbox'
7272
}.freeze
73+
# Hostkey algorithms
74+
# In the current implementation they are server specific so we need own data hash for it
75+
HOSTKEY_ALGORITHMS ||= {
76+
5.3 => %w(rsa),
77+
6.0 => %w(rsa ecdsa ed25519)
78+
}.freeze
7379

7480
class << self
7581
def get_server_privilege_separarion # rubocop:disable Style/AccessorMethodName
@@ -80,6 +86,14 @@ def get_server_privilege_separarion # rubocop:disable Style/AccessorMethodName
8086
ret
8187
end
8288

89+
def get_server_algorithms # rubocop:disable Style/AccessorMethodName
90+
Chef::Log.debug('Called get_server_algorithms')
91+
found_ssh_version = find_ssh_version(get_ssh_server_version, HOSTKEY_ALGORITHMS.keys)
92+
ret = HOSTKEY_ALGORITHMS[found_ssh_version]
93+
Chef::Log.debug("Using configuration for ssh version #{found_ssh_version}, value: #{ret}")
94+
ret
95+
end
96+
8397
def get_client_macs(enable_weak = false)
8498
get_crypto_data(:macs, :client, enable_weak)
8599
end

recipes/server.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@
170170
mac: node['ssh-hardening']['ssh']['server']['mac'] || DevSec::Ssh.get_server_macs(node['ssh-hardening']['ssh']['server']['weak_hmac']),
171171
kex: node['ssh-hardening']['ssh']['server']['kex'] || DevSec::Ssh.get_server_kexs(node['ssh-hardening']['ssh']['server']['weak_kex']),
172172
cipher: node['ssh-hardening']['ssh']['server']['cipher'] || DevSec::Ssh.get_server_ciphers(node['ssh-hardening']['ssh']['server']['cbc_required']),
173-
use_priv_sep: node['ssh-hardening']['ssh']['use_privilege_separation'] || DevSec::Ssh.get_server_privilege_separarion
173+
use_priv_sep: node['ssh-hardening']['ssh']['use_privilege_separation'] || DevSec::Ssh.get_server_privilege_separarion,
174+
hostkeys: node['ssh-hardening']['ssh']['server']['host_key_files'] || DevSec::Ssh.get_server_algorithms.map { |alg| "/etc/ssh/ssh_host_#{alg}_key"}
174175
)
175176
notifies :restart, 'service[sshd]'
176177
end

spec/libraries/devsec_ssh_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,27 @@ def self.debug(*); end
202202
end
203203
end
204204

205+
describe 'get_server_algorithms' do
206+
DevSec::Ssh::HOSTKEY_ALGORITHMS.each do |openssh_version, conf_data|
207+
context "when openssh is >= #{openssh_version}" do
208+
before :each do
209+
# mock get_ssh_server_version. We test it somewhere else
210+
expect(subject).to receive(:get_ssh_server_version) { openssh_version }
211+
end
212+
213+
it "get the config value #{conf_data}" do
214+
expect(subject.get_server_algorithms).to eq conf_data
215+
end
216+
end
217+
end
218+
context 'when openssh has a totaly unsupported version, e.g. 3.0' do
219+
it 'should raise an exception' do
220+
expect(subject).to receive(:get_ssh_server_version) { 3.0 }
221+
expect { subject.get_server_algorithms }.to raise_exception(/Unsupported ssh version/)
222+
end
223+
end
224+
end
225+
205226
# Here we test the public functions:
206227
# get_[client|server]_[kexs|macs|ciphers]
207228
# In order to cover all possible combinations, we need a complex nested loops:-\

templates/default/opensshd.conf.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ListenAddress <%= ssh_ip %>
3232
<% end %>
3333

3434
# List HostKeys here.
35-
<% Array(@node['ssh-hardening']['ssh']['server']['host_key_files']).each do |host_key_file| %>
35+
<% @hostkeys.each do |host_key_file| %>
3636
HostKey <%= host_key_file %> # Req 20
3737
<% end %>
3838

0 commit comments

Comments
 (0)