Skip to content
This repository was archived by the owner on Dec 26, 2020. It is now read-only.

Commit f5f4521

Browse files
authored
Merge branch 'master' into feature/2fa_auth
2 parents 4d3f10f + 5eec48b commit f5f4521

File tree

9 files changed

+146
-42
lines changed

9 files changed

+146
-42
lines changed

.kitchen.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ driver:
99
transport:
1010
max_ssh_sessions: 5
1111

12-
transport:
13-
max_ssh_sessions: 5
14-
15-
1612
provisioner:
1713
name: ansible_playbook
1814
hosts: all
@@ -26,6 +22,7 @@ provisioner:
2622
http_proxy: <%= ENV['http_proxy'] || nil %>
2723
https_proxy: <%= ENV['https_proxy'] || nil %>
2824
playbook: default.yml
25+
ansible_diff: true
2926
ansible_extra_flags:
3027
- "--skip-tags=sysctl"
3128

CHANGELOG.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Change Log
22

3+
## [4.3.0](https://github.com/dev-sec/ansible-ssh-hardening/tree/4.3.0) (2017-08-03)
4+
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/4.2.0...4.3.0)
5+
6+
**Implemented enhancements:**
7+
8+
- Fix ansible.cfg settings [\#122](https://github.com/dev-sec/ansible-ssh-hardening/pull/122) ([fazlearefin](https://github.com/fazlearefin))
9+
- Finish 94 [\#116](https://github.com/dev-sec/ansible-ssh-hardening/pull/116) ([rndmh3ro](https://github.com/rndmh3ro))
10+
11+
**Merged pull requests:**
12+
13+
- Don't overwrite ssh\_host\_key\_files if set manually [\#125](https://github.com/dev-sec/ansible-ssh-hardening/pull/125) ([oakey-b1](https://github.com/oakey-b1))
14+
- Add comment filter to {{ansible\_managed}} string [\#121](https://github.com/dev-sec/ansible-ssh-hardening/pull/121) ([fazlearefin](https://github.com/fazlearefin))
15+
316
## [4.2.0](https://github.com/dev-sec/ansible-ssh-hardening/tree/4.2.0) (2017-06-30)
417
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/4.1.2...4.2.0)
518

@@ -187,29 +200,29 @@
187200
- sftp\_enable option [\#41](https://github.com/dev-sec/ansible-ssh-hardening/pull/41) ([fitz123](https://github.com/fitz123))
188201

189202
## [1.2.1](https://github.com/dev-sec/ansible-ssh-hardening/tree/1.2.1) (2015-10-16)
190-
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.2...1.2.1)
203+
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.2.0...1.2.1)
191204

192205
**Merged pull requests:**
193206

194207
- Allow whitelisted groups on ssh [\#40](https://github.com/dev-sec/ansible-ssh-hardening/pull/40) ([fheinle](https://github.com/fheinle))
195208

196-
## [1.2](https://github.com/dev-sec/ansible-ssh-hardening/tree/1.2) (2015-09-28)
197-
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.2.0...1.2)
198-
199209
## [1.2.0](https://github.com/dev-sec/ansible-ssh-hardening/tree/1.2.0) (2015-09-28)
200-
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.1...1.2.0)
210+
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.2...1.2.0)
211+
212+
## [1.2](https://github.com/dev-sec/ansible-ssh-hardening/tree/1.2) (2015-09-28)
213+
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.1.0...1.2)
201214

202215
**Merged pull requests:**
203216

