Skip to content

Commit

Permalink
Move process hook to dllmain
Browse files Browse the repository at this point in the history
  • Loading branch information
nomi-san committed Jul 24, 2023
1 parent 2517299 commit 7d946ec
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
35 changes: 0 additions & 35 deletions core/src/browser/browser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,38 +244,6 @@ static int Hooked_CefInitialize(const struct _cef_main_args_t* args,
return CefInitialize(args, settings, app, windows_sandbox_info);
}

static hook::Hook<decltype(CreateProcessW)> Old_CreateProcessW;
static BOOL WINAPI Hooked_CreateProcessW(
_In_opt_ LPCWSTR lpApplicationName,
_Inout_opt_ LPWSTR lpCommandLine,
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ BOOL bInheritHandles,
_In_ DWORD dwCreationFlags,
_In_opt_ LPVOID lpEnvironment,
_In_opt_ LPCWSTR lpCurrentDirectory,
_In_ LPSTARTUPINFOW lpStartupInfo,
_Out_ LPPROCESS_INFORMATION lpProcessInformation)
{
bool is_renderer = std::regex_search(lpCommandLine,
std::wregex(L"LeagueClientUxRender\\.exe.+--type=renderer", std::wregex::icase));

if (is_renderer)
dwCreationFlags |= CREATE_SUSPENDED;

BOOL success = Old_CreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes,
bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);

if (success && is_renderer)
{
void InjectThisDll(HANDLE hProcess);
InjectThisDll(lpProcessInformation->hProcess);
ResumeThread(lpProcessInformation->hThread);
}

return success;
}

void HookBrowserProcess()
{
// Open console window.
Expand All @@ -290,7 +258,4 @@ void HookBrowserProcess()

// Hook CefBrowserHost::CreateBrowser().
CefBrowserHost_CreateBrowser.hook("libcef.dll", "cef_browser_host_create_browser", Hooked_CefBrowserHost_CreateBrowser);

// Hook CreateProcessW().
Old_CreateProcessW.hook(&CreateProcessW, Hooked_CreateProcessW);
}
42 changes: 38 additions & 4 deletions core/src/dllmain.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
#include <string>
#include <regex>
#include <windows.h>

#include "commons.h"
#include "include/cef_version.h"

EXTERN_C IMAGE_DOS_HEADER __ImageBase;

bool LoadLibcefDll(bool is_browser);
void HookBrowserProcess();
void HookRendererProcess();
void InjectThisDll(HANDLE hProcess);

static hook::Hook<decltype(CreateProcessW)> Old_CreateProcessW;
static BOOL WINAPI Hooked_CreateProcessW(
_In_opt_ LPCWSTR lpApplicationName,
_Inout_opt_ LPWSTR lpCommandLine,
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ BOOL bInheritHandles,
_In_ DWORD dwCreationFlags,
_In_opt_ LPVOID lpEnvironment,
_In_opt_ LPCWSTR lpCurrentDirectory,
_In_ LPSTARTUPINFOW lpStartupInfo,
_Out_ LPPROCESS_INFORMATION lpProcessInformation)
{
bool is_renderer = std::regex_search(lpCommandLine,
std::wregex(L"LeagueClientUxRender\\.exe.+--type=renderer", std::wregex::icase));

if (is_renderer)
dwCreationFlags |= CREATE_SUSPENDED;

BOOL success = Old_CreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes,
bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);

if (success && is_renderer)
{
InjectThisDll(lpProcessInformation->hProcess);
ResumeThread(lpProcessInformation->hThread);
}

return success;
}

static void Initialize()
{
Expand All @@ -22,7 +51,12 @@ static void Initialize()
std::wregex(L"LeagueClientUx\\.exe$", std::wregex::icase)))
{
if (LoadLibcefDll(true))
{
HookBrowserProcess();

// Hook CreateProcessW.
Old_CreateProcessW.hook(&CreateProcessW, Hooked_CreateProcessW);
}
}
// Render process.
else if (std::regex_search(exe_path,
Expand Down

0 comments on commit 7d946ec

Please sign in to comment.