通过设置全局消息钩子来实现dll注入,然后窗体有相关消息请求的时候就会自动加载注入dll,
然后在入口处做处理就可以了。注入方式简单很多,比代码注入和lsp等注入都简单,就不解释了。
这个也可以传递不同的劫持类型来进行键盘按键记录等。
}
注意{
1.dll注入期间注入程序不可以退出,否则dll内核句柄有可能被释放。
2.注意32位和64位注入问题
}
DLL调用
/*
HMODULE h = LoadLibrary(L"xx.dll");
_SetHook SetHook = (_SetHook)GetProcAddress(h,"SetHook");
_UnHook UnHook = (_UnHook)GetProcAddress(h,"UnHook");
SetHook();
Sleep(10000);
UnHook();
CloseHandle(h);
*/
//DLL相关代码
#include <windows.h>
#include <tlhelp32.h>
#include <Psapi.h>
#pragma comment(lib,"psapi.lib")
#pragma data_seg("Yrrehs")
HHOOK HT = NULL;
#pragma data_seg()
HINSTANCE DLLhinst = NULL;
LRESULT CALLBACK CProc(int nCode,WPARAM wParam,LPARAM lParam){
return CallNextHookEx(HT,nCode,wParam,lParam);
}
//安装钩子
extern "C" __declspec(dllexport) BOOL SetHook(){
HT = SetWindowsHookEx(WH_CALLWNDPROC,CProc,DLLhinst,0);
if(HT == NULL){
return false;
}
return true;
}
//卸载钩子
extern "C" __declspec(dllexport) BOOL UnHook(){
BOOL HM_BOOL = FALSE;
if(HT != NULL){
HM_BOOL = UnhookWindowsHookEx(HT);
}
return HM_BOOL;
}
//获得进程名
wchar_t* GetProcessName(DWORD processID){
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,processID);
wchar_t *procName = new wchar_t[MAX_PATH];
GetModuleFileNameEx(hProcess,NULL,procName,MAX_PATH);
CloseHandle(hProcess);
return procName;
}
//获得进程名
wchar_t* GetProcessName(wchar_t *FileName){
size_t len = wcslen(FileName);
size_t i = len-1;
for(;i>=0;i--){
if(FileName[i] == L'\'){
break;
}
}
wchar_t *temp = FileName + i + 1;
return temp;
}
BOOL WINAPI DllMain(HINSTANCE hinstDll,DWORD fdwReason,LPVOID lpvReserved){
DLLhinst = hinstDll;
if(DLL_PROCESS_ATTACH == fdwReason){
wchar_t *procName = GetProcessName(GetCurrentProcessId());
if(_wcsicmp(L"xxx.exe",GetProcessName(procName))==0){
//XXXXXX
}
}
if(DLL_PROCESS_DETACH == fdwReason){
}
return TRUE;
}