SSH Hardening บน VPS — เสริมความปลอดภัยด้วย Command Line

SSH (Secure Shell) เป็นโปรโตคอลที่ใช้ในการเข้าถึง VPS จากระยะไกลอย่างปลอดภัย แต่หากไม่ตั้งค่าอย่างถูกต้อง SSH สามารถเป็นจุดอ่อนในการบุกรุกเซิร์ฟเวอร์ของคุณ SSH Hardening คือกระบวนการเพิ่มความปลอดภัย SSH ด้วยการเปลี่ยนการตั้งค่า Configuration บทความนี้จะแนะนำวิธี Harden SSH บน VPS Ubuntu เพื่อให้คุณมีเซิร์ฟเวอร์ที่ปลอดภัยและมั่นคง เพิ่มเติมจากการใช้ Cloud VPS จาก Dot Enterprise

1. สร้าง SSH Key Pair

ขั้นแรกสร้าง SSH Key Pair บนเครื่อง Client ของคุณเพื่อใช้ในการ Login:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/my_vps_key -C "[email protected]"

ตรวจสอบ Key ที่สร้างขึ้น:

ls -la ~/.ssh/
# my_vps_key (Private Key - เก็บไว้ที่เครื่อง Client)
# my_vps_key.pub (Public Key - อัปโหลดไปที่เซิร์ฟเวอร์)

2. อัปโหลด Public Key ไปยัง VPS

คัดลอก Public Key ไปยัง VPS:

ssh-copy-id -i ~/.ssh/my_vps_key.pub root@your_vps_ip

หรือคัดลอกด้วยตนเอง:

# เข้าสู่ VPS ด้วยวิธีปกติ
ssh root@your_vps_ip

# บน VPS
mkdir -p ~/.ssh
echo "your_public_key_content" >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

3. ปิด Password Authentication

แก้ไขไฟล์ SSH Configuration:

sudo nano /etc/ssh/sshd_config

ค้นหา PasswordAuthentication และเปลี่ยนเป็น no:

PasswordAuthentication no

4. เปลี่ยน SSH Port

SSH Default ใช้ Port 22 หากเปลี่ยนไปใช้ Port อื่น จะลดจำนวนการโจมตี Brute Force:

sudo nano /etc/ssh/sshd_config

# ค้นหา Port 22 และเปลี่ยนเป็น Port ที่ต่างไป (เช่น 2222)
Port 2222

5. ปิดการ Login ด้วย Root

ป้องกันการ Login ด้วย Root Account โดยปิด Root Login:

sudo nano /etc/ssh/sshd_config

# ค้นหา PermitRootLogin และเปลี่ยนเป็น no
PermitRootLogin no

6. ปิด Empty Password Login

ตรวจสอบว่ากำหนด PermitEmptyPasswords เป็น no:

sudo nano /etc/ssh/sshd_config

PermitEmptyPasswords no

7. จำกัดจำนวนการ Login ผิดพลาด

ป้องกันการโจมตี Brute Force:

sudo nano /etc/ssh/sshd_config

# จำกัดจำนวนการพยายาม Login
MaxAuthTries 3
MaxSessions 10

8. ปิด X11 Forwarding

หากไม่จำเป็น ให้ปิด X11 Forwarding:

sudo nano /etc/ssh/sshd_config

X11Forwarding no

9. ตั้งค่า SSH Timeout

ปิด Connection ที่ Idle นานเกินไป:

sudo nano /etc/ssh/sshd_config

# ตั้ง Timeout 300 วินาที (5 นาที)
ClientAliveInterval 300
ClientAliveCountMax 0

10. เปิดเฉพาะ Protocol SSH2

SSH Protocol 1 เป็นรุ่นเก่าและมีช่องโหว่ ให้ใช้เฉพาะ Protocol 2:

sudo nano /etc/ssh/sshd_config

Protocol 2

11. ตั้งค่า SSH Key Exchange Algorithms

ใช้ Cipher และ Key Exchange Algorithms ที่ปลอดภัยเท่านั้น:

sudo nano /etc/ssh/sshd_config

# เพิ่มบรรทัดเหล่านี้
KexAlgorithms curve25519-sha256,[email protected],diffie-hellman-group-exchange-sha256
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
MACs [email protected],[email protected],hmac-sha2-512,hmac-sha2-256

12. เปิด SSH Banner

ตั้ง SSH Banner เพื่อแสดง Warning Message:

sudo nano /etc/ssh/sshd_config

Banner /etc/ssh/banner.txt

# สร้างไฟล์ Banner
sudo nano /etc/ssh/banner.txt

# เพิ่มข้อความ:
WARNING: Unauthorized access to this system is forbidden and will be prosecuted by law.

13. Restart SSH Service

หลังจากเปลี่ยนแปลง Configuration ให้ Restart SSH Service:

sudo systemctl restart ssh

# ตรวจสอบสถานะ
sudo systemctl status ssh

14. ทดสอบการ Login ด้วย SSH Key

จากเครื่อง Client ของคุณ ทดสอบการ Login ด้วย SSH Key:

ssh -i ~/.ssh/my_vps_key -p 2222 your_username@your_vps_ip

15. ติดตั้ง Fail2ban เพื่อป้องกัน Brute Force

ติดตั้ง Fail2ban ในการป้องกันการโจมตี Brute Force:

sudo apt install fail2ban -y
sudo systemctl start fail2ban
sudo systemctl enable fail2ban

# ตั้งค่า Fail2ban สำหรับ SSH
sudo nano /etc/fail2ban/jail.local

# เพิ่มบรรทัดนี้:
[sshd]
enabled = true
port = 2222
maxretry = 3
findtime = 3600
bantime = 3600

สรุป

SSH Hardening เป็นขั้นตอนสำคัญในการเสริมความปลอดภัยของ VPS โดยการปิด Password Authentication, เปลี่ยน Port, ปิด Root Login และการตั้งค่า Cipher ที่ปลอดภัย คุณจะลดความเสี่ยงของการโจมตี Brute Force และ Unauthorized Access อย่างมีนัยสำคัญ เมื่อใช้บริการ Cloud VPS จาก Dot Enterprise และตั้งค่า SSH Hardening อย่างถูกต้อง คุณจะได้เซิร์ฟเวอร์ที่ปลอดภัยและเชื่อถือได้