2017-11-18 4 views
-1

Ich möchte meine KeyboardProc von DLL in mein erstelltes Fenster mit Hilfe von DLL-Injektion einhaken. Ich möchte Nachrichtenfelder sehen, wenn ich Tasten mit dem fokussierten Fenster "injected" drücke, aber mein Code funktioniert nicht richtig.Dll Prozess Hook funktioniert nicht

injizierte Fenstercode:

#include <windows.h> 
#include <iostream> 

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (lParam) 
    { 
    default: 
     return DefWindowProc(hwnd, uMsg, wParam, lParam); 
    } 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgs, int nCmdShow) 
{ 
    HWND hwnd; 
    LPCTSTR className = L"WNDCLASS"; 
    LPCTSTR windowName = L"Window"; 

    WNDCLASSEX wcex; 
    memset(&wcex, 0, sizeof(wcex)); 

    wcex.cbSize = sizeof(wcex); 
    wcex.hInstance = hInstance; 
    wcex.lpszClassName = className; 
    wcex.style = CS_DBLCLKS; 
    wcex.lpfnWndProc = WndProc; 
    wcex.hbrBackground = CreateSolidBrush(RGB(255, 255, 255)); 

    if (!RegisterClassEx(&wcex)) 
    { 
     return -1; 
    } 

    hwnd = CreateWindowEx(NULL, className, windowName, WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hInstance, NULL); 

    if (!hwnd) 
    { 
     return -2; 
    } 

    MSG msg; 

    ShowWindow(hwnd, SW_NORMAL); 
    UpdateWindow(hwnd); 

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

    return 
     msg.lParam; 
} 

DLL-Code:

// dllmain.cpp : Defines the entry point for the DLL application. 
#include "stdafx.h" 
#include <iostream> 
#include <Windows.h> 
#include <TlHelp32.h> 
#include <tchar.h> 

HHOOK hhkKb; 

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
    if (wParam == WM_KEYDOWN) 
    { 
     MessageBox(0, L"DOWN", L"keyboard key down in dll", MB_ICONINFORMATION); 
    } 
    else if (wParam == WM_KEYUP) 
    { 
     MessageBox(0, L"UP", L"keyboard key up in dll", MB_ICONINFORMATION); 
    } 

    return 
     CallNextHookEx(hhkKb, nCode, wParam, lParam); 
} 

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) 
{ 
    switch (ul_reason_for_call) 
    { 
     case DLL_PROCESS_ATTACH: 
     { 
      HWND windowHandle = FindWindow(NULL, L"Window"); 

      if (windowHandle == NULL) 
      { 
       MessageBox(NULL, L"Error", L"Handle is null", MB_ICONERROR); 
       return TRUE; 
      } 

      DWORD threadId = GetWindowThreadProcessId(windowHandle, NULL); 
      hhkKb = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hModule, threadId); 
      MessageBox(NULL, L"Success", L"Sucessfully injected dll", MB_ICONINFORMATION); //shows that message 

      break; 
     } 
     case DLL_PROCESS_DETACH: 
     { 
      UnhookWindowsHookEx(hhkKb); 
      break; 
     } 
    } 
    return TRUE; 
} 

I "Erfolgreich injiziert dll" -Meldung zu sehen, aber wenn ich Schlüssel in injiziert Fenster drücken bin, wird die KeyboardProc nicht genannt was mache ich falsch?

Antwort

1

mit DLL-Injection

unklar, wie Sie diese DLL-Injektion, aber aufgrund Ihrer bisherigen question annehmen können, dass Sie manuell Ihre dll von CreateRemoteThread-LoadLibraryA injizieren. und in jedem Fall Anruf SetWindowsHookEx von DLL-Einstiegspunkt ist Fehler nach Sinn.

formal, wenn Thread, die SetWindowsHookEx anrufen, Ausfahrt - Haken wird automatisch entfernt. So kann man sagen, dass beim indirekten Aufruf des Ausgangs-Thread UnhookWindowsHookEx. nicht sicher sind diese klar dokumentiert, kann aber in Sicht neben einfach testen

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
    DbgPrint("%x>KeyboardProc(%x)\n", GetCurrentThreadId(), wParam); 

    return CallNextHookEx(0, nCode, wParam, lParam); 
} 

ULONG HookThread(PVOID threadId) 
{ 
    DbgPrint("%x>HookThread(%x)\n", GetCurrentThreadId(), (ULONG)(ULONG_PTR)threadId); 
    if (HHOOK hhk = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, (HINSTANCE)&__ImageBase, (ULONG)(ULONG_PTR)threadId)) 
    { 
     Sleep(10000);//10 sec 
     //MessageBoxW(0,0,L"Close Me", MB_ICONWARNING); 
    } 
    return 0; 
} 

void test() 
{ 
    if (HANDLE hThread = CreateThread(0, 0, HookThread, (PVOID)(ULONG_PTR)GetCurrentThreadId(), 0,0)) 
    { 
     CloseHandle(hThread); 
     MessageBoxW(0,0,L"Press Buttons here", MB_ICONINFORMATION); 
    } 
} 

ersten Sekunden sein (oder bis Sie MessageBoxW L schließen „Close Me“, wenn diese Variante gewählt) können Sie sehen DbgPrint aus KeyboardProc wenn Sie die Tasten der ersten Nachrichtenbox drücken. nach HookThread Ausfahrt - nicht mehr KeyboardProc wird aufgerufen.

wenn Sie also dll über CreateRemoteThread injizieren - diesen Thread Aufruf LoadLibrary, dann SetWindowsHookEx wird in diesem Thread aufgerufen werden, und schließlich nur Ausfahrt einfädeln - und dieser Effekt von Call entfernen SetWindowsHookEx - Haken entfernen wird.

aber wenn wir SetWindowsHookEx verwenden wir brauchen nicht manuell injizieren dll zu verarbeiten. vice versa - dieses api spezielle design für automatisch injizieren dll zu remote prozess. und natürlich nicht SetWindowsHookEx von dll Einstiegspunkt anrufen - das ist Unsinn. Sie müssen SetWindowsHookEx aus dem Remote-Prozess anrufen - als Ergebnis Ihrer DLL und wird in den Zielprozess injiziert werden. erneut lesen Installing and Releasing Hook Procedures

Verwandte Themen