Skip to content

Commit

Permalink
Also patch CreateThread if OriginalFirstThunk is not 0
Browse files Browse the repository at this point in the history
  • Loading branch information
CookiePLMonster committed Feb 12, 2022
1 parent 4c14f99 commit 5a00ece
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions source/SilentPatchScarface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ namespace AffinityChanges
return hThread;
}

static void ReplaceFunction(void** funcPtr)
{
DWORD dwProtect;

VirtualProtect(funcPtr, sizeof(*funcPtr), PAGE_READWRITE, &dwProtect);
*funcPtr = &CreateThread_GameThread;
VirtualProtect(funcPtr, sizeof(*funcPtr), dwProtect, &dwProtect);
}

static bool RedirectImports()
{
const DWORD_PTR instance = reinterpret_cast<DWORD_PTR>(GetModuleHandle(nullptr));
Expand All @@ -122,21 +131,35 @@ namespace AffinityChanges
{
if ( _stricmp(reinterpret_cast<const char*>(instance + pImports->Name), "kernel32.dll") == 0 )
{
assert ( pImports->OriginalFirstThunk == 0 );

void** pFunctions = reinterpret_cast<void**>(instance + pImports->FirstThunk);
if ( pImports->OriginalFirstThunk != 0 )
{
const PIMAGE_THUNK_DATA pThunk = reinterpret_cast<PIMAGE_THUNK_DATA>(instance + pImports->OriginalFirstThunk);

for ( ptrdiff_t j = 0; pFunctions[j] != nullptr; j++ )
for ( ptrdiff_t j = 0; pThunk[j].u1.AddressOfData != 0; j++ )
{
if ( strcmp(reinterpret_cast<PIMAGE_IMPORT_BY_NAME>(instance + pThunk[j].u1.AddressOfData)->Name, "CreateThread") == 0 )
{
void** pAddress = reinterpret_cast<void**>(instance + pImports->FirstThunk) + j;
ReplaceFunction(pAddress);
return true;
}
}
}
else
{
if ( pFunctions[j] == &CreateThread )
void** pFunctions = reinterpret_cast<void**>(instance + pImports->FirstThunk);

for ( ptrdiff_t j = 0; pFunctions[j] != nullptr; j++ )
{
pFunctions[j] = &CreateThread_GameThread;
return true;
if ( pFunctions[j] == &CreateThread )
{
ReplaceFunction(&pFunctions[j]);
return true;
}
}
}
}
}

return false;
}
}
Expand Down

0 comments on commit 5a00ece

Please sign in to comment.