1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| ALLOC_ON_CODE unsigned char CallbackStub[] = { 0x48, 0x89, 0xd3, 0x48, 0x8b, 0x03, 0x48, 0x8b, 0x4b, 0x08, 0x48, 0x8b, 0x53, 0x10, 0x4c, 0x8b, 0x43, 0x18, 0x4c, 0x8b, 0x4b, 0x20, 0xff, 0xe0
};
typedef ULONG LOGICAL;
typedef struct _LDRLOADDLL { UINT_PTR pLdrLoadDll; PWSTR PathToFile; ULONG Flags; PUNICODE_STRING ModuleFileName; PHANDLE ModuleHandle; } LOADLIBRARY_ARGS, * PLOADLIBRARY_ARGS;
typedef NTSTATUS(NTAPI* TPALLOCWAIT)(_Out_ PTP_WAIT* WaitReturn, _In_ PTP_WAIT_CALLBACK Callback, _Inout_opt_ PVOID Context, _In_opt_ PTP_CALLBACK_ENVIRON CallbackEnviron );
typedef NTSTATUS(NTAPI* TPSETWAIT) (_Inout_ PTP_WAIT Wait, _In_opt_ HANDLE Handle, _In_opt_ PLARGE_INTEGER Timeout );
typedef NTSTATUS(NTAPI* TPWAITFORWAIT) (_Inout_ PTP_WAIT Wait, _In_ LOGICAL CancelPendingCallbacks );
typedef VOID(NTAPI* pRtlInitUnicodeString)( PUNICODE_STRING DestinationString, PCWSTR SourceString );
wchar_t* ascii_to_unicode(const char* ascii_str) { if (ascii_str == NULL) { return NULL; } int ascii_str_len = (int)strlen(ascii_str);
int unicode_str_len = MultiByteToWideChar(CP_ACP, 0, ascii_str, ascii_str_len, NULL, 0); if (unicode_str_len == 0) return NULL;
wchar_t* unicode_str = (wchar_t*)malloc((unicode_str_len + 1) * sizeof(wchar_t)); if (!unicode_str) return NULL;
int result = MultiByteToWideChar(CP_ACP, 0, ascii_str, ascii_str_len, unicode_str, unicode_str_len); if (result == 0) return NULL;
unicode_str[unicode_str_len] = L'\0';
return unicode_str; }
HMODULE ProxyLoadLibraryA(LPCSTR libName) { HANDLE result = NULL; PTP_WAIT WaitReturn = NULL; HANDLE hEvent = NULL; UINT i = 0; UNICODE_STRING unicode_string = { 0 }; LOADLIBRARY_ARGS loadLibraryArgs = { 0 }; wchar_t* libNameW = ascii_to_unicode(libName); if (libNameW == NULL) return NULL;
pRtlInitUnicodeString RtlInitUnicodeString = (pRtlInitUnicodeString)GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlInitUnicodeString");
RtlInitUnicodeString(&unicode_string, libNameW);
loadLibraryArgs.Flags = 0; loadLibraryArgs.PathToFile = 0; loadLibraryArgs.pLdrLoadDll = (UINT_PTR)GetProcAddress(GetModuleHandleA("ntdll.dll"), "LdrLoadDll"); loadLibraryArgs.ModuleFileName = (PUNICODE_STRING) & unicode_string; loadLibraryArgs.ModuleHandle = &result;
FARPROC pTpAllocWait = GetProcAddress(GetModuleHandleA("ntdll.dll"), "TpAllocWait"); FARPROC pTpSetWait = GetProcAddress(GetModuleHandleA("ntdll.dll"), "TpSetWait"); FARPROC pTpWaitForWait = GetProcAddress(GetModuleHandleA("ntdll.dll"), "TpWaitForWait");
hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
if (NULL == hEvent) { return 0; }
((TPALLOCWAIT)pTpAllocWait)(&WaitReturn, (PTP_WAIT_CALLBACK)(unsigned char*)CallbackStub, &loadLibraryArgs, 0);
for (i = 0; i < 5; i++) { ((TPSETWAIT)pTpSetWait)(WaitReturn, hEvent, NULL); SetEvent(hEvent); WaitForSingleObject(hEvent, 10); ((TPWAITFORWAIT)pTpWaitForWait)(WaitReturn, FALSE); } return (HMODULE)result; }
|