diff --git a/postinstall b/postinstall index cd51411..f18af62 100755 --- a/postinstall +++ b/postinstall @@ -522,22 +522,36 @@ pve_conf_backup(){ harden_ssh(){ if [ $ssh_hardening -gt 0 ]; then log "Hardening ssh server" - rm /etc/ssh/ssh_host_* - log "Creating new SSH host keys" - ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N "" > /dev/null 2>&1 - ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" > /dev/null 2>&1 - log "Creating new SSH moduli" - awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe - mv /etc/ssh/moduli.safe /etc/ssh/moduli - log "Writing hardened SSH config" - if [[ $VERSION_CODENAME == "bookworm" ]]; then - echo -e "\n# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com\n# hardening guide.\nKexAlgorithms sntrup761x25519-sha512@openssh.com,curve25519-sha256,curve25519-sha256@libssh.org,gss-curve25519-sha256-,diffie-hellman-group16-sha512,gss-group16-sha512-,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256\nCiphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\nMACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com\nHostKeyAlgorithms ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com" > /etc/ssh/sshd_config.d/ssh-audit_hardening.conf - elif [[ $VERSION_CODENAME == "bullseye" ]]; then - sed -i 's/^\#HostKey \/etc\/ssh\/ssh_host_\(rsa\|ed25519\)_key$/HostKey \/etc\/ssh\/ssh_host_\1_key/g' /etc/ssh/sshd_config - echo -e echo -e "\n# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com\n# hardening guide.\nKexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256\nCiphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\nMACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com\nHostKeyAlgorithms ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-512,rsa-sha2-256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com" > /etc/ssh/sshd_config.d/ssh-audit_hardening.conf + log "Creating backup of current SSH config" + cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S) + + rm -f /etc/ssh/ssh_host_* + ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N "" > /dev/null + ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" > /dev/null + + if [ $(awk '$5 >= 3071' /etc/ssh/moduli | wc -l) -gt 0 ]; then + awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe + mv /etc/ssh/moduli.safe /etc/ssh/moduli + fi + + INCLUDE_LINE="Include /etc/ssh/sshd_config.d/*.conf" + grep -qxF "$INCLUDE_LINE" /etc/ssh/sshd_config || echo "$INCLUDE_LINE" >> /etc/ssh/sshd_config + + cat << EOF > /etc/ssh/sshd_config.d/ssh-audit_hardening.conf +# Hardening profile by bashclub.org +KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256 +Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com +MACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com +HostKeyAlgorithms ssh-ed25519,rsa-sha2-512 +EOF + + if sshd -t; then + systemctl restart ssh.service + else + echo "Fehlerhafte SSH-Konfiguration erkannt. Bitte prüfen: /etc/ssh/sshd_config.d/ssh-audit_hardening.conf" + exit 1 fi - systemctl restart ssh.service fi }