两个关于Windows 简短内核函数的还原

初始化DPC与APC
本文介绍了Windows内核中延迟过程调用(DPC)及异步过程调用(APC)的初始化过程。通过反编译代码详细展示了KeInitializeDpc与KeInitializeApc函数的具体实现细节,帮助读者理解DPC和APC结构体的内部组织。

(反编译代码均来自Windows的WinDbg的输出)
KeInitializedpc,如下:

nt!KeInitializeDpc:
840a2d25 8bff            mov     edi,edi
840a2d27 55              push    ebp
840a2d28 8bec            mov     ebp,esp
840a2d2a 8b4508          mov     eax,dword ptr [ebp+8]
840a2d2d 33c9            xor     ecx,ecx
840a2d2f 83601c00        and     dword ptr [eax+1Ch],0
840a2d33 c60013          mov     byte ptr [eax],13h
840a2d36 c6400101        mov     byte ptr [eax+1],1
840a2d3a 66894802        mov     word ptr [eax+2],cx
840a2d3e 8b4d0c          mov     ecx,dword ptr [ebp+0Ch]
840a2d41 89480c          mov     dword ptr [eax+0Ch],ecx
840a2d44 8b4d10          mov     ecx,dword ptr [ebp+10h]
840a2d47 894810          mov     dword ptr [eax+10h],ecx
840a2d4a 5d              pop     ebp
840a2d4b c20c00          ret     0Ch
lkd> dt nt!_kdpc   //dpc结构体
   +0x000 Type             : UChar
   +0x001 Importance       : UChar
   +0x002 Number           : Uint2B
   +0x004 DpcListEntry     : _LIST_ENTRY
   +0x00c DeferredRoutine  : Ptr32     void 
   +0x010 DeferredContext  : Ptr32 Void
   +0x014 SystemArgument1  : Ptr32 Void
   +0x018 SystemArgument2  : Ptr32 Void
   +0x01c DpcData          : Ptr32 Void
 VOID  KeInitializeDpc(  
 IN PRKDPC Dpc,
 IN PKDEFERRED_ROUTINE  DeferredRoutine,//指向与Dpc相关的 CustomDpc 例程
 IN PVOID  DeferredContext//将作为参数传给DeferredRoutine)
{ 
    Dpc->DpcData=0;
    Dpc->Type=0x13;   
    Dpc->Importance=1;
    Dpc->Number=0x0000;
    Dpc->DeferredRoutine=DeferredRoutine;
    Dpc->DeferredContext=DeferredContext;
 }

KDEFERRED_ROUTINE CustomDpc;
VOID
  CustomDpc(
    __in struct _KDPC  *Dpc,
    __in_opt PVOID  DeferredContext,
    __in_opt PVOID  SystemArgument1,
    __in_opt PVOID  SystemArgument2
    )
  {...}//to do sth here!

KeInitializeApc如下:

nt!KeInitializeApc:
84104df3 8bff            mov     edi,edi
84104df5 55              push    ebp
84104df6 8bec            mov     ebp,esp
84104df8 8b4508          mov     eax,dword ptr [ebp+8]
84104dfb 8b5510          mov     edx,dword ptr [ebp+10h]
84104dfe 8b4d0c          mov     ecx,dword ptr [ebp+0Ch]
84104e01 c60012          mov     byte ptr [eax],12h
84104e04 c6400230        mov     byte ptr [eax+2],30h
84104e08 83fa02          cmp     edx,2
84104e0b 7506            jne     nt!KeInitializeApc+0x20 (84104e13)

nt!KeInitializeApc+0x1a:
84104e0d 8a9134010000    mov     dl,byte ptr [ecx+134h]

nt!KeInitializeApc+0x20:
84104e13 894808          mov     dword ptr [eax+8],ecx
84104e16 8b4d14          mov     ecx,dword ptr [ebp+14h]
84104e19 894814          mov     dword ptr [eax+14h],ecx
84104e1c 8b4d18          mov     ecx,dword ptr [ebp+18h]
84104e1f 88502c          mov     byte ptr [eax+2Ch],dl
84104e22 894818          mov     dword ptr [eax+18h],ecx
84104e25 8b4d1c          mov     ecx,dword ptr [ebp+1Ch]
84104e28 33d2            xor     edx,edx
84104e2a 89481c          mov     dword ptr [eax+1Ch],ecx
84104e2d 3bca            cmp     ecx,edx
84104e2f 740e            je      nt!KeInitializeApc+0x4c (84104e3f)

nt!KeInitializeApc+0x3e:
84104e31 8a4d20          mov     cl,byte ptr [ebp+20h]
84104e34 88482d          mov     byte ptr [eax+2Dh],cl
84104e37 8b4d24          mov     ecx,dword ptr [ebp+24h]
84104e3a 894820          mov     dword ptr [eax+20h],ecx
84104e3d eb06            jmp     nt!KeInitializeApc+0x52 (84104e45)

nt!KeInitializeApc+0x4c:
84104e3f 88502d          mov     byte ptr [eax+2Dh],dl
84104e42 895020          mov     dword ptr [eax+20h],edx

nt!KeInitializeApc+0x52:
84104e45 88502e          mov     byte ptr [eax+2Eh],dl
84104e48 5d              pop     ebp
84104e49 c22000          ret     20h

 void KeInitializeApc(PRKAPC Apc,PRKTHREAD Thread,CHAR Index,
     PVOID KernelRoutine,PVOID RundownRoutine,PVOID NormalRoutine,
     CHAR Mode,PVOID Context)
 {
     Apc->Type=0x12;
     Apc->Size=sizeof(KAPC)/*0x30*/;
     if(2==Index)
     {
         Index=Thread->ApcStateIndex;
     }
     Apc->Thread=Thread;
     Apc->KernelRoutine=KernelRoutine;
     Apc->ApcStateIndex=Index;
     Apc->RoudownRoutine=RundownRoutine;
     Apc->NormalRoutine=NormalRoutine;
     if(!NormalRoutine)
     {
         Apc->ApcMode=Mode;
         Apc->NormalContext=Context;
         Apc->Inserted=0;
     }
     Apc->ApcMode=0;
     Apc->NormalContext=NULL;
     Apc->Inserted=0;
 }

还原的正确性不敢保证:)。我觉得这个工作很有意义:可以训练逻辑能力,简单的代码优化,还可以理解内核的实现过程。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值