Skip to content

Commit 29566df

Browse files
committed
cleaner error handling when DLL injection fails
1 parent f70c03d commit 29566df

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

DebugLoop.cpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ static bool g_running;
3434
// ----------------------------------------------------------------------------
3535
static void HandleNewProcess(DWORD pid, LPCWSTR filePath)
3636
{
37+
bool ok = false;
38+
HANDLE hFile = INVALID_HANDLE_VALUE;
3739
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
3840

3941
snprintf(g_path, sizeof(g_path), PIPE_NAME, pid);
@@ -43,31 +45,30 @@ static void HandleNewProcess(DWORD pid, LPCWSTR filePath)
4345
if (hPipe == INVALID_HANDLE_VALUE)
4446
{
4547
PID_DEBUG("couldn't open named pipe (error 0x%x)\n", pid, GetLastError());
46-
return;
4748
}
4849
else
4950
{
5051
g_namedPipes.push_back(hPipe);
51-
}
5252

53-
PID_DEBUG("starting %ws\n", pid, filePath);
53+
PID_DEBUG("Starting %ws\n", pid, filePath);
5454

55-
GetCurrentDirectoryW(MAX_PATH, g_pathw);
56-
std::wstring currentDir(g_pathw);
55+
GetCurrentDirectoryW(MAX_PATH, g_pathw);
56+
std::wstring currentDir(g_pathw);
5757

58-
GetModuleFileNameW(NULL, g_pathw, MAX_PATH);
59-
wcsrchr(g_pathw, L'\\')[1] = 0;
60-
SetCurrentDirectoryW(g_pathw);
61-
62-
WIN32_FIND_DATAW findData;
63-
HANDLE hFile = FindFirstFileW(L".\\UnrealKey64.dll", &findData);
58+
GetModuleFileNameW(NULL, g_pathw, MAX_PATH);
59+
wcsrchr(g_pathw, L'\\')[1] = 0;
60+
SetCurrentDirectoryW(g_pathw);
61+
62+
WIN32_FIND_DATAW findData = { 0 };
63+
hFile = FindFirstFileW(L".\\UnrealKey64.dll", &findData);
64+
wcscat_s(g_pathw, findData.cFileName);
6465

65-
SetCurrentDirectoryW(currentDir.c_str());
66+
SetCurrentDirectoryW(currentDir.c_str());
67+
}
6668

6769
if (hFile != INVALID_HANDLE_VALUE)
6870
{
6971
// got DLL path, load it in the remote process
70-
wcscat_s(g_pathw, findData.cFileName);
7172

7273
LPVOID procMem = VirtualAllocEx(hProcess, NULL, sizeof(g_pathw), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
7374
if (WriteProcessMemory(hProcess, procMem, g_pathw, sizeof(g_pathw), NULL))
@@ -79,23 +80,40 @@ static void HandleNewProcess(DWORD pid, LPCWSTR filePath)
7980
if (hThread)
8081
{
8182
WaitForSingleObject(hThread, INFINITE);
83+
84+
DWORD exitCode;
85+
if (GetExitCodeThread(hThread, &exitCode) && exitCode)
86+
{
87+
ok = true;
88+
}
89+
else
90+
{
91+
PID_DEBUG("Remote thread init error\n", pid);
92+
}
93+
8294
CloseHandle(hThread);
8395
}
8496
else
8597
{
86-
PID_DEBUG("couldn't start remote thread\n", pid);
98+
PID_DEBUG("Couldn't start remote thread (error 0x%x)\n", pid, GetLastError());
8799
}
88100
}
89101
else
90102
{
91-
PID_DEBUG("couldn't write to process memory\n", pid);
103+
PID_DEBUG("Couldn't write to process memory (error 0x%x)\n", pid, GetLastError());
92104
}
93105

94106
FindClose(hFile);
95107
}
96108
else
97109
{
98-
PID_DEBUG("couldn't find UnrealKey64.dll\n", pid);
110+
PID_DEBUG("Couldn't find UnrealKey64.dll\n", pid);
111+
}
112+
113+
if (!ok)
114+
{
115+
// can't do anything useful here, just leave
116+
TerminateProcess(hProcess, 1);
99117
}
100118

101119
CloseHandle(hProcess);

dllmain.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ static NTSTATUS hook_ZwSetInformationThread(HANDLE hThread, ULONG infoClass, PVO
238238
}
239239

240240
// ----------------------------------------------------------------------------
241-
static void Init()
241+
static BOOL Init()
242242
{
243243
CHAR pipeName[MAX_PATH];
244244

@@ -251,8 +251,7 @@ static void Init()
251251
}
252252
else
253253
{
254-
sprintf_s(pipeName, "Couldn't open pipe for pid=%d (error=%x)", GetCurrentProcessId(), GetLastError());
255-
MessageBoxA(GetForegroundWindow(), pipeName, NULL, MB_OK);
254+
return FALSE;
256255
}
257256

258257
MH_STATUS hookStatus = MH_Initialize();
@@ -273,12 +272,16 @@ static void Init()
273272
else
274273
{
275274
SendStringMessage(L"MinHook enable failed: %hs", MH_StatusToString(hookStatus));
275+
return FALSE;
276276
}
277277
}
278278
else
279279
{
280280
SendStringMessage(L"MinHook init failed: %hs", MH_StatusToString(hookStatus));
281+
return FALSE;
281282
}
283+
284+
return TRUE;
282285
}
283286

284287
// ----------------------------------------------------------------------------
@@ -294,7 +297,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
294297
switch (ul_reason_for_call)
295298
{
296299
case DLL_PROCESS_ATTACH:
297-
Init();
300+
return Init();
298301
break;
299302

300303
case DLL_PROCESS_DETACH:

0 commit comments

Comments
 (0)