Skip to content

Commit

Permalink
Try to fix EasyHook#295 once and for all:
Browse files Browse the repository at this point in the history
The sleep needs to be after ForceLdrInitializeThunk to have an effect.
Also instead of an arbitrary sleep, we can actually poll the result in a loop, at the risk of a deadlock instead of an injection error.
  • Loading branch information
MeFisto94 authored and MeFisto94 committed Nov 6, 2023
1 parent 9bcf1a6 commit e1d0d0c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions EasyHookDll/RemoteHook/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,11 +1237,13 @@ EASYHOOK_NT_EXPORT RhInjectLibrary(
THROW(STATUS_NO_MEMORY, L"Unable to allocate memory in current process.");

// Ensure that if we have injected into a suspended process that we can retrieve the remote function addresses
Sleep(1000);
FORCE(NtForceLdrInitializeThunk(hProc));

// The first GetRemoteModuleHandle call in GetRemoteFuncAddress can return NULL in some applications started in a suspended. Call GetRemoteFuncAddress once to prevent an access violation error.
GetRemoteFuncAddress(InTargetPID, hProc, "kernel32.dll", "LoadLibraryW");
// It may take a while for the Ldr to initialize the thunk, so we just continuously poll and wait.
while(GetRemoteFuncAddress(InTargetPID, hProc, "kernel32.dll", "LoadLibraryW") == NULL)
{
Sleep(100);
}

// Determine function addresses within remote process
Info->LoadLibraryW = (PVOID)GetRemoteFuncAddress(InTargetPID, hProc, "kernel32.dll", "LoadLibraryW");
Expand Down

0 comments on commit e1d0d0c

Please sign in to comment.