LInk to ntdll.lib and call functions inside ntdll.dll

Kelvin Zhang

I'm recently doing some research on private APIs. I tried to call functions such as NtOpenFile in ntdll.dll with LoadLibrary and GetProcAddress at runtime. Luckly, it succeed. This morning I performed a file search on my computer and find ntdll.lib in my C drive. As far as I know of, such .lib file should contain stubs for dll exports available for linking. So, I tried to link my application to that lib but I'm constantly getting unresolved external symbol errors. However, a dumpbin /EXPORTS shows that ntdll.lib clearly has NtOpenFile exported. How could I resolve this error?

Frankie_C

The problem is the name of the function as recorded in the library and as it is generated from compiler.
dumpbin just shows you the base exported symbol NtOpenFile (the undecorated one), but there is also a import symbol __imp_NtOpenFile. Now if you try to link statically NtOpenFile declaring it as:

NTSTATUS NtOpenFile(
  _Out_ PHANDLE            FileHandle,
  _In_  ACCESS_MASK        DesiredAccess,
  _In_  POBJECT_ATTRIBUTES ObjectAttributes,
  _Out_ PIO_STATUS_BLOCK   IoStatusBlock,
  _In_  ULONG              ShareAccess,
  _In_  ULONG              OpenOptions
);

The compiler will generate, for a __stdcall function under 32bits, the symbol _NtOpenFile@24, if I'm not wrong counting the bytes size of call arguments, that obviously is not in the library. This is due to the fact that ntdll.lib is intended to be used under DDK for drivers development, where the compiler generates undecorated symbols.
To clarify the concept open the ntdll.lib file with a binary editor and look for NtOpenFile, you will see only it and the import version __imp_NtOpenFile. Now open a standar library as gdi32.lib, just to name one, and search for CreateDIBSection you'll find a _CreateDIBSection@24 and also __imp__CreateDIBSection@24.
So what's going on? Simple dumpbin shows always the undecorated names, but the compiler generates decorated ones, result: the linker fails. It is said that names use PASCAL convention, that is the same as __stdcall, but doesn't decorate symbols (i.e. read this https://msdn.microsoft.com/en-us/library/aa235591(v=vs.60).aspx).
There is a way to solve the problem? Yes you have to create your own import library assigning an alias to the wanted function having the correct decorations. Start reading this https://msdn.microsoft.com/en-us/library/0b9xe492.aspx.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ReactOS – Where are the ntdll.dll stubs?

From Dev

UWP debug/release error with ntdll.dll

From Dev

MFC application Crashes in NTDLL.dll

From Dev

windows - how to link against API sets (*ms-win*) instead kernel32.dll, ntdll.dll etc.?

From Dev

Are Win32 applications automatically linked against ntdll.dll?

From Dev

Symbols for "ntdll.dll" could not be downloaded from Microsoft Symbol Servers

From Dev

Is it good to use ntdll.dll in a win32 console application?

From Dev

Error from ntdll.dll because of a malloc (C++)

From Dev

realloc fails in visual studio, but work using gcc (ntdll.dll)

From Dev

C# How to import ntdll.dll to use NtDelayExecution and ZwSetTimerResolution?

From Dev

Is there a wsprintf()-type function from a low-level library such as kernel32.dll or ntdll.dll?

From Dev

Windows 10 explorer.exe heap corruption ntdll.dll when opened from taskbar

From Dev

Exception thrown at (ntdll.dll) in Parser.exe: Access violation reading location

From Dev

Visual Leak Detector throwing "Access violation reading location" on ntdll.dll

From Dev

explorer.exe ntdll.dll c0000005 failure when log in

From Dev

Windows 7 SP1 32-bit - Windows Explorer Crashing with faulting "ntdll.dll" module

From Dev

Does NtDll really export C runtime functions, and can I use these in my application?

From Dev

Use .lib to compile then use .dll to link or explicitly call dll in C++?

From Dev

Call functions dynamics of DLL's inside a Web Service C#

From Dev

Is there a difference in dt nt!_TEB and dt ntdll!_TEB?

From Dev

Bad symbols for NTDLL (error 3). Aborting

From Dev

cmake link against dll/lib

From Dev

cmake to link to dll without lib

From Dev

Compiling a dll with functions defined in other dll / lib

From Dev

How to link dll/lib written in c++ by others and call the API in C#?

From Dev

Unhandled exception at 0x00000000772CA267 (ntdll.dll) in *****.exe : 0xC0000005: Access violation writing location 0xFFFFFCA800000000

From Dev

Unhandled exception at 0x777122D2 (ntdll.dll) in ArticxEngine.exe: 0xC0000005: Access violation writing location 0x00000004

From Dev

Statically link google protobuf lib into a dll library

From Dev

Explorer.exe svchost ntdll threads hang, lags Windows 10

Related Related

  1. 1

    ReactOS – Where are the ntdll.dll stubs?

  2. 2

    UWP debug/release error with ntdll.dll

  3. 3

    MFC application Crashes in NTDLL.dll

  4. 4

    windows - how to link against API sets (*ms-win*) instead kernel32.dll, ntdll.dll etc.?

  5. 5

    Are Win32 applications automatically linked against ntdll.dll?

  6. 6

    Symbols for "ntdll.dll" could not be downloaded from Microsoft Symbol Servers

  7. 7

    Is it good to use ntdll.dll in a win32 console application?

  8. 8

    Error from ntdll.dll because of a malloc (C++)

  9. 9

    realloc fails in visual studio, but work using gcc (ntdll.dll)

  10. 10

    C# How to import ntdll.dll to use NtDelayExecution and ZwSetTimerResolution?

  11. 11

    Is there a wsprintf()-type function from a low-level library such as kernel32.dll or ntdll.dll?

  12. 12

    Windows 10 explorer.exe heap corruption ntdll.dll when opened from taskbar

  13. 13

    Exception thrown at (ntdll.dll) in Parser.exe: Access violation reading location

  14. 14

    Visual Leak Detector throwing "Access violation reading location" on ntdll.dll

  15. 15

    explorer.exe ntdll.dll c0000005 failure when log in

  16. 16

    Windows 7 SP1 32-bit - Windows Explorer Crashing with faulting "ntdll.dll" module

  17. 17

    Does NtDll really export C runtime functions, and can I use these in my application?

  18. 18

    Use .lib to compile then use .dll to link or explicitly call dll in C++?

  19. 19

    Call functions dynamics of DLL's inside a Web Service C#

  20. 20

    Is there a difference in dt nt!_TEB and dt ntdll!_TEB?

  21. 21

    Bad symbols for NTDLL (error 3). Aborting

  22. 22

    cmake link against dll/lib

  23. 23

    cmake to link to dll without lib

  24. 24

    Compiling a dll with functions defined in other dll / lib

  25. 25

    How to link dll/lib written in c++ by others and call the API in C#?

  26. 26

    Unhandled exception at 0x00000000772CA267 (ntdll.dll) in *****.exe : 0xC0000005: Access violation writing location 0xFFFFFCA800000000

  27. 27

    Unhandled exception at 0x777122D2 (ntdll.dll) in ArticxEngine.exe: 0xC0000005: Access violation writing location 0x00000004

  28. 28

    Statically link google protobuf lib into a dll library

  29. 29

    Explorer.exe svchost ntdll threads hang, lags Windows 10

HotTag

Archive