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 }}~"
|
2023-11-15 20:07:32 +00:00
|
|
|
ssh_hardening_distri: "{{ (ansible_distribution|lower).split(' ')[0] }}-{{ ansible_distribution_major_version|default(ansible_distribution_version) }}"
|
2021-10-11 20:58:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
- name: Get SSH version number
|
2024-11-11 11:26:12 +00:00
|
|
|
shell: 'sshd -V 2>&1 | grep -Eo "OpenSSH_[0-9]+\.[0-9]+" | grep -Eo "[0-9]+\.[0-9]+"'
|
2021-10-11 20:58:09 +00:00
|
|
|
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
|
2023-11-15 20:07:32 +00:00
|
|
|
group: "{{ ssh_hardening_root_group }}"
|
2021-10-11 20:58:09 +00:00
|
|
|
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
|
|
|
|
2024-07-10 09:21:47 +00:00
|
|
|
- name: "Ensure ED25519 hostkey is available"
|
|
|
|
openssh_keypair:
|
|
|
|
path: /etc/ssh/ssh_host_ed25519_key
|
|
|
|
type: ed25519
|
|
|
|
when: '"ed25519" in ssh_hardening_hostkeys'
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
####################
|
|
|
|
|
2023-03-20 19:32:35 +00:00
|
|
|
- name: "Check for SSH include directory"
|
|
|
|
stat:
|
|
|
|
path: /etc/ssh/sshd_config.d
|
|
|
|
register: ssh_hardening_sshd_include_dir
|
|
|
|
|
|
|
|
|
|
|
|
#- name: "SSH include directory exists"
|
|
|
|
# find:
|
|
|
|
# path: /etc/ssh/sshd_config.d
|
|
|
|
# pattern: "*.conf"
|
|
|
|
# register: ssh_hardening_sshd_include_files
|
|
|
|
|
|
|
|
|
|
|
|
- name: "Check for crypto-policy file"
|
2021-10-11 20:58:09 +00:00
|
|
|
stat:
|
|
|
|
path: /etc/crypto-policies/back-ends/opensshserver.config
|
2023-03-20 19:32:35 +00:00
|
|
|
register: ssh_hardening_policy_file
|
|
|
|
|
|
|
|
|
|
|
|
- name: "BLOCK: what to do with crypto policy file"
|
|
|
|
when: ssh_hardening_policy_file.stat.exists
|
|
|
|
block:
|
2021-10-11 20:58:09 +00:00
|
|
|
|
2023-03-20 19:32:35 +00:00
|
|
|
- name: "Get systemd service file location"
|
|
|
|
service:
|
|
|
|
name: "{{ ssh_hardening_service_name }}"
|
|
|
|
register: ssh_hardening_systemd_file
|
2021-10-11 20:58:09 +00:00
|
|
|
|
2023-03-20 19:32:35 +00:00
|
|
|
- name: "Check for USED crypto-policies variable"
|
|
|
|
command: "awk '/CRYPTO_POLICY/{print}' {{ ssh_hardening_systemd_file.status.FragmentPath }}"
|
|
|
|
changed_when: False
|
|
|
|
register: ssh_hardening_policy_var
|
|
|
|
# stdout == "" → SSH-Cfg in CP
|
|
|
|
# stdout != "" → Variablen in CP
|
|
|
|
|
|
|
|
|
|
|
|
# Nicht bei SUSE Tumbleweed!
|
|
|
|
- name: "(policy file) Move original link"
|
|
|
|
command: mv /etc/crypto-policies/back-ends/opensshserver.config /etc/crypto-policies/back-ends/opensshserver.config.not-hardened
|
|
|
|
args:
|
|
|
|
creates: /etc/crypto-policies/back-ends/opensshserver.config.not-hardened
|
|
|
|
when: ssh_hardening_policy_file.stat.islnk
|
|
|
|
|
|
|
|
|
|
|
|
- name: "The (RedHat-8) crypto policy variable way..."
|
2021-10-11 20:58:09 +00:00
|
|
|
include_tasks: restrictions_crypto_policy.yml
|
2023-03-20 19:32:35 +00:00
|
|
|
when: ssh_hardening_policy_var.stdout is defined and ssh_hardening_policy_var.stdout|length > 0
|
|
|
|
|
|
|
|
|
|
|
|
- name: "The (RedHat-X) crypto policy include way..."
|
|
|
|
include_tasks: restrictions_crypto_policy_include.yml
|
|
|
|
when: ssh_hardening_policy_var.stdout is defined and ssh_hardening_policy_var.stdout|length == 0 and ssh_hardening_policy_file.stat.exists
|
2021-10-11 20:58:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
- name: "The standard config way..."
|
|
|
|
include_tasks: restrictions_configfile.yml
|
2023-03-20 19:32:35 +00:00
|
|
|
when: not ssh_hardening_policy_file.stat.exists
|
|
|
|
|
2021-10-11 20:58:09 +00:00
|
|
|
|