# 產生密碼雜湊
grub2-mkpasswd-pbkdf2
# 輸入密碼兩次,複製輸出雜湊值
# 編輯 /etc/grub.d/40_custom
cat <> /etc/grub.d/40_custom
set superusers="root"
password_pbkdf2 root grub.pbkdf2.sha512.10000.XXXXXXXXXXXXXXXX
EOF
# 更新 GRUB
grub2-mkconfig -o /boot/grub2/grub.cfg
# 或 UEFI: grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
效果:編輯啟動項、進入單用戶模式、載入自定核心參數均需密碼。
systemctl list-unit-files --state=enabled | grep -v "@"
# 逐項評估:關閉 cups, avahi, bluetooth, rpcbind 等非必要服務
systemctl disable --now cups avahi-daemon bluetooth rpcbind
# 檢查 /etc/passwd 中無需登入的帳號
# 鎖定帳號 (禁止登入,保留檔案擁有者)
usermod -L -s /sbin/nologin games
usermod -L -s /sbin/nologin gopher
usermod -L -s /sbin/nologin news
usermod -L -s /sbin/nologin uucp
# 刪除真正不需要的帳號/群組
# userdel -r lp; groupdel lp
# /etc/login.defs
PASS_MAX_DAYS 90 # 密碼最長使用天數
PASS_MIN_DAYS 7 # 密碼最短變更間隔
PASS_MIN_LEN 12 # 最小長度 (建議 12+)
PASS_WARN_AGE 14 # 過期前警告天數
# /etc/security/pwquality.conf (pam_pwquality)
minlen = 12
dcredit = -1 # 至少 1 個數字
ucredit = -1 # 至少 1 個大寫
lcredit = -1 # 至少 1 個小寫
ocredit = -1 # 至少 1 個特殊字元
difok = 5 # 新舊密碼至少 5 字元不同
enforce_for_root
# 現代系統預設已啟用
authconfig --enableshadow --passalgo=sha512 --update
# 或手動確認 /etc/passwd 為 x,/etc/shadow 存雜湊
# /etc/ssh/sshd_config
Port 2222 # 修改預設埠 (降低掃描噪音)
PermitRootLogin no # 禁止 root 直接登入
PubkeyAuthentication yes # 僅允許金鑰認證
PasswordAuthentication no # 禁用密碼認證
PermitEmptyPasswords no
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
LoginGraceTime 30
AllowUsers admin user1 # 白名單
DenyUsers * # 或使用 AllowGroups
Protocol 2
Ciphers aes256-ctr,aes192-ctr,aes128-ctr
MACs hmac-sha2-256,hmac-sha2-512
KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
systemctl reload sshd
# /etc/pam.d/system-auth /etc/pam.d/password-auth
auth required pam_faillock.so preauth silent audit deny=5 unlock_time=900 fail_interval=900
auth [success=1 default=bad] pam_unix.so
auth [default=die] pam_faillock.so authfail audit deny=5 unlock_time=900 fail_interval=900
account required pam_faillock.so
# 連續 5 次失敗鎖定 15 分鐘
chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow
chattr +i /etc/ssh/sshd_config /etc/sudoers
chattr +i /boot/grub2/grub.cfg
# 解除: chattr -i 檔案
# IP 轉發與重導向
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
# 來源路由驗證
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
net.ipv4.tcp_synack_retries = 2
# ICMP
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
# 記憶體保護
kernel.randomize_va_space = 2
kernel.exec_shield = 1
kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
kernel.yama.ptrace_scope = 1
# 載入
sysctl --system
yum install -y audit audit-libs
systemctl enable --now auditd
# 關鍵規則 /etc/audit/rules.d/audit.rules
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k scope
-w /etc/ssh/sshd_config -p wa -k config
-a always,exit -F arch=b64 -S adjtimex -S settimeofday -k time-change
-a always,exit -F arch=b64 -S sethostname -S setdomainname -k system-locale
-a always,exit -F arch=b64 -S mount -S umount2 -k mounts
-a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -F auid>=1000 -F auid!=4294967295 -k delete
# 載入規則
augenrules --load
yum install -y aide
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
# 設定 cron 每日檢查
echo "0 3 * * * root /usr/sbin/aide --check | mail -s 'AIDE Report' root" > /etc/cron.d/aide
yum install -y rkhunter chkrootkit
rkhunter --update && rkhunter --propupd && rkhunter -c --sk
chkrootkit
/var/log/secure (認證)、/var/log/audit/audit.log (審計)、/var/log/messages (系統)。logrotate 保留 90 天、壓縮、遠端同步。--security-opt=no-new-privileges、--cap-drop=ALL、--read-only、用戶命名空間映射。oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_stig --results results.xml /usr/share/xml/scap/ssg/content/ssg-centos7-ds.xmllynis audit system 快速安全掃描。| 層級 | 關鍵措施 | 驗證指令 |
|---|---|---|
| 實體/啟動 | BIOS/GRUB 密碼、Secure Boot、關閉外部啟動 | 重啟嘗試進入 BIOS/GRUB 編輯 |
| 作業系統 | 最小安裝、關閉無用服務、核心參數硬化 | systemctl list-unit-files、sysctl -a |
| 帳號/認證 | 強密碼策略、SSH 金鑰、鎖定策略、移除預設帳號 | chage -l user、ssh -v、faillock --user user |
| 檔案系統 | 關鍵檔 immutable、noexec/nodev/nosuid 掛載選項、AIDE | lsattr、mount、aide --check |
| 網路 | 防火牆預設拒絕、最小開放、Fail2ban、IDS/IPS | nmap、firewall-cmd --list-all、ss -tulpn |
| 審計/日誌 | auditd 規則、集中日誌、完整性監控 | aureport、logstash/ELK 儀表板 |
| 應用 | 最小權限、輸入驗證、WAF、依賴掃描、秘密管理 | 應用專屬掃描工具 |