-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecure-sshd.sls
108 lines (91 loc) · 3.25 KB
/
secure-sshd.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
## Install openssh-server package if it is not installed
## with version not less than 6.6
Openssh-Package:
pkg.installed:
- pkgs:
- openssh-server: '>=6.5'
## Modify sshd-keygen service in order to generate
## only RSA and ED25519 Keys
Edit-sysconfig-sshd:
file.replace:
- name: /etc/sysconfig/sshd
- pattern: 'AUTOCREATE_SERVER_KEYS="RSA ECDSA ED25519"'
- repl: 'AUTOCREATE_SERVER_KEYS="RSA ED25519"'
## Delete ECDSA host keys
Delete-ECDSA-Keyfiles:
file.absent:
- names:
- /etc/ssh/ssh_host_ecdsa_key
- /etc/ssh/ssh_host_ecdsa_key.pub
# Filter out good moduli only. If paranoid, disable this section and enable the
# next two. Be warned however, generating moduli takes many hours!
Filter-Good-Moduli:
cmd.run:
- name: >
awk '$5 > 2000' /etc/ssh/moduli > "/tmp/moduli";
mv /tmp/moduli /etc/ssh/moduli
- onlyif: awk '$5 < 2000' /etc/ssh/moduli | wc -l | grep -v -w 0
## Delete Moduli file if it has weak keys
## Then, generate good moduli file if absent
# Delete-bad-Moduli:
# file.absent:
# - name: /etc/ssh/moduli
# - onlyif: awk '$5 < 2000' /etc/ssh/moduli | wc -l | grep -v -w 0
#
# Generate-Good-Moduli:
# cmd.run:
# - name: >
# ssh-keygen -G /etc/ssh/moduli.all -b 4096;
# ssh-keygen -T /etc/ssh/moduli.safe -f /etc/ssh/moduli.all;
# mv /etc/ssh/moduli.safe /etc/ssh/moduli;
# rm /etc/ssh/moduli.all
# - creates: /etc/ssh/moduli
## Modify the main config file and add KeyAlgorithms, Ciphers and MACs
Config-Changes:
augeas.change:
- lens: Sshd
- context: /files/etc/ssh/sshd_config
- changes:
- set KexAlgorithms '[email protected],diffie-hellman-group-exchange-sha256'
- set Ciphers '[email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr'
- set MACs/1 "[email protected]"
- set MACs/2 "[email protected]"
- set MACs/3 "[email protected]"
- set MACs/4 "[email protected]"
- set MACs/5 "hmac-sha2-512"
- set MACs/6 "hmac-sha2-256"
- set MACs/7 "hmac-ripemd160"
- set MACs/8 "[email protected]"
- rm HostKey
- set HostKey[1] "/etc/ssh/ssh_host_ed25519_key"
- set HostKey[2] "/etc/ssh/ssh_host_rsa_key"
# - onchanges_in:
# - file: Delete-bad-RSA-Key
## First deletes weak RSA key, second generates good one
## if RSA is absent, third sets the right permissions
## and mode.
## the 3 states will not be executed if there is no change
## in configuration file
Delete-bad-RSA-Key:
file.absent:
- names:
- /etc/ssh/ssh_host_rsa_key.pub
- /etc/ssh/ssh_host_rsa_key
- onlyif: ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key| awk '$1 < 4096' | grep RSA
Generate-good-RSA-key:
cmd.run:
- name: ssh-keygen -t rsa -b 4096 -N '' -f /etc/ssh/ssh_host_rsa_key < /dev/null
- creates: /etc/ssh/ssh_host_rsa_key
RSA-key-Permissions:
file.managed:
- name: /etc/ssh/ssh_host_rsa_key
- user: root
- group: root
- mode: 600
## Restart sshd only if there is a change in configuration file
sshd-restart:
module.run:
- name: service.restart
- m_name: sshd
- onchanges:
- augeas: Config-Changes