SSH Güvenlik Sıkılaştırma

SSH sunucusunu güvenli hale getiren, brute-force saldırılarını engelleyen ve best practice ayarlarını uygulayan script.

Yayınlanma: 20.03.2024

Detaylı Bilgi

Bu script, SSH sunucunuzu güvenli hale getirir ve brute-force saldırılarını engeller. SSH, sunuculara uzaktan erişim için kullanılan en yaygın protokoldür ve güvenliği kritik öneme sahiptir.

Script Ne İşe Yarar?

Bu script, SSH güvenliğini artırmak için:

  • SSH portunu değiştirir (varsayılan 22 yerine özel port)
  • Root girişini devre dışı bırakır
  • Şifre tabanlı kimlik doğrulamayı devre dışı bırakır (opsiyonel)
  • Kimlik doğrulama denemelerini sınırlandırır
  • Güvenlik ayarlarını uygular
  • fail2ban kurar ve yapılandırır
  • Yapılandırma yedeği oluşturur

Neden Kullanmalısınız?

SSH güvenliği, sunucu güvenliğinin temelidir:

  • Brute-Force Koruması: fail2ban ile saldırıları engeller
  • Root Erişim Engelleme: Root girişini kapatır
  • Key-Based Auth: Şifre yerine SSH key kullanımı
  • Port Değiştirme: Varsayılan portu değiştirerek saldırı yüzeyini azaltır

Nasıl Kullanılır?

Adım Adım Kullanım Kılavuzu

1. SSH Key Oluşturma (Önerilen)

Önce SSH key oluşturun:

ssh-keygen -t ed25519 -C "[email protected]"

2. Key'i Sunucuya Kopyalama

ssh-copy-id user@server

3. Scripti Çalıştırın

sudo chmod +x ssh_hardening.sh
sudo ./ssh_hardening.sh

4. Soruları Yanıtlayın

Script size şu soruları soracak:

  • SSH portunu değiştirmek ister misiniz? (y/n)
  • Şifre tabanlı kimlik doğrulamayı devre dışı bırakmak ister misiniz? (y/n)
  • Değişiklikleri uygulayıp SSH'yi yeniden başlatmak ister misiniz? (y/n)

5. Bağlantıyı Test Edin

Yeni bir terminal açıp SSH bağlantısını test edin. Mevcut terminali kapatmayın!

Gereksinimler

Gereksinimler

  • Root Yetkisi: Script root olarak çalıştırılmalı
  • SSH Kurulu: OpenSSH server kurulu olmalı
  • SSH Key: Şifre tabanlı auth'u kapatmak için (önerilen)

Kullanım Senaryoları

Kullanım Senaryoları

1. Yeni Sunucu Güvenliği

Yeni bir sunucu kurduğunuzda ilk yapılacak işlerden biri SSH güvenliğini artırmaktır.

2. Brute-Force Saldırıları

SSH'ye yönelik brute-force saldırılarını engellemek için kullanın.

3. Production Sunucular

Production sunucularında SSH güvenliğini maksimize edin.

Örnekler

Kullanım Örnekleri

Örnek 1: Temel Güvenlik

sudo ./ssh_hardening.sh
# Port değiştirme: n
# Şifre devre dışı: n (önce SSH key kurun)

Örnek 2: Maksimum Güvenlik

sudo ./ssh_hardening.sh
# Port değiştirme: y (örn: 2222)
# Şifre devre dışı: y

Kod

#!/bin/bash

# SSH Hardening Script

if [ "$EUID" -ne 0 ]; then 
    echo "Please run as root"
    exit 1
fi

SSHD_CONFIG="/etc/ssh/sshd_config"
BACKUP_FILE="${SSHD_CONFIG}.backup.$(date +%Y%m%d_%H%M%S)"

echo "======================================"
echo "   SSH HARDENING SCRIPT"
echo "======================================"
echo ""

echo "1. Creating backup..."
cp "$SSHD_CONFIG" "$BACKUP_FILE"
echo "✓ Backup created: $BACKUP_FILE"
echo ""

read -p "Change SSH port from 22? (y/n): " CHANGE_PORT
if [ "$CHANGE_PORT" = "y" ]; then
    read -p "Enter new SSH port (1024-65535): " NEW_PORT
    sed -i "s/^#*Port .*/Port $NEW_PORT/" "$SSHD_CONFIG"
    echo "✓ SSH port changed to $NEW_PORT"
    echo "  Remember to update your firewall!"
