0x00 前言
參考資料
Windows 從 vista 版本引入一種進(jìn)程保護(hù)機(jī)制(Process Protection),用于更進(jìn)一步的控制進(jìn)程的訪問級(jí)別,在此之前,用戶只需要使用 SeDebugPrivilege 令牌權(quán)限即可獲取任意進(jìn)程的所有訪問權(quán)限;隨后 Windows8.1 在此進(jìn)程保護(hù)的基礎(chǔ)上,擴(kuò)展引入了進(jìn)程保護(hù)光機(jī)制(protected Process Light),簡(jiǎn)稱 PPL 機(jī)制,其能提供更加細(xì)粒度化的進(jìn)程訪問權(quán)限控制。
本文將介紹 Windows 的 PPL 安全機(jī)制,以及在實(shí)驗(yàn)環(huán)境下如何繞過該機(jī)制,從而實(shí)現(xiàn)對(duì) PPL 的進(jìn)程進(jìn)行動(dòng)態(tài)調(diào)試。
本文實(shí)驗(yàn)環(huán)境:
代碼語言:JavaScript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制
Windows 10 專業(yè)版 22H2Visual Studio 2019
0x01 PPL機(jī)制
參考資料
使用 Process Explorer 工具查看進(jìn)程列表,我們可以看到 Windows 的部分核心進(jìn)程設(shè)置了 PPL 保護(hù):

對(duì)于安全研究來說,PPL機(jī)制最直觀的感受就是即便使用管理員權(quán)限也無法 attach 這個(gè)進(jìn)程進(jìn)行調(diào)試:

