驅動開發:內核枚舉DpcTimer定時器

在筆者上一篇文章《驅動開發:內核枚舉IoTimer定時器》中我們通過IoInitializeTimer這個API函數為跳板,向下掃描特征碼獲取到了IopTimerQueueHead也就是IO定時器的隊列頭,本章學習的枚舉DPC定時器依然使用特征碼掃描,唯一不同的是在新版系統中DPC是被異或加密的,想要找到正確的地址,只是需要在找到DPC表頭時進行解密操作即可 。

驅動開發:內核枚舉DpcTimer定時器

文章插圖
DPC定時器的作用: 在內核中可以使用DPC定時器設置任意定時任務,當到達某個節點時自動觸發定時回調,定時器的內部使用KTIMER對象 , 當設置任務時會自動插入到DPC隊列,由操作系統循環讀取DPC隊列并執行任務,枚舉DPC定時器可得知系統中存在的DPC任務 。
要想在新版系統中得到DPC定時器則需要執行以下步驟
  • 1.找到KiProcessorBlock地址并解析成_KPRCB結構
  • 2.在_KPRCB結構中得到_KTIMER_TABLE偏移
  • 3.解析_KTIMER_TABLE_ENTRY得到加密后的雙向鏈表
首先_KPRCB這個結構體與CPU內核對應,獲取方式可通過一個未導出的變量nt!KiProcessorBlock來得到,如下雙核電腦,結構體存在兩個與之對應的結構地址 。
lyshark.com 0: kd> dq nt!KiProcessorBlockfffff807`70a32cc0fffff807`6f77c180 ffffbe81`3cee0180fffff807`70a32cd000000000`00000000 00000000`00000000fffff807`70a32ce000000000`00000000 00000000`00000000KiProcessorBlock是一個數組,其第一個結構體TimerTable則是結構體的偏移 。
lyshark.com 0: kd> dt _KPRCB fffff807`6f77c180ntdll!_KPRCB+0x000 MxCsr: 0x1f80+0x3680 TimerTable: _KTIMER_TABLE (此處)+0x5880 DpcGate: _KGATE接下來是把所有的KTIMER都枚舉出來 , KTIMER在TimerTable中的存儲方式是數組+雙向鏈表 。
lyshark.com 0: kd> dt _KTIMER_TABLEntdll!_KTIMER_TABLE+0x000 TimerExpiry: [64] Ptr64 _KTIMER+0x200 TimerEntries: [256] _KTIMER_TABLE_ENTRY (此處)到了_KTIMER_TABLE_ENTRY這里,Entry開始的雙向鏈表,每一個元素都對應一個Timer也就是說我們已經可以遍歷所有未解密的Time變量了 。
lyshark.com 0: kd> dt _KTIMER_TABLE_ENTRY 0xfffff807`6f77c180 + 0x3680ntdll!_KTIMER_TABLE_ENTRY+0x000 Lock: 0+0x008 Entry: _LIST_ENTRY [ 0x00000000`00000000 - 0x00000000`00000000 ]+0x018 Time: _ULARGE_INTEGER 0x0lyshark.com 0: kd> dt _KTIMER_TABLE_ENTRY 0xfffff807`6f77c180 + 0x3680 + 0x200ntdll!_KTIMER_TABLE_ENTRY+0x000 Lock: 0+0x008 Entry: _LIST_ENTRY [ 0xffffa707`a0d3e1a0 - 0xffffa707`a0d3e1a0 ]+0x018 Time: _ULARGE_INTEGER 0x00000001`a8030353至于如何解密,我們需要得到加密位置,如下通過KeSetTimer找到KeSetTimerEx從中得到DCP加密流程 。
lyshark.com 0: kd> u nt!KeSetTimernt!KeSetTimer:fffff803`0fc63a40 4883ec38subrsp,38hfffff803`0fc63a44 4c89442420movqword ptr [rsp+20h],r8fffff803`0fc63a49 4533c9xorr9d,r9dfffff803`0fc63a4c 4533c0xorr8d,r8dfffff803`0fc63a4f e80c000000callnt!KiSetTimerEx (fffff803`0fc63a60)fffff803`0fc63a54 4883c438addrsp,38hfffff803`0fc63a58 c3retfffff803`0fc63a59 ccint30: kd> u nt!KiSetTimerEx l50nt!KiSetTimerEx:fffff803`0fc63a60 48895c2408movqword ptr [rsp+8],rbxfffff803`0fc63a65 48896c2410movqword ptr [rsp+10h],rbpfffff803`0fc63a6a 4889742418movqword ptr [rsp+18h],rsifffff803`0fc63a6f 57pushrdifffff803`0fc63a70 4154pushr12fffff803`0fc63a72 4155pushr13fffff803`0fc63a74 4156pushr14fffff803`0fc63a76 4157pushr15fffff803`0fc63a78 4883ec50subrsp,50hfffff803`0fc63a7c 488b057d0c5100movrax,qword ptr [nt!KiWaitNever (fffff803`10174700)]fffff803`0fc63a83 488bf9movrdi,rcxfffff803`0fc63a86 488b35630e5100movrsi,qword ptr [nt!KiWaitAlways (fffff803`101748f0)]fffff803`0fc63a8d 410fb6e9movzxebp,r9bfffff803`0fc63a91 4c8bac24a0000000 movr13,qword ptr [rsp+0A0h]fffff803`0fc63a99 458bf8movr15d,r8dfffff803`0fc63a9c 4933f5xorrsi,r13fffff803`0fc63a9f 488bdamovrbx,rdxfffff803`0fc63aa2 480fcebswaprsifffff803`0fc63aa5 4833f1xorrsi,rcxfffff803`0fc63aa8 8bc8movecx,eaxfffff803`0fc63aaa 48d3cerorrsi,clfffff803`0fc63aad 4833f0xorrsi,raxfffff803`0fc63ab0 440f20c1movrcx,cr8fffff803`0fc63ab4 48898c24a0000000 movqword ptr [rsp+0A0h],rcxfffff803`0fc63abc b802000000moveax,2fffff803`0fc63ac1 440f22c0movcr8,raxfffff803`0fc63ac5 8b05dd0a5100moveax,dword ptr [nt!KiIrqlFlags (fffff803`101745a8)]fffff803`0fc63acb 85c0testeax,eaxfffff803`0fc63acd 0f85b72d1a00jnent!KiSetTimerEx+0x1a2e2a (fffff803`0fe0688a)fffff803`0fc63ad3 654c8b342520000000 movr14,qword ptr gs:[20h]fffff803`0fc63adc 33d2xoredx,edxfffff803`0fc63ade 488bcfmovrcx,rdifffff803`0fc63ae1 e86aa2fdffcallnt!KiCancelTimer (fffff803`0fc3dd50)fffff803`0fc63ae6 440fb6e0movzxr12d,alfffff803`0fc63aea 48897730movqword ptr [rdi+30h],rsifffff803`0fc63aee 33c0xoreax,eaxfffff803`0fc63af0 44897f3cmovdword ptr [rdi+3Ch],r15dfffff803`0fc63af4 8b0fmovecx,dword ptr [rdi]fffff803`0fc63af6 4889442430movqword ptr [rsp+30h],raxfffff803`0fc63afb 894c2430movdword ptr [rsp+30h],ecxfffff803`0fc63aff 488bcbmovrcx,rbxfffff803`0fc63b02 48c1e920shrrcx,20hfffff803`0fc63b06 4889442438movqword ptr [rsp+38h],raxfffff803`0fc63b0b 4889442440movqword ptr [rsp+40h],raxfffff803`0fc63b10 40886c2431movbyte ptr [rsp+31h],bplfffff803`0fc63b15 85c9testecx,ecxfffff803`0fc63b17 0f89c0000000jnsnt!KiSetTimerEx+0x17d (fffff803`0fc63bdd)fffff803`0fc63b1d 33c9xorecx,ecxfffff803`0fc63b1f 8bd1movedx,ecxfffff803`0fc63b21 40f6c5fctestbpl,0FChfffff803`0fc63b25 0f85a3000000jnent!KiSetTimerEx+0x16e (fffff803`0fc63bce)fffff803`0fc63b2b 48894c2420movqword ptr [rsp+20h],rcxfffff803`0fc63b30 48b80800000080f7ffff mov rax,0FFFFF78000000008hfffff803`0fc63b3a 4d8bc5movr8,r13fffff803`0fc63b3d 488b00movrax,qword ptr [rax]fffff803`0fc63b40 804c243340orbyte ptr [rsp+33h],40hfffff803`0fc63b45 482bc3subrax,rbxfffff803`0fc63b48 48894718movqword ptr [rdi+18h],raxfffff803`0fc63b4c 4803c2addrax,rdxfffff803`0fc63b4f 48c1e812shrrax,12hfffff803`0fc63b53 488bd7movrdx,rdifffff803`0fc63b56 440fb6c8movzxr9d,alfffff803`0fc63b5a 44884c2432movbyte ptr [rsp+32h],r9bfffff803`0fc63b5f 8b442430moveax,dword ptr [rsp+30h]fffff803`0fc63b63 8907movdword ptr [rdi],eaxfffff803`0fc63b65 894f04movdword ptr [rdi+4],ecxfffff803`0fc63b68 498bcemovrcx,r14fffff803`0fc63b6b e8209ffdffcallnt!KiInsertTimerTable (fffff803`0fc3da90)fffff803`0fc63b70 84c0testal,alfffff803`0fc63b72 0f8495000000jent!KiSetTimerEx+0x1ad (fffff803`0fc63c0d)fffff803`0fc63b78 f7058608510000000200 test dword ptr [nt!PerfGlobalGroupMask+0x8 (fffff803`10174408)],20000hfffff803`0fc63b82 0f852f2d1a00jnent!KiSetTimerEx+0x1a2e57 (fffff803`0fe068b7)fffff803`0fc63b88 f081277ffffffflock and dword ptr [rdi],0FFFFFF7Fhfffff803`0fc63b8f 488b8424a0000000 movrax,qword ptr [rsp+0A0h]fffff803`0fc63b97 4533c9xorr9d,r9dfffff803`0fc63b9a 33d2xoredx,edxfffff803`0fc63b9c 88442420movbyte ptr [rsp+20h],alfffff803`0fc63ba0 498bcemovrcx,r14fffff803`0fc63ba3 458d4101lear8d,[r9+1]fffff803`0fc63ba7 e8044efeffcallnt!KiExitDispatcher (fffff803`0fc489b0)

推薦閱讀