diff --git a/StartHelper/main.cpp b/StartHelper/main.cpp index 056ce08..78c2c99 100755 --- a/StartHelper/main.cpp +++ b/StartHelper/main.cpp @@ -16,6 +16,8 @@ _In_ int nShowCmd ) { + int retCode = 0; + int numArgs = 0; LPWSTR* args = CommandLineToArgvW(lpCmdLine, &numArgs); @@ -25,9 +27,9 @@ MessageBox(NULL, L"Provide at least a target and a AUMI", L"ERROR", MB_OK); return -1; } - - LPWSTR cmd = args[0]; - LPWSTR aumi = args[1]; + + LPWSTR aumi = args[0]; + LPWSTR cmd = args[1]; numArgs -= 2; if (numArgs == 0) @@ -59,17 +61,31 @@ ZeroMemory(&startupInfo, sizeof(startupInfo)); startupInfo.cb = sizeof(startupInfo); + startupInfo.dwFlags |= STARTF_TITLEISAPPID; + + startupInfo.lpTitle = new wchar_t[5]; + wcscpy_s(startupInfo.lpTitle, 5, L"TEST"); + ZeroMemory(&processInfo, sizeof(processInfo)); + HANDLE process; + HMODULE* remoteModuleList; + DWORD needed; + int numModules; + bool found = false; + wchar_t* name = new wchar_t[1024]; + + const char* msg = "TestInjected"; + + bool requiresResume = false; + //if (!DetourCreateProcessWithDllEx(cmd, cmdLineC, NULL, NULL, false, 0, NULL, NULL, &startupInfo, &processInfo, injectDllStr.c_str(), NULL)) - if (!CreateProcess(cmd, cmdLineC, NULL, NULL, false, CREATE_SUSPENDED, NULL, NULL, &startupInfo, &processInfo/*, injectDllStr.c_str(), NULL*/)) + if (!CreateProcess(cmd, cmdLineC, NULL, NULL, false, 0, NULL, NULL, &startupInfo, &processInfo/*, injectDllStr.c_str(), NULL*/)) { - ShowLastError(); - //MessageBox(NULL, L"Failed to DetourCreateProcessWithDllEx", L"Error", MB_OK); - return -1; + goto lastError; } - HANDLE process = processInfo.hProcess; + process = processInfo.hProcess; /* HMODULE injected = LoadLibraryA(injectDllStr.c_str()); @@ -89,25 +105,19 @@ INT_PTR offset = (INT_PTR)targetAddr - (INT_PTR)injected; */ - Sleep(10000); + //Sleep(10000); //SuspendThread(processInfo.hThread); - const char* msg = "TestInjected"; + - HMODULE* remoteModuleList = new HMODULE[1000]; - DWORD needed = 0; + remoteModuleList = new HMODULE[1000]; + needed = 0; if (!EnumProcessModules(process, remoteModuleList, 1000, &needed)) { - ShowLastError(); - return -1; + goto lastError; } - int numModules = needed / sizeof(HMODULE); - - bool found = false; - - wchar_t* name = new wchar_t[1024]; - + numModules = needed / sizeof(HMODULE); //for (int tries = 5; tries >= 0; tries--) @@ -125,8 +135,7 @@ void* remoteMem = VirtualAllocEx(process, NULL, strlen(msg) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); if (remoteMem == NULL) { - ShowLastError(); - return -1; + goto lastError; } else { @@ -135,8 +144,7 @@ if (!WriteProcessMemory(process, remoteMem, msg, strlen(msg) + 1, NULL)) { - ShowLastError(); - return -1; + goto lastError; } //ResumeThread(processInfo.hThread); @@ -145,8 +153,7 @@ HANDLE thread = CreateRemoteThread(process, NULL, 0, (LPTHREAD_START_ROUTINE)((INT_PTR)remoteModuleList[i] + offset), remoteMem, 0, NULL); if (thread == NULL) { - ShowLastError(); - return -1; + goto lastError; } WaitForSingleObject(thread, INFINITE); @@ -159,7 +166,8 @@ char err[128]; sprintf_s(err, "Error from injected function: %d", ret); MessageBoxA(NULL, err, "Error", MB_OK); - return -1; + + goto quitError; } */ @@ -171,13 +179,12 @@ // Sleep(1000); //} - //ResumeThread(processInfo.hThread); + ResumeThread(processInfo.hThread); if (!found) - { - ResumeThread(processInfo.hThread); + { MessageBox(NULL, L"Couldn't find injected dll", L"Error", MB_OK); - return -1; + goto quitError; } else { @@ -187,6 +194,16 @@ MessageBox(NULL, L"Done", L"Done", MB_OK); return 0; + +lastError: + ShowLastError(); +quitError: + retCode = -1; + goto cleanup; + +cleanup: + if (requiresResume) ResumeThread(processInfo.hThread); + return retCode; }