通過官網(wǎng)文檔(https://learn.microsoft.com/en-us/windows/win32/procthread/zwqueryinformationprocess)可以了解到 PS_PROTECTION 的結(jié)構(gòu)如下:
代碼語言:javascript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制
typedef struct _PS_PROTECTION { union { UCHAR Level; struct { UCHAR Type : 3; UCHAR Audit : 1; // Reserved UCHAR Signer : 4; }; };} PS_PROTECTION, *PPS_PROTECTION;
前 3 位包含進(jìn)程保護(hù)的類型:
代碼語言:javascript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制
typedef enum _PS_PROTECTED_TYPE { PsProtectedTypeNone = 0, PsProtectedTypeProtectedLight = 1, PsProtectedTypeProtected = 2} PS_PROTECTED_TYPE, *PPS_PROTECTED_TYPE;
后 4 位包含進(jìn)程保護(hù)的簽名者標(biāo)識(shí):
代碼語言:javascript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制
typedef enum _PS_PROTECTED_SIGNER { PsProtectedSignerNone = 0, PsProtectedSignerAuthenticode, PsProtectedSignerCodeGen, PsProtectedSignerAntimalware, PsProtectedSignerLsa, PsProtectedSignerWindows, PsProtectedSignerWinTcb, PsProtectedSignerWinSystem, PsProtectedSignerApp, PsProtectedSignerMax} PS_PROTECTED_SIGNER, *PPS_PROTECTED_SIGNER;
通過 WinDBG 進(jìn)行本地內(nèi)核調(diào)試,查看上圖進(jìn)程 smss.exe(412) 的內(nèi)核對(duì)象 EPROCESS 可以查看 PPL=0x61,如下:

PPL 機(jī)制在內(nèi)核函數(shù) NtOpenProcess 進(jìn)行實(shí)現(xiàn),當(dāng)我們?cè)L問進(jìn)程時(shí)最終都會(huì)調(diào)用該函數(shù);NtOpenProcess 位于 ntoskrnl.exe 內(nèi),結(jié)合符號(hào)表逆向如下:

經(jīng)過一系列的調(diào)用,最終進(jìn)入到 PPL 檢查的關(guān)鍵邏輯 RtlTestProtectedaccess,其調(diào)用棧如下:

RtlTestProtectedAccess 的判斷邏輯如下:

其中 Protection.Signer 經(jīng)過 RtlProtectedAccess 轉(zhuǎn)換的權(quán)限如下:
代碼語言:javascript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制
PsProtectedSignerNone 0 => 0x0PsProtectedSignerAuthenticode 1 => 0x2PsProtectedSignerCodeGen 2 => 0x4PsProtectedSignerAntimalware 3 => 0x108PsProtectedSignerLsa 4 => 0x110PsProtectedSignerWindows 5 => 0x13ePsProtectedSignerWinTcb 6 => 0x17ePsProtectedSignerWinSystem 7 => 0x1fePsProtectedSignerApp 8 => 0x0
0x02 雙機(jī)調(diào)試bypass
參考資料
使用雙機(jī)內(nèi)核調(diào)試可以無視大多數(shù)的安全機(jī)制,這里我使用網(wǎng)絡(luò)雙機(jī)調(diào)試,成功連接被調(diào)試主機(jī)后,再進(jìn)入到有 PPL 機(jī)制的 smss.exe(412) 的進(jìn)程空間下,直接就可以正常調(diào)試:

但是實(shí)際場(chǎng)景下雙機(jī)調(diào)試可能受環(huán)境限制,同時(shí)雙機(jī)調(diào)試也不如用戶模式下方便,下面我們看看通過本地調(diào)試的方法來繞過 PPL 機(jī)制。
0x03 本地調(diào)試bypass
參考資料
通過上文對(duì) PPL 機(jī)制的介紹,我們知道 PPL 的標(biāo)識(shí)位是以 _PS_PROTECTION 結(jié)構(gòu)存放于 EPROCESS 進(jìn)程對(duì)象中,雖然本地內(nèi)核調(diào)試無法控制程序執(zhí)行流,但可以修改內(nèi)存值;那么我們可以先通過本地內(nèi)核調(diào)試去除 PPL 標(biāo)識(shí),隨后便可以在用戶模式下調(diào)試目標(biāo)進(jìn)程。
配置好本地內(nèi)核調(diào)試環(huán)境后,使用管理員權(quán)限啟動(dòng) WinDBG,覆寫 smss.exe(412) 進(jìn)程的 Protection = 0x00 命令如下:
代碼語言:javascript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制
# 獲取 smss.exe 進(jìn)程的 EPROCESS 地址lkd > !process 0 0 smss.exe# 從 EPROCESS 獲取 Protection 的偏移和值lkd > dt nt!_eprocess ffffc40b2c45e080 Protectionlkd > db ffffc40b2c45e080+0x87a l1# 將 Protection 值修改為 0x00lkd > eb ffffc40b2c45e080+0x87a 0x00
執(zhí)行如下:

隨后我們?cè)僖怨芾韱T權(quán)限啟動(dòng) WinDBG,attach 到目標(biāo)進(jìn)程上,可以成功進(jìn)行調(diào)試:

0x04 工具化
參考資料
根據(jù)本地內(nèi)核調(diào)試去除 PPL 標(biāo)識(shí)的思路,我們可以編寫驅(qū)動(dòng)程序如下,使用 ZwQuerySystemInformation() 遍歷進(jìn)程,使用 PsLookupProcessByProcessId() 獲取進(jìn)程的 EPROCESS,隨后按 Protection 的偏移將其內(nèi)存值覆寫為 0x00:
代碼語言:javascript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制
#include <ntifs.h>#include <wdf.h>#define EPROCESS_PROTECTION_OFFSET 0x87A // windows10 professional 22H2DRIVER_INITIALIZE DriverEntry;typedef enum _SYSTEM_INFORMATION_CLASS { SystemProcessInformation = 5, // ...} SYSTEM_INFORMATION_CLASS;typedef struct _SYSTEM_PROCESS_INFORMATION { ULONG NextEntryOffset; ULONG NumberOfThreads; BYTE Reserved1[48]; PVOID Reserved2[3]; HANDLE UniqueProcessId; PVOID Reserved3; ULONG HandleCount; BYTE Reserved4[4]; PVOID Reserved5[11]; SIZE_T PeakPagefileUsage; SIZE_T PrivatePageCount; LARGE_INTEGER Reserved6[6];} SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION;NTSTATUS NTAPI ZwQuerySystemInformation( _In_ SYSTEM_INFORMATION_CLASS SystemInformationClass, _Inout_ PVOID SystemInformation, _In_ ULONG SystemInformationLength, _Out_opt_ PULONG ReturnLength);NTKERNELAPI UCHAR* PsGetProcessImageFileName(__in PEPROCESS Process);VOID OnUnload(_In_ PDRIVER_OBJECT DriverObject){ UNREFERENCED_PARAMETER(DriverObject); KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "remove_ppl: unload drivern"));}NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath) { ULONG BufferSize = 0; NTSTATUS Status = STATUS_SUCCESS; PVOID Buffer = NULL; PSYSTEM_PROCESS_INFORMATION pInfo = NULL; UNREFERENCED_PARAMETER(DriverObject); UNREFERENCED_PARAMETER(RegistryPath); KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "remove_ppl: driver entryn")); // register unload function DriverObject->DriverUnload = OnUnload; // get size of SYSTEM_PROCESS_INFORMATION Status = ZwQuerySystemInformation(SystemProcessInformation, NULL, 0, &BufferSize); if (Status != STATUS_INFO_LENGTH_MISMATCH) { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "remove_ppl: ZwQuerySystemInformation get size failed status=0x%xn", Status)); goto _LABEL_EXIT; } // alloc memory and get SYSTEM_PROCESS_INFORMATION Buffer = ExAllocatePoolWithTag(PagedPool, BufferSize, '1gaT'); if (Buffer == NULL) { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "remove_ppl: ExAllocatePoolWithTag failedn")); goto _LABEL_EXIT; } Status = ZwQuerySystemInformation(SystemProcessInformation, Buffer, BufferSize, &BufferSize); if (Status != STATUS_SUCCESS) { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "remove_ppl: ZwQuerySystemInformation get info failed status=0x%xn", Status)); goto _LABEL_EXIT; } // traverse all processes and rewrite "Protection" to 0x00 pInfo = (PSYSTEM_PROCESS_INFORMATION)Buffer; do { PEPROCESS Process = NULL; Status = PsLookupProcessByProcessId(pInfo->UniqueProcessId, &Process); if (NT_SUCCESS(Status)) { BYTE* Protection = (BYTE*)Process + EPROCESS_PROTECTION_OFFSET; if (*Protection != 0) { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "remove_ppl: rewrite %s[%d] Protection=0x%x to 0x00n", PsGetProcessImageFileName(Process), pInfo->UniqueProcessId, *Protection)); *Protection = 0x00; } } else { KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "remove_ppl: PsLookupProcessByProcessId [%d] failed status=0x%xn", pInfo->UniqueProcessId, Status)); } pInfo = (PSYSTEM_PROCESS_INFORMATION)((PUCHAR)pInfo + pInfo->NextEntryOffset); } while (pInfo->NextEntryOffset);_LABEL_EXIT: if (Buffer != NULL) { ExFreePoolWithTag(Buffer, '1gaT'); } return STATUS_SUCCESS;}
成功編譯后,將驅(qū)動(dòng)程序注冊(cè)為服務(wù)來啟動(dòng)運(yùn)行(需設(shè)置主機(jī)為測(cè)試模式):
代碼語言:javascript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制
# 注冊(cè)驅(qū)動(dòng)程序?yàn)榉?wù)sc.exe create remove_ppl type= kernel start= demand binPath= [src]remove_ppl.sys# 查看服務(wù)信息sc.exe queryex remove_ppl# 啟動(dòng)驅(qū)動(dòng)程序/服務(wù)sc.exe start remove_ppl
運(yùn)行驅(qū)動(dòng)程序,并使用 Process Explorer 查看,所有進(jìn)程的 PPL 標(biāo)識(shí)都被去除了:

除了以上實(shí)驗(yàn)代碼外,也可以參考更加完善的 PPL 控制工具:
https://github.com/Mattiwatti/PPLKillerhttps://github.com/itm4n/PPLcontrol
0x05 References
參考資料
https://learn.microsoft.com/en-us/windows/win32/services/protecting-anti-malware-services-https://learn.microsoft.com/en-us/windows/win32/procthread/zwqueryinformationprocesshttps://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rightshttps://download.microsoft.com/download/a/f/7/af7777e5-7dcd-4800-8a0a-b18336565f5b/process_vista.dochttps://www.crowdstrike.com/blog/evolution-protected-processes-part-1-pass-hash-mitigations-windows-81/https://www.crowdstrike.com/blog/evolution-protected-processes-part-2-exploitjailbreak-mitigations-unkillable-processes-and/https://www.cnblogs.com/H4ck3R-XiX/p/15872255.htmlhttps://www.cnblogs.com/revercc/p/16961961.htmlhttps://itm4n.github.io/debugging-protected-processes/https://paper.seebug.org/1892/https://github.com/Mattiwatti/PPLKillerhttps://github.com/itm4n/PPLcontrol