ตรวจสอบความปลอดภัย VPS ด้วย Lynis Security Audit
Lynis คือ open-source security auditing tool สำหรับ Linux/Unix ที่ได้รับความนิยมสูงสุดตัวหนึ่ง ทำงานโดยสแกนระบบอย่างละเอียดตั้งแต่ kernel parameters, file permissions, ไปจนถึง installed packages และ authentication settings แล้วคำนวณ Hardening Index ออกมาเป็นคะแนน 0–100 พร้อม warning และ suggestion ที่ชัดเจน
ต่างจาก Nessus หรือ OpenVAS ที่ต้องการ server แยก Lynis รัน locally บน host เลย ไม่ต้องการ agent พิเศษ เหมาะสำหรับ sysadmin ที่อยาก audit VPS ตัวเองก่อน go live หรือตั้ง schedule audit รายเดือน
ทำไมต้องใช้ Lynis?
VPS ที่ติดตั้ง Ubuntu/Debian ใหม่ ส่วนใหญ่มี hardening index แค่ 55–65 จากเต็ม 100 หมายความว่ายังมีช่องโหว่ที่ควรปิดอีกมาก เช่น:
- SSH ยังอนุญาต root login / password authentication
- Kernel parameters ยังไม่ได้ tune ด้าน security
- ไม่มี audit daemon (auditd) ทำงาน
- File permissions ไม่เหมาะสมบาง directory
- Outdated packages ที่มี known CVE
ติดตั้ง Lynis บน Ubuntu/Debian
แนะนำติดตั้งจาก CISOfy repository โดยตรง เพื่อให้ได้ version ล่าสุดเสมอ (package ใน Ubuntu apt มักล้าหลัง):
# เพิ่ม GPG key และ repository
curl -fsSL https://packages.cisofy.com/keys/cisofy-software-public.key | \
sudo gpg --dearmor -o /etc/apt/keyrings/cisofy.gpg
echo "deb [signed-by=/etc/apt/keyrings/cisofy.gpg] \
https://packages.cisofy.com/community/lynis/deb/ stable main" | \
sudo tee /etc/apt/sources.list.d/cisofy-lynis.list
sudo apt update
sudo apt install lynis -y
# ตรวจสอบ version
lynis --version
ติดตั้งบน CentOS/AlmaLinux/Rocky Linux
# เพิ่ม CISOfy repo
cat <
รัน Security Audit ครั้งแรก
คำสั่งหลักคือ lynis audit system ต้องรันด้วย root:
sudo lynis audit system
Lynis จะสแกนผ่านกว่า 200 tests ใช้เวลาประมาณ 2–5 นาที ผลลัพธ์จะแสดงใน terminal แบ่งเป็นสี:
- [OK] — ผ่านแล้ว ปลอดภัย
- [WARNING] — ควรตรวจสอบ มีความเสี่ยง
- [SUGGESTION] — แนะนำให้ปรับปรุง
- [FOUND] — พบสิ่งผิดปกติ
อ่าน Hardening Index และ Report
ท้าย output จะมีสรุปสำคัญ:
================================================================================
Lynis security scan details:
Hardening index : 67 [############# ]
Tests performed : 247
Plugins enabled : 0
Components:
- Firewall [V]
- Malware scanner [X]
- File integrity tool [X]
Lynis Modules:
- Compliance Status [?]
- Security Audit [V]
- Vulnerability Scan [V]
================================================================================
Report เต็มๆ ถูกบันทึกที่ /var/log/lynis.log และ /var/log/lynis-report.dat:
# ดู warnings ทั้งหมด
grep "^warning\[\]" /var/log/lynis-report.dat
# ดู suggestions
grep "^suggestion\[\]" /var/log/lynis-report.dat
# ดู hardening index
grep "hardening_index" /var/log/lynis-report.dat
แก้ไข Warning ที่พบบ่อย
1. SSH Root Login ยังเปิดอยู่
Warning: SSH root login is allowed
sudo sed -i 's/^PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
2. SSH Password Authentication เปิดอยู่
Warning: SSH password authentication is enabled
sudo sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
3. ไม่มี Malware Scanner
Suggestion: Install a file integrity tool
sudo apt install rkhunter -y
sudo rkhunter --update
sudo rkhunter --check --sk
4. auditd ไม่ได้รัน
Warning: auditd not running
sudo apt install auditd -y
sudo systemctl enable auditd
sudo systemctl start auditd
5. Kernel Hardening Parameters ยังไม่ครบ
Suggestion: Consider hardening sysctl settings
cat <
ตั้ง Schedule Audit รายสัปดาห์
ตั้ง cron job ให้ Lynis รันทุกอาทิตย์และส่ง report ทาง email:
# สร้างสคริปต์
cat <<'EOF' | sudo tee /usr/local/bin/lynis-weekly.sh
#!/bin/bash
REPORT_DIR=/var/log/lynis-reports
mkdir -p $REPORT_DIR
DATE=$(date +%Y-%m-%d)
lynis audit system --no-colors > $REPORT_DIR/lynis-$DATE.log 2>&1
# เก็บ report ไว้ 30 วัน
find $REPORT_DIR -name "lynis-*.log" -mtime +30 -delete
EOF
sudo chmod +x /usr/local/bin/lynis-weekly.sh
# เพิ่ม cron ทุกวันอาทิตย์ 02:00
echo "0 2 * * 0 root /usr/local/bin/lynis-weekly.sh" | \
sudo tee /etc/cron.d/lynis-audit
Non-interactive Mode สำหรับ CI/CD
รัน Lynis ใน pipeline โดยไม่ต้องการ interaction:
sudo lynis audit system --quick --no-colors 2>&1 | \
tee /tmp/lynis-ci.log
# ดึง hardening index
SCORE=$(grep "hardening_index" /var/log/lynis-report.dat | cut -d'=' -f2)
echo "Hardening Index: $SCORE"
# fail pipeline ถ้า score ต่ำกว่า 70
[ "$SCORE" -lt 70 ] && echo "FAIL: Score too low" && exit 1 || echo "PASS"
Custom Profiles สำหรับ Compliance
Lynis รองรับ custom profile เพื่อ skip tests ที่ไม่เกี่ยวข้อง หรือเพิ่ม hardening rules ของบริษัท:
# Copy default profile
sudo cp /etc/lynis/default.prf /etc/lynis/custom.prf
# แก้ไข profile เช่น skip test ที่รู้ว่า false positive
echo "skip-test=AUTH-9308" >> /etc/lynis/custom.prf
# รันด้วย custom profile
sudo lynis audit system --profile /etc/lynis/custom.prf
ตรวจสอบว่า Lynis ทำงานถูกต้อง
# ตรวจสอบ version และ database integrity
sudo lynis show version
sudo lynis show dbdir
# รัน test เฉพาะ category (เช่น authentication)
sudo lynis audit system --tests-from-category "authentication"
# รัน test เฉพาะ test ID
sudo lynis audit system --tests "AUTH-9328 SSH-7408"
# ดู warning ล่าสุด
sudo grep "^warning" /var/log/lynis-report.dat | tail -20
# ตรวจสอบว่า report ถูกสร้าง
ls -lh /var/log/lynis*.log /var/log/lynis-report.dat
sudo lynis audit system อีกครั้ง เพื่อดูว่า Hardening Index เพิ่มขึ้นหรือเปล่า — ทำซ้ำจนได้คะแนนเกิน 75+แก้ปัญหาที่พบบ่อย
Error: "lynis: command not found" หลังติดตั้งแล้ว
PATH ไม่มี /usr/sbin สำหรับ user ปกติ ต้องรันด้วย sudo หรือ full path:
sudo /usr/sbin/lynis audit system
# หรือ
which lynis
ls -la /usr/sbin/lynis /usr/bin/lynis 2>/dev/null
Error: "Lynis is outdated" warning ใน output
Lynis มี built-in version check ถ้าใช้ apt version อาจล้าหลัง ให้ update จาก CISOfy repo:
sudo apt update && sudo apt upgrade lynis -y
# ตรวจสอบ version ล่าสุด
lynis show version
# version ล่าสุดดูได้ที่ https://cisofy.com/lynis/
Error: "No suitable home directory found for user" ใน report
มี user ใน /etc/passwd ที่ไม่มี home directory จริง — ปกติเป็น system user ไม่อันตราย:
# ดูรายชื่อ user ที่มีปัญหา
sudo lynis show details AUTH-9234
# ตรวจสอบว่าเป็น service account จริง
grep "^nobody\|^www-data\|^daemon" /etc/passwd
Error: "Found password in /etc/passwd (NIS)" false positive
เกิดกับบาง system ที่ migrate มาจาก NIS สามารถ skip test นี้ได้:
echo "skip-test=AUTH-9252" | sudo tee -a /etc/lynis/custom.prf
sudo lynis audit system --profile /etc/lynis/custom.prf
Warning: "AIDE not installed" — ต้องการ file integrity monitoring
AIDE (Advanced Intrusion Detection Environment) คือ file integrity checker ที่ Lynis แนะนำ:
sudo apt install aide -y
sudo aideinit
sudo cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db
# ตรวจสอบ integrity
sudo aide --check
คำถามที่พบบ่อย
Lynis vs. OpenVAS ต่างกันอย่างไร?
Lynis เป็น local audit tool — รันบนเครื่องเป้าหมายโดยตรง เหมาะสำหรับ system hardening check และ compliance OpenVAS เป็น network scanner ที่สแกนจากภายนอก เหมาะสำหรับหา open ports และ network-level vulnerabilities ควรใช้ทั้งสองร่วมกันเพื่อ coverage ครบ
Hardening Index เท่าไหร่ถึงจะ "ปลอดภัย"?
ไม่มีตัวเลข "magic" — แต่ทั่วไปถือว่า score 70+ คือ acceptable สำหรับ production server ทั่วไป 80+ สำหรับ server ที่เก็บข้อมูล sensitive และ 85+ สำหรับ compliance (PCI-DSS, HIPAA) ค่าเริ่มต้น Ubuntu 22.04 มักได้ราว 60–65
รัน Lynis บน container (Docker) ได้ไหม?
รันได้แต่ผลลัพธ์จะ limited เพราะ container ไม่มี kernel access เต็มรูปแบบ หลาย tests จะ skip ไปอัตโนมัติ Lynis เหมาะสำหรับ audit host OS มากกว่า container — สำหรับ container ควรใช้ docker bench หรือ trivy แทน