gpt4 book ai didi

c++ - CreateRemoteThread 不适用于 DLL

转载 作者:行者123 更新时间:2023-11-28 04:27:25 26 4
gpt4 key购买 nike

我正在尝试注入(inject)一个在目标进程中创建 MessageBox 的简单 dll。使用来自 www 的注入(inject)器没有任何问题。但是使用我自己的代码注入(inject)根本没有做任何事情(我在 notepad.exe 上使用它)

我在 VS2017 中将 dll 和此代码编译为 x64 调试。注入(inject)器被创建为 Win32 控制台项目。

代码中的所有阶段都通过了。我得到了进程的句柄,线程句柄也有效。但是 GetExitCode 返回 0,所以它一直失败,但我不知道为什么?

    HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, false, pid);

if (process == NULL)
{
std::cout << "Error opening process." << std::endl;
return false;
}

const char * dllString = "C:\\test.dll";

// load memory for dll

int bytes = sizeof(dllString);


PVOID mem = VirtualAllocEx(process, NULL, sizeof(dllString) + 1, MEM_COMMIT, PAGE_READWRITE);

if (mem == NULL)
{
std::cout << "Unable to allocate mem." << std::endl;
CloseHandle(process);
return false;
}

// write dll path to that location
SIZE_T bytesWritten;
BOOL status = WriteProcessMemory(process, mem, dllString, sizeof(dllString) + 1, &bytesWritten);

if (!status)
{
std::cout << "Writing dll path failed." << std::endl;
VirtualFreeEx(process, mem, sizeof(dllString) + 1, MEM_RELEASE);
CloseHandle(process);
return false;
}

FARPROC loadLibrary = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

HANDLE thread = CreateRemoteThread(process, NULL, NULL, reinterpret_cast<LPTHREAD_START_ROUTINE>(loadLibrary), mem, NULL, NULL);

if (thread == INVALID_HANDLE_VALUE)
{
std::cout << "Unable to create thread in remote process. " << std::endl;
VirtualFreeEx(process, mem, sizeof(dllString) + 1, MEM_RELEASE);
CloseHandle(process);
}


WaitForSingleObject(thread, INFINITE);

DWORD exitCode = 0;
GetExitCodeThread(thread, &exitCode);

if (exitCode != 0)
std::cout << "DLL loaded successfully." << std::endl;
else
std::cout << "DLL loading failed." << std::endl;

CloseHandle(thread);
VirtualFreeEx(process, mem, sizeof(dllString) + 1, MEM_RELEASE);
CloseHandle(process);
return true;

最佳答案

自己解决了。实际上是一个菜鸟问题。 sizeof 实际上返回指针的大小,x64 是 64 位,而不是我需要分配的内存的字符串长度。所以在将其更改为 strlen 后它起作用了。

关于c++ - CreateRemoteThread 不适用于 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53972307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com