Security Hardening Checklist สำหรับ Cloud VPS — SSH, Firewall, Kernel และ Audit

Cloud VPS ที่เพิ่ง Provision มาใหม่มักมีช่องโหว่ดด้านความปลอดภัยหลายจุด ไม่ว่าจะเป็น SSH ที่เปิด Password Login, Port ที่เปิดไว้โดยไม่จำเป็น, Service ที่รันด้วยสิทธิ์สูงเกินไป หรือ Package ที่ไม่ได้ Update Security Patch การทำ Hardening ตั้งแต่เริ่มต้นช่วยลดพื้นที่โจมตี (Attack Surface) ได้มากที่สุด

บทความนี้เป็น Checklist ที่ครอบคลุมทุกด้านของ Security Hardening สำหรับ Cloud VPS ตั้งแต่ SSH Hardening, Firewall Setup, User Management, Automatic Security Updates, Kernel Hardening ด้วย sysctl, Auditd Logging, และ Service Minimization พร้อม Script ตรวจสอบสถานะ

1. SSH Hardening

SSH เป็นประตูหลักเข้า Server ต้องล็อคให้แน่นที่สุดก่อนทำอย่างอื่น

# แก้ไข SSH Config
sudo nano /etc/ssh/sshd_config

# ค่าที่ต้องเปลี่ยน:
Port 2222                       # เปลี่ยนจาก Port 22 (ลด Brute Force Noise)
PermitRootLogin no              # ห้าม Root Login โดยตรง
PasswordAuthentication no       # ห้าม Password Login — ใช้ SSH Key เท่านั้น
PubkeyAuthentication yes        # อนุญาต SSH Key Login
AuthorizedKeysFile .ssh/authorized_keys
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yes

# จำกัดจำนวนครั้งที่ลอง Auth
MaxAuthTries 3
MaxSessions 5

# ป้องกัน Connection Hanging
ClientAliveInterval 300
ClientAliveCountMax 2
LoginGraceTime 30

# จำกัด User/Group ที่ SSH ได้
AllowUsers deploy admin
# หรือ
AllowGroups sshusers

# Disable ฟีเจอร์ที่ไม่ใช้
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
PermitTunnel no

# Restart SSH (ต้องมี Session เดิมไว้ก่อน!)
sudo sshd -t   # ทดสอบ Config ก่อน
sudo systemctl restart sshd
# เพิ่ม SSH Key สำหรับ User ที่จะใช้งาน
# บน Local Machine:
ssh-keygen -t ed25519 -C "[email protected]"
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 admin@your-server-ip

# หรือ Manual Copy:
# สร้าง ~/.ssh/authorized_keys บน Server แล้ว paste Public Key

# ทดสอบ Login ก่อน Disable Password
ssh -p 2222 -i ~/.ssh/id_ed25519 admin@your-server-ip

# ตรวจสอบว่าเข้าได้ก่อนปิด Session เดิม!

2. Firewall Setup

# UFW (Ubuntu/Debian) — ง่ายและพอเพียงสำหรับ VPS ทั่วไป
sudo apt install ufw -y

# Default Policy: Deny All Incoming, Allow All Outgoing
sudo ufw default deny incoming
sudo ufw default allow outgoing

# เปิดเฉพาะ Port ที่ต้องการ
sudo ufw allow 2222/tcp comment 'SSH Custom Port'
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'

# เปิดใช้งาน UFW
sudo ufw enable
sudo ufw status verbose

# จำกัด Rate (ป้องกัน Brute Force บน SSH)
sudo ufw limit 2222/tcp comment 'SSH Rate Limit'
# firewalld (RHEL/Rocky/AlmaLinux)
sudo systemctl enable --now firewalld

# ดู Zone ปัจจุบัน
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all

# เพิ่ม SSH Port ใหม่ และลบ Port 22
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --permanent --remove-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# Reload
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

3. User Management และ Privilege Control

# สร้าง Admin User (อย่า Login ด้วย root โดยตรง)
sudo adduser admin
sudo usermod -aG sudo admin    # Ubuntu/Debian
sudo usermod -aG wheel admin   # RHEL/Rocky