fi
echo ""

echo "2. Disabling root login..."
sed -i "s/^#*PermitRootLogin .*/PermitRootLogin no/" "$SSHD_CONFIG"
echo "✓ Root login disabled"
echo ""

read -p "Disable password authentication? (Requires SSH key setup) (y/n): " DISABLE_PASS
if [ "$DISABLE_PASS" = "y" ]; then
    sed -i "s/^#*PasswordAuthentication .*/PasswordAuthentication no/" "$SSHD_CONFIG"
    echo "✓ Password authentication disabled"
    echo "  ⚠️  Make sure you have SSH keys configured!"
fi
echo ""

echo "3. Applying security settings..."

sed -i "s/^#*PermitEmptyPasswords .*/PermitEmptyPasswords no/" "$SSHD_CONFIG"
sed -i "s/^#*MaxAuthTries .*/MaxAuthTries 3/" "$SSHD_CONFIG"
sed -i "s/^#*LoginGraceTime .*/LoginGraceTime 20/" "$SSHD_CONFIG"
sed -i "s/^#*X11Forwarding .*/X11Forwarding no/" "$SSHD_CONFIG"

cat >> "$SSHD_CONFIG" << EOF

# Security hardening
Protocol 2
HostbasedAuthentication no
IgnoreRhosts yes
UsePAM yes
ClientAliveInterval 300
ClientAliveCountMax 2
MaxSessions 3
EOF

echo "✓ Security settings applied"
echo ""

echo "4. Installing fail2ban..."
if command -v apt-get &> /dev/null; then
    apt-get update && apt-get install -y fail2ban
elif command -v yum &> /dev/null; then
    yum install -y fail2ban
fi

if [ -f /etc/fail2ban/jail.local ]; then
    cp /etc/fail2ban/jail.local /etc/fail2ban/jail.local.backup
fi

cat > /etc/fail2ban/jail.local << EOF
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
EOF

systemctl enable fail2ban
systemctl restart fail2ban

echo "✓ fail2ban installed and configured"
echo ""

echo "5. Testing SSH configuration..."
sshd -t

if [ $? -eq 0 ]; then
    echo "✓ Configuration is valid"
    echo ""
    
    read -p "Apply changes and restart SSH? (y/n): " RESTART
    if [ "$RESTART" = "y" ]; then
        systemctl restart sshd
        echo "✓ SSH service restarted"
    fi
else
    echo "✗ Configuration has errors!"
    echo "Restoring backup..."
    cp "$BACKUP_FILE" "$SSHD_CONFIG"
    exit 1
fi

echo ""
echo "======================================"
echo "   SSH HARDENING COMPLETED"
echo "======================================"
echo ""
echo "Summary of changes:"
echo "- Root login: DISABLED"
echo "- Max auth tries: 3"
echo "- Login grace time: 20 seconds"
echo "- fail2ban: ENABLED"
[ "$CHANGE_PORT" = "y" ] && echo "- SSH port: $NEW_PORT"
[ "$DISABLE_PASS" = "y" ] && echo "- Password auth: DISABLED"
echo ""
echo "⚠️  IMPORTANT:"
echo "1. Test your SSH connection in a new terminal"
echo "2. Keep this terminal open until you verify access"
echo "3. Update firewall if you changed the port"
echo "4. Make sure you have SSH keys if you disabled passwords"

Kullanım

# Root olarak çalıştır
sudo chmod +x ssh_hardening.sh
sudo ./ssh_hardening.sh

# SSH key oluştur (önerilir)
ssh-keygen -t ed25519 -C "[email protected]"

# Key'i sunucuya kopyala
ssh-copy-id user@server

# Fail2ban durumunu kontrol et
sudo fail2ban-client status sshd

Sorun Giderme

Sorun Giderme

Problem: Kendimi Kilitledim

Çözüm: VPS kontrol panelinden veya fiziksel erişimle yedeği geri yükleyin:

sudo cp /etc/ssh/sshd_config.backup.* /etc/ssh/sshd_config
sudo systemctl restart sshd

Problem: "sshd: command not found"

Çözüm: OpenSSH server kurun:

sudo apt-get install openssh-server

Etiketler

ssh security fail2ban brute force güvenlik hardening