Windows - 系统调用以一种奇怪的方式被调用?

逆向工程 艾达 视窗 内核模式 核心 系统调用
2021-06-25 07:40:07

我一直在阅读有关在 Windows 中调用系统调用的方式。
我读过的所有文章的总体主题是:
64bit-在 ntdll 32bit 中调用-
从 ntdll 跳转到 KiFastSystemcall
但是当我打开带有 64 位和 32 位 ntdlls 的 IDA 以验证这些文章时,我看到的是:
(32 位)

NtCreateFile proc near
mov     eax, 55h        ; syscall num
mov     edx, offset j_Wow64Transition
call    edx ; weird stub is called instead of KiFastSystemcall.
            ; I couldn't find anything about it.perhaps a wrapper around KiFastSystemcall?

retn    2Ch
NtCreateFile endp

(64位)

NtCreateFile proc near
mov     r10, rcx        ; NtCreateFile
mov     eax, 55h
test    byte ptr ds:7FFE0308h, 1 ; some test to decide wether to use int 0x2E or syscall?
                                 ; I don't know why int 0x2E be used. I thought it causes overhead?
jnz     short loc_18009CB15
syscall                 
retn
loc_18009CB15:          
int     2Eh             
retn
NtCreateFile endp

如果有人知道为什么系统调用是这样调用的,我很想知道。
总结一下:
(32 位)为什么是 j_Wow64Transition 而不是 KiFastSystemcall?
(64 位)正在比较什么,为什么?
谢谢。

2个回答

以下引用,回答您的第一个问题来自Windows Internals Sixth Edition Part 1,第 225 页:

Wow64(64 位 Windows 上的 Win32 仿真)是指允许在 64 位 Windows 上执行 32 位 x86 应用程序的软件。它是作为一组用户模式 ​​DLL 实现的,在内核的一些支持下,可以创建通常只有 64 位数据结构的 32 位版本 [...]

Wow64 钩住 32 位代码转换到本地 64 位系统或本地系统需要调用 32 位用户模式代码的所有代码路径。

Wow64 转换到原生 64 位模式,捕获与系统调用相关的参数(将 32 位指针转换为 64 位指针),并发出相应的原生 64 位系统调用。当本机系统调用返回时,如果需要,Wow64 会将任何输出参数从 64 位格式转换为 32 位格式,然后再返回到 32 位模式。

因此,当您在位 Windows运行某个32x86程序时64,可能会发生此类转换以使该应用程序能够进行本机系统调用。

你的第二个问题的答案已经在这里

我会在第一个答案中添加信息。

模式从 Wow64 到 64bit 的切换,又名“天堂之门”,在wow64cpu.dll. offset j_Wow64Transition是 的一部分wow64cpu.dll

这些幻灯片以汇编代码为例,帮助您了解从 Wow64 进程执行 64 位系统调用的过程。