Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

GRUB Legacy 密碼保護實戰:MD5 加密配置、多啟動項鎖定與安全策略

編輯:Linux 教學

威脅模型與防護目標

  • 防止物理接觸者通過 GRUB 命令行修改內核參數(單用戶模式、init=/bin/bash)繞過認證
  • 防止未授權選擇其他啟動項(Windows、舊內核、救援模式)
  • 保護啟動配置完整性,配合 BIOS 密碼構建纵深防禦

MD5 密碼生成

[root@linux ~]# /sbin/grub-md5-crypt
Password: ********      # 輸入明文密碼,如 Grub@2024
Retype password: ******** # 確認
$1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0  # 輸出的 MD5 密文,複製備用

注意: GRUB Legacy 僅支持 MD5 (grub-md5-crypt);GRUB2 使用 PBKDF2 (grub-mkpasswd-pbkdf2),兩者不兼容。

/boot/grub/grub.conf 配置模式

模式一:僅保護 GRUB 交互界面(菜單可選,參數不可改)

default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
password --md5 $1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0

title CentOS 7 (3.10.0-1160.el7.x86_64)
    root (hd0,0)
    kernel /vmlinuz-3.10.0-1160.el7.x86_64 ro root=/dev/mapper/centos-root
    initrd /initramfs-3.10.0-1160.el7.x86_64.img

title Windows 10
    rootnoverify (hd0,1)
    chainloader +1

效果:直接啟動默認項;按 p 輸入密碼後可進入命令行、編輯參數、選擇其他菜單項。

模式二:保護 GRUB 界面 + 鎖定特定啟動項(推薦生產)

default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
password --md5 $1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0

title CentOS 7 (3.10.0-1160.el7.x86_64)
    lock
    root (hd0,0)
    kernel /vmlinuz-3.10.0-1160.el7.x86_64 ro root=/dev/mapper/centos-root
    initrd /initramfs-3.10.0-1160.el7.x86_64.img

title Windows 10
    rootnoverify (hd0,1)
    chainloader +1

效果:默認項啟動需密碼;Windows 項無密碼可直接啟動(雙系統場景)。

模式三:差異化密碼保護(多管理員分級)

# 生成第二個密碼
# grub-md5-crypt -> $1$salt2$hash2

default=0
timeout=5
# 全局密碼(超級管理員)
password --md5 $1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0

title CentOS 7 Production
    lock
    password --md5 $1$salt2$hash2   # 運行維護密碼
    root (hd0,0)
    kernel /vmlinuz-prod ro root=...
    initrd /initramfs-prod.img

title CentOS 7 Rescue
    lock
    # 僅超級管理員密碼可進
    root (hd0,0)
    kernel /vmlinuz-rescue ro root=... single
    initrd /initramfs-rescue.img

效果:生產內核可用維護密碼或超級密碼解鎖;救援模式僅超級密碼。

關鍵指令語法詳解

指令位置作用
password --md5 <hash>全局/菜單項設置密碼,全局放在 timeout 後,菜單項放在 lock 後
lock菜單項首行鎖定該項,需密碼解鎖(必須配合 password 使用)
timeout=N全局N 秒後自動啟動 default 項;N=0 立即啟動;N=-1 無限等待

配置生效驗證

  1. 重啟系統,觀察 GRUB 菜單
  2. 嘗試按 e 編輯默認項 → 應提示 "Enter password:"
  3. 輸入明文密碼 Grub@2024 → 進入編輯模式
  4. 嘗試選擇被 lock 的項 → 應提示密碼

進階安全加固

1. 文件權限鎖定

chown root:root /boot/grub/grub.conf
chmod 600 /boot/grub/grub.conf
# 防止非 root 讀取 MD5 密文(雖然不可逆,但防彩虹表)

2. 結合 BIOS/UEFI 密碼

  • 設置 BIOS 管理員密碼,禁用外部設備啟動順序修改
  • UEFI Secure Boot 啟用,簽名內核與 GRUB,防止惡意啟動載入器替換

3. 物理安全補充

  • 機箱鎖防止硬盤被拆卸離線掛載
  • 端口鎖禁用 USB/光驅啟動
  • GRUB 密碼僅防軟件層面繞過,物理訪問需物理防護

GRUB2 (現代發行版) 差異說明

# 1. 生成 PBKDF2 密碼
grub-mkpasswd-pbkdf2
# 輸入密碼 -> grub.pbkdf2.sha512.10000.XXXX

# 2. 編輯 /etc/grub.d/40_custom
set superusers="root"
password_pbkdf2 root grub.pbkdf2.sha512.10000.XXXX

# 3. 針對菜單項加鎖 (在 menuentry 內)
menuentry "CentOS 7" --users root --unrestricted {
    # --unrestricted 表示無需密碼啟動
    # 去掉 --unrestricted 則需密碼
}

# 4. 重新生成配置
grub2-mkconfig -o /boot/grub2/grub.cfg

應急恢復:忘記 GRUB 密碼

  1. 使用 LiveCD 啟動,掛載根分區
  2. 編輯 /mnt/sysimage/boot/grub/grub.conf,刪除或註釋 password 行
  3. 或進入 GRUB 命令行(若未鎖定):cat /boot/grub/grub.conf 查看配置
  4. 極端情況:重新安裝 GRUB grub-install /dev/sda
Copyright © Windows教程網 All Rights Reserved