一直都很想写个钩子程序,曾经玩游戏的时候就特想。不过那是在个人电脑上的情况,现在我实现的再wince上的事情。
不过,在开始讲之前还是讲点“背景知识"先。^_^,千万别扔鸡蛋哦。前天,老总要我写个假死程序,在公司的个人电脑上面跑。“假死”?怎么回事?就是要机子动不了,当按下“后门”键或者输入后门指令,就恢复正常工作。
听他的要求,我立即想起了钩子。之前,看过一点wince钩子资料,但因为wince平台支持的钩子特少(键盘钩子似乎不支持),好像有人说只有三类——具体哪三类,goole一下就知道了。你说google强大吧,什么东西都有,我键盘钩子就是这样goole整出来的。
好了,开始进入主题了!
首先是KbHook.dll这个程序的主要代码部分:
KbHook.cpp
/*KbHook.dll
功能描述:
判断是否指定的平台,若不是则调用KbHook.dll键盘钩子,对键盘和鼠标进行简单的处理
*/
// KbHook.cpp : Defines the initialization routines for the DLL.
//
功能描述:
判断是否指定的平台,若不是则调用KbHook.dll键盘钩子,对键盘和鼠标进行简单的处理
*/
// KbHook.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "KbHook.h"
#include "winuser.h"
#include "pwinuser.h"
#include "KbHook.h"
#include "winuser.h"
#include "pwinuser.h"
//告诉编译器将变量放入它自己的数据共享节中
#pragma data_seg("KeyHookData")
HHOOK k_hook;
#pragma data_seg()
#pragma data_seg("KeyHookData")
HHOOK k_hook;
#pragma data_seg()
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CKbHookApp
BEGIN_MESSAGE_MAP(CKbHookApp, CWinApp)
//{{AFX_MSG_MAP(CKbHookApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//{{AFX_MSG_MAP(CKbHookApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CKbHookApp construction
// CKbHookApp construction
CKbHookApp::CKbHookApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/
// The one and only CKbHookApp object
CKbHookApp theApp;
//*******************************************************************************************安装钩子函数
extern "C" KbHook_API void InstallHook(void)
{
if (!k_hook)
{
k_hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardProc, theApp.m_hInstance, 0);
}
}
//*******************************************************************************************
//*******************************************************************************************卸载钩子函数
extern "C" KbHook_API void UnHook(void)
{
// The one and only CKbHookApp object
CKbHookApp theApp;
//*******************************************************************************************安装钩子函数
extern "C" KbHook_API void InstallHook(void)
{
if (!k_hook)
{
k_hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardProc, theApp.m_hInstance, 0);
}
}
//*******************************************************************************************
//*******************************************************************************************卸载钩子函数
extern "C" KbHook_API void UnHook(void)
{
if (k_hook)
{
UnhookWindowsHookEx(k_hook);
k_hook = NULL;
}
k_hook = NULL;
}
//*******************************************************************************************
//*******************************************************************************************键盘钩子处理函数
extern "C" KbHook_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
UnhookWindowsHookEx(k_hook);
k_hook = NULL;
}
k_hook = NULL;
}
//*******************************************************************************************
//*******************************************************************************************键盘钩子处理函数
extern "C" KbHook_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
BOOL fEatKeystroke = FALSE;
PKBDLLHOOKSTRUCT p = NULL;
BOOL fEatKeystroke = FALSE;
PKBDLLHOOKSTRUCT p = NULL;
if (nCode == HC_ACTION)
{
p = (PKBDLLHOOKSTRUCT) lParam;
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
fEatKeystroke = (p->vkCode == VK_LWIN) || (p->vkCode == VK_RWIN) || // 屏蔽Win
(p->vkCode == VK_ESCAPE) || (p->vkCode == VK_SPACE) || (p->vkCode == 0x13) || //屏蔽ESC、SPACE、PauseBreak
(p->vkCode == VK_RETURN) || (p->vkCode == VK_TAB) ||(p->vkCode == VK_MENU) ||// 屏蔽回车、TAB、ALT
(p->vkCode == VK_CONTROL) ||(p->vkCode == VK_MENU) || (p->vkCode == VK_DELETE)||// 屏蔽Ctrl、alt、del
((p->vkCode == VK_TAB) && (GetKeyState(VK_MENU) != 0)) || // 屏蔽Alt+Tab
((p->vkCode == VK_ESCAPE) && (GetKeyState(VK_MENU) != 0)) || // 屏蔽Alt+Esc
((p->vkCode == VK_ESCAPE) && ((GetKeyState(VK_CONTROL) & 0x8000) != 0)) ; // 屏蔽Ctrl+Esc
break;
default:
break;
}
}
return (fEatKeystroke ? TRUE : CallNextHookEx(k_hook,nCode,wParam,lParam));
{
p = (PKBDLLHOOKSTRUCT) lParam;
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
fEatKeystroke = (p->vkCode == VK_LWIN) || (p->vkCode == VK_RWIN) || // 屏蔽Win
(p->vkCode == VK_ESCAPE) || (p->vkCode == VK_SPACE) || (p->vkCode == 0x13) || //屏蔽ESC、SPACE、PauseBreak
(p->vkCode == VK_RETURN) || (p->vkCode == VK_TAB) ||(p->vkCode == VK_MENU) ||// 屏蔽回车、TAB、ALT
(p->vkCode == VK_CONTROL) ||(p->vkCode == VK_MENU) || (p->vkCode == VK_DELETE)||// 屏蔽Ctrl、alt、del
((p->vkCode == VK_TAB) && (GetKeyState(VK_MENU) != 0)) || // 屏蔽Alt+Tab
((p->vkCode == VK_ESCAPE) && (GetKeyState(VK_MENU) != 0)) || // 屏蔽Alt+Esc
((p->vkCode == VK_ESCAPE) && ((GetKeyState(VK_CONTROL) & 0x8000) != 0)) ; // 屏蔽Ctrl+Esc
break;
default:
break;
}
}
return (fEatKeystroke ? TRUE : CallNextHookEx(k_hook,nCode,wParam,lParam));
}
//*******************************************************************************************
//*******************************************************************************************
KbHook.cpp
KbHook.h
// KbHook.h : main header file for the KBHOOK DLL
//
//
#if !defined(AFX_KBHOOK_H__B7B006D8_EC81_4815_989E_F1F81AB21684__INCLUDED_)
#define AFX_KBHOOK_H__B7B006D8_EC81_4815_989E_F1F81AB21684__INCLUDED_
#define AFX_KBHOOK_H__B7B006D8_EC81_4815_989E_F1F81AB21684__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
#ifdef KbHook_EXPORTS
#define KbHook_API __declspec(dllexport)
#else
#define KbHook_API __declspec(dllimport)
#endif
// CKbHookApp
// See KbHook.cpp for the implementation of this class
//
// CKbHookApp
// See KbHook.cpp for the implementation of this class
//
class CKbHookApp : public CWinApp
{
public:
CKbHookApp();
{
public:
CKbHookApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CKbHookApp)
//}}AFX_VIRTUAL
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CKbHookApp)
//}}AFX_VIRTUAL
//{{AFX_MSG(CKbHookApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//KbHook_API int fnKeyBoardHook(void);
//*******************************************************************************************声明函数
//*******************************************************************************************声明函数
extern "C" KbHook_API void InstallHook(void);
extern "C" KbHook_API void UnHook(void);
extern "C" KbHook_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam);
//*******************************************************************************************
//*******************************************************************************************
//{{AFX_INSERT_LOCATION}}
// Microsoft eMbedded Visual C++ will insert additional declarations immediately before the previous line.
// Microsoft eMbedded Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_KBHOOK_H__B7B006D8_EC81_4815_989E_F1F81AB21684__INCLUDED_)
KbHook.h
KbHook.def
; KbHook.def : Declares the module parameters for the DLL.
LIBRARY "KBHOOK"
;DESCRIPTION 'KBHOOK Windows CE Dynamic Link Library'
;DESCRIPTION 'KBHOOK Windows CE Dynamic Link Library'
EXPORTS
; Explicit exports can go here
InstallHook;
UnHook;
; InstallHook2;
; UnHook2;
KbHook.def
; Explicit exports can go here
InstallHook;
UnHook;
; InstallHook2;
; UnHook2;
KbHook.def
以上是KbHook.dll部分的主要代码了。接下来的代码全部是SysInfo.EXE的代码
/*SysInfo.exe
功能描述:
判断是否指定的平台,若不是则调用KbHook.dll键盘钩子,对键盘和鼠标进行简单的处理
*/
功能描述:
判断是否指定的平台,若不是则调用KbHook.dll键盘钩子,对键盘和鼠标进行简单的处理
*/
// SysInfo.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
typedef BOOL (CALLBACK *instkbhook)(); //读取键盘钩子所设置的结构体
instkbhook InHook, UnHook;//, InHook2, UnHook2;
instkbhook InHook, UnHook;//, InHook2, UnHook2;
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
//*******************************************************************************************装载键盘钩子
static HINSTANCE hinstDLL;
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
//*******************************************************************************************装载键盘钩子
static HINSTANCE hinstDLL;
if(hinstDLL=LoadLibrary(L"KbHook.dll"))
{
// MessageBox(L"成功加载KbHook.dll");
InHook = (instkbhook)GetProcAddress(hinstDLL, L"InstallHook"); //获取钩子InstallHook函数的地址
UnHook = (instkbhook)GetProcAddress(hinstDLL, L"UnHook"); //获取钩子UnHook函数的地址
// InHook2 = (instkbhook)GetProcAddress(hinstDLL, L"InstallHook2");
// UnHook2 = (instkbhook)GetProcAddress(hinstDLL, L"UnHook2");
{
// MessageBox(L"成功加载KbHook.dll");
InHook = (instkbhook)GetProcAddress(hinstDLL, L"InstallHook"); //获取钩子InstallHook函数的地址
UnHook = (instkbhook)GetProcAddress(hinstDLL, L"UnHook"); //获取钩子UnHook函数的地址
// InHook2 = (instkbhook)GetProcAddress(hinstDLL, L"InstallHook2");
// UnHook2 = (instkbhook)GetProcAddress(hinstDLL, L"UnHook2");
}
else
{
MessageBox(NULL,L"Counld not find 'KbHook.dll'!/nPlease contact author!/nE_mail:lanjackg2003@126.com",L"Menq",MB_ICONERROR);
}
//*******************************************************************************************
else
{
MessageBox(NULL,L"Counld not find 'KbHook.dll'!/nPlease contact author!/nE_mail:lanjackg2003@126.com",L"Menq",MB_ICONERROR);
}
//*******************************************************************************************
HWND hwnd;
WNDCLASS wc;
MSG msg;
WNDCLASS wc;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = L"SysInfo";
if(!RegisterClass(&wc))
{
MessageBox(NULL,L"This program is Error!",L"xiao lan",MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(L"SysInfo",
TEXT(""),
WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
//*******************************************************************************************读平台信息 ARM920TM
TCHAR pPlatformType[64];
SystemParametersInfo(SPI_GETPLATFORMTYPE, sizeof(pPlatformType),pPlatformType, 0);
if(wcscmp(pPlatformType,_T("ARM920TM"))!=0)
{
ShowWindow (hwnd, SW_SHOW) ;
UpdateWindow (hwnd);
RegisterHotKey(hwnd,10001,MOD_SHIFT,VK_F8); //注册退出程序和钩子的后门热键
// MessageBox(NULL,L"ARM920TM",0,0);
InHook();
}
else
{
// MessageBox(NULL,L"ARM920TM",0,0);
exit(0);
}
//*******************************************************************************************
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = L"SysInfo";
if(!RegisterClass(&wc))
{
MessageBox(NULL,L"This program is Error!",L"xiao lan",MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(L"SysInfo",
TEXT(""),
WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
//*******************************************************************************************读平台信息 ARM920TM
TCHAR pPlatformType[64];
SystemParametersInfo(SPI_GETPLATFORMTYPE, sizeof(pPlatformType),pPlatformType, 0);
if(wcscmp(pPlatformType,_T("ARM920TM"))!=0)
{
ShowWindow (hwnd, SW_SHOW) ;
UpdateWindow (hwnd);
RegisterHotKey(hwnd,10001,MOD_SHIFT,VK_F8); //注册退出程序和钩子的后门热键
// MessageBox(NULL,L"ARM920TM",0,0);
InHook();
}
else
{
// MessageBox(NULL,L"ARM920TM",0,0);
exit(0);
}
//*******************************************************************************************
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:
SetCursorPos(0,0);
break;
case WM_HOTKEY:
//*******************************************************************************************响应热键卸载钩子及热键,退出程序
UnHook();
UnregisterHotKey(hWnd,1001);
PostQuitMessage(0);
//*******************************************************************************************
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
DrawText(hdc, L"请使用正确平台!", _tcslen(L"请使用正确平台!"), &rt,
DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
break;
default:
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
{
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:
SetCursorPos(0,0);
break;
case WM_HOTKEY:
//*******************************************************************************************响应热键卸载钩子及热键,退出程序
UnHook();
UnregisterHotKey(hWnd,1001);
PostQuitMessage(0);
//*******************************************************************************************
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
DrawText(hdc, L"请使用正确平台!", _tcslen(L"请使用正确平台!"), &rt,
DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
break;
default:
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
所有的代码已经贴完了,顺便问一句哪位亲朋好友搞wince/嵌入式类的有空交流一下!
嘿嘿!
自己还是比较喜欢在QQ空间写东西,这篇东东在空间转过来D
呵呵。