首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CreateRemoteThread不适用于DLL

CreateRemoteThread不适用于DLL
EN

Stack Overflow用户
提问于 2018-12-30 02:35:02
回答 1查看 76关注 0票数 0

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

我在VS2017中将dll和这段代码编译为x64 debug。Injector被创建为Win32控制台项目。

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

代码语言:javascript
复制
    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;
EN

回答 1

Stack Overflow用户

发布于 2018-12-30 03:00:13

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53972307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档