飞龙在天 发表于 2024-3-3 09:28:15

枚举DPC定时器源码(代码简洁)


鉴于目前驱动保护市场上的一两P有DPC定时器需要处理,放出一点简洁的枚举DPC定时器的源码,只有枚举,没有摘出。自己加点就行了,代码来源于网上搜集,但是搜集到的错误比较多,送出的是我修正编译可以通过的代码,没有盲目的复制代码。以下是代码,如果对你有那么一点点用那么很荣幸。很基础的东西大神勿笑。
#include <ntddk.h>
#include <windef.h>
ULONG KiTimerTableList=0;
//通过函数名获取函数地址
ULONG GetFunctionAddr( IN PCWSTR FunctionName)      
{
UNICODE_STRING UniCodeFunctionName;
RtlInitUnicodeString( &UniCodeFunctionName, FunctionName );
return (ULONG)MmGetSystemRoutineAddress( &UniCodeFunctionName );   
}
//定位dpc定时器链表首地址(仅Windows xp 下有效,其他系统要自己重写特征定位)
ULONG GetKiTimerList(ULONG addr)
{
int i=0;
PBYTE opcode =(PBYTE)addr;
for (;i<0x150;i++)
{
if (opcode == 0x8d &&
opcode == 0xc &&
opcode== 0xc5)
{
    return *(PULONG)&opcode;
}
}
return 0;
}
//枚举dpc定时器
VOID SearchForDPCTimer(ULONG addr)
{
PLIST_ENTRY pnode,pnext;
ULONG count = 0;
KIRQL irql;
ULONG routine;
int i=0;
irql = KeRaiseIrqlToDpcLevel();
for(;i<256;i++)
{
pnode = (PLIST_ENTRY)(addr+i*8);
pnext = pnode->Blink;
while(pnext!=pnode)
{
   PKTIMER ptimer = CONTAINING_RECORD(pnext,KTIMER,TimerListEntry);
   if (MmIsAddressValid(ptimer)
   && MmIsAddressValid(ptimer->Dpc)
   && MmIsAddressValid(ptimer->Dpc->DeferredRoutine))
   {
    routine = (ULONG)ptimer->Dpc->DeferredRoutine;
    DbgPrint("kitimer:0x%x addr:0x%x",(ULONG)ptimer,routine);
    count ++;
    //在这里判断如果是否在目标模块内
    //if (routine > imgBase && routine<imgBase+imgSize)
    //imgBase目标模块入口,imgSize目标模块大小,符合条件将其kill
   }
   pnext = pnext->Blink;
}
}
KeLowerIrql(irql);
DbgPrint("count:%d\n",count+1);
}


VOID OnUnload(IN PDRIVER_OBJECT DriverObject)
{

}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT theDriverObject,
      IN PUNICODE_STRING theRegistryPath)
{
theDriverObject->DriverUnload= OnUnload;
KiTimerTableList=GetKiTimerList((ULONG)GetFunctionAddr(L"KeUpdateSystemTime"));
if(KiTimerTableList!=0)
{
DbgPrint("KiTimerTableList:%x\n",KiTimerTableList);
SearchForDPCTimer(KiTimerTableList);
}
return STATUS_SUCCESS;
}
以上是完整的代码,可以直接编译测试的。但是仅仅对xp系统有用,其他系统别乱试,蓝屏概不负责,主要的是在通过特征定位DPC 链表首地址那里,其它操作系统跟xp的特征不一样

页: [1]
查看完整版本: 枚举DPC定时器源码(代码简洁)