分析ntdll.dll和ntoskrnl.exe中的 NT*和ZW*函数区别

OpenProcess-->ntdll!NtOpenProcess-->ntdll!zwOpenProcess-->ntdll!KiFastSystemCall()
                                  ↓mov edx, esp
                                  ↓sysenter 用户层
------------------------------------------------------------------------------------------------------
                                  ↓ 内核层
                                  nt!KiFastCallEntry()
                                  ↙                  ↘
                            nt!zwopenprocess()           nt!ntopenprocess()
                                                                                           ↙                                 ↘
                                                                 nt!KiSystemService()                                nt!PsOpenProcess()

1. ntdll!NtOpenProcess() 会定向到 ntdll!zwOpenProcess() 里,它们是完全一样的,只是名字不同而已!
2. nt!ntopenprocess是实现函数具体过程, nt!zwopenprocess是在ring 0下通过SSDT服务表,KiSystemService调用ntoskrnl.exe中的中断处理程序Nt*函数。
3. 在用户层的 stub 函数会使用 sysenter 指令切入到内核层的 KiFastCallEntry() 函数,再由 KiFastCallEntry() 函数分发到相应的系统服务例程执行。


ntdll.dll和ntoskrnl.exe中的NT*和ZW*函数调用流程分析
NtOpenProcess和ZwOpenProcess为例,结合Windbg的调试来说明
1.ntdll.dll中的Nt*和Zw*区别?

0:000> u ntdll!zwopenprocess l4
ntdll!NtOpenProcess:
774d5e70 b8be000000 mov eax,0BEh
774d5e75 ba0003fe7f mov edx,offset SharedUserData!SystemCallStub (7ffe0300)
774d5e7a ff12 call dword ptr [edx]
774d5e7c c21000 ret 10h
0:000> u ntdll!ntopenprocess l4
ntdll!NtOpenProcess:
774d5e70 b8be000000 mov eax,0BEh
774d5e75 ba0003fe7f mov edx,offset SharedUserData!SystemCallStub (7ffe0300)
774d5e7a ff12 call dword ptr [edx]
774d5e7c c21000 ret 10h

 


A:从汇编代码来看,两者别无区别,都是通过系统服务调度程序KiSystemService调用ntoskrnl.exe中的中断处理程序Nt*函数。

lkd> u nt!zwopenprocess l6
nt!ZwOpenProcess:
8403ce58 b8be000000 mov eax,0BEh
8403ce5d 8d542404 lea edx,[esp+4]
8403ce61 9c pushfd
8403ce62 6a08 push 8
8403ce64 e8b5190000 call nt!KiSystemService (8403e81e)
8403ce69 c21000 ret 10h

lkd> u nt!ntopenprocess l20
nt!NtOpenProcess:
84217205 8bff mov edi,edi
84217207 55 push ebp
84217208 8bec mov ebp,esp
8421720a 51 push ecx
8421720b 51 push ecx
8421720c 64a124010000 mov eax,dword ptr fs:[00000124h]
84217212 8a803a010000 mov al,byte ptr [eax+13Ah]
84217218 8b4d14 mov ecx,dword ptr [ebp+14h]
8421721b 8b5510 mov edx,dword ptr [ebp+10h]
8421721e 8845fc mov byte ptr [ebp-4],al
84217221 ff75fc push dword ptr [ebp-4]
84217224 ff75fc push dword ptr [ebp-4]
84217227 ff750c push dword ptr [ebp+0Ch]
8421722a ff7508 push dword ptr [ebp+8]
8421722d e889510600 call nt!PsOpenProcess (8427c3bb)
84217232 c9 leave
84217233 c21000 ret 10h

 

从汇编代码可知 NT*是实现函数具体过程, 而ZW*函数是在ring 0下通过SSDT服务表,KiSystemService调用ntoskrnl.exe中的中断处理程序Nt*函数。

posted @ 2016-04-19 16:11  鑫鑫1  阅读(1367)  评论(0编辑  收藏  举报