简体   繁体   中英

C++ DLL Injection "CreateRemoteThread" returning 0

So I'm writing a program for DLL injection, using OpenProcess, VirtualAllocEx, WriteProcessMemory, etc.

Everything (appears) to be running smoothly until I call CreateRemoteThread. I triple-checked the Microsoft documentation and looked at a few examples, so I'm pretty sure I've used the function properly, however it's returning 0.

Here's my code:

// path to DLL
LPCSTR dllPath = "C:\\Users\\user\\OneDrive\\Desktop\\dllname.dll";

// process handle
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 9440);

// dll base address
LPVOID baseAddress = VirtualAllocEx(hProcess, NULL, strlen(dllPath) + 1, MEM_COMMIT, PAGE_READWRITE);

// write to process memory
WriteProcessMemory(hProcess, baseAddress, (LPCVOID) dllPath, strlen(dllPath) + 1, NULL);

// kernel32's LoadLibraryA function
LPVOID loadLibrary = (LPVOID) GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");

// create remote thread -- This is what returns 0 (not sure why)
HANDLE hLoadThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE) loadLibrary, baseAddress, 0, 0);

// wait to inject
WaitForSingleObject(hLoadThread, INFINITE);

// print debugs
std::cout << "Successfully injected." << std::endl << std::endl;
std::cout << hProcess << std::endl;
std::cout << baseAddress << std::endl;
std::cout << loadLibrary << std::endl;
std::cout << hLoadThread << std::endl;
std::cin.get();

// free memory
VirtualFreeEx(hProcess, baseAddress, strlen(dllPath) + 1, MEM_RELEASE);

And here's the output to the console:

Successfully injected.

0x110
0x3a9c0000
0x75dd0bd0
0 <-- this is the return value of CreateRemoteThread

I've tried fidgeting with the null/0s in the parameters and removing the WaitForSingleObject call but neither gives a result.

Any help would be appreciated.

Found the issue. I was compiling with MingGW 32 bit instead of using 64 bit, so I created a VS solution and it fixed. Thank you for the help @Retired Ninja.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM