欢迎来到 嗅灵易学

零基础也能上手的脚本技术课,一对一答疑带你入门

[原创]x64枚举DPC定时器

[原创]x64枚举DPC定时器

@写在前面
     不同于x86,x64的DPC是被加密了的。对于x64DPC的兴趣始于我已经流产的scalpel计划。当时问某牛怎么遍历,得到的答案是“500大洋给代码”。真是R了狗了,好歹小哥我也是搞技术的,自己搞吧。
PS:这个代码编辑功能。。。醉的不行了。
@前言
     在早期的x86系统上面,DPCTimer都是裸奔的,直接用过已导出的KiTimerTableListHead就可以遍历,网上的枚举资料也是一大把。但是等DPC被加密之后,突然就没有资料了(资料约等于拿来直接F7的代码)。So,搞起。
        简单整理下思路,枚举DPC可以分为两道工序:
1.        找到x64的”KiTimerTableListHead”
2.        把加密了的DPC进行解密
PS:其实PG_Disable里面有代码的,奈何从来没人说过啊。。。
@找到x64的” KiTimerTableListHead”
       翻翻以前的资料,在早期的windows中导出了一个名为“KiTimerTableListHead”的神奇东西,直接就可以遍历DPC定时器。但是到了win7 x64上面,符号表抹去了这个可怜的小家伙。因此我们需要找一条新的道路来找到所有的DPC定时器。
      参考论坛里的一个帖子:http://bbs.pediy.com/showthread.php?t=148135
      枚举DPC的新路线就是:_KPRCB->_KTIMER->_KDPC
      先简单说一下_KPRCB,这个结构体是与CPU的核心对应的,一个核心对应一个。获取方法以及判断数量的方法,都可以通过一个未导出的变量nt!KiProcessorBlock来获取:

kd> dq nt!KiProcessorBlock
fffff800`040fd900  [COLOR="red"]fffff800`0403ee80[/COLOR] 00000000`00000000
fffff800`040fd910  00000000`00000000 00000000`00000000
fffff800`040fd920  00000000`00000000 00000000`00000000
fffff800`040fd930  00000000`00000000 00000000`00000000
fffff800`040fd940  00000000`00000000 00000000`00000000
fffff800`040fd950  00000000`00000000 00000000`00000000
fffff800`040fd960  00000000`00000000 00000000`00000000
fffff800`040fd970  00000000`00000000 00000000`00000000

      KiProcessorBlock就是一个数组,数组里有几个元素就是CPU有几个核心and每个元素对应一个_KPRCB的结构体。
kd> dt _KPRCB fffff800`0403ee80
ntdll!_KPRCB
   +0x000 MxCsr            : 0x1f80
   +0x004 LegacyNumber     : 0 ''
   +0x005 ReservedMustBeZero : 0 ''
   +0x006 InterruptRequest : 0 ''
   +0x007 IdleHalt         : 0x1 ''
   +0x008 CurrentThread    : 0xfffff800`0404ccc0 _KTHREAD
   +0x010 NextThread       : (null) 
 ………..
   [COLOR="Red"]+0x2200 TimerTable       : _KTIMER_TABLE[/COLOR]
   +0x4400 DpcGate          : _KGATE
   +0x4418 PrcbPad52        : (null) 
 …………

      这个结构体实在有点大,其他的我们都不关心,只需要看offset在0x2200处的TimerTable就行了。
      接下来是把所有的KTIMER都枚举出来。KTIMER在TimerTable中的存储方式是:数组+双向链表。