204217
- bugfix. Now option true for PrintLastLog is available again [\#39](https://github.com/dev-sec/ansible-ssh-hardening/pull/39) ([fitz123](https://github.com/fitz123))
205218
- Add more travis-tests [\#38](https://github.com/dev-sec/ansible-ssh-hardening/pull/38) ([rndmh3ro](https://github.com/rndmh3ro))
206219
- Support for selinux and pam. fix \#23 [\#35](https://github.com/dev-sec/ansible-ssh-hardening/pull/35) ([rndmh3ro](https://github.com/rndmh3ro))
207220

208-
## [1.1](https://github.com/dev-sec/ansible-ssh-hardening/tree/1.1) (2015-09-01)
209-
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.1.0...1.1)
210-
211221
## [1.1.0](https://github.com/dev-sec/ansible-ssh-hardening/tree/1.1.0) (2015-09-01)
212-
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.0.0...1.1.0)
222+
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.1...1.1.0)
223+
224+
## [1.1](https://github.com/dev-sec/ansible-ssh-hardening/tree/1.1) (2015-09-01)
225+
[Full Changelog](https://github.com/dev-sec/ansible-ssh-hardening/compare/1.0.0...1.1)
213226

214227
**Closed issues:**
215228

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Warning: This role disables root-login on the target server! Please make sure yo
2727
|`ssh_server_ports` | ['22'] |ports on which ssh-server should listen|
2828
|`ssh_client_port` | '22' |port to which ssh-client should connect|
2929
|`ssh_listen_to` | ['0.0.0.0'] |one or more ip addresses, to which ssh-server should listen to. Default is all adresseses, but should be configured to specific addresses for security reasons!|
30-
|`ssh_host_key_files` | ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_dsa_key', '/etc/ssh/ssh_host_ecdsa_key'] |Host keys to look for when starting sshd.|
30+
|`ssh_host_key_files` | [] |Host keys for sshd. If empty ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_ecdsa_key', '/etc/ssh/ssh_host_ed25519_key'] will be used, as far as supported by the installed sshd version|
3131
|`ssh_client_alive_interval` | 600 | specifies an interval for sending keepalive messages |
3232
|`ssh_client_alive_count` | 3 | defines how often keep-alive messages are sent |
3333
|`ssh_permit_tunnel` | false | true if SSH Port Tunneling is required |
@@ -50,6 +50,18 @@ Warning: This role disables root-login on the target server! Please make sure yo
5050
|`ssh_client_password_login` | false | `true` to allow password-based authentication with the ssh client |
5151
|`ssh_server_password_login` | false | `true` to allow password-based authentication with the ssh server |
5252
|`ssh_google_auth` | false | `true` to enable google authenticator based TOTP 2FA |
53+
|`ssh_banner` | `false` | `true` to print a banner on login |
54+
|`ssh_client_hardening` | `true` | `false` to stop harden the client |
55+
|`ssh_client_port` | `'22'` | Specifies the port number to connect on the remote host. |
56+
|`ssh_compression` | `false` | Specifies whether compression is enabled after the user has authenticated successfully. |
57+
|`ssh_max_auth_retries` | `2` | Specifies the maximum number of authentication attempts permitted per connection. |
58+
|`ssh_print_debian_banner` | `false` | `true` to print debian specific banner |
59+
|`ssh_server_enabled` | `true` | `false` to disable the opensshd server |
60+
|`ssh_server_hardening` | `true` | `false` to stop harden the server |
61+
|`ssh_server_match_group` | '' | Introduces a conditional block. If all of the criteria on the Match line are satisfied, the keywords on the following lines override those set in the global section of the config file, until either another Match line or the end of the file. |
62+
|`ssh_server_match_user` | '' | Introduces a conditional block. If all of the criteria on the Match line are satisfied, the keywords on the following lines override those set in the global section of the config file, until either another Match line or the end of the file. |
63+
|`ssh_server_permit_environment_vars` | `false` | `true` to specify that ~/.ssh/environment and environment= options in ~/.ssh/authorized_keys are processed by sshd |
64+
|`ssh_use_dns` | `false` | Specifies whether sshd should look up the remote host name, and to check that the resolved host name for the remote IP address maps back to the very same IP address. |
5365
|`ssh_server_revoked_keys` | [] | a list of revoked public keys that the ssh server will always reject, useful to revoke known weak or compromised keys.|
5466

5567
## Example Playbook

ansible.cfg

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
# finds first
99

1010
[defaults]
11-
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d by {uid} on {host}
11+
ansible_managed = Ansible managed: {file} modified by {uid} on {host}
12+
roles_path = /vagrant
1213

13-
role_path = /vagrant
14+
[ssh_connection]
1415
scp_if_ssh = True

default.yml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,42 @@
1818
vars:
1919
network_ipv6_enable: true
2020
ssh_allow_root_with_key: true
21+
ssh_allow_tcp_forwarding: true
22+
ssh_allow_agent_forwarding: true
23+
ssh_server_permit_environment_vars: 'PWD'
24+
ssh_client_alive_interval: 100
25+
ssh_client_alive_count: 10
2126
ssh_client_password_login: true
2227
ssh_client_cbc_required: true
23-
ssh_server_weak_hmac: true
2428
ssh_client_weak_kex: true
29+
ssh_challengeresponseauthentication: true
30+
ssh_compression: true
31+
ssh_allow_users: 'root kitchen vagrant'
32+
ssh_allow_groups: 'root kitchen vagrant'
33+
ssh_deny_users: 'foo bar'
34+
ssh_deny_groups: 'foo bar'
35+
ssh_max_auth_retries: 10
36+
ssh_permit_tunnel: true
37+
ssh_print_motd: true
38+
ssh_print_last_log: true
39+
ssh_banner: true
40+
ssh_server_password_login: true
41+
ssh_server_enabled: false
42+
ssh_server_weak_hmac: true
43+
sftp_enabled: true
44+
ssh_server_match_group:
45+
- group: 'root'
46+
rules: 'AllowTcpForwarding yes'
47+
ssh_server_match_user:
48+
- user: 'root'
49+
rules: 'AllowTcpForwarding yes'
2550
ssh_remote_hosts:
2651
- names: ['example.com', 'example2.com']
2752
options: ['Port 2222', 'ForwardAgent yes']
2853
- names: ['example3.com']
2954
options: ['StrictHostKeyChecking no']
55+
ssh_use_dns: true
56+
ssh_use_pam: true
3057

3158
- name: wrapper playbook for kitchen testing "ansible-ssh-hardening" with default settings
3259
hosts: localhost

defaults/main.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# true if IPv6 is needed
22
network_ipv6_enable: false # sshd + ssh
33

4+
# true if sshd should be started and enabled
5+
ssh_server_enabled: true # sshd
6+
7+
# true if DNS resolutions are needed, look up the remote host name, defaults to false from 6.8, see: http://www.openssh.com/txt/release-6.8
8+
ssh_use_dns: false # sshd
9+
10+
# true or value if compression is needed
11+
ssh_compression: false # sshd
12+
413
# For which components (client and server) to generate the configuration for. Can be useful when running against a client without an SSH server.
514
ssh_client_hardening: true # ssh
615
ssh_server_hardening: true # sshd
@@ -31,7 +40,7 @@ ssh_client_port: '22' # ssh
3140
ssh_listen_to: ['0.0.0.0'] # sshd
3241

3342
# Host keys to look for when starting sshd.
34-
ssh_host_key_files: ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_ecdsa_key', '/etc/ssh/ssh_host_ed25519_key'] # sshd
43+
ssh_host_key_files: [] # sshd
3544

3645
# Specifies the maximum number of authentication attempts permitted per connection. Once the number of failures reaches half this value, additional failures are logged.
3746
ssh_max_auth_retries: 2
@@ -99,6 +108,14 @@ sftp_chroot_dir: /home/%u
99108
# enable experimental client roaming
100109
ssh_client_roaming: false
101110

111+
# list of hashes (containing user and rules) to generate Match User blocks for.
112+
ssh_server_match_user: false # sshd
113+
114+
# list of hashes (containing group and rules) to generate Match Group blocks for.
115+
ssh_server_match_group: false # sshd
116+
117+
ssh_server_permit_environment_vars: false
118+
102119

103120
ssh_ps53: 'yes'
104121
ssh_ps59: 'sandbox'

handlers/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
- name: restart sshd
22
service: name={{ sshd_service_name }} state=restarted
3+
when: "(ssh_server_enabled|bool)"

tasks/main.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515

1616
- name: set hostkeys according to openssh-version
1717
set_fact:
18-
ssh_host_key_files: ['/etc/ssh/ssh_host_rsa_key']
19-
when: sshd_version.stdout >= '5.3'
18+
ssh_host_key_files: ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_ecdsa_key', '/etc/ssh/ssh_host_ed25519_key']
19+
when: sshd_version.stdout >= '6.3' and not ssh_host_key_files
2020

2121
- name: set hostkeys according to openssh-version
2222
set_fact:
2323
ssh_host_key_files: ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_ecdsa_key']
24-
when: sshd_version.stdout >= '6.0'
24+
when: sshd_version.stdout >= '6.0' and not ssh_host_key_files
2525

2626
- name: set hostkeys according to openssh-version
2727
set_fact:
28-
ssh_host_key_files: ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_ecdsa_key', '/etc/ssh/ssh_host_ed25519_key']
29-
when: sshd_version.stdout >= '6.3'
28+
ssh_host_key_files: ['/etc/ssh/ssh_host_rsa_key']
29+
when: sshd_version.stdout >= '5.3' and not ssh_host_key_files
3030

3131
- name: create revoked_keys and set permissions to root/600
3232
template: src='revoked_keys.j2' dest='/etc/ssh/revoked_keys' mode=0600 owner="{{ ssh_owner }}" group="{{ ssh_group }}"

templates/opensshd.conf.j2

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
# ===================
88

99
# Either disable or only allowssh root login via certificates.
10-
PermitRootLogin {{ 'without-password' if ssh_allow_root_with_key else 'no' }}
10+
PermitRootLogin {{ 'without-password' if (ssh_allow_root_with_key|bool) else 'no' }}
1111

1212
# Define which port sshd should listen to. Default to `22`.
1313
{% for port in ssh_server_ports -%}
1414
Port {{port}}
1515
{% endfor %}
1616

1717
# Address family should always be limited to the active network configuration.
18-
AddressFamily {{ 'any' if network_ipv6_enable else 'inet' }}
18+
AddressFamily {{ 'any' if (network_ipv6_enable|bool) else 'inet' }}
1919

2020
# Define which addresses sshd should listen to. Default to `0.0.0.0`, ie make sure you put your desired address in here, since otherwise sshd will listen to everyone.
2121
{% for address in ssh_listen_to -%}
@@ -113,7 +113,6 @@ LogLevel VERBOSE
113113
UseLogin no
114114
UsePrivilegeSeparation {% if (ansible_distribution == 'Debian' and ansible_distribution_major_version <= '6') or (ansible_os_family in ['Oracle Linux', 'RedHat'] and ansible_distribution_major_version <= '6') -%}{{ssh_ps53}}{% else %}{{ssh_ps59}}{% endif %}
115115

116-
PermitUserEnvironment no
117116
LoginGraceTime 30s
118117
MaxAuthTries {{ssh_max_auth_retries}}
119118
MaxSessions 10
@@ -128,16 +127,16 @@ IgnoreUserKnownHosts yes
128127
HostbasedAuthentication no
129128

130129
# Enable PAM to enforce system wide rules
131-
UsePAM {{ 'yes' if ssh_use_pam else 'no' }}
130+
UsePAM {{ 'yes' if (ssh_use_pam|bool) else 'no' }}
132131
{% if ssh_google_auth %}
133132
# Force public key auth then ask for google auth code
134133
AuthenticationMethods publickey,keyboard-interactive
135134
{% endif %}
136135

137136
# Disable password-based authentication, it can allow for potentially easier brute-force attacks.
138-
PasswordAuthentication {{ 'yes' if ssh_server_password_login else 'no' }}
137+
PasswordAuthentication {{ 'yes' if (ssh_server_password_login|bool) else 'no' }}
139138
PermitEmptyPasswords no
140-
ChallengeResponseAuthentication {{ 'yes' if ssh_challengeresponseauthentication else 'no' }}
139+
ChallengeResponseAuthentication {{ 'yes' if (ssh_challengeresponseauthentication|bool) else 'no' }}
141140

142141
# Only enable Kerberos authentication if it is configured.
143142
KerberosAuthentication no
@@ -177,15 +176,15 @@ ClientAliveInterval {{ssh_client_alive_interval}}
177176
ClientAliveCountMax {{ssh_client_alive_count}}
178177

179178
# Disable tunneling
180-
PermitTunnel {{ 'yes' if ssh_permit_tunnel else 'no' }}
179+
PermitTunnel {{ 'yes' if (ssh_permit_tunnel|bool) else 'no' }}
181180

182181
# Disable forwarding tcp connections.
183182
# no real advantage without denied shell access
184-
AllowTcpForwarding {{ 'yes' if ssh_allow_tcp_forwarding else 'no' }}
183+
AllowTcpForwarding {{ 'yes' if (ssh_allow_tcp_forwarding|bool) else 'no' }}
185184

186185
# Disable agent formwarding, since local agent could be accessed through forwarded connection.
187186
# no real advantage without denied shell access
188-
AllowAgentForwarding {{ 'yes' if ssh_allow_agent_forwarding else 'no' }}
187+
AllowAgentForwarding {{ 'yes' if (ssh_allow_agent_forwarding|bool) else 'no' }}
189188

190189
# Do not allow remote port forwardings to bind to non-loopback addresses.
191190
GatewayPorts no
@@ -194,34 +193,50 @@ GatewayPorts no
194193
X11Forwarding no
195194
X11UseLocalhost yes
196195

197-
# Look up the remote host name, defaults to false from 6.8, see: http://www.openssh.com/txt/release-6.8
198-
UseDNS {{ 'yes' if ssh_use_dns else 'no' }}
196+
# User environment configuration
197+
# ==============================
198+
199+
{% if ssh_server_permit_environment_vars %}
200+
PermitUserEnvironment yes
201+
{% for item in ssh_server_permit_environment_vars %}
202+
AcceptEnv {{ item }}
203+
{% endfor %}
204+
{% else %}
205+
PermitUserEnvironment no
206+
{% endif %}
199207

200208
# Misc. configuration
201209
# ===================
202210

203-
PrintMotd {{ 'yes' if ssh_print_motd else 'no' }}
211+
Compression {{ 'yes' if (ssh_compression|bool) else 'no' }}
212+
213+
UseDNS {{ 'yes' if (ssh_use_dns|bool) else 'no' }}
214+
215+
PrintMotd {{ 'yes' if (ssh_print_motd|bool) else 'no' }}
204216

205217
{% if ansible_os_family != 'FreeBSD' %}
206-
PrintLastLog {{ 'yes' if ssh_print_last_log else 'no' }}
218+
PrintLastLog {{ 'yes' if (ssh_print_last_log|bool) else 'no' }}
207219
{% endif %}
208220

209-
Banner {{ '/etc/ssh/banner.txt' if ssh_banner else 'none' }}
221+
Banner {{ '/etc/ssh/banner.txt' if (ssh_banner|bool) else 'none' }}
210222

211223
{% if ansible_os_family == 'Debian' %}
212-
DebianBanner {{ 'yes' if ssh_print_debian_banner else 'no' }}
224+
DebianBanner {{ 'yes' if (ssh_print_debian_banner|bool) else 'no' }}
213225
{% endif %}
214226

215227
# Reject keys that are explicitly blacklisted
216228
RevokedKeys /etc/ssh/revoked_keys
217229

218230
{% if sftp_enabled %}
231+
# SFTP matching configuration
232+
# ===========================
219233
# Configuration, in case SFTP is used
220-
## override default of no subsystems
221-
## Subsystem sftp /opt/app/openssh5/libexec/sftp-server
234+
# override default of no subsystems
235+
# Subsystem sftp /opt/app/openssh5/libexec/sftp-server
236+
222237
Subsystem sftp internal-sftp -l INFO -f LOCAL6
223-
#
224-
## These lines must appear at the *end* of sshd_config
238+
239+
# These lines must appear at the *end* of sshd_config
225240
Match Group sftponly
226241
ForceCommand internal-sftp -l INFO -f LOCAL6
227242
ChrootDirectory {{ sftp_chroot_dir }}
@@ -231,3 +246,24 @@ PasswordAuthentication no
231246
PermitRootLogin no
232247
X11Forwarding no
233248
{% endif %}
249+
250+
{% if ssh_server_match_group %}
251+
# Group matching configuration
252+
# ============================
253+
254+
{% for item in ssh_server_match_group %}
255+
Match Group {{ item.group }}
256+
{{ item.rules | indent(4) }}
257+
{% endfor %}
258+
{% endif %}
259+
260+
261+
{% if ssh_server_match_user %}
262+
# User matching configuration
263+
# ===========================
264+
265+
{% for item in ssh_server_match_user %}
266+
Match User {{ item.user }}
267+
{{ item.rules | indent(4) }}
268+
{% endfor %}
269+
{% endif %}

0 commit comments

Comments
 (0)