#!/bin/bash # Script Name: ssh_harden.sh # Description: SSH hardening script for Debian 12 (Bookworm) based on https://www.sshaudit.com/hardening_guides.html#debian_12 # Author: Victor Bishop | https://github.com/Heretic312/devsecops-wrappers # Version: 1.1 # Usage: sudo ./ssh_harden.sh # Notes: Adjust SSH_PORT as needed if not using port 22 SSH_PORT=22 # Change this to your SSH port if not using 22 # Ensure script is run as root if [[ $EUID -ne 0 ]]; then echo "This script must be run as root." >&2 exit 1 fi # Function to regenerate SSH host keys regenerate_ssh_keys() { read -p "WARNING: This will DELETE and REGENERATE SSH host keys. Continue? (y/n): " confirm [[ $confirm != [yY] ]] && echo "Cancelled." && return echo "Regenerating SSH host keys..." rm -f /etc/ssh/ssh_host_* ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N "" ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" # Clean existing HostKey lines to prevent duplication sed -i '/^HostKey \/etc\/ssh\/ssh_host_/d' /etc/ssh/sshd_config echo -e "HostKey /etc/ssh/ssh_host_ed25519_key\nHostKey /etc/ssh/ssh_host_rsa_key" >> /etc/ssh/sshd_config awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe mv /etc/ssh/moduli.safe /etc/ssh/moduli echo "SSH host keys regenerated successfully." } # Function to apply SSH hardening configuration apply_ssh_hardening() { echo "Applying SSH hardening configuration..." cat > /etc/ssh/sshd_config.d/ssh-audit_hardening.conf <