Skip to content

Commit 8f10f45

Browse files
Allow building of own DH primes
Per default its disabled, as it takes a lot of time
1 parent c668f66 commit 8f10f45

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ override['ssh-hardening']['ssh']['server']['listen_to'] = node['ipaddress']
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.
4949
* `['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)
50+
* `['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).
51+
* `['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.
5052
* `['ssh-hardening']['ssh']['server']['listen_to']` `#override attribute#` - one or more ip addresses, to which ssh-server should listen to. Default is to listen on all interfaces. It should be configured for security reasons!
5153
* `['ssh-hardening']['ssh']['server']['allow_root_with_key']` - `false` to disable root login altogether. Set to `true` to allow root to login via key-based mechanism
5254
* `['ssh-hardening']['ssh']['server']['allow_tcp_forwarding']` - `false`. Set to `true` to allow TCP Forwarding

attributes/default.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
default['ssh-hardening']['ssh']['server']['weak_hmac'] = false
7272
default['ssh-hardening']['ssh']['server']['weak_kex'] = false
7373
default['ssh-hardening']['ssh']['server']['dh_min_prime_size'] = 2048
74+
default['ssh-hardening']['ssh']['server']['dh_build_primes'] = false
75+
default['ssh-hardening']['ssh']['server']['dh_build_primes_size'] = 4096
7476
default['ssh-hardening']['ssh']['server']['host_key_files'] = ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_dsa_key', '/etc/ssh/ssh_host_ecdsa_key']
7577
default['ssh-hardening']['ssh']['server']['client_alive_interval'] = 600 # 10min
7678
default['ssh-hardening']['ssh']['server']['client_alive_count'] = 3 # ~> 3 x interval

recipes/server.rb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,23 @@
3131
end
3232

3333
# some internal definitions
34+
cache_dir = ::File.join(Chef::Config[:file_cache_path], cookbook_name.to_s)
3435
dh_moduli_file = '/etc/ssh/moduli'
3536

37+
# create a cache dir for this cookbook
38+
# we use it for storing of lock files or selinux files
39+
directory cache_dir
40+
3641
# installs package name
3742
package 'openssh-server' do
3843
package_name node['ssh-hardening']['sshserver']['package']
3944
end
4045

4146
# Handle addional SELinux policy on RHEL/Fedora for different UsePAM options
4247
if %w(fedora rhel).include?(node['platform_family'])
43-
policy_dir = ::File.join(Chef::Config[:file_cache_path], cookbook_name.to_s)
44-
policy_file = ::File.join(policy_dir, 'ssh_password.te')
45-
module_file = ::File.join(policy_dir, 'ssh_password.mod')
46-
package_file = ::File.join(policy_dir, 'ssh_password.pp')
48+
policy_file = ::File.join(cache_dir, 'ssh_password.te')
49+
module_file = ::File.join(cache_dir, 'ssh_password.mod')
50+
package_file = ::File.join(cache_dir, 'ssh_password.pp')
4751

4852
package 'policycoreutils-python'
4953
# on fedora we need an addtional package for semodule_package
@@ -59,8 +63,6 @@
5963
else
6064
# UsePAM no: enable and install the additional SELinux policy
6165

62-
directory policy_dir
63-
6466
cookbook_file policy_file do
6567
source 'ssh_password.te'
6668
end
@@ -77,6 +79,23 @@
7779
end
7880

7981
# handle Diffie-Hellman moduli
82+
# build own moduli file if required
83+
own_primes_lock_file = ::File.join(cache_dir, 'moduli.lock')
84+
bash 'build own primes for DH' do
85+
code <<-EOS
86+
set -e
87+
tempdir=$(mktemp -d)
88+
ssh-keygen -G $tempdir/moduli.all -b #{node['ssh-hardening']['ssh']['server']['dh_build_primes_size']}
89+
ssh-keygen -T $tempdir/moduli.safe -f $tempdir/moduli.all
90+
cp $tempdir/moduli.safe #{dh_moduli_file}
91+
rm -rf $tempdir
92+
touch #{own_primes_lock_file}
93+
EOS
94+
only_if { node['ssh-hardening']['ssh']['server']['dh_build_primes'] }
95+
not_if { ::File.exist?(own_primes_lock_file) }
96+
notifies :restart, 'service[sshd]'
97+
end
98+
8099
# remove all small primes
81100
# https://stribika.github.io/2015/01/04/secure-secure-shell.html
82101
dh_min_prime_size = node['ssh-hardening']['ssh']['server']['dh_min_prime_size'].to_i - 1 # 4096 is 4095 in the moduli file

spec/recipes/server_spec.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
stub_command("test $(awk '$5 < 2047 && $5 ~ /^[0-9]+$/ { print $5 }' /etc/ssh/moduli | uniq | wc -c) -eq 0").and_return(dh_primes_ok)
3333
end
3434

35+
it 'should create cache directory' do
36+
expect(chef_run).to create_directory('/tmp/ssh-hardening-file-cache/ssh-hardening')
37+
end
38+
3539
it 'installs openssh-server' do
3640
expect(chef_run).to install_package('openssh-server')
3741
end
@@ -234,7 +238,6 @@
234238
let(:version) { '16.04' }
235239

236240
it 'does not invoke any SELinux resources' do
237-
expect(chef_run).not_to create_directory('/tmp/ssh-hardening-file-cache/ssh-hardening')
238241
expect(chef_run).not_to render_file('/tmp/ssh-hardening-file-cache/ssh-hardening/ssh_password.te')
239242
expect(chef_run).not_to run_execute('remove selinux policy')
240243
expect(chef_run).not_to run_bash('build selinux package and install it')
@@ -305,10 +308,6 @@
305308
expect(chef_run).to render_file('/etc/ssh/sshd_config').with_content('UsePAM no')
306309
end
307310

308-
it 'should create cache directory for policy files' do
309-
expect(chef_run).to create_directory('/tmp/ssh-hardening-file-cache/ssh-hardening')
310-
end
311-
312311
it 'should create selinux source policy file' do
313312
expect(chef_run).to render_file('/tmp/ssh-hardening-file-cache/ssh-hardening/ssh_password.te')
314313
end
@@ -332,6 +331,10 @@
332331
end
333332
end
334333

334+
it 'should not build own DH primes per default' do
335+
expect(chef_run).not_to run_bash('build own primes for DH')
336+
end
337+
335338
describe 'DH primes handling' do
336339
let(:chef_run) do
337340
ChefSpec::ServerRunner.new.converge(described_recipe)

0 commit comments

Comments
 (0)