首页
社区
课程
招聘
[求助]KiInsertQueueApc
发表于: 2009-11-14 20:36 8859

[求助]KiInsertQueueApc

2009-11-14 20:36
8859
不知道把此问题发在这儿对不对,希望版主不要介意。
最近看inlineHook,看到一些文章在hook  KiInsertQueueApc 这个函数
但我不知道此函数的作用是啥,我在DDK里没有看到它的文档,在网上也搜索了
都是如何hook它,我想可能和我一样在学习中的人也会遇到此问题,所以
想请大家帮忙给解释一下KiInsertQueueApc 的作用,为什么hook了它
就能保护进程。

万分感谢!

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (8)
雪    币: 254
活跃值: (30)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
这跟结束进程内部实现有关。看一下NtTerminateProcess的实现可知,本质上就是PsGetNextProcessThread、PspTerminateThreadByPointer的循环,也就是说结束一个进程实际上是终止掉该进程的所有线程。最后再清理一下该进程的句柄表等资源。而在PspTerminateThreadByPointer的实现核心,就是初始化一个APC,然后插入到目标线程的APC队列中:
push    [ebp+arg_4]
push    ebx
push    offset _PspExitNormalApc@12 ; PspExitNormalApc(x,x,x)
push    offset _ExFreeCallBack@4 ; ExFreeCallBack(x)
push    offset _PsExitSpecialApc@20 ; PsExitSpecialApc(x,x,x,x,x)
push    ebx
push    [ebp+arg_0]
push    edi
call    _KeInitializeApc@32 ; KeInitializeApc(x,x,x,x,x,x,x,x)
push    2
push    ebx
push    edi
push    edi
call    _KeInsertQueueApc@16 ; KeInsertQueueApc(x,x,x,x)
继续跟进KeInsertQueueApc,其实现十分简单:
.text:00423918 8B FF                                                        mov     edi, edi
.text:0042391A 55                                                           push    ebp
.text:0042391B 8B EC                                                        mov     ebp, esp
.text:0042391D 83 EC 0C                                                     sub     esp, 0Ch
.text:00423920 53                                                           push    ebx
.text:00423921 56                                                           push    esi
.text:00423922 57                                                           push    edi
.text:00423923 8B 7D 08                                                     mov     edi, [ebp+arg_0]
.text:00423926 8B 77 08                                                     mov     esi, [edi+8]
.text:00423929 8D 8E E8 00 00 00                                            lea     ecx, [esi+0E8h]
.text:0042392F 8D 55 F4                                                     lea     edx, [ebp+var_C]
.text:00423932 FF 15 90 06 40 00                                            call    ds:__imp_@KeAcquireInStackQueuedSpinLockRaiseToSynch@8 ; __declspec(dllimport) KeAcquireInStackQueuedSpinLockRaiseToSynch(x,x)
.text:00423938 32 DB                                                        xor     bl, bl
.text:0042393A 38 9E 66 01 00 00                                            cmp     [esi+166h], bl
.text:00423940 74 18                                                        jz      short loc_42395A
.text:00423942 8B 45 0C                                                     mov     eax, [ebp+arg_4]
.text:00423945 8B 55 14                                                     mov     edx, [ebp+arg_C]
.text:00423948 89 47 24                                                     mov     [edi+24h], eax
.text:0042394B 8B 45 10                                                     mov     eax, [ebp+arg_8]
.text:0042394E 8B CF                                                        mov     ecx, edi
.text:00423950 89 47 28                                                     mov     [edi+28h], eax
.text:00423953 E8 B8 29 00 00                                               call    @KiInsertQueueApc@8 ; KiInsertQueueApc(x,x)
.text:00423958 8A D8                                                        mov     bl, al
.text:0042395A
.text:0042395A                                              loc_42395A:                             ; CODE XREF: KeInsertQueueApc(x,x,x,x)+28j
.text:0042395A 8D 4D F4                                                     lea     ecx, [ebp+var_C]
.text:0042395D FF 15 08 07 40 00                                            call    ds:__imp_@KeReleaseInStackQueuedSpinLock@4 ; __declspec(dllimport) KeReleaseInStackQueuedSpinLock(x)
.text:00423963 5F                                                           pop     edi
.text:00423964 5E                                                           pop     esi
.text:00423965 8A C3                                                        mov     al, bl
.text:00423967 5B                                                           pop     ebx
.text:00423968 C9                                                           leave
.text:00423969 C2 10 00                                                     retn    10h
其核心就是一个KiInsertQueueApc,所以Hook KiInsertQueueApc能实现保护进程。
关于APC以及InsertQueueApc的原理,建议你看看毛先生的漫谈兼容内核或Windows内核情景分析
2009-11-14 21:29
0
雪    币: 38
活跃值: (11)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
3
十分感谢margen的细心解答。谢谢!

看雪真让感到是个好地方啊。
2009-11-14 22:30
0
雪    币: 38
活跃值: (11)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
4
十分感谢margen的细心解答。看雪真让感到是个好地方啊。
2009-11-14 22:30
0
雪    币: 38
活跃值: (11)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
5
有个很奇怪的事,我用WINDBG汇编出来的KeInsertQueueApc代码前一段是如下:
(为什么和别人反汇编的结果有点不一样,我的少了 mov edi,edi 这条指令)

kd> u nt!KeInsertQueueApc
nt!KeInsertQueueApc:
804edf6e 55              push    ebp
804edf6f 8bec            mov     ebp,esp
804edf71 83ec0c          sub     esp,0Ch
804edf74 53              push    ebx
804edf75 56              push    esi
804edf76 57              push    edi
804edf77 8b7d08          mov     edi,dword ptr [ebp+8]
804edf7a 8b7708          mov     esi,dword ptr [edi+8]
2009-11-14 22:44
0
雪    币: 225
活跃值: (10)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
6
我在黑防写过这个文章。。。。。。
2009-11-14 23:06
0
雪    币: 38
活跃值: (11)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
7
这个问题知道了,原来我的VM里安装的WINDOWS不是SP2的,所以出现这种情况。
2009-11-16 12:41
0
雪    币: 695
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
2楼回答很详细 学习了
2009-11-16 13:23
0
雪    币: 132
活跃值: (30)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
9
劫杀过路高手
2010-10-29 19:59
0
游客
登录 | 注册 方可回帖
返回
//