Server Hardening คืออะไร? เทคนิคเสริมความแข็งแกร่งให้ Server

Server Hardening คือกระบวนการปรับ Configuration และลด Attack Surface ของ Server เพื่อป้องกันการถูกโจมตี บทความนี้อธิบายเทคนิคและ Best Practices สำหรับการทำ Server Hardening บน Linux Server ทั้งใน On-premise และ Cloud Environment

Server Hardening คืออะไร?

Server Hardening หรือการเสริมความแข็งแกร่งให้ Server คือการลด Attack Surface หรือจุดที่ผู้โจมตีสามารถเข้าถึงระบบได้ โดยการปิดการใช้งาน Services ที่ไม่จำเป็น ปรับ Configuration ให้ปลอดภัย และใช้หลักการ Least Privilege ในการจัดการสิทธิ์

ขั้นตอนการทำ Server Hardening

1. การอัปเดตระบบ

ขั้นตอนแรกและสำคัญที่สุดคือการให้ระบบทันสมัยอยู่เสมอ:

# อัปเดตแพ็กเกจทั้งหมด (Ubuntu/Debian)
sudo apt update && sudo apt upgrade -y

# เปิดใช้งาน Automatic Security Updates
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades

# CentOS/RHEL
sudo yum update -y
sudo yum install yum-cron
sudo systemctl enable yum-cron

2. ปิด Services ที่ไม่จำเป็น

# ดู Services ที่กำลังทำงาน
systemctl list-units --type=service --state=running

# ปิดและ Disable Services ที่ไม่ต้องการ
sudo systemctl stop 
sudo systemctl disable 

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

3. ตั้งค่า Firewall (UFW / iptables)

# ติดตั้งและตั้งค่า UFW
sudo apt install ufw

# ตั้งค่า Default Policy
sudo ufw default deny incoming
sudo ufw default allow outgoing

# เปิด Port ที่จำเป็นเท่านั้น
sudo ufw allow 22/tcp    # SSH
sudo ufw allow 80/tcp    # HTTP
sudo ufw allow 443/tcp   # HTTPS

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

4. การจัดการ User Accounts

  • ลบ หรือ Disable User Accounts ที่ไม่ได้ใช้งาน
  • ตั้งค่า Password Policy ที่เข้มงวด
  • ปิดการ Login ด้วย Root โดยตรง
  • ใช้ sudo แทนการ Login เป็น Root
# ตั้งค่า Password Policy ใน /etc/login.defs
PASS_MAX_DAYS   90
PASS_MIN_DAYS   7
PASS_WARN_AGE   14
PASS_MIN_LEN    12

# ล็อก Account ที่ไม่ใช้งาน
sudo usermod -L username

# ดู Users ทั้งหมดในระบบ
cut -d: -f1 /etc/passwd

5. Filesystem Permissions

# ตั้งค่า Permissions ที่เหมาะสม
chmod 700 /root
chmod 755 /etc
chmod 600 /etc/shadow
chmod 644 /etc/passwd

# หา SUID/SGID Files
find / -perm /4000 -type f 2>/dev/null
find / -perm /2000 -type f 2>/dev/null

# หา World-Writable Files
find / -perm -0002 -type f 2>/dev/null

6. Kernel Hardening ด้วย sysctl

# แก้ไข /etc/sysctl.conf
# ป้องกัน IP Spoofing
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# ปิด IP Forwarding (ถ้าไม่ใช่ Router)
net.ipv4.ip_forward = 0

# ป้องกัน SYN Flood
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048

# ปิด ICMP Redirects
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0

# Apply การเปลี่ยนแปลง
sudo sysctl -p

7. Logging และ Monitoring

# ติดตั้ง auditd สำหรับ Audit Logging
sudo apt install auditd
sudo systemctl enable auditd

# ตั้งค่า Audit Rules
sudo auditctl -w /etc/passwd -p wa -k passwd_changes
sudo auditctl -w /etc/shadow -p wa -k shadow_changes
sudo auditctl -w /var/log/auth.log -p wa -k auth_changes

# ดู Audit Log
sudo ausearch -k passwd_changes

CIS Benchmark สำหรับ Server Hardening

Center for Internet Security (CIS) มี Benchmark เป็น Guideline มาตรฐานสำหรับการทำ Server Hardening หมวดหมู่หลัก ได้แก่:

หมวดหมู่ รายละเอียด
Initial Setup Filesystem Configuration, Software Updates, Process Hardening
Services ปิด inetd, Time Synchronization, MTA Configuration
Network Configuration Network Parameters, IPv6, TCP Wrappers, Firewall
Logging & Auditing System Accounting, Syslog, Advanced Audit
Access Control PAM, User Accounts, Root Login, Sudo
System Maintenance File Permissions, User/Group Settings

เครื่องมือช่วย Server Hardening

  • Lynis: เครื่องมือ Audit ความปลอดภัยของ Linux/Unix
  • OpenSCAP: ตรวจสอบ Compliance ตาม SCAP Standards
  • Ansible Hardening: Automate การทำ Hardening ด้วย Ansible
  • Fail2ban: ป้องกัน Brute Force โดย Block IP ที่ Login ผิดซ้ำ
  • AIDE: Intrusion Detection ตรวจจับการเปลี่ยนแปลงไฟล์
# ติดตั้งและใช้งาน Lynis
sudo apt install lynis
sudo lynis audit system

# ติดตั้ง Fail2ban
sudo apt install fail2ban
sudo systemctl enable fail2ban

# ตรวจสอบสถานะ Fail2ban
sudo fail2ban-client status

สรุป

Server Hardening เป็นขั้นตอนสำคัญในการรักษาความปลอดภัยของโครงสร้างพื้นฐาน IT การนำ Best Practices ไปปฏิบัติตาม CIS Benchmark และใช้เครื่องมืออัตโนมัติจะช่วยลดความเสี่ยงได้อย่างมีประสิทธิภาพ สำหรับ Cloud VPS และ Cloud Hosting ของ Dot Enterprise (DE) มาพร้อมกับการตั้งค่า Security พื้นฐาน ช่วยให้ผู้ใช้งานมีจุดเริ่มต้นที่ปลอดภัย