在 Linux 救援实例上离线修改 Windows 注册表(hivex)
2026/7/3大约 3 分钟
在 Linux 救援实例上离线修改 Windows 注册表(hivex)
EC2 Windows 实例无法启动或无法 RDP 时,可以用 Linux 救援实例挂载 Windows 根卷,通过 hivex 工具离线读写注册表 hive 文件。常见场景:启用/禁用存储驱动修复蓝屏、重置 RDP 端口、关闭防火墙、修改服务启动类型。
前提条件
- 一台 Amazon Linux 2023 救援实例,与目标 EBS 卷在同一可用区
- 目标 Windows 实例已停止
- 目标根卷已从原实例摘下并挂到救援实例
1. 挂载目标卷
# AL2023 内核自带 ntfs3 驱动
sudo modprobe ntfs3
# 确认设备名(通过 SERIAL 字段识别目标卷)
sudo lsblk -o NAME,SIZE,SERIAL,MOUNTPOINT,FSTYPE
# 挂载
sudo mkdir -p /mnt/win
sudo mount -t ntfs3 -o rw /dev/nvme1n1p1 /mnt/win注册表 hive 文件位于 /mnt/win/Windows/System32/config/:
| 文件名 | 对应注册表树 | 典型用途 |
|---|---|---|
| SYSTEM | HKLM\SYSTEM | 服务、驱动、启动配置 |
| SOFTWARE | HKLM\SOFTWARE | 已安装软件配置 |
| SAM | HKLM\SAM | 本地用户和组 |
| SECURITY | HKLM\SECURITY | 安全策略 |
修改前备份:
sudo cp /mnt/win/Windows/System32/config/SYSTEM /tmp/SYSTEM_BACKUP2. 编译安装 hivex
AL2023 仓库没有 hivex 包,从源码编译:
sudo dnf install -y gcc make perl
cd /tmp
curl -sL https://download.libguestfs.org/hivex/hivex-1.3.24.tar.gz | tar xz
cd hivex-1.3.24
./configure --disable-ocaml --disable-perl --disable-python --disable-ruby
make -j$(nproc)
sudo make install
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH| 工具 | 功能 |
|---|---|
| hivexsh | 交互式 shell,支持读写 |
| hivexget | 只读查询键值 |
| hivexml | 导出 hive 为 XML |
3. 读取注册表(hivexget)
# 查看 RDP 服务启动类型
hivexget /mnt/win/Windows/System32/config/SYSTEM \
'ControlSet001\Services\TermService' Start
# 2=Automatic, 3=Manual, 4=Disabled
# 查看 RDP 端口
hivexget /mnt/win/Windows/System32/config/SYSTEM \
'ControlSet001\Control\Terminal Server\WinStations\RDP-Tcp' PortNumber
# 列出某键下所有值
hivexget /mnt/win/Windows/System32/config/SYSTEM \
'ControlSet001\Services\storahci'4. 修改注册表(hivexsh)
hivexsh 用 -w 开启写模式,heredoc 批量执行:
/usr/local/bin/hivexsh -w <<'EOF'
load /mnt/win/Windows/System32/config/SYSTEM
cd ControlSet001\Services\storahci
setval 1
Start
dword:0x00000000
commit
EOFStart 值含义:
| 值 | 含义 |
|---|---|
| 0 | Boot(内核引导阶段最早加载) |
| 1 | System |
| 2 | Automatic |
| 3 | Manual |
| 4 | Disabled |
批量启用存储驱动(修复蓝屏 UNMOUNTABLE_BOOT_VOLUME)
EC2 Windows 导出到 VMware 后蓝屏,需启用 SATA/SCSI 驱动:
for svc in storahci intelide pciide LSI_SAS iaStorV stornvme; do
/usr/local/bin/hivexsh -w <<EOF
load /mnt/win/Windows/System32/config/SYSTEM
cd ControlSet001\Services\\$svc
setval 1
Start
dword:0x00000000
commit
EOF
done修改 RDP 端口
/usr/local/bin/hivexsh -w <<'EOF'
load /mnt/win/Windows/System32/config/SYSTEM
cd ControlSet001\Control\Terminal Server\WinStations\RDP-Tcp
setval 1
PortNumber
dword:0x00000D3D
commit
EOF0xD3D = 3389。
5. 验证并挂回
# 验证
hivexget /mnt/win/Windows/System32/config/SYSTEM \
'ControlSet001\Services\storahci' Start
# 预期输出:0
# 卸载
sudo umount /mnt/win
# 挂回原实例
aws ec2 detach-volume --volume-id <vol-id>
aws ec2 wait volume-available --volume-ids <vol-id>
aws ec2 attach-volume --volume-id <vol-id> --instance-id <原实例ID> --device /dev/sda1
aws ec2 start-instances --instance-ids <原实例ID>踩坑
忘记 commit:hivexsh 的写操作在内存中,不调用 commit 就退出等于没改。heredoc 最后一行必须是 commit。
改错 ControlSet:Windows 启动用哪个 ControlSet 由 SYSTEM\Select 的 Current 值决定。先查:
hivexget /mnt/win/Windows/System32/config/SYSTEM 'Select' Current
# 输出 1 → ControlSet001NTFS dirty flag:Windows 没正常关机时挂载会报 unclean shutdown。用 ntfsfix 清除:
sudo dnf install -y ntfsprogs
sudo ntfsfix /dev/nvme1n1p1heredoc 反斜杠转义:无变量时用 <<'EOF'(单引号)避免反斜杠被吃掉;有变量(如循环 $svc)用 <<EOF 并对路径反斜杠双写 \\。
