2021-10-11 20:58:09 +00:00
|
|
|
---
|
|
|
|
- name: Set some variables
|
|
|
|
set_fact:
|
|
|
|
ssh_hardening_backup_suffix: "42.{{ ansible_date_time.date }}@{{ ansible_date_time.time }}~"
|
2021-10-19 07:58:38 +00:00
|
|
|
ssh_hardening_distri: "{{ (ansible_distribution|lower).split(' ')[0] }}-{{ ansible_distribution_major_version }}"
|
2021-10-11 20:58:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
- name: Get SSH version number
|
|
|
|
shell: 'ssh -V 2>&1 | grep -o "OpenSSH_[0-9]\+\.[0-9]" | grep -o "[0-9]\+\.[0-9]"'
|
|
|
|
changed_when: False
|
|
|
|
register: ssh_hardening_version
|
|
|
|
|
|
|
|
|
2021-10-19 07:38:38 +00:00
|
|
|
- name: Read forced config file
|
|
|
|
include_vars: "ssh_{{ ssh_hardening_force }}.yml"
|
|
|
|
when: ssh_hardening_force is defined
|
|
|
|
|
|
|
|
|
2021-10-11 20:58:09 +00:00
|
|
|
- name: Read SSH version config file
|
2022-03-21 12:17:44 +00:00
|
|
|
include_vars: "{{ item }}"
|
|
|
|
with_first_found:
|
|
|
|
- "ssh_{{ ssh_hardening_version.stdout_lines.0 }}.yml"
|
|
|
|
- "ssh_default.yml"
|
2021-10-19 07:38:38 +00:00
|
|
|
when: ssh_hardening_force is not defined
|
2021-10-11 20:58:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
- name: Read distribution specific variables
|
|
|
|
include_vars: "{{ item }}"
|
|
|
|
with_first_found:
|
|
|
|
- "os_{{ ssh_hardening_distri }}.yml"
|
2021-10-19 07:58:38 +00:00
|
|
|
- "os_{{ (ansible_distribution|lower).split(' ')[0] }}.yml"
|
|
|
|
- "os_{{ (ansible_os_family|lower).split(' ')[0] }}.yml"
|
2021-10-11 20:58:09 +00:00
|
|
|
- "os_default.yml"
|
|
|
|
|
|
|
|
|
|
|
|
- name: Backup sshd_config
|
|
|
|
copy:
|
|
|
|
src: "{{ ssh_hardening_sshd_config }}"
|
|
|
|
dest: "{{ ssh_hardening_sshd_config }}.{{ ssh_hardening_backup_suffix }}"
|
|
|
|
remote_src: yes
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0600
|
|
|
|
|
|
|
|
####################
|
|
|
|
|
2021-10-19 08:24:47 +00:00
|
|
|
- name: "Hostkeys: Disable (EC)DSA (and maybe RSA)"
|
2021-10-11 20:58:09 +00:00
|
|
|
lineinfile:
|
|
|
|
dest: "{{ ssh_hardening_sshd_config }}"
|
|
|
|
regexp: '(?i)\s*#*\s*hostkey.*{{ item }}_key'
|
|
|
|
state: absent
|
2021-10-19 08:24:47 +00:00
|
|
|
loop: "{{ ssh_hardening_hostkeys_all|difference(ssh_hardening_hostkeys) }}"
|
2021-10-11 20:58:09 +00:00
|
|
|
notify: Restart SSH
|
|
|
|
|
|
|
|
|
|
|
|
- name: "Hostkeys: Enable (RSA &) ED25519"
|
|
|
|
lineinfile:
|
|
|
|
dest: "{{ ssh_hardening_sshd_config }}"
|
|
|
|
regexp: '(?i)\s*#*\s*hostkey.*{{ item }}_key'
|
|
|
|
line: 'HostKey /etc/ssh/ssh_host_{{ item }}_key'
|
|
|
|
loop: '{{ ssh_hardening_hostkeys }}'
|
|
|
|
notify: Restart SSH
|
|
|
|
|
2021-10-19 08:23:13 +00:00
|
|
|
|
|
|
|
- name: "Renew RSA hostkeys if too short"
|
|
|
|
openssh_keypair:
|
|
|
|
path: /etc/ssh/ssh_host_rsa_key
|
|
|
|
type: rsa
|
|
|
|
size: 4096
|
|
|
|
when: '"rsa" in ssh_hardening_hostkeys'
|
|
|
|
|
2021-10-11 20:58:09 +00:00
|
|
|
####################
|
|
|
|
|
|
|
|
- name: "INCLUDE: Remove small Diffie-Hellman moduli"
|
|
|
|
include_tasks: moduli.yml
|
|
|
|
|
|
|
|
####################
|
|
|
|
|
|
|
|
- name: "Check for crypto-policies"
|
|
|
|
stat:
|
|
|
|
path: /etc/crypto-policies/back-ends/opensshserver.config
|
|
|
|
register: ssh_hardening_use_policies
|
|
|
|
|
|
|
|
|
|
|
|
- name: "The (RedHat) crypto policy way..."
|
|
|
|
include_tasks: restrictions_crypto_policy.yml
|
|
|
|
when: ssh_hardening_use_policies.stat.exists
|
|
|
|
|
|
|
|
|
|
|
|
- name: "The standard config way..."
|
|
|
|
include_tasks: restrictions_configfile.yml
|
|
|
|
when: not ssh_hardening_use_policies.stat.exists
|
|
|
|
|