Code:
#include "stdafx.h"
#include <tchar.h>
#pragma data_seg(".shared")
DWORD ProtectingPid = 0;
#pragma data_seg()
HMODULE MyModuleHandle;
HHOOK hhk = NULL;
DWORD MyPid = 0;
unsigned char Store[10];
long _stdcall SelfInject();
long _stdcall SelfEject();
LRESULT CALLBACK GetMsgProc(int nCode,WPARAM wParam,LPARAM lParam);
int _stdcall MessageBoxA2(HWND hWnd, LPCSTR lpText, LPCSTR lpCation, UINT uType);
void HookAPI();
void UnHookAPI();
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
MyModuleHandle = (HMODULE)hModule;
// Khong su dung ham MessageBoxA de thong bao vi ta hook ham MessageBoxA ma
// MessageBoxA(0, "Hook noi dung","Hook", MB_OK);
HookAPI();
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
{
UnHookAPI();
}
return TRUE;
}
long _stdcall SelfInject()
{
//ProtectingPid = MyPid;
hhk = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,MyModuleHandle,0);
return (hhk != NULL);
}
long _stdcall SelfEject()
{
return UnhookWindowsHookEx(hhk);
}
LRESULT CALLBACK GetMsgProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if (nCode < 0)
{
return CallNextHookEx(hhk,nCode,wParam,lParam);
}
if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
{
switch (wParam)
{
case VK_F2:
MessageBox(0, _T("aaaaa"), _T("bbbbb"),MB_OK);
break;
}
}
return CallNextHookEx(hhk,nCode,wParam,lParam);
}
void HookAPI()
{
DWORD OldProtect, NewProtect = PAGE_EXECUTE_READWRITE;
HMODULE hmod = GetModuleHandle("user32");
long pa = (long)GetProcAddress(hmod,"MessageBoxA");
long pa2 = (long)MessageBoxA2;
long dAddr = pa2 - pa - 5;
unsigned char *p = (unsigned char *)pa;
unsigned char *p2 = (unsigned char *)(&dAddr);
VirtualProtect((void *)pa,5,NewProtect,&OldProtect);
for (int i=0;i<5;i++)
Store[i] = p[i];
p[0] = (unsigned char)0xE9;
for (int i=0;i<4;i++)
p[i + 1] = p2[i];
VirtualProtect((void *)pa,5,OldProtect,&NewProtect);
}
void UnHookAPI()
{
DWORD OldProtect, NewProtect = PAGE_EXECUTE_READWRITE;
HMODULE hmod = GetModuleHandle("user32");
long pa = (long)GetProcAddress(hmod,"MessageBoxA");
unsigned char *p = (unsigned char *)pa;
VirtualProtect((void *)pa,5,NewProtect,&OldProtect);
for (int i=0;i<5;i++)
p[i] = Store[i];
VirtualProtect((void *)pa,5,OldProtect,&NewProtect);
}
int _stdcall MessageBoxA2(HWND hWnd, LPCSTR lpText, LPCSTR lpCation, UINT uType)
{
HWND hwndtest = FindWindow("Notepad",NULL);
hwndtest = FindWindowExA(hwndtest,0, "Edit", NULL);
PostMessage(hwndtest, WM_KEYDOWN, VkKeyScanA('A'),NULL);
PostMessage(hwndtest, WM_KEYUP, VkKeyScanA('A'),NULL);
// MessageBox(0, _T("abc"), _T("def"), MB_OK);
return 0;
}
Mình có 3 vấn đề muốn hỏi: