首页 文章

Windows和Native API中的系统调用?

提问于
浏览
23

最近我在* NIX操作系统中使用了很多汇编语言 . 我想知道Windows域名 .


Calling convention in linux:

mov $SYS_Call_NUM, %eax
mov $param1 , %ebx
mov $param2 , %ecx
int $0x80

而已 . 这就是我们应该如何在linux中进行系统调用 .

Reference of all system calls in linux:

关于哪个$ SYS_Call_NUM&我们可以使用哪个参数引用:http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html

官方参考:http://kernel.org/doc/man-pages/online/dir_section_2.html


Calling convention in Windows:

???

Reference of all system calls in Windows:

???

非官方:http://www.metasploit.com/users/opcode/syscalls.html,但除非我知道调用约定,否则如何在汇编中使用它们 .

官方:???

  • 如果你说,他们没有记录它 . 那么如何在不知道系统调用的情况下为windows编写libc?如何进行Windows程序集编程?至少在驱动程序编程中需要了解这些 . 对?

现在,与所谓的Native API有什么关系? Native APISystem calls for windows 是否都是指同一件事的不同术语?为了证实我将这些来自两个非官方来源进行了比较

系统调用:http://www.metasploit.com/users/opcode/syscalls.html

原生API:http://undocumented.ntinternals.net/aindex.html

我的观察:

  • 所有系统调用都以字母 Nt 开头,其中Native API由许多不以字母 Nt 开头的函数组成 .

  • System Call of windowsNative API 的子集 . 系统调用只是Native API的一部分 .

任何人都可以证实并解释 .

EDIT:

还有另一个答案 . 这是第二个答案 . 我真的很喜欢它,但我不知道为什么回答者已删除它 . 我请他转发他的答案 .

