Update Linux/SSH/secure_ssh.sh
This commit is contained in:
@@ -7,6 +7,7 @@ SSH_MAIN_CONFIG="/etc/ssh/sshd_config"
|
|||||||
SSH_CONFIG_FILE="$SSH_CONFIG_DIR/secure.conf"
|
SSH_CONFIG_FILE="$SSH_CONFIG_DIR/secure.conf"
|
||||||
ED25519_KEY="/etc/ssh/ssh_host_ed25519_key"
|
ED25519_KEY="/etc/ssh/ssh_host_ed25519_key"
|
||||||
ECDSA_KEY="/etc/ssh/ssh_host_ecdsa_key"
|
ECDSA_KEY="/etc/ssh/ssh_host_ecdsa_key"
|
||||||
|
RSA_KEY="/etc/ssh/ssh_host_rsa_key" # Hinzugefügt für Kompatibilität mit älteren Clients
|
||||||
|
|
||||||
# Farben für bessere Lesbarkeit
|
# Farben für bessere Lesbarkeit
|
||||||
RED='\033[1;31m'
|
RED='\033[1;31m'
|
||||||
@@ -177,6 +178,16 @@ install_package() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Funktion zur Erkennung der OpenSSH-Version
|
||||||
|
detect_openssh_version() {
|
||||||
|
if command -v ssh -V &> /dev/null; then
|
||||||
|
local version_output=$(ssh -V 2>&1)
|
||||||
|
echo "$version_output" | grep -oP 'OpenSSH_\K[0-9]+\.[0-9]+' | head -1
|
||||||
|
else
|
||||||
|
echo "unknown"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Erkennung des Service-Managers
|
# Erkennung des Service-Managers
|
||||||
SERVICE_MANAGER=$(detect_service_manager)
|
SERVICE_MANAGER=$(detect_service_manager)
|
||||||
log "Erkannter Service-Manager: $SERVICE_MANAGER"
|
log "Erkannter Service-Manager: $SERVICE_MANAGER"
|
||||||
@@ -235,6 +246,12 @@ else
|
|||||||
log "OpenSSH-Server ist bereits installiert."
|
log "OpenSSH-Server ist bereits installiert."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Erkennen der OpenSSH-Version
|
||||||
|
SSH_VERSION=$(detect_openssh_version)
|
||||||
|
if [ "$SSH_VERSION" != "unknown" ]; then
|
||||||
|
log "Erkannte OpenSSH-Version: $SSH_VERSION"
|
||||||
|
fi
|
||||||
|
|
||||||
# Stelle sicher, dass das Konfigurationsverzeichnis existiert
|
# Stelle sicher, dass das Konfigurationsverzeichnis existiert
|
||||||
if [ ! -d "$SSH_CONFIG_DIR" ]; then
|
if [ ! -d "$SSH_CONFIG_DIR" ]; then
|
||||||
warn "SSH Konfigurationsverzeichnis existiert nicht. Es wird erstellt..."
|
warn "SSH Konfigurationsverzeichnis existiert nicht. Es wird erstellt..."
|
||||||
@@ -271,6 +288,21 @@ else
|
|||||||
log "Host-Schlüssel ssh_host_ecdsa_key ist bereits vorhanden."
|
log "Host-Schlüssel ssh_host_ecdsa_key ist bereits vorhanden."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Prüfe auf RSA-Schlüssel für die Kompatibilität
|
||||||
|
if [ ! -f "$RSA_KEY" ]; then
|
||||||
|
warn "Host-Schlüssel fehlt. Generiere ssh_host_rsa_key für Kompatibilität..."
|
||||||
|
if sudo ssh-keygen -t rsa -b 4096 -f "$RSA_KEY" -N "" &> /dev/null; then
|
||||||
|
sudo chown root:root "$RSA_KEY"
|
||||||
|
sudo chmod 600 "$RSA_KEY"
|
||||||
|
log "Host-Schlüssel ssh_host_rsa_key wurde erstellt."
|
||||||
|
else
|
||||||
|
error "Fehler beim Erstellen des Host-Schlüssels."
|
||||||
|
# Kein Exit hier, da RSA optional ist
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log "Host-Schlüssel ssh_host_rsa_key ist bereits vorhanden."
|
||||||
|
fi
|
||||||
|
|
||||||
# Konfiguriere SSH für die Include-Direktive
|
# Konfiguriere SSH für die Include-Direktive
|
||||||
warn "Setze /etc/ssh/sshd_config zurück auf Include-Direktive..."
|
warn "Setze /etc/ssh/sshd_config zurück auf Include-Direktive..."
|
||||||
sudo tee "$SSH_MAIN_CONFIG" > /dev/null <<EOF
|
sudo tee "$SSH_MAIN_CONFIG" > /dev/null <<EOF
|
||||||
@@ -438,6 +470,62 @@ if [ -f "$MODULI_FILE" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Frage, ob ein nicht-Standard-Port genutzt werden soll (besserer Schutz vor automatisierten Scans)
|
||||||
|
echo -en "${BLUE}Möchtest du einen non-Standard SSH-Port verwenden? (Standard ist 22) ${YELLOW}[${WHITE}y/n${YELLOW}]${RESET}: "
|
||||||
|
read USE_CUSTOM_PORT
|
||||||
|
SSH_PORT="22"
|
||||||
|
if [ "$USE_CUSTOM_PORT" == "y" ]; then
|
||||||
|
echo -en "${BLUE}Gib einen alternativen SSH-Port (1024-65535) ein: ${RESET}"
|
||||||
|
read CUSTOM_PORT
|
||||||
|
if [[ "$CUSTOM_PORT" =~ ^[0-9]+$ ]] && [ "$CUSTOM_PORT" -gt 1023 ] && [ "$CUSTOM_PORT" -lt 65536 ]; then
|
||||||
|
SSH_PORT="$CUSTOM_PORT"
|
||||||
|
log "Verwende benutzerdefinierten SSH-Port: $SSH_PORT"
|
||||||
|
else
|
||||||
|
warn "Ungültiger Port. Verwende Standard-Port 22."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Frage nach Fail2Ban-Installation
|
||||||
|
echo -en "${BLUE}Möchtest du Fail2Ban installieren, um dich vor Brute-Force-Angriffen zu schützen? ${YELLOW}[${WHITE}y/n${YELLOW}]${RESET}: "
|
||||||
|
read INSTALL_FAIL2BAN
|
||||||
|
if [ "$INSTALL_FAIL2BAN" == "y" ]; then
|
||||||
|
warn "Installiere Fail2Ban..."
|
||||||
|
install_package "fail2ban"
|
||||||
|
|
||||||
|
# Konfiguriere Fail2Ban für SSH
|
||||||
|
if [ -d "/etc/fail2ban" ]; then
|
||||||
|
# Erstelle eine SSH-Jail-Konfiguration
|
||||||
|
sudo mkdir -p /etc/fail2ban/jail.d
|
||||||
|
sudo tee /etc/fail2ban/jail.d/sshd.conf > /dev/null <<EOF
|
||||||
|
[sshd]
|
||||||
|
enabled = true
|
||||||
|
port = $SSH_PORT
|
||||||
|
filter = sshd
|
||||||
|
logpath = /var/log/auth.log
|
||||||
|
maxretry = 5
|
||||||
|
findtime = 600
|
||||||
|
bantime = 3600
|
||||||
|
EOF
|
||||||
|
log "Fail2Ban wurde für SSH konfiguriert."
|
||||||
|
|
||||||
|
# Starte Fail2Ban neu
|
||||||
|
case "$SERVICE_MANAGER" in
|
||||||
|
systemd)
|
||||||
|
sudo systemctl enable fail2ban
|
||||||
|
sudo systemctl restart fail2ban
|
||||||
|
;;
|
||||||
|
sysvinit)
|
||||||
|
sudo service fail2ban restart
|
||||||
|
;;
|
||||||
|
openrc)
|
||||||
|
sudo rc-service fail2ban restart
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
else
|
||||||
|
error "Fail2Ban-Konfigurationsverzeichnis wurde nicht gefunden."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# Erstelle die Konfigurationsdatei mit dynamischen Einstellungen je nach System
|
# Erstelle die Konfigurationsdatei mit dynamischen Einstellungen je nach System
|
||||||
sudo tee "$SSH_CONFIG_FILE" > /dev/null <<EOL
|
sudo tee "$SSH_CONFIG_FILE" > /dev/null <<EOL
|
||||||
# SSHD Config - maximale Sicherheit (IPv4)
|
# SSHD Config - maximale Sicherheit (IPv4)
|
||||||
@@ -445,8 +533,8 @@ sudo tee "$SSH_CONFIG_FILE" > /dev/null <<EOL
|
|||||||
# Nur IPv4-Verbindungen
|
# Nur IPv4-Verbindungen
|
||||||
AddressFamily inet
|
AddressFamily inet
|
||||||
|
|
||||||
# Standardport (22), kann bei Bedarf geändert werden
|
# SSH-Port
|
||||||
Port 22
|
Port $SSH_PORT
|
||||||
|
|
||||||
# Nur Protokoll 2 zulassen (Protokoll 1 ist unsicher)
|
# Nur Protokoll 2 zulassen (Protokoll 1 ist unsicher)
|
||||||
Protocol 2
|
Protocol 2
|
||||||
@@ -454,15 +542,16 @@ Protocol 2
|
|||||||
# Host-Schlüssel (nur moderne Schlüssel)
|
# Host-Schlüssel (nur moderne Schlüssel)
|
||||||
HostKey /etc/ssh/ssh_host_ed25519_key
|
HostKey /etc/ssh/ssh_host_ed25519_key
|
||||||
HostKey /etc/ssh/ssh_host_ecdsa_key
|
HostKey /etc/ssh/ssh_host_ecdsa_key
|
||||||
|
HostKey /etc/ssh/ssh_host_rsa_key # Für Kompatibilität mit älteren Clients
|
||||||
|
|
||||||
# Sichere Key Exchange-Algorithmen (Kex)
|
# Sichere Key Exchange-Algorithmen (Kex)
|
||||||
KexAlgorithms curve25519-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,diffie-hellman-group14-sha256
|
KexAlgorithms curve25519-sha256@libssh.org,curve25519-sha256,diffie-hellman-group18-sha512,diffie-hellman-group16-sha512,diffie-hellman-group14-sha256,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256
|
||||||
|
|
||||||
# Sichere Ciphers (Chiffren)
|
# Sichere Ciphers (Chiffren)
|
||||||
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes256-ctr
|
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
|
||||||
|
|
||||||
# Sichere MAC-Algorithmen
|
# Sichere MAC-Algorithmen
|
||||||
MACs hmac-sha2-512,hmac-sha2-256
|
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512,hmac-sha2-256
|
||||||
|
|
||||||
# Log-Einstellungen
|
# Log-Einstellungen
|
||||||
SyslogFacility AUTHPRIV
|
SyslogFacility AUTHPRIV
|
||||||
@@ -478,6 +567,18 @@ PermitRootLogin no
|
|||||||
PubkeyAuthentication yes
|
PubkeyAuthentication yes
|
||||||
PasswordAuthentication no
|
PasswordAuthentication no
|
||||||
ChallengeResponseAuthentication no
|
ChallengeResponseAuthentication no
|
||||||
|
|
||||||
|
# Benutzer ohne Passwort verbieten
|
||||||
|
PermitEmptyPasswords no
|
||||||
|
|
||||||
|
# Maximale Authentifizierungsversuche
|
||||||
|
MaxAuthTries 3
|
||||||
|
|
||||||
|
# Maximale Anzahl simultaner Sessions
|
||||||
|
MaxSessions 5
|
||||||
|
|
||||||
|
# Sicherheitsrelevante Zeitbeschränkungen
|
||||||
|
MaxStartups 10:30:60
|
||||||
EOL
|
EOL
|
||||||
|
|
||||||
# Füge PAM-Konfiguration hinzu, wenn verfügbar
|
# Füge PAM-Konfiguration hinzu, wenn verfügbar
|
||||||
@@ -495,10 +596,7 @@ else
|
|||||||
|
|
||||||
# PAM ist nicht verfügbar auf diesem System
|
# PAM ist nicht verfügbar auf diesem System
|
||||||
# Basic-Auth-Einstellungen
|
# Basic-Auth-Einstellungen
|
||||||
UsePrivilegeSeparation sandbox
|
|
||||||
StrictModes yes
|
StrictModes yes
|
||||||
MaxAuthTries 3
|
|
||||||
MaxSessions 5
|
|
||||||
EOL
|
EOL
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -508,6 +606,23 @@ cat <<EOL | sudo tee -a "$SSH_CONFIG_FILE" > /dev/null
|
|||||||
# X11-Weiterleitung deaktivieren (falls nicht benötigt)
|
# X11-Weiterleitung deaktivieren (falls nicht benötigt)
|
||||||
X11Forwarding no
|
X11Forwarding no
|
||||||
|
|
||||||
|
# TCP-Weiterleitung deaktivieren
|
||||||
|
AllowTcpForwarding no
|
||||||
|
GatewayPorts no
|
||||||
|
|
||||||
|
# Verhindern von Tunnel-Nutzung
|
||||||
|
PermitTunnel no
|
||||||
|
|
||||||
|
# Verhindern von SSH-Agent-Forwarding (wenn nicht benötigt)
|
||||||
|
AllowAgentForwarding no
|
||||||
|
|
||||||
|
# Banner-Anzeige deaktivieren (keine Systeminfo für potenzielle Angreifer)
|
||||||
|
Banner none
|
||||||
|
|
||||||
|
# Deaktiviere .rhosts Dateien
|
||||||
|
IgnoreRhosts yes
|
||||||
|
HostbasedAuthentication no
|
||||||
|
|
||||||
# Pfad zur Authorized Keys-Datei
|
# Pfad zur Authorized Keys-Datei
|
||||||
AuthorizedKeysFile .ssh/authorized_keys
|
AuthorizedKeysFile .ssh/authorized_keys
|
||||||
|
|
||||||
@@ -518,7 +633,17 @@ Subsystem sftp $SFTP_PATH
|
|||||||
ClientAliveInterval 300
|
ClientAliveInterval 300
|
||||||
ClientAliveCountMax 2
|
ClientAliveCountMax 2
|
||||||
|
|
||||||
|
# Kompression deaktivieren (Schutz vor CRIME/BREACH-ähnlichen Angriffen)
|
||||||
|
Compression no
|
||||||
|
|
||||||
|
# Erlaubte Benutzer festlegen
|
||||||
AllowUsers $VALID_USERS
|
AllowUsers $VALID_USERS
|
||||||
|
|
||||||
|
# Trendchecking aktivieren für verdächtige Aktivitäten
|
||||||
|
DebianBanner no
|
||||||
|
|
||||||
|
# Anzeige der letzten Anmeldung deaktivieren
|
||||||
|
PrintLastLog no
|
||||||
EOL
|
EOL
|
||||||
|
|
||||||
log "Prüfe SSH-Konfiguration..."
|
log "Prüfe SSH-Konfiguration..."
|
||||||
@@ -532,65 +657,25 @@ if [ ! -d /run/sshd ]; then
|
|||||||
log "/run/sshd wurde erstellt."
|
log "/run/sshd wurde erstellt."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Erstelle oder modifiziere die Datei /etc/hosts.deny für zusätzliche Sicherheit
|
||||||
# Überprüfe, ob die Konfiguration gültig ist
|
if [ -f "/etc/hosts.deny" ]; then
|
||||||
if command -v sshd &> /dev/null; then
|
# Prüfe, ob SSHD bereits in hosts.deny vorhanden ist
|
||||||
if ! sudo sshd -t &> /dev/null; then
|
if ! grep -q "sshd" /etc/hosts.deny; then
|
||||||
error "Die SSH-Konfiguration enthält Fehler! Überprüfe die Konfiguration manuell."
|
echo "sshd: ALL" | sudo tee -a /etc/hosts.deny > /dev/null
|
||||||
exit 1
|
log "SSHD-Eintrag zu /etc/hosts.deny hinzugefügt."
|
||||||
else
|
|
||||||
log "SSH-Konfiguration ist gültig."
|
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
echo "sshd: ALL" | sudo tee /etc/hosts.deny > /dev/null
|
||||||
|
log "Datei /etc/hosts.deny mit SSHD-Eintrag erstellt."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Erkenne den SSH-Dienst und starte ihn neu
|
# Erstelle oder modifiziere die Datei /etc/hosts.allow für vertrauenswürdige IPs
|
||||||
SSH_SERVICE=$(detect_ssh_service "$SERVICE_MANAGER")
|
echo -en "${BLUE}Möchtest du bestimmte IP-Adressen explizit für SSH zulassen? ${YELLOW}[${WHITE}y/n${YELLOW}]${RESET}: "
|
||||||
|
read ADD_ALLOWED_IPS
|
||||||
if [ "$SSH_SERVICE" == "unknown" ]; then
|
if [ "$ADD_ALLOWED_IPS" == "y" ]; then
|
||||||
error "Kein SSH-Dienst gefunden. Bitte überprüfe deine Installation."
|
echo -en "${BLUE}Gib vertrauenswürdige IP-Adressen ein (durch Leerzeichen getrennt, z.B. 192.168.1.10 10.0.0.5): ${RESET}"
|
||||||
exit 1
|
read ALLOWED_IPS
|
||||||
fi
|
|
||||||
|
|
||||||
log "Erkannter SSH-Dienst: $SSH_SERVICE"
|
|
||||||
|
|
||||||
if ! restart_ssh_service "$SERVICE_MANAGER" "$SSH_SERVICE"; then
|
|
||||||
error "Fehler beim Neustart des SSH-Dienstes. Versuche einen alternativen Ansatz..."
|
|
||||||
|
|
||||||
# Fallback-Versuche für verschiedene Distributionen
|
if [ -n "$ALLOWED_IPS" ]; then
|
||||||
if [ "$SERVICE_MANAGER" == "systemd" ]; then
|
FORMATTED_IPS=$(echo "$ALLOWED_IPS" | tr ' ' ',')
|
||||||
# Versuche alle möglichen Dienste bei systemd
|
echo "sshd: $FORMATTED_IPS : allow" | sudo tee -a /etc/hosts.allow > /dev/null
|
||||||
for service in sshd ssh openssh openssh-server; do
|
|
||||||
if sudo systemctl restart "$service" 2>/dev/null; then
|
|
||||||
log "SSH-Dienst ($service) erfolgreich neu gestartet."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
elif [ "$SERVICE_MANAGER" == "sysvinit" ]; then
|
|
||||||
# Versuche alle möglichen Dienste bei sysvinit
|
|
||||||
for service in sshd ssh openssh openssh-server; do
|
|
||||||
if sudo service "$service" restart 2>/dev/null; then
|
|
||||||
log "SSH-Dienst ($service) erfolgreich neu gestartet."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
elif [ "$SERVICE_MANAGER" == "openrc" ]; then
|
|
||||||
# Versuche alle möglichen Dienste bei OpenRC
|
|
||||||
for service in sshd ssh openssh openssh-server; do
|
|
||||||
if sudo rc-service "$service" restart 2>/dev/null; then
|
|
||||||
log "SSH-Dienst ($service) erfolgreich neu gestartet."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Als letzte Möglichkeit, versuche einen Kill/HUP-Signal an sshd zu senden
|
|
||||||
if pgrep sshd &>/dev/null && sudo killall -HUP sshd; then
|
|
||||||
log "SSH-Dienst durch Signal neu gestartet."
|
|
||||||
else
|
|
||||||
error "Konnte den SSH-Dienst nicht neu starten. Bitte starte den SSH-Dienst manuell neu."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
log "Setup abgeschlossen! Nur folgende Benutzer dürfen sich per SSH anmelden: ${WHITE}$VALID_USERS"
|
|
||||||
log "Die SSH-Konfiguration wurde für maximale Sicherheit optimiert."
|
|
||||||
Reference in New Issue
Block a user