Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Using IDA Free, Part 3, Investigate Windows syscall

views
     
TSFlierMate
post Jun 12 2023, 11:19 PM, updated 3y ago

On my way
****
Validating
543 posts

Joined: Nov 2020
Today I will investigate syscall function in Windows. As we know, Windows library are provided through its API, but actually can skip the Windows API and use syscall directly, just like in Linux x64.
But the syscall numbers on Windows, from release to release, are not stable.

For syscall table in Windows, see: https://github.com/j00ru/windows-syscalls

So I will try my luck for my version of Windows, by debugging two EXEs (PE 64-bit) using IDA Freeware 8.3.

Below is the normal code for a program to exit with errorlevel 7:
CODE
format PE64 console
entry start

include "win64a.inc"

section ".code" code executable readable

start:

   mov  rcx, 7
   call [ExitProcess]

section ".idata" import readable

   library kernel, "kernel32.dll"

   import kernel, ExitProcess, "ExitProcess"


As you see from above, there is an extra section called "idata", which is import table mandatory for every Win32 programs to be useful.

But today I will just hack it so that the newly created program will be even smaller, and without the import table at all.

After loading the exit.exe (source code listed above), I step into "call [ExitProcess]", and this is what I entered into, RtlExitUserProcess:

RtlExitUserProcess
user posted image
user posted image

If I further step into NtTerminateProcess, this is what we see:
Now it uses "syscall" with ID "0x2C" (the syscall number varies from one release of Windows to another)

NtTerminateProcess
user posted image

After some investigation, I came up with this program, also exit with errorlevel 7:

CODE
format PE64 console
entry start

section ".code" code executable readable

start:

   mov rdx, 7
   or  rcx, 0xFFFFFFFFFFFFFFFF
  ;xor rcx, rcx
   mov r10, rcx
   mov rax, 0x2C
   syscall  


And after I load it in IDA Freeware, this is the output.

Disassembly of NtExit.exe (source code above):
user posted image

The file size:
CODE
Directory of C:\Users\BOO\Projects

06/12/2023  10:48 PM             1,536 exit.EXE
06/12/2023  10:59 PM             1,024 ntexit.EXE
              2 File(s)          2,560 bytes
              0 Dir(s)  91,136,335,872 bytes free


The NtExit.exe is without extra 512 bytes section of import table.

In summary, it is not practical to use syscall number directly in a Windows program, but I heard some malware are using it. @junyian @KLKS

Thank you for reading!

flashang
post Jun 13 2023, 11:05 AM

Casual
***
Junior Member
355 posts

Joined: Aug 2021


When some system develop,
common functions might changed / update / upgrade from time to time.
"Internal" functions are designed to used by system and system-app.

For better error handling, logs, redirect to appropriate function, ...,
API was released for "public" or "3rd party software" to called.

smile.gif


MatQuasar
post Sep 15 2023, 08:04 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
Since there is no syscall in 32-bit mode....

user posted image

....I make direct call to ntdll.dll instead, x86 code as below:

CODE
format PE console
entry start

include "win32a.inc"

section ".code" code executable readable

start:

  push 7
  push 0xFFFFFFFF
  call [NtTerminateProcess]

section ".idata" import readable

  library ntdll, "ntdll.dll"

  import ntdll, NtTerminateProcess, "NtTerminateProcess"      

MatQuasar
post Sep 15 2023, 11:34 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
Readers might ask, what is "ntdll.dll"? It is a library part of "kernel32.dll" dependency:

user posted image

Many Win32 API are provided through "kernel32.dll" (along with "user32.dll" and others), but underlying it there is "ntdll.dll" which is even deeper.

So , kernel32.dll ExitProcess API will call ntdll.dll NtTerminateProcess.....

 

Change to:
| Lo-Fi Version
0.0130sec    0.77    5 queries    GZIP Disabled
Time is now: 24th November 2025 - 08:38 PM