今時今日,Linux Server 仲可以唔加固就咁放出街?
同你吹下水。呢排「Dirty Frag」漏洞爆出嚟,搞到全天下 sysadmin 人人自危。個漏洞可以喺大部分 2017 年之後嘅 Linux kernel 上面直接拎 root,連 patch 都未有就已經俾人 leak 咗出街,真係災難級別。
👉 延伸閱讀:Linux Server SSH 安全加固 5 步搞掂:由基本到進階嘅完整指南
但講真吖,就算冇呢個特定漏洞,淨係靠 default config 就放出街嘅 Linux server,本身都已經係一個計時炸彈。今日同大家探討下,點樣由頭到尾幫你部 Linux server 做好安全加固,就算下一波 zero-day 殺到埋身,你都仲有最後一道防線。
Step 1:基本系統更新同 Kernel 加固
第一步永遠都係最簡單但又最多人忽略嘅 — keep your system updated:
# Debian/Ubuntu
sudo apt update && sudo apt upgrade -y
sudo apt install linux-image-amd64 -y
# RHEL/CentOS/Rocky
sudo dnf update -y
sudo dnf install kernel -y
# 記得 reboot 載入新 kernel
sudo reboot
跟住,加幾個重要嘅 kernel parameters 去 /etc/sysctl.d/99-security.conf:
# 防止 IP spoofing
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# 忽略 ICMP redirects(防止中間人攻擊)
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# 防止 kernel pointer leaks
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
# ASLR 強化(令 memory corruption 更難 exploit)
kernel.randomize_va_space = 2
# 限制 ptrace(防止 process injection)
kernel.yama.ptrace_scope = 2
# 防止 magic SysRq key 被濫用
kernel.sysrq = 0
sudo sysctl -p /etc/sysctl.d/99-security.conf
Step 2:SSH 安全設定 — 第一道城牆
SSH 係最多人打嘅入口。Default port 22 + password login = 自殺。編輯 /etc/ssh/sshd_config:
# 轉 port(至少擋走 90% 自動掃描)
Port 2222
# 嚴禁用 root login
PermitRootLogin no
# 只用 SSH Key,禁用 password
PasswordAuthentication no
PubkeyAuthentication yes
# 限制可登入用戶
AllowUsers youruser
# 只用最強 cipher 同 MAC
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
# Connection 限制
MaxAuthTries 3
MaxSessions 5
ClientAliveInterval 300
ClientAliveCountMax 2
sudo systemctl restart sshd
仲有,裝個 fail2ban 防 brute force:
sudo apt install fail2ban -y
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# 編輯 jail.local,至少 enable sshd jail:
# [sshd]
# enabled = true
# port = 2222
# maxretry = 3
# bantime = 86400
sudo systemctl enable --now fail2ban
Step 3:Firewall — iptables 換 nftables
仲用緊 iptables 嘅朋友,係時候轉 nftables 喇。現代 Linux distro 都已經 default 用 nftables:
# 基本 nftables config (/etc/nftables.conf)
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
# Allow loopback
iif lo accept
# Allow established/related
ct state established,related accept
# Allow ICMP
ip protocol icmp icmp type { echo-request, echo-reply, destination-unreachable, time-exceeded } accept
ip6 nexthdr icmpv6 accept
# Allow SSH
tcp dport 2222 accept
# Allow HTTP/HTTPS(如有 web server)
tcp dport { 80, 443 } accept
# Log dropped packets(限速防 flood)
log prefix "NFT-DROP: " limit rate 10/minute
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
sudo systemctl enable --now nftables
Step 4:Auditd + AIDE — 入侵偵測
就算做好防禦,都要有偵測能力。裝 auditd 監控關鍵檔案變動:
sudo apt install auditd -y
# 加 rules 去 /etc/audit/rules.d/security.rules
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k privilege
-w /etc/ssh/sshd_config -p wa -k sshd_config
-w /var/log/auth.log -p wa -k auth_log
-w /etc/crontab -p wa -k cron
sudo systemctl restart auditd
另外裝 AIDE 做檔案完整性檢查,建立 baseline 之後定期 scan:
sudo apt install aide -y
sudo aideinit
sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
# 每日 cron job 檢查
echo "0 3 * * * root /usr/bin/aide.wrapper --check" | sudo tee /etc/cron.d/aide-check
Step 5:AppArmor / SELinux — 最後防線
呢個係對付 zero-day 嘅終極武器。AppArmor(Ubuntu/Debian)或 SELinux(RHEL)可以限制每個 process 可以做嘅嘢,就算有人 exploit 到你嘅 application,佢都受限於 Mandatory Access Control 框架入面:
# 檢查 AppArmor 狀態
sudo aa-status
# 幫你嘅 web app 設定 AppArmor profile
sudo apt install apparmor-profiles apparmor-utils -y
# 例如幫 nginx 做 complain mode 學習
sudo aa-complain /usr/sbin/nginx
# 行一輪正常 traffic 之後轉 enforce
sudo aa-enforce /usr/sbin/nginx
Step 6:Log 集中管理 + 自動警報
出事先睇 log 太遲。用 rsyslog forwarding 集中去一個 log server,或直上雲端 SIEM:
# 最簡單:forward 去 central log server
# /etc/rsyslog.d/50-forward.conf
*.* @192.168.1.100:514
sudo systemctl restart rsyslog
再裝個 lightweight HIDS 例如 Wazuh agent,有異常行為即刻 alert。
小結
坦白講,冇 server 係 100% 安全嘅 — 特別係面對 Dirty Frag 呢類 kernel-level zero-day。但做好以上幾步,你個 attack surface 會大幅縮細,就算俾人入到第一步,後面仲有層層關卡等住佢。Defense in depth,從來都係資安嘅王道。
🔗 參考資料:CIS Benchmarks
趁下個漏洞爆出嚟之前,搵個 weekend 同你啲 server 做個全面 body check 啦!🦾
#Server #安全 #IT教學 #監控 #技術分享