kd> dt _KTIMER_TABLE fffff800`0403ee80+0x2200
ntdll!_KTIMER_TABLE
   +0x000 TimerExpiry      : [64] (null) 
   [COLOR="red"]+0x200 TimerEntries     : [256] _KTIMER_TABLE_ENTRY[/COLOR]

kd> dt _KTIMER_TABLE_ENTRY fffff800`0403ee80+0x2200+0x200
ntdll!_KTIMER_TABLE_ENTRY
   +0x000 Lock             : 0
   [COLOR="red"]+0x008 Entry            : _LIST_ENTRY [ 0xfffff800`04041288 - 0xfffff800`04041288 ][/COLOR]
   +0x018 Time             : _ULARGE_INTEGER 0xffffffff`9cac3e4d
       
        到了_KTIMER_TABLE_ENTRY这里,Entry开始的双向链表,每一个元素都对应一个Timer,也就是说我们已经可以愉快的遍历所有未解密的Timer了。
@愉快的解密DPC
      想找解密代码,最好的办法就是去找到他是怎么加密的。So,我们跟踪一下KeSetTimer的流程就好了。
kd> u nt!KeSetTimer
nt!KeSetTimer:
fffff800`03ed80a8 4883ec38        sub     rsp,38h
fffff800`03ed80ac 4c89442420      mov     qword ptr [rsp+20h],r8
fffff800`03ed80b1 4533c9          xor     r9d,r9d
fffff800`03ed80b4 4533c0          xor     r8d,r8d
[COLOR="red"]fffff800`03ed80b7 e814000000      call    nt!KiSetTimerEx (fffff800`03ed80d0)[/COLOR]
fffff800`03ed80bc 4883c438        add     rsp,38h
fffff800`03ed80c0 c3              ret
fffff800`03ed80c1 90              nop
kd> u nt!KiSetTimerEx l20
nt!KiSetTimerEx:
fffff800`03ed80d0 48895c2408      mov     qword ptr [rsp+8],rbx
fffff800`03ed80d5 4889542410      mov     qword ptr [rsp+10h],rdx
fffff800`03ed80da 55              push    rbp
fffff800`03ed80db 56              push    rsi
fffff800`03ed80dc 57              push    rdi
fffff800`03ed80dd 4154            push    r12
fffff800`03ed80df 4155            push    r13
fffff800`03ed80e1 4156            push    r14
fffff800`03ed80e3 4157            push    r15
fffff800`03ed80e5 4883ec50        sub     rsp,50h
[COLOR="red"]fffff800`03ed80e9 488b0518502200  mov     rax,qword ptr [nt!KiWaitNever (fffff800`040fd108)]
fffff800`03ed80f0 488b1de9502200  mov     rbx,qword ptr [nt!KiWaitAlways (fffff800`040fd1e0)]
fffff800`03ed80f7 4c8bb424b0000000 mov     r14,qword ptr [rsp+0B0h]
fffff800`03ed80ff 4933de          xor     rbx,r14
fffff800`03ed8102 488bf1          mov     rsi,rcx[/COLOR]
fffff800`03ed8105 450fb6f9        movzx   r15d,r9b
[COLOR="red"]fffff800`03ed8109 480fcb          bswap   rbx[/COLOR]
fffff800`03ed810c 418bf8          mov     edi,r8d
[COLOR="red"]fffff800`03ed810f 4833d9          xor     rbx,rcx
fffff800`03ed8112 8bc8            mov     ecx,eax
fffff800`03ed8114 48d3cb          ror     rbx,cl
fffff800`03ed8117 4833d8          xor     rbx,rax[/COLOR]
fffff800`03ed811a 450f20c4        mov     r12,cr8
fffff800`03ed811e b802000000      mov     eax,2
fffff800`03ed8123 440f22c0        mov     cr8,rax
fffff800`03ed8127 65488b2c2520000000 mov   rbp,qword ptr gs:[20h]
fffff800`03ed8130 33d2            xor     edx,edx
fffff800`03ed8132 488bce          mov     rcx,rsi
fffff800`03ed8135 e88ae6ffff      call    nt!KiCancelTimer (fffff800`03ed67c4)
[COLOR="red"]fffff800`03ed813a 48895e30        mov     qword ptr [rsi+30h],rbx[/COLOR]
fffff800`03ed813e 488b9c2498000000 mov     rbx,qword ptr [rsp+98h]
fffff800`03ed8146 440fb6e8        movzx   r13d,al

      流程就是:KeSetTimer/KeSetTimerEx->KiSetTimerEx->加密DPC。
      OK,这坨蛋疼的x64汇编我就不多说了。看不懂的拖进ida然后f5。如果还是看不懂,就看我下面的代码。(其实就是把待加密的DPC用KTimer、KiWaitNever和KiWaitAlways进行异或操作,中间再进行一些ror,bswap的位移操作。)
@写一下代码自己撸个痛快
代码里没有过多的注释,上面的看懂了,这个代码看一眼就行。
里面的三个未导出符号的获取,可以通过PDB或者硬编码
#include<ntddk.h>
typedefstruct_KTIMER_TABLE_ENTRY
{
	ULONG_PTR	Lock;
	LIST_ENTRY	Entry;
	ULONG_PTR	Time;
}KTIMER_TABLE_ENTRY, *PKTIMER_TABLE_ENTRY;
#defineKTIMER_TABLE_ENTRY_MAX	(256)
typedefstruct_KTIMER_TABLE
{
	ULONG_PTR			TimerExpiry[64];
	KTIMER_TABLE_ENTRY	TimerEntries[KTIMER_TABLE_ENTRY_MAX];
}KTIMER_TABLE, *PKTIMER_TABLE;
ULONG_PTR		ptrKiProcessorBlock		= 0;
ULONG_PTR		ptrOffsetKTimerTable	= 0;
ULONG_PTR		ptrKiWaitNever			= 0;
ULONG_PTR		ptrKiWaitAlways		= 0;
int				nTotalCount			= 0;
#definep2dq(x)	(*((ULONG_PTR*)x))
void DPC_Print(PKTIMERptrTimer)
{
	ULONG_PTR	ptrDpc	= (ULONG_PTR)ptrTimer->Dpc;
	KDPC*		DecDpc	= NULL;
	int			nShift	= (p2dq(ptrKiWaitNever) & 0xFF);
	//_RSI->Dpc = (_KDPC *)v19;
	//_RSI = Timer;
	ptrDpc ^= p2dq(ptrKiWaitNever);//v19 = KiWaitNever ^ v18;
	ptrDpc = _rotl64(ptrDpc, nShift);//v18 = __ROR8__((unsigned __int64)Timer ^ _RBX, KiWaitNever);
	ptrDpc ^= (ULONG_PTR)ptrTimer;
	ptrDpc = _byteswap_uint64(ptrDpc);//__asm { bswap   rbx }
	ptrDpc ^= p2dq(ptrKiWaitAlways);//_RBX = (unsigned __int64)DPC ^ KiWaitAlways;
	//real DPC
	if (MmIsAddressValid((PVOID)ptrDpc))
	{
		nTotalCount++;
		DecDpc = (KDPC*)ptrDpc;
		DbgPrint("[dpc]dpc:%p,routine:%p\n", DecDpc, DecDpc->DeferredRoutine);
	}
}
void DPC_Enum()
{
	PKTIMER_TABLE		ptrKTimerTable		= 0;
	//PKTIMER_TABLE_ENTRY	ptrTimerTabEntry	= NULL;
	PKTIMER				ptrTimer			= NULL;
	PLIST_ENTRY			ptrListEntry		= NULL;
	PLIST_ENTRY			ptrListEntryHead	= NULL;
	int					i				= 0;
	DbgBreakPoint();
	ptrKTimerTable = (PKTIMER_TABLE)(p2dq(ptrKiProcessorBlock) + ptrOffsetKTimerTable);
	KdPrint(("ptrKTimerTable:%p\n", ptrKTimerTable));
	for (i = 0; i<KTIMER_TABLE_ENTRY_MAX; i++)
	{
		DbgPrint("\n[dpc]ptrTimerTabEntry:%d\n", i);
		ptrListEntryHead = &(ptrKTimerTable->TimerEntries[i].Entry);
		for (ptrListEntry = ptrListEntryHead->Flink; ptrListEntry != ptrListEntryHead; ptrListEntry = ptrListEntry->Flink)
		{
			ptrTimer = CONTAINING_RECORD(ptrListEntry, KTIMER, TimerListEntry);
			if (!MmIsAddressValid(ptrTimer))	
				continue;
			if (!ptrTimer->Dpc)
				continue;
			DPC_Print(ptrTimer);
		}
	}
	DbgPrint("TotalDPC:%d\n", nTotalCount);
}
void DriverUnload(INPDRIVER_OBJECTDriverObject)
{
}
NTSTATUS DriverEntry(INPDRIVER_OBJECTDriverObject, INPUNICODE_STRINGRegistryPath)
{
	DriverObject->DriverUnload = DriverUnload;
	ptrKiProcessorBlock		= 0xfffff800040fd900;
	ptrOffsetKTimerTable	= 0x2200;
	ptrKiWaitNever			= 0xfffff800040fd108;
	ptrKiWaitAlways		= 0xfffff800040fd1e0;
	DPC_Enum();
	returnSTATUS_SUCCESS;
}

@最后
      其实很简单,奈何没人说啊,写个帖子也当是给自己做个笔记了吧。
      最后鸣谢:论坛若干DPC科普贴 and PG_Disable.
Ps:其实还是愉快的做伸手党舒服啊。。。

注意:上传附件及图片大小不得大于30M。

⚠️ 版权声明:
本博客所有内容(含教程、源码、工具)仅供个人技术学习与研究交流使用,严禁商用、倒卖、二次分发及非法用途
未经作者书面授权,任何组织或个人不得转载、复制或用于其他平台,违者将追究相关责任。

0 0 0 举报
复制成功