Refactored dynamically linking ntdll

This commit is contained in:
mkrsym1 2024-01-07 20:51:55 +02:00
parent 9f011ff103
commit 35cd117ffc
4 changed files with 31 additions and 19 deletions

View File

@ -27,10 +27,32 @@ typedef union _LDR_DLL_NOTIFICATION_DATA {
typedef void (*LdrDllNotification_t)(ULONG reason, const PLDR_DLL_NOTIFICATION_DATA data, void *context);
typedef NTSTATUS (*LdrRegisterDllNotification_t)(ULONG flags, LdrDllNotification_t notificationFunction, void *context, void **cookie);
typedef NTSTATUS (*LdrUnregisterDllNotification_t)(void *cookie);
#define DYNAMIC_FN_TYPE(ret, name, args) typedef ret (*name##_t)args
extern LdrRegisterDllNotification_t LdrRegisterDllNotification;
extern LdrUnregisterDllNotification_t LdrUnregisterDllNotification;
#ifdef NTDLL_DYNAMIC_LINK_IMPL
#define DYNAMIC_FN_VAR(name) extern name##_t name; name##_t name
#else
#define DYNAMIC_FN_VAR(name) extern name##_t name
#endif
void ntdll_link();
#define DYNAMIC_FN_DEF(ret, name, args) DYNAMIC_FN_TYPE(ret, name, args); DYNAMIC_FN_VAR(name)
DYNAMIC_FN_DEF(NTSTATUS, LdrRegisterDllNotification, (ULONG flags, LdrDllNotification_t notification, void *context, void **cookie));
DYNAMIC_FN_DEF(NTSTATUS, LdrUnregisterDllNotification, (void *cookie));
#ifdef NTDLL_DYNAMIC_LINK_IMPL
#define DYNAMIC_FN_LINK(module, name) name = (name##_t)GetProcAddress(module, #name)
static void _ntdll_link() {
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
DYNAMIC_FN_LINK(ntdll, LdrRegisterDllNotification);
DYNAMIC_FN_LINK(ntdll, LdrUnregisterDllNotification);
}
#undef DYNAMIC_FN_LINK
#endif
#undef DYNAMIC_FN_TYPE
#undef DYNAMIC_FN_VAR
#undef DYNAMIC_FN_DEF

View File

@ -5,7 +5,6 @@ include_dir = include_directories('include')
# Input files
sources = [
'src/main.c',
'src/ntdll.c',
'src/ace.c',
'src/pe.c',
'src/game.c',

View File

@ -1,6 +1,5 @@
#include <windows.h>
#include <ntdll.h>
#include <ace.h>
#include <game.h>
#include <core.h>
@ -8,6 +7,9 @@
#include <msg.h>
#include <tx.h>
#define NTDLL_DYNAMIC_LINK_IMPL
#include <ntdll.h>
#include <main.h>
HMODULE this_module;
@ -76,7 +78,7 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) {
this_module = instance;
// Dynamically link functions from ntdll
ntdll_link();
_ntdll_link();
// Detect which game the user is trying to run
struct game_data game;

View File

@ -1,11 +0,0 @@
#include <ntdll.h>
LdrRegisterDllNotification_t LdrRegisterDllNotification;
LdrUnregisterDllNotification_t LdrUnregisterDllNotification;
void ntdll_link() {
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
LdrRegisterDllNotification = (LdrRegisterDllNotification_t)GetProcAddress(ntdll, "LdrRegisterDllNotification");
LdrUnregisterDllNotification = (LdrUnregisterDllNotification_t)GetProcAddress(ntdll, "LdrUnregisterDllNotification");
}