ทำไมต้อง Security Audit Server ของคุณ
ผมดูแลเซิร์ฟเวอร์มามากกว่า 28 ปีและสิ่งที่ผมทำเป็นประจำทุกเดือนคือ security audit ไม่ว่าเซิร์ฟเวอร์จะตั้งค่าดีแค่ไหนตอนแรกเมื่อเวลาผ่านไป configuration อาจเปลี่ยนแปลง packages ใหม่ถูกติดตั้ง services ถูกเพิ่มและ security patches อาจถูกละเลย Lynis เป็นเครื่องมือที่ผมไว้วางใจมากที่สุดสำหรับงานนี้
Lynis เป็น open-source security auditing tool ที่เขียนด้วย Shell Script ทำงานได้บน Linux, macOS และ Unix-like systems ทั้งหมดมันตรวจสอบเซิร์ฟเวอร์ของคุณตาม security best practices ครอบคลุมตั้งแต่ boot loader, kernel parameters, file permissions, user accounts, network settings, firewall rules, installed software จนถึง web server configuration ผลลัพธ์จะให้คะแนน Hardening Index ตั้งแต่ 0-100 พร้อม suggestions ว่าควรแก้ไขอะไร
Lynis กับ CIS Benchmark
CIS (Center for Internet Security) Benchmarks เป็นมาตรฐาน security configuration ที่ได้รับการยอมรับทั่วโลก Lynis ตรวจสอบหลายรายการที่ตรง CIS Benchmark แม้จะไม่ครบ 100% แต่ก็ครอบคลุมรายการที่สำคัญที่สุดสำหรับองค์กรที่ต้องผ่าน compliance audit เช่น PCI DSS, HIPAA, SOC 2 การใช้ Lynis เป็นจุดเริ่มต้นที่ดีมาก
ติดตั้ง Lynis
# วิธีที่ 1: จาก Package Manager (Ubuntu/Debian)
apt install lynis
# วิธีที่ 2: จาก Official Repository (แนะนำ ได้ version ใหม่สุด)
wget -O - https://packages.cisofy.com/keys/cisofy-software-public.key | apt-key add -
echo "deb https://packages.cisofy.com/community/lynis/deb/ stable main" > /etc/apt/sources.list.d/cisofy-lynis.list
apt update && apt install lynis
# วิธีที่ 3: จาก Git (ไม่ต้อง install)
cd /opt
git clone https://github.com/CISOfy/lynis.git
cd lynis
./lynis audit system
# ตรวจสอบ version
lynis show version
# 3.1.1
รัน Audit ครั้งแรก
# รัน full system audit
sudo lynis audit system
# รัน audit แบบ non-interactive (สำหรับ automation)
sudo lynis audit system --no-colors --quiet
# รัน audit เฉพาะ section
sudo lynis audit system --tests-from-group "firewalls"
sudo lynis audit system --tests-from-group "ssh"
sudo lynis audit system --tests-from-group "authentication"
# รัน audit และบันทึก report
sudo lynis audit system --auditor "Bom SiamCafe" 2>&1 | tee /var/log/lynis-audit-$(date +%Y%m%d).log
ผลลัพธ์ที่ได้
# ตัวอย่างผลลัพธ์ส่วนท้าย
================================================================================
Lynis security scan details:
Hardening index : 67 [############# ]
Tests performed : 268
Plugins enabled : 2
================================================================================
- Warnings (3):
----------------------------
! Found one or more vulnerable packages [PKGS-7392]
! iptables module(s) loaded, but no rules active [FIRE-4512]
! No password set for single mode [AUTH-9308]
- Suggestions (42):
----------------------------
* Install libpam-tmpdir to set $TMP and $TMPDIR for PAM sessions [CUST-0280]
* Set a password on GRUB boot loader to prevent altering boot configuration [BOOT-5122]
* Harden compilers like restricting access to root user only [HRDN-7222]
...
อ่านผลลัพธ์ Lynis ให้เป็น
Lynis แบ่งผลลัพธ์เป็นหลายส่วนแต่ละส่วนมีความสำคัญต่างกันผมจะอธิบายวิธีอ่านและจัดลำดับความสำคัญ
สีของผลลัพธ์
สีเขียว (OK/FOUND) — ผ่านเกณฑ์แล้วไม่ต้องทำอะไร สีเหลือง (SUGGESTION) — แนะนำให้ปรับปรุงไม่เร่งด่วนแต่ควรทำ สีแดง (WARNING) — มีปัญหา security ที่ต้องแก้ไขด่วน สีขาว (NONE/UNKNOWN) — Lynis ไม่สามารถตรวจสอบได้หรือไม่เกี่ยวข้อง
ดูรายละเอียดเพิ่มเติม
# ดู details ของแต่ละ test
sudo lynis show details FIRE-4512
# ดู log file สำหรับรายละเอียดทั้งหมด
cat /var/log/lynis.log
# ดู report file (machine-readable)
cat /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
จัดลำดับความสำคัญของ Findings
ผมจัดลำดับตามความเสี่ยงดังนี้ Critical คือ vulnerable packages, default passwords, world-writable files ต้องแก้ทันที High คือ firewall ไม่ active, SSH allows root login, weak file permissions แก้ภายใน 24 ชั่วโมง Medium คือ kernel parameters ไม่ optimize, missing security headers, logging ไม่ครบแก้ภายใน 1 สัปดาห์ Low คือ cosmetic issues เช่น banner messages, unused packages ทำเมื่อมีเวลา
Hardening ตาม Lynis Suggestions
ผมจะแสดง hardening steps ที่พบบ่อยที่สุดจากประสบการณ์รัน Lynis บน server หลายร้อยเครื่อง
SSH Hardening
# /etc/ssh/sshd_config — ค่าที่ผมตั้งเสมอ
Protocol 2
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
MaxSessions 5
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2
X11Forwarding no
AllowTcpForwarding no
PermitEmptyPasswords no
Banner /etc/issue.net
# Restart SSH
systemctl restart sshd
# ทดสอบ ก่อน logout ให้เปิด terminal ใหม่เพื่อ test ว่า login ได้!
File Permissions
# แก้ไข permissions ที่ Lynis แจ้ง
chmod 600 /etc/crontab
chmod 600 /etc/ssh/sshd_config
chmod 700 /etc/cron.d
chmod 700 /etc/cron.daily
chmod 700 /etc/cron.hourly
chmod 700 /etc/cron.weekly
chmod 700 /etc/cron.monthly
chmod 644 /etc/passwd
chmod 640 /etc/shadow
chmod 644 /etc/group
chmod 640 /etc/gshadow
# ตรวจหา world-writable files
find / -xdev -type f -perm -0002 -ls 2>/dev/null
# ตรวจหา SUID/SGID files
find / -xdev \( -perm -4000 -o -perm -2000 \) -type f -ls 2>/dev/null
User Account Security
# ตั้ง password policy
# /etc/login.defs
PASS_MAX_DAYS 90
PASS_MIN_DAYS 1
PASS_WARN_AGE 14
PASS_MIN_LEN 12
# /etc/security/pwquality.conf
minlen = 12
dcredit = -1
ucredit = -1
ocredit = -1
lcredit = -1
maxrepeat = 3
# Lock unused accounts
usermod -L nobody
usermod -L games
# ตรวจหา accounts ที่ไม่มี password
awk -F: '($2 == "" ) { print $1 }' /etc/shadow
# ตรวจหา UID 0 accounts (นอกจาก root)
awk -F: '($3 == 0 && $1 != "root") { print $1 }' /etc/passwd
GRUB Boot Loader Protection
# ตั้ง password สำหรับ GRUB
grub-mkpasswd-pbkdf2
# จะได้ hash เช่น grub.pbkdf2.sha512.10000.xxxx
# เพิ่มใน /etc/grub.d/40_custom
cat >> /etc/grub.d/40_custom << 'EOF'
set superusers="admin"
password_pbkdf2 admin grub.pbkdf2.sha512.10000.YOUR_HASH_HERE
EOF
# Update GRUB
update-grub
Sysctl Security Parameters
# /etc/sysctl.d/99-security.conf
# ป้องกัน IP spoofing
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# ป้องกัน SYN flood
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
# ปิด ICMP redirect
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# ปิด source routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# Log martian packets
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
# ป้องกัน SMURF attack
net.ipv4.icmp_echo_ignore_broadcasts = 1
# ปิด IPv6 ถ้าไม่ใช้
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
# Kernel hardening
kernel.randomize_va_space = 2
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
kernel.yama.ptrace_scope = 1
fs.suid_dumpable = 0
# Apply
sysctl -p /etc/sysctl.d/99-security.conf
Firewall Setup
# ถ้า Lynis บอก firewall ไม่ active
# ตั้งค่า UFW (Ubuntu)
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw status verbose
สำหรับการป้องกัน brute force ใช้ Fail2ban ร่วมกับ firewall
รัน Lynis อัตโนมัติทุกสัปดาห์
#!/bin/bash
# /usr/local/bin/lynis-weekly.sh
REPORT_DIR="/var/log/lynis-reports"
DATE=$(date +%Y%m%d)
REPORT_FILE="/lynis-.log"
mkdir -p "$REPORT_DIR"
# รัน Lynis
lynis audit system --no-colors --quiet --auditor "Automated Scan" > "$REPORT_FILE" 2>&1
# ดึง hardening index
SCORE=$(grep "Hardening index" "$REPORT_FILE" | awk '{print $NF}' | tr -d '[]')
WARNINGS=$(grep -c "Warning" "$REPORT_FILE")
# ส่ง email สรุป
{
echo "Lynis Security Audit Report - $(hostname)"
echo "Date: $(date)"
echo "Hardening Index: /100"
echo "Warnings: "
echo ""
echo "=== Warnings ==="
grep "!" "$REPORT_FILE"
echo ""
echo "Full report: "
} | mail -s "[Security] Lynis Audit $(hostname) - Score: " admin@example.com
# เก็บ reports ไว้ 90 วัน
find "$REPORT_DIR" -name "lynis-*.log" -mtime +90 -delete
# Crontab entry
# 0 3 * * 0 /usr/local/bin/lynis-weekly.sh
เปรียบเทียบผลลัพธ์ข้ามเวลา
# เปรียบเทียบ report 2 ครั้ง
diff /var/log/lynis-reports/lynis-20260221.log /var/log/lynis-reports/lynis-20260228.log
# หรือใช้ Lynis built-in
lynis audit system --cronjob
# จะบันทึกผลใน /var/log/lynis-report.dat
# เปรียบเทียบกับ report ก่อนหน้าได้
สร้าง Custom Profile
# /etc/lynis/custom.prf
# Skip tests ที่ไม่เกี่ยวข้อง
skip-test=FIRE-4512 # ถ้าใช้ nftables แทน iptables
skip-test=KRNL-5820 # ถ้า kernel module ที่ Lynis ไม่รู้จัก
skip-test=USB-1000 # ถ้าเป็น VM ไม่มี USB
# กำหนด compliance standards
compliance-standards=cis, hipaa, pci-dss
# ตั้งค่าเพิ่มเติม
min-hardening-index=80
machine-role=server
log-tests-incorrect-os=no
# รัน Lynis ด้วย custom profile
lynis audit system --profile /etc/lynis/custom.prf
ส่ง Lynis Metrics ไปยัง Monitoring
# Script สำหรับส่ง hardening index ไปยัง Prometheus
#!/bin/bash
# /usr/local/bin/lynis-prometheus.sh
TEXTFILE_DIR="/var/lib/node_exporter/textfile_collector"
lynis audit system --no-colors --quiet > /dev/null 2>&1
SCORE=$(grep "hardening_index" /var/log/lynis-report.dat | cut -d= -f2)
WARNINGS=$(grep -c "^warning\[\]" /var/log/lynis-report.dat)
SUGGESTIONS=$(grep -c "^suggestion\[\]" /var/log/lynis-report.dat)
cat > "/lynis.prom" << EOF
# HELP lynis_hardening_index Lynis hardening index score
# TYPE lynis_hardening_index gauge
lynis_hardening_index
# HELP lynis_warnings_total Total number of Lynis warnings
# TYPE lynis_warnings_total gauge
lynis_warnings_total
# HELP lynis_suggestions_total Total number of Lynis suggestions
# TYPE lynis_suggestions_total gauge
lynis_suggestions_total
EOF
ใช้ metrics นี้สร้าง dashboard ใน Grafana เพื่อ track security score ของ server ทุกเครื่อง
Lynis Hardening Index ควรได้เท่าไร?
สำหรับ production server ผมตั้งเป้าไว้ที่ 80+ ถ้าได้ 70-79 ถือว่าพอใช้ได้ต่ำกว่า 70 ต้องปรับปรุงด่วนแต่อย่ามุ่งเน้นที่คะแนนมากเกินไปบาง suggestion อาจไม่เหมาะกับ use case ของคุณเช่น Lynis แนะนำให้ปิด USB แต่ถ้าเป็น physical server ที่ต้องใช้ USB บางครั้งก็ skip ได้สิ่งสำคัญคือ warnings ต้องเป็น 0
Lynis ต่างจาก OpenSCAP ยังไง?
Lynis เป็น lightweight tool ที่ใช้ Shell Script เหมาะสำหรับ quick audit และ regular scanning OpenSCAP เป็น framework ที่ซับซ้อนกว่ารองรับ SCAP content จาก CIS, DISA STIG ได้ตรงๆเหมาะสำหรับ enterprise compliance ที่ต้อง certify ตาม standard เฉพาะผมใช้ Lynis สำหรับ weekly scans และ OpenSCAP สำหรับ quarterly compliance audits
Lynis ทำงานบน Container ได้ไหม?
ได้ครับแต่ผลลัพธ์จะต่างจากการรันบน host เพราะ container ไม่มี kernel, boot loader, hardware modules Lynis จะ skip tests ที่ไม่เกี่ยวข้องอัตโนมัติสำหรับ Docker security scanning ผมแนะนำ Trivy หรือ Grype แทนเพราะมันตรวจ vulnerabilities ใน container images ได้ดีกว่า
รัน Lynis บน server ที่มี traffic หนักจะกระทบ performance ไหม?
แทบไม่กระทบเลยครับ Lynis เป็น read-only tool ไม่ได้เขียนอะไรลง disk (ยกเว้น log files) ไม่ได้ทำ network scan ไม่ได้ start/stop services ใช้เวลารันประมาณ 3-5 นาทีบน server ทั่วไปผมรัน Lynis บน production server ที่มี 10,000+ concurrent users ได้โดยไม่มีปัญหา
สรุป
Lynis เป็นเครื่องมือ security audit ที่ทุก Linux administrator ต้องรู้จักมันง่ายในการติดตั้งใช้ resource น้อยรันได้เร็วและให้ผลลัพธ์ที่ actionable ทันทีผมแนะนำให้รัน Lynis อย่างน้อยสัปดาห์ละครั้งตั้งค่าให้ส่ง report ทาง email อัตโนมัติและ track hardening index ใน monitoring system
จำไว้ว่า security ไม่ใช่สิ่งที่ทำครั้งเดียวแล้วจบมันเป็น continuous process ที่ต้องทำสม่ำเสมอ Lynis ช่วยให้คุณจับจุดอ่อนได้ก่อนที่ attacker จะเจอใช้ร่วมกับ Fail2ban สำหรับ active defense และ WireGuard VPN สำหรับ secure remote access ก็จะได้ security posture ที่แข็งแกร่งครับ
อ่านเพิ่มเติม: สอนเทรด Forex | XM Signal | IT Hardware | อาชีพ IT