5 回答

  • -2

    如果您在Windows下进行汇编编程,则不要进行手动系统调用 . 您可以使用NTDLL和Native API为您执行此操作 .

    Native API只是内核模式方面的包装器 . 它所做的就是为正确的API执行系统调用 .

    您永远不需要手动系统调用,因此您的整个问题都是多余的 .

    Linux系统调用代码不会改变,Windows会这样做,这就是为什么你需要通过一个额外的抽象层(又名NTDLL) .

    编辑:

    此外,即使您在程序集级别工作,您仍然可以完全访问Win32 API,因此没有理由使用NT API开始!进口,出口等都可以在装配程序中正常工作 .

    EDIT2:

    如果您真的想要进行手动系统调用,则需要为每个相关的Windows版本反转NTDLL,添加版本检测(通过PEB),并为每个调用执行系统调用查找 .

    然而,这将是愚蠢的 . NTDLL是有原因的 .

  • 8

    关于windows syscall约定你需要知道的另一件事是,据我所知,syscall表是作为构建过程的一部分生成的 . 这意味着它们可以简单地改变 - 没有人跟踪它们 . 如果有人在列表顶部添加新的,则无关紧要 . NTDLL仍然有效,因此调用NTDLL的其他人仍然可以工作 .

    甚至用于执行系统调用(int或sysenter)的机制也不是固定不变的,并且在过去已经发生了变化,我认为曾经有一段时间相同版本的Windows使用不同的DLL,这些DLL使用不同的输入机制,具体取决于CPU在机器中 .

  • 1

    Windows系统调用是通过调用系统DLL来执行的,例如 kernel32.dllgdi32.dll ,这是通过普通的子程序调用完成的 . 陷入OS特权层的机制没有记录,但这没关系,因为像_2843959这样的DLL会为你做这件事 .

    通过系统调用,我指的是文档化的Windows API入口点,如 CreateProcess()GetWindowText() . 设备驱动程序通常使用Windows DDK中的其他API .

  • 1

    我有兴趣在没有导入的程序集中进行Windows API调用(作为教育练习),所以我编写了以下FASM程序集来执行NtDll!NtCreateFile所做的事情 . 这是我的64位版本的Windows(Win10 1803版本10.0.17134)的粗略演示,它在调用后崩溃,但系统调用的返回值为零,因此它成功 . 根据Windows x64调用约定设置所有内容,然后将系统调用号加载到RAX中,然后运行调用的是系统调用程序集指令 . 我的示例创建了文件c:\ HelloWorldFile_FASM,因此必须以“管理员”身份运行 .

    format PE64 GUI 4.0
    
    
    entry start
    
    
    section '.text' code readable executable
    
    
     start: 
     ;puting the first four parameters into the right registers
    
                                mov rcx, _Handle
                                mov rdx, [_access_mask]
                                mov r8, objectAttributes
                                mov r9, ioStatusBlock
    
     ;I think we need 1 stack word of padding:
    
                                push 0x0DF0AD8B
    
    
     ;pushing the other params in reverse order:
    
                                push [_eaLength]
                                push [_eaBuffer]
                                push [_createOptions]
                                push [_createDisposition]
                                push [_shareAcceses]
                                push [_fileAttributes]
                                push [_pLargeInterger]
    
     ;adding the shadow space (4x8)
    
     ;                               push 0x0
     ;                               push 0x0
     ;                               push 0x0
     ;                               push 0x0
    
     ;pushing the 4 register params into the shadow space for ease of debugging
    
                                push r9
                                push r8
                                push rdx
                                push rcx
    
     ;now pushing the return address to the stack:
    
                                push endOfProgram
    
                                mov r10, rcx ;copied from ntdll!NtCreateFile, not sure of the reason for this
                                mov eax, 0x55
                                syscall
    
     endOfProgram:
                                retn
    
    
    
    
     section '.data' data readable writeable
    
     ;parameters------------------------------------------------------------------------------------------------
    
     _Handle                         dq      0x0
     _access_mask                    dq      0x00000000c0100080
     _pObjectAttributes              dq      objectAttributes        ; at 00402058
     _pIoStatusBlock                 dq           ioStatusBlock
     _pLargeInterger                 dq      0x0
     _fileAttributes                 dq      0x0000000000000080
     _shareAcceses                   dq      0x0000000000000002
     _createDisposition              dq      0x0000000000000005
     _createOptions                  dq      0x0000000000000060
     _eaBuffer                       dq      0x0000000000000000       ; "optional" param
     _eaLength                       dq      0x0000000000000000
    
     ;----------------------------------------------------------------------------------------------------------
    
    
                                align   16
     objectAttributes:
     _oalength                       dq      0x30
     _rootDirectory                  dq      0x0
     _objectName                     dq           unicodeString
     _attributes                     dq      0x40
     _pSecurityDescriptor            dq      0x0
     _pSecurityQualityOfService      dq      securityQualityOfService
    
    
     unicodeString:
     _unicodeStringLength            dw      0x34
     _unicodeStringMaxumiumLength    dw      0x34, 0x0, 0x0
     _pUnicodeStringBuffer           dq      _unicodeStringBuffer
    
    
     _unicodeStringBuffer            du      '\??\c:\HelloWorldFile_FASM'       ; may need to "run as adinistrator" for the file create to work.
    
    
    
     ioStatusBlock:
     _status_pointer                 dq      0x0
     _information                    dq      0x0
    
    
     securityQualityOfService:
     _sqlength                       dd      0xC
     _impersonationLevel             dd      0x2
     _contextTrackingMode            db      0x1
     _effectiveOnly                  db      0x1, 0x0, 0x0
    

    我使用了Ntdll!NtCreateFile的文档,我还使用内核调试器查看并复制了很多参数 .

    __kernel_entry NTSTATUS NtCreateFile(
      OUT PHANDLE                      FileHandle,
      IN ACCESS_MASK                   DesiredAccess,
      IN POBJECT_ATTRIBUTES            ObjectAttributes,
      OUT PIO_STATUS_BLOCK             IoStatusBlock,
      IN PLARGE_INTEGER AllocationSize OPTIONAL,
      IN ULONG                         FileAttributes,
      IN ULONG                         ShareAccess,
      IN ULONG                         CreateDisposition,
      IN ULONG                         CreateOptions,
      IN PVOID EaBuffer                OPTIONAL,
      IN ULONG                         EaLength
    );
    
  • 24

    Windows中的官方调用约定:http://msdn.microsoft.com/en-us/library/7kcdt6fy.aspx

    (希望这个链接能够存活下来在将来;如果没有,只需在MSDN上搜索“x64软件约定”) .

    函数调用约定在Linux和Windows x86_64中有所不同 . 在两个ABI中,参数最好通过寄存器传递,但使用的寄存器不同 . 有关Linux ABI的更多信息,请访问http://www.x86-64.org/documentation/abi.pdf

相关问题