# ล็อค Root Account (ไม่ให้ Login ด้วย Password)
sudo passwd -l root

# ตรวจสอบ User ที่มี UID 0 (ควรมีแค่ root)
awk -F: '$3 == 0 {print $1}' /etc/passwd

# ตรวจสอบ User ที่มี Shell สามารถ Login ได้
awk -F: '$7 !~ /nologin|false/ {print $1, $7}' /etc/passwd

# ล็อค User ที่ไม่ควรมี Login Shell
sudo usermod -s /usr/sbin/nologin daemon
sudo usermod -s /usr/sbin/nologin bin

# ตรวจสอบ Sudoers ที่ให้สิทธิ์ NOPASSWD (ควรมีน้อยที่สุด)
sudo grep -r NOPASSWD /etc/sudoers /etc/sudoers.d/
# Password Policy (libpam-pwquality สำหรับ Ubuntu)
sudo apt install libpam-pwquality -y

# แก้ไข /etc/security/pwquality.conf
sudo nano /etc/security/pwquality.conf

# ตั้งค่า:
minlen = 12
dcredit = -1
ucredit = -1
lcredit = -1
ocredit = -1
maxrepeat = 3
reject_username = 1

# ตั้งค่า Password Aging
sudo chage -M 90 -m 1 -W 14 admin  # Max 90 วัน, Min 1 วัน, Warn 14 วัน
sudo chage -l admin                 # ตรวจสอบ

4. Automatic Security Updates

# Ubuntu/Debian: unattended-upgrades
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

# แก้ไข Config
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

# ตรวจสอบว่า Security Updates เปิดอยู่:
# Unattended-Upgrade::Allowed-Origins {
#   "${distro_id}:${distro_codename}-security";
# };
# Unattended-Upgrade::Automatic-Reboot "false";
# Unattended-Upgrade::Mail "[email protected]";
# Unattended-Upgrade::Remove-Unused-Dependencies "true";

# ตรวจสอบ Auto-Update Schedule
sudo nano /etc/apt/apt.conf.d/20auto-upgrades
# APT::Periodic::Update-Package-Lists "1";
# APT::Periodic::Unattended-Upgrade "1";

# Test Run
sudo unattended-upgrade --dry-run --debug
# RHEL/Rocky: dnf-automatic
sudo dnf install dnf-automatic -y

# แก้ไข Config
sudo nano /etc/dnf/automatic.conf

# [commands] section:
# upgrade_type = security   # อัพเดตเฉพาะ Security Patches
# apply_updates = yes
# reboot = never

# [emitters] section:
# emit_via = email
# email_to = [email protected]

# เปิดใช้งาน
sudo systemctl enable --now dnf-automatic.timer
sudo systemctl status dnf-automatic.timer

5. Kernel Hardening ด้วย sysctl

# /etc/sysctl.d/99-hardening.conf
# Kernel Hardening Parameters

sudo tee /etc/sysctl.d/99-hardening.conf <<'EOF'
# ป้องกัน IP Spoofing
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# ป้องกัน Source Routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0

# ปิด ICMP Redirect (ป้องกัน MITM)
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

# SYN Flood Protection
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2

# ป้องกัน Time-Wait Assassination
net.ipv4.tcp_rfc1337 = 1

# Log Suspicious Packets
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1

# ปิด IPv4/IPv6 IP Forwarding (ถ้าไม่ใช้ Router)
net.ipv4.ip_forward = 0
net.ipv6.conf.all.forwarding = 0

# Kernel Pointer Restriction
kernel.kptr_restrict = 2

# ป้องกัน dmesg Leaking
kernel.dmesg_restrict = 1

# ป้องกัน Core Dump จาก SUID Programs
fs.suid_dumpable = 0

# ASLR — Address Space Layout Randomization
kernel.randomize_va_space = 2
EOF

# Apply
sudo sysctl --system

6. Service Minimization

# ตรวจสอบ Service ที่รันอยู่ทั้งหมด
systemctl list-units --type=service --state=running

# ตรวจสอบ Port ที่เปิดอยู่
ss -tlnp
ss -ulnp  # UDP

# Service ที่ควร Disable ถ้าไม่ใช้
sudo systemctl disable --now avahi-daemon   # mDNS/Bonjour (ไม่ต้องใช้บน Server)
sudo systemctl disable --now cups           # Print Spooler
sudo systemctl disable --now bluetooth      # Bluetooth (ไม่มีบน VPS)
sudo systemctl disable --now rpcbind        # NFS RPC (ถ้าไม่ใช้ NFS)
sudo systemctl disable --now nfs-server     # NFS Server

# ตรวจสอบ Network Service ที่ Listen บน 0.0.0.0 (Exposed ทุก Interface)
ss -tlnp | grep '0.0.0.0'

# ดู Service ที่ Enable ทั้งหมด
systemctl list-unit-files --state=enabled --type=service

7. Auditd — Audit Logging

# ติดตั้งและเปิดใช้งาน auditd
sudo apt install auditd audispd-plugins -y   # Ubuntu
sudo dnf install audit -y                     # RHEL/Rocky
sudo systemctl enable --now auditd

# ตั้งค่า Audit Rules พื้นฐาน
sudo tee /etc/audit/rules.d/hardening.rules <<'EOF'
# Monitor การเปลี่ยนแปลง User/Group
-w /etc/passwd -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k sudoers
-w /etc/sudoers.d/ -p wa -k sudoers

# Monitor SSH Config
-w /etc/ssh/sshd_config -p wa -k sshd

# Monitor Login Events
-w /var/log/faillog -p wa -k logins
-w /var/log/lastlog -p wa -k logins

# Monitor Privilege Escalation
-a always,exit -F arch=b64 -S setuid -k privilege_escalation
-a always,exit -F arch=b64 -S setgid -k privilege_escalation

# Monitor สั่ง sudo
-a always,exit -F arch=b64 -S execve -F uid=0 -F auid>=1000 -k root_commands

# Monitor การเปลี่ยนแปลง Network Config
-w /etc/hosts -p wa -k network
-w /etc/sysctl.conf -p wa -k sysctl
EOF

# Reload Rules
sudo augenrules --load
sudo auditctl -l   # ตรวจสอบ Rules ที่ Active

# ค้นหา Audit Events
sudo ausearch -k sudoers --start today
sudo ausearch -k identity --start today

8. File Permission Hardening

# ตรวจสอบ SUID/SGID Files (ควรมีน้อยที่สุด)
find / -perm -4000 -type f 2>/dev/null  # SUID files
find / -perm -2000 -type f 2>/dev/null  # SGID files

# ตรวจสอบ World-Writable Files
find / -type f -perm -0002 -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null

# ล็อค Permission ของ Sensitive Files
sudo chmod 640 /etc/shadow
sudo chmod 644 /etc/passwd
sudo chmod 600 /etc/ssh/sshd_config
sudo chmod 700 /root
sudo chmod 700 /root/.ssh

# ตรวจสอบ Sticky Bit บน World-Writable Directories
find / -type d -perm -1002 -not -path "/proc/*" 2>/dev/null | head -10
# /tmp, /var/tmp ควรมี Sticky Bit
ls -la / | grep tmp

# ตั้ง Secure Mount Options สำหรับ /tmp
# /etc/fstab:
# tmpfs /tmp tmpfs defaults,noexec,nosuid,nodev 0 0

9. Log Management

# ตรวจสอบ Log Rotation
sudo nano /etc/logrotate.conf
sudo ls /etc/logrotate.d/

# ตรวจสอบ Failed Login Attempts
sudo journalctl -u sshd --since "24 hours ago" | grep Failed
sudo lastb | head -20   # Bad Login Attempts
sudo last | head -20    # Successful Logins

# ดู Auth Log
sudo tail -f /var/log/auth.log        # Ubuntu/Debian
sudo journalctl -fu sshd              # systemd

# ตั้งค่า Remote Logging (ส่ง Log ไป SIEM/syslog-ng)
# แก้ไข /etc/rsyslog.conf เพิ่ม:
# *.* @siem-server:514    # UDP
# *.* @@siem-server:514   # TCP

# ป้องกัน Log Tampering — ใช้ systemd-journald persistent storage
sudo mkdir -p /var/log/journal
sudo systemctl restart systemd-journald

Security Hardening Checklist Script

#!/bin/bash
# /usr/local/bin/security-check.sh
# ตรวจสอบ Security Hardening Status

PASS=0
FAIL=0
WARN=0

check() {
    local desc="$1"
    local cmd="$2"
    if eval "$cmd" >/dev/null 2>&1; then
        echo "✅ $desc"
        ((PASS++))
    else
        echo "❌ $desc"
        ((FAIL++))
    fi
}

warn() {
    local desc="$1"
    local cmd="$2"
    if eval "$cmd" >/dev/null 2>&1; then
        echo "✅ $desc"
        ((PASS++))
    else
        echo "⚠️  $desc"
        ((WARN++))
    fi
}

echo "=== Security Hardening Check ==="
echo ""

echo "--- SSH ---"
check "SSH PasswordAuthentication disabled" "grep -q '^PasswordAuthentication no' /etc/ssh/sshd_config"
check "SSH PermitRootLogin disabled"         "grep -q '^PermitRootLogin no' /etc/ssh/sshd_config"
warn  "SSH Port changed from 22"            "! ss -tlnp | grep ':22 '"

echo ""
echo "--- Firewall ---"
check "UFW enabled"    "ufw status 2>/dev/null | grep -q 'Status: active'" || \
check "firewalld active" "systemctl is-active firewalld >/dev/null 2>&1"

echo ""
echo "--- Updates ---"
warn "unattended-upgrades installed" "dpkg -l unattended-upgrades 2>/dev/null | grep -q '^ii'"

echo ""
echo "--- Kernel ---"
check "IP Forward disabled"    "sysctl net.ipv4.ip_forward | grep -q '= 0'"
check "SYN Cookies enabled"    "sysctl net.ipv4.tcp_syncookies | grep -q '= 1'"
check "ASLR enabled"           "sysctl kernel.randomize_va_space | grep -q '= 2'"
check "kptr_restrict set"      "sysctl kernel.kptr_restrict | grep -q '= 2'"

echo ""
echo "--- Services ---"
warn "avahi-daemon disabled" "! systemctl is-active avahi-daemon >/dev/null 2>&1"

echo ""
echo "--- Audit ---"
warn "auditd running" "systemctl is-active auditd >/dev/null 2>&1"

echo ""
echo "=== Summary: ✅ $PASS Passed | ❌ $FAIL Failed | ⚠️  $WARN Warnings ==="

สรุป

Security Hardening สำหรับ Cloud VPS ต้องทำเป็น Layer โดยเริ่มจาก SSH ที่ต้องปิด Password Login และห้าม Root Login, ต่อด้วย Firewall ที่ Block ทุก Port ที่ไม่ใช้, User Management ที่จำกัดสิทธิ์ให้น้อยที่สุด, Automatic Security Updates เพื่อ Patch ช่องโหว่ดใหม่ ๆ, Kernel Hardening ผ่าน sysctl เพื่อป้องกัน Network Attacks, และ Auditd สำหรับติดตามกิจกรรมสงสัย การทำ Hardening ไม่ใช่ทำครั้งเดียวแล้วจบ ควรรัน Security Check Script เป็นประจำและทบทวน Config เมื่อมีการเปลี่ยนแปลง

แนะนำบริการ DE

การทำ Security Hardening ต้องการ Root Access เพื่อแก้ไข System Configuration ทุกระดับ ตั้งแต่ SSH Config ไปจนถึง Kernel Parameters Cloud VPS ของ DE ให้สิทธิ์ Root เต็มรูปแบบ สามารถ Harden ทุก Layer ได้ตามต้องการ พร้อม Network Isolation ระหว่าง VPS แต่ละตัวเพื่อความปลอดภัยเพิ่มเติม

สำหรับผู้ที่ต้องการโฮสต์เว็บไซต์โดยไม่ต้องดูแล Security Configuration เอง Cloud Hosting ของ DE มีระบบรักษาความปลอดภัยที่จัดการให้ ทั้ง Firewall, DDoS Protection และ Automatic Security Updates ในระดับ Infrastructure