我想试着拦截文件读写等函数来达到控制,程序对磁盘的访问,在网上找了个MessageBox的
例子,我改了一下用CreateFile做实验,在程序内可行,不过出错了;而其它程序调用CreateFile
时则一点反应没有,急阿!走过的路过的帮支持一把,
谢谢了大哥。。
解决了再开帖,送分。HHOOK g_hHook;
HINSTANCE g_hinstDll;
FARPROC pfCreateFileA;
BYTE OldCreateFileACode[5], NewCreateFileACode[5];
HANDLE WINAPI MyCreateFileA(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile );
HMODULE hModule ;
DWORD dwIdOld,dwIdNew;
BOOL bHook=false;
void HookOn();
void HookOff();
BOOL init();
HWND g_hWend;
DWORD g_uiID;BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if (false == init())
MessageBox(NULL,"Install failed!",0,0);
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is the constructor of a class that has been exported.
// see dll.h for the class definition
CDll::CDll()
{
return;
}
LRESULT WINAPI Hook(int nCode,WPARAM wParam,LPARAM lParam)//空的钩子函数
{
return(CallNextHookEx(g_hHook,nCode,wParam,lParam));
}
BOOL WINAPI InstallHook(HWND hwnd,DWORD id)//输出安装空的钩子函数HOOKAPI2_API
{
g_hWend = hwnd;
g_uiID = id;
g_hinstDll=LoadLibrary("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);
}
BOOL WINAPI UninstallHook()//输出御在钩子函数HOOKAPI2_API
{
HookOff();
BOOL ret = (UnhookWindowsHookEx(g_hHook));
if (ret)
MessageBox(NULL,"Finish!",0,0);
return ret;
}BOOL init()//初始化得到MessageBoxA的地址,并生成Jmp XXX(MyMessageBoxA)的跳转指令
{
hModule=LoadLibrary("Kernel32.dll");
pfCreateFileA=GetProcAddress(hModule,"CreateFileA");
if(pfCreateFileA==NULL)
return false;
_asm
{
lea edi,OldCreateFileACode
mov esi,pfCreateFileA
cld
movsd
movsb
}
NewCreateFileACode[0]=0xe9;//jmp MyMessageBoxA的相对地址的指令
_asm
{
lea eax,MyCreateFileA
mov ebx,pfCreateFileA
sub eax,ebx
sub eax,5
mov dword ptr [NewCreateFileACode+1],eax
}
dwIdNew=GetCurrentProcessId(); //得到所属进程的ID
dwIdOld=dwIdNew;
HookOn();//开始拦截
return(true);
}
void HookOn()
{
if (!bHook)
{
HANDLE hProc;
dwIdOld=dwIdNew;
hProc=OpenProcess(PROCESS_ALL_ACCESS,0,dwIdOld);//得到所属进程的句柄
VirtualProtectEx(hProc,pfCreateFileA,5,PAGE_READWRITE,&dwIdOld);//修改所属进程中MessageBoxA的前5个字节的属性为可写
WriteProcessMemory(hProc,pfCreateFileA,NewCreateFileACode,5,0);//将所属进程中MessageBoxA的前5个字节改为JMP 到MyMessageBoxA
VirtualProtectEx(hProc,pfCreateFileA,5,dwIdOld,&dwIdOld);//修改所属进程中MessageBoxA的前5个字节的属性为原来的属性
bHook=true;
}
}
void HookOff()//将所属进程中JMP MyMessageBoxA的代码改为Jmp MessageBoxA
{
if (bHook)
{
HANDLE hProc;
dwIdOld=dwIdNew;
hProc=OpenProcess(PROCESS_ALL_ACCESS,0,dwIdOld);
VirtualProtectEx(hProc,pfCreateFileA,5,PAGE_READWRITE,&dwIdOld);
WriteProcessMemory(hProc,pfCreateFileA,OldCreateFileACode,5,0);
VirtualProtectEx(hProc,pfCreateFileA,5,dwIdOld,&dwIdOld);
bHook=false;
}
}
HANDLE WINAPI MyCreateFileA(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile )
{
HANDLE ret;
HookOff();
SetDlgItemText(g_hWend,g_uiID, (char *)lpFileName);
ret = CreateFileA((const char*)lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
// if (ret)
// SetDlgItemText(g_hWend,g_uiID, (char *)lpFileName);
HookOn(); return ret;
}
例子,我改了一下用CreateFile做实验,在程序内可行,不过出错了;而其它程序调用CreateFile
时则一点反应没有,急阿!走过的路过的帮支持一把,
谢谢了大哥。。
解决了再开帖,送分。HHOOK g_hHook;
HINSTANCE g_hinstDll;
FARPROC pfCreateFileA;
BYTE OldCreateFileACode[5], NewCreateFileACode[5];
HANDLE WINAPI MyCreateFileA(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile );
HMODULE hModule ;
DWORD dwIdOld,dwIdNew;
BOOL bHook=false;
void HookOn();
void HookOff();
BOOL init();
HWND g_hWend;
DWORD g_uiID;BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if (false == init())
MessageBox(NULL,"Install failed!",0,0);
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is the constructor of a class that has been exported.
// see dll.h for the class definition
CDll::CDll()
{
return;
}
LRESULT WINAPI Hook(int nCode,WPARAM wParam,LPARAM lParam)//空的钩子函数
{
return(CallNextHookEx(g_hHook,nCode,wParam,lParam));
}
BOOL WINAPI InstallHook(HWND hwnd,DWORD id)//输出安装空的钩子函数HOOKAPI2_API
{
g_hWend = hwnd;
g_uiID = id;
g_hinstDll=LoadLibrary("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);
}
BOOL WINAPI UninstallHook()//输出御在钩子函数HOOKAPI2_API
{
HookOff();
BOOL ret = (UnhookWindowsHookEx(g_hHook));
if (ret)
MessageBox(NULL,"Finish!",0,0);
return ret;
}BOOL init()//初始化得到MessageBoxA的地址,并生成Jmp XXX(MyMessageBoxA)的跳转指令
{
hModule=LoadLibrary("Kernel32.dll");
pfCreateFileA=GetProcAddress(hModule,"CreateFileA");
if(pfCreateFileA==NULL)
return false;
_asm
{
lea edi,OldCreateFileACode
mov esi,pfCreateFileA
cld
movsd
movsb
}
NewCreateFileACode[0]=0xe9;//jmp MyMessageBoxA的相对地址的指令
_asm
{
lea eax,MyCreateFileA
mov ebx,pfCreateFileA
sub eax,ebx
sub eax,5
mov dword ptr [NewCreateFileACode+1],eax
}
dwIdNew=GetCurrentProcessId(); //得到所属进程的ID
dwIdOld=dwIdNew;
HookOn();//开始拦截
return(true);
}
void HookOn()
{
if (!bHook)
{
HANDLE hProc;
dwIdOld=dwIdNew;
hProc=OpenProcess(PROCESS_ALL_ACCESS,0,dwIdOld);//得到所属进程的句柄
VirtualProtectEx(hProc,pfCreateFileA,5,PAGE_READWRITE,&dwIdOld);//修改所属进程中MessageBoxA的前5个字节的属性为可写
WriteProcessMemory(hProc,pfCreateFileA,NewCreateFileACode,5,0);//将所属进程中MessageBoxA的前5个字节改为JMP 到MyMessageBoxA
VirtualProtectEx(hProc,pfCreateFileA,5,dwIdOld,&dwIdOld);//修改所属进程中MessageBoxA的前5个字节的属性为原来的属性
bHook=true;
}
}
void HookOff()//将所属进程中JMP MyMessageBoxA的代码改为Jmp MessageBoxA
{
if (bHook)
{
HANDLE hProc;
dwIdOld=dwIdNew;
hProc=OpenProcess(PROCESS_ALL_ACCESS,0,dwIdOld);
VirtualProtectEx(hProc,pfCreateFileA,5,PAGE_READWRITE,&dwIdOld);
WriteProcessMemory(hProc,pfCreateFileA,OldCreateFileACode,5,0);
VirtualProtectEx(hProc,pfCreateFileA,5,dwIdOld,&dwIdOld);
bHook=false;
}
}
HANDLE WINAPI MyCreateFileA(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile )
{
HANDLE ret;
HookOff();
SetDlgItemText(g_hWend,g_uiID, (char *)lpFileName);
ret = CreateFileA((const char*)lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
// if (ret)
// SetDlgItemText(g_hWend,g_uiID, (char *)lpFileName);
HookOn(); return ret;
}
CreateFile为什么不行?有A和W两个版本吗?
原因是第一条。
现在又出了点问题,我想拦截文件读写函数达到对硬盘,U盘等进行文件的监控(如果用API
函数必须要打开某盘的句柄不能完全监控,而且不能退出U盘),现在又出了一个问题,
我拦住了这些读写函数但是,看起来,没完全拦下,有的软件对盘的读写没反应,比如说,压缩一个文件,
拦WriteFile函数就没反应,我想再问一下,有更好的方法么?或者是我拦的函数不对头????