본문 바로가기

Windows/Kernel

NT vs. Zw

커널 드라이버를 만들다 보면 동일 함수인데 앞에 붙은 접두어가 Nt인 것이 있고 Zw인 것이 있습니다. 둘의 차이점은 뭘까요?

 

결론:

근본적인 루틴은 같지만 호출시 전달된 함수의 인자들을 검사하는 방식이 다르다. Nt 는 인자들이 적합한지 깐깐하게 살펴보고 Zw 는 믿고 넘어간다. 

 

그러나 User mode에서 호출한 경우라면 Nt = Zw 는 서로 동일하다. 인자들을 다 본다는 말이겠죠. 이게 가능한 이유는 PreviousMode() 라는 API로 해당 스레드의 콜 스택을 보고 이전 동작 환경이 User mode 인지 Kernel mode인지 확인하기 때문이다. 

 

The Windows native operating system services API is implemented as a set of routines that run in kernel mode. These routines have names that begin with the prefix Nt or Zw. Kernel-mode drivers can call these routines directly. User-mode applications can access these routines by using system calls.

 

With a few exceptions, each native system services routine has two slightly different versions that have similar names but different prefixes. For example, calls to NtCreateFile and ZwCreateFile perform similar operations and are, in fact, serviced by the same kernel-mode system routine. For system calls from user mode, the Nt and Zw versions of a routine behave identically. For calls from a kernel-mode driver, the Nt and Zw versions of a routine differ in how they handle the parameter values that the caller passes to the routine.

 

 

A kernel-mode driver calls the Zw version of a native system services routine to inform the routine that the parameters come from a trusted, kernel-mode source. In this case, the routine assumes that it can safely use the parameters without first validating them. However, if the parameters might be from either a user-mode source or a kernel-mode source, the driver instead calls the Nt version of the routine, which determines, based on the history of the calling thread, whether the parameters originated in user mode or kernel mode. For more information about how the routine distinguishes user-mode parameters from kernel-mode parameters, see PreviousMode.

 

출처:

 

 

Using Nt and Zw Versions of the Native System Services Routines - Windows drivers

Using Nt and Zw Versions of the Native System Services Routines

docs.microsoft.com

그외 API 접두사들에 대한 이야기:

Nt/Zw Nt 또는 Zw는 ntdll.dll과 ntoskrnl.exe에서 선언된 시스템 호출이다. 사용자 모드의 ntdll.dll에서 호출되면, 이 그룹들은 다음과 같은 면에서 거의 같다. 커널 모드에 들어가서 SSDT를 통해 ntoskrnl.exe의 같은 의미를 가진 함수를 호출한다. ntoskrnl.exe에서 직접적으로 호출될 경우(커널 모드에서만 가능)에는 Zw는 Nt와 달리 커널 모드인 것을 보장한다.[1] Zw 접두사는 아무것도 의미하지 않는다.[2]
Rtl Rtlntdll 함수 중에서 두번째로 많은 그룹이다. 이것은 (확장된) C 런타임 라이브러리를 구성하고 있다. (확장된) C 런타임 라이브러리는 (커널 지원을 직접적으로 포함하지 않지만) 네이티브 응용 프로그램들에 쓰이는 많은 유틸리티 함수를 포함한다.
Csr Csr은 Win32 하위 시스템 프로세스(csrss.exe)와 통신할 때 사용되는 클라이언트-서버 함수들이다.
Dbg Dbg는 소프트웨어 브레이크포인트같은디버깅함수들이다.
Ki Ki APC디스패칭 같은 이벤트들을 위한 커널 모드에서의 upcall들이다.
Ldr Ldr은 새로운 프로세스들을 시작시키는 것을 다루는 PE파일을 위한 로더 함수들이다.
Nls Nls 네이티브 언어 지원을 위해 사용된다.
Pfx Pfx는 prefix를 다루기 위해 사용된다.

 

출처:

 

네이티브 API - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. 네이티브 API(Native API)는 윈도우 NT와 사용자 모드의 응용 프로그램에서 사용되는 API이다. 다른 윈도우 구성 요소들이 사용되기 힘들 때 사용되며, 주로 시스템 �

ko.wikipedia.org