简体   繁体   English

DLL 注入两个进程

[英]DLL Injection on two processes

I am trying to build a dll (in order to inject in a process) and am having some issues with it.我正在尝试构建一个 dll (以便注入进程)并且遇到了一些问题。

Let me explain:让我解释:

When I inject my dll in my target, Everything works perfectly.当我在我的目标中注入我的 dll 时,一切正常。 Once I add another target (the same executable) while the first one is still running, there will be some conflicts.一旦我在第一个目标仍在运行时添加另一个目标(相同的可执行文件),就会出现一些冲突。

To be fair, I think there is "only" one conflict.公平地说,我认为“只有”一个冲突。 Here is the code.这是代码。

DWORD WINAPI MainThread(LPVOID param)
{
    // Same behaviour with GetKeyState
    while (!GetAsyncKeyState(VK_F9)) // While F9 is not pressed, do nothing
        Sleep(5);
    fprintf(stdout, "Clicked !"); // Print Clicked when F9 is pressed
    while(true); // Just to stop.
    return false;
}

BOOL APIENTRY DllMain(HMODULE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH: // Gets ran when injected
        AllocConsole(); // Enable the console
        freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
        freopen_s((FILE**)stdin, "CONIN$", "r", stdin);
        CreateThread(0, 0, MainThread, hModule, 0, 0); // Creates our thread 
        break;
    }
    return TRUE;
}

This sample will do the following: I start Target.exe, I inject Inject.dll into it, I press F9, "Clicked" appears.此示例将执行以下操作:我启动 Target.exe,将 Inject.dll 注入其中,按 F9,出现“单击”。 Expected.预期的。
Now, I start Target.exe, I inject Indect.dll, I don't press F9.现在,我启动 Target.exe,我注入 Indect.dll,我不按 F9。 Instead, I start another Target.exe, I inject Inject.dll into it, and now, if I press F9, Clicked will be printed on both consoles.相反,我启动了另一个 Target.exe,我将 Inject.dll 注入其中,现在,如果我按 F9,Clicked 将在两个控制台上打印。 Unexpected.意外。

Why?为什么?

I read on https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate the following我在https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate上阅读了以下内容

Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application.尽管返回值的最低有效位指示自上次查询以来是否已按下键,但由于 Windows 的抢占式多任务性质,另一个应用程序可以调用 GetAsyncKeyState 并接收“最近按下”位而不是您的应用程序。 The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit My Windows applications (which are non-preemptive) and should not be relied upon.严格保留返回值的最低有效位的行为是为了与 16 位 My Windows 应用程序(非抢占式)兼容,不应依赖。

Target.exe and Inject.dll are both 32 bits, so I guess I do not need to be bothered by that quote, so I am really clueless on what is causing it. Target.exe 和 Inject.dll 都是 32 位的,所以我想我不需要被那个引用所困扰,所以我真的不知道是什么原因造成的。

Note that I also tested with getchar, but since it is console-related, I can not use it, even if it does not print on the second console.请注意,我也使用 getchar 进行了测试,但由于它与控制台相关,因此我无法使用它,即使它没有在第二个控制台上打印。

Well, as I stated in my last comment好吧,正如我在上一条评论中所说

Tried with SetWindowsHook, same behaviour.尝试使用 SetWindowsHook,行为相同。 Came up with a window rename + name comparison.想出了一个 window 重命名 + 名称比较。 Works perfectly !完美运行! Thanks for your help谢谢你的帮助

I used rename + name comparison.我使用了重命名+名称比较。 Others issues came out, so I had to rethink my way of achieving it.其他问题出现了,所以我不得不重新考虑实现它的方式。 Here is the final version:这是最终版本:

bool isSameProcess(HWND window, DWORD pid)
{
    DWORD activePId;
    DWORD activeThreadId = GetWindowThreadProcessId(window, &activePId);
    return activePId == pid;
}

Explaination: window is the active window, pid our own process id.说明:window是活跃的window,pid是我们自己的进程id。 we will check if our pid is equal to active window's process id.我们将检查我们的 pid 是否等于活动窗口的进程 ID。 If it is, we return true, else, false.如果是,我们返回 true,否则返回 false。

So this way, you can use it like that:所以这样,你可以像这样使用它:

if ((GetAsyncKeyState(VK_F9) & 0x8000))
{
    window = GetForegroundWindow();
    if(isSameProcess(window, pid))
    {/* Whatever you want to do */}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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