Win2K下的Api函数的拦截
副标题#e#
这么多好手在这里,哎,小弟愿意向列位好手进修。
Api拦截并不是一个新的技能,许多贸易软件都回收这种技能。对windows的Api函数的拦截,不过乎两种要领,第一种是Mr. Jeffrey Richter 的修改exe文件的模块输入节,种要领,很安详,但很巨大,并且有些exe文件,没有Dll的输入标记的列表,有大概呈现拦截不到的环境。第二种要领就是常用的JMP XXX的要领,固然很陈腐,却很简朴实用。
本文一先容第二种要领在Win2k下的利用。第二种要领,Win98/me 下因为进入Ring0级的要领许多,有LDT,IDT,Vxd等要领,很容易在内存中动态修改代码,但在Win2k下,这些要领都不能用,写WDM过分巨大,外貌上看来很难实现,
其实否则。Win2k为我们提供了一个强大的内存Api操纵函数—VirtualProtectEx,WriteProcessMemeory,ReadProcessMemeory,有了它们我们就能在内存中动态修改代码了,其原型为:
BOOL VirtualProtectEx(
HANDLE hProcess, // 要修改内存的历程句柄
LPVOID lpAddress, // 要修改内存的起始地点
DWORD dwSize, // 修改内存的字节
DWORD flNewProtect, // 修改后的内存属性
PDWORD lpflOldProtect // 修改前的内存属性的地点
);
BOOL WriteProcessMemory(
HANDLE hProcess, // 要写历程的句柄
LPVOID lpBaseAddress, // 写内存的起始地点
LPVOID lpBuffer, // 写入数据的地点
DWORD nSize, // 要写的字节数
LPDWORD lpNumberOfBytesWritten // 实际写入的子节数
);
BOOL ReadProcessMemory(
HANDLE hProcess, // 要读历程的句柄
LPCVOID lpBaseAddress, // 读内存的起始地点
LPVOID lpBuffer, // 读入数据的地点
DWORD nSize, // 要读入的字节数
LPDWORD lpNumberOfBytesRead // 实际读入的子节数
);
#p#副标题#e#
详细的参数请参看MSDN辅佐。在Win2k下因为Dll和所属历程在同一地点空间,这点又和Win9x/me存在所有历程存在共享的地点空间差异,
因此,必需通过钩子函数和长途注入历程的要领,现以一个简朴回收钩子函数对MessageBoxA举办拦截例子来说明:
个中Dll文件为:
//---------------------------------------------------------------------------
#include <windows.h>
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------
#pragma argsused
HHOOK g_hHook;
HINSTANCE g_hinstDll;
FARPROC fpMessageBoxA;
HMODULE hModule ;
BYTE OldMessageBoxACode[5], NewMessageBoxACode[5];
DWORD dwIdOld, dwIdNew;
BOOL bHook = false;
void HookOn();
void HookOff();
BOOL Init();
int WINAPI MyMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
//---------------------------------------------------------------------------
// 空的钩子函数
LRESULT WINAPI Hook(int nCode, WPARAM wParam, LPARAM lParam)
{
return(CallNextHookEx(g_hHook, nCode, wParam, lParam));
}
//---------------------------------------------------------------------------
// 输出,安装空的钩子函数
extern "C" __declspec(dllexport) __stdcall
BOOL InstallHook()
{
g_hinstDll = LoadLibrary("project1.dll"); // 这里的文件名为Dll自己的文件名
g_hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)Hook, g_hinstDll, 0);
if (!g_hHook)
{
MessageBoxA(NULL, "SET ERROR", "ERROR", MB_OK);
return(false);
}
return(true);
}
//---------------------------------------------------------------------------
// 输出,Uninstall钩子函数
extern "C" __declspec(dllexport) __stdcall
BOOL UninstallHook()
{
return(UnhookWindowsHookEx(g_hHook));
}
//---------------------------------------------------------------------------
// 初始化获得MessageBoxA的地点,并生成Jmp XXX(MyMessageBoxA)的跳转指令
BOOL Init()
{
hModule = LoadLibrary("user32.dll");
fpMessageBoxA = GetProcAddress(hModule, "GetSystemDirectoryA");
if(fpMessageBoxA == NULL)
return false;
_asm
{
pushad
lea edi, OldMessageBoxACode
mov esi, fpMessageBoxA
cld
movsd
movsb
popad
}
NewMessageBoxACode[0] = 0xe9; // jmp MyMessageBoxA的相对地点的指令
_asm
{
lea eax, MyGetSystemDirectory
mov ebx, fpMessageBoxA
sub eax, ebx
sub eax, 5
mov dword ptr [NewMessageBoxACode + 1], eax
}
dwIdNew = GetCurrentProcessId(); // 获得所属历程的ID
dwIdOld = dwIdNew;
HookOn(); // 开始拦截
return(true);
}
//---------------------------------------------------------------------------
// 首先封锁拦截,然后才气挪用被拦截的Api 函数
int WINAPI MyMessageBoxA(HWND hWnd, LPCTSTR lpText,LPCTSTR lpCaption, UINT uType)
{
int nReturn;
HookOff();
nReturn = MessageBoxA(hWnd, "来自钩子中的内容", lpCaption, MB_OK | MB_ICONINFORMATION);
HookOn();
return(nReturn);
}
//---------------------------------------------------------------------------
// 本文代码由ccrun整理并在BCB 6.0情况下调试通过。
// Welcome to C++ Builder 研究
// http://www.ccrun.com
//---------------------------------------------------------------------------
void HookOn()
{
HANDLE hProc;
dwIdOld = dwIdNew;
// 获得所属历程的句柄
hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, dwIdOld);
// 修改所属历程中MessageBoxA的前5个字节的属性为可写
VirtualProtectEx(hProc, fpMessageBoxA, 5, PAGE_READWRITE,&dwIdOld);
// 将所属历程中MessageBoxA的前5个字节改为JMP 到MyMessageBoxA
WriteProcessMemory(hProc, fpMessageBoxA, NewMessageBoxACode, 5, 0);
// 修改所属历程中MessageBoxA的前5个字节的属性为本来的属性
VirtualProtectEx(hProc, fpMessageBoxA, 5, dwIdOld, &dwIdOld);
bHook=true;
}
//---------------------------------------------------------------------------
// 将所属历程中JMP MyMessageBoxA的代码改为Jmp MessageBoxA
void HookOff()
{
HANDLE hProc;
dwIdOld = dwIdNew;
hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, dwIdOld);
VirtualProtectEx(hProc, fpMessageBoxA,5, PAGE_READWRITE, &dwIdOld);
WriteProcessMemory(hProc, fpMessageBoxA, OldMessageBoxACode, 5, 0);
VirtualProtectEx(hProc, fpMessageBoxA, 5, dwIdOld, &dwIdOld);
bHook = false;
}
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
if(!Init())
{
MessageBoxA(NULL,"Init","ERROR",MB_OK);
return(false);
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
if(bHook)
UninstallHook();
break;
}
return TRUE;
}
// 测试文件:
extern "C" __declspec(dllimport) __stdcall
BOOL InstallHook();
extern "C" __declspec(dllimport) __stdcall
BOOL UninstallHook();
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// 本文代码由ccrun整理并在BCB 6.0情况下调试通过。
// Welcome to C++ Builder 研究
// http://www.ccrun.com
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(!InstallHook())
{
Label1->Caption = "Hook Error!";
}
MessageBoxA(NULL, "内容", "标题", MB_OK);
// 可以瞥见"内容酿成了"来自钩子中的内容"
if(!UninstallHook())
{
Label1->Caption = "Uninstall Error!";
}
}
//---------------------------------------------------------------------------