急!求教Hook文件操作的问题!

neucaibai 2010-04-21 08:44:59
这是小弟按照detours帮助文档写的截获系统DeleteFile操作的代码
在另外一个程序里LoadLibrary()和FreeLibrary()
编译,链接都正常,并且Attach和Detach都成功,但就是无法截获系统的DeleteFile操作,删除文件就和正常一样
请问是什么原因呢?

#include <windows.h>
#include <detours.h>

BOOL (WINAPI * SysDeleteFileA)(LPCTSTR lpFileName)= DeleteFile;
BOOL WINAPI MyHookDeleteFileA(LPCTSTR lpFileName);

//如果不注释掉编译总显示error C2440: 'initializing' : cannot convert from '' to 'int (__stdcall *)(const char //*)'的错误,不知为什么?谁能解一下?
//BOOL (WINAPI * SysDeleteFileW)(LPCTSTR lpFileName)= DeleteFileW;
//BOOL WINAPI MyHookDeleteFileW(LPCTSTR lpFileName);

__declspec(dllexport) void ExportFunc(void)
{
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)SysDeleteFileA,MyHookDeleteFileA);
if(DetourTransactionCommit()==NO_ERROR)
{
MessageBox(NULL,"Attach Successfully!","Successful",MB_OK);
}

// DetourTransactionBegin();
// DetourUpdateThread(GetCurrentThread());
// DetourAttach(&(PVOID&)SysDeleteFileW,MyHookDeleteFileW);

break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)SysDeleteFileA, MyHookDeleteFileA);
if(DetourTransactionCommit()==NO_ERROR)
{
MessageBox(NULL,"Detach Successfully!","Successful",MB_OK);
}

break;

}

return true;
}

BOOL WINAPI MyHookDeleteFileA(LPCTSTR lpFileName)
{
MessageBox(NULL,"You Can Not Delete This File!","ERROR",MB_OK);
return true;
}

/*BOOL WINAPI MyHookDeleteFileW(LPCTSTR lpFileName)
{
return true;
}*/


再弱弱的问一下~谁有HOOK API比较详细的教程?给小弟发一套吧?谢了!
...全文
321 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
kalony 2010-12-21
  • 打赏
  • 举报
回复
DeleteFileW和DeleteFileA的参数类型是不一样的,请注意这一点。
tMinix 2010-06-15
  • 打赏
  • 举报
回复
我最近也在用Detours库做hook程序,lz貌似你的dll中attach处完全可以这样写:(ps:我一直是这样写的而且没有问题:))
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)SysDeleteFileW,MyHookDeleteFileW);
DetourAttach(&(PVOID&)SysMoveFileExW,MyHookMoveFileExW);
DetourAttach(&(PVOID&)SysCreateFileW,MyHookCreateFileW);

我在hook IsBadReadPtr这个api的时候也遇到了lz的问题:
error C2440: 'initializing' : cannot convert from '' to 'int (__stdcall *)(const void *,unsigned long)'

我是这么写的:
BOOL (_stdcall *SysIsBadReadPtr)(
CONST VOID *lp, // memory address
UINT_PTR ucb // size of block
)=IsBadReadPtr;

这样写为什么错啦???麻烦大家帮忙看看,谢谢啦~~~
testing2007 2010-05-26
  • 打赏
  • 举报
回复
我最近在写一个文件监控的程序,能不能给我也发一下啊,weizhiqiang.wzq@gmail.com, 谢谢先!请问neucaibai 有什么发现啊。
neucaibai 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lisunlin0 的回复:]

我有个hook CreateProcessW的代码,你邮箱多少?发给你看看吧
[/Quote]yztcaibai8869@163.com 太感谢了!
sunlin7 2010-04-23
  • 打赏
  • 举报
回复
我有个hook CreateProcessW的代码,你邮箱多少?发给你看看吧
finder_zhang 2010-04-23
  • 打赏
  • 举报
回复
楼主,你5楼的问题,你HOOK的是 xxxxW 的函数,参数却把TCHAR解释写成了 char ,报的就是这个错.
如果有 xxxxA xxxxW 2个函数,系统最后一定会调用 xxxxW ,而参数,是 unsigned short ,不是char

在指定了 xxxxW 的情况,就最好不要用UNICODE与ASCII兼容的宏,直接用UNICODE的.否则换了环境又会出错.
fangchao918628 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cnzdgs 的回复:]
你的DLL注入到目标进程了吗?
你先测试Hook自己的进程,程序调用DeleteFileW看能否Hook到。
[/Quote]
顶...
如果要拦截所有的删除得全注入...而且对于一些进程得提升权限才能注入成功
cnzdgs 2010-04-23
  • 打赏
  • 举报
回复
你的DLL注入到目标进程了吗?
你先测试Hook自己的进程,程序调用DeleteFileW看能否Hook到。
sunlin7 2010-04-22
  • 打赏
  • 举报
回复
应该hook掉DeleteFileW和MoveFileExW这两个函数,因为有些程序会把文件“移动”到回收站里面。
jingzhongrong 2010-04-22
  • 打赏
  • 举报
回复
修改工程设置为Unicode字符集。
cdsnpeter 2010-04-22
  • 打赏
  • 举报
回复
需要改你的Project Setting,修改字符集。
neucaibai 2010-04-22
  • 打赏
  • 举报
回复
我试过截获W函数,但
//如果不注释掉编译总显示error C2440: 'initializing' : cannot convert from '' to 'int (__stdcall *)(const char //*)'的错误,不知为什么?谁能解一下?
//BOOL (WINAPI * SysDeleteFileW)(LPCTSTR lpFileName)= DeleteFileW;
//BOOL WINAPI MyHookDeleteFileW(LPCTSTR lpFileName);
尹成 2010-04-22
  • 打赏
  • 举报
回复
你应该拦截DeleteFileW
mcaok 2010-04-22
  • 打赏
  • 举报
回复
DeleteFileW现在哪还有用DeleteFileA的了。
小生我怕怕 2010-04-22
  • 打赏
  • 举报
回复
这个问题有点难,
我还不会~
友情帮顶~
neucaibai 2010-04-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lisunlin0 的回复:]

应该hook掉DeleteFileW和MoveFileExW这两个函数,因为有些程序会把文件“移动”到回收站里面。
[/Quote]
按照各位的说明改成这样,编译可以通过了,ATTACH和DETACH也成功了,但还是截获不了~我就直接编一个DeleteFile()的程序也照删不误~,小弟迷茫了~

#include <windows.h>
#include <detours.h>
#include <tchar.h>

//BOOL (WINAPI * SysDeleteFileA)(LPCTSTR lpFileName)= DeleteFile;
//BOOL WINAPI MyHookDeleteFileA(LPCTSTR lpFileName);

BOOL (WINAPI * SysDeleteFileW)(LPCTSTR lpFileName)= DeleteFileW;
BOOL WINAPI MyHookDeleteFileW(LPCTSTR lpFileName);

BOOL (WINAPI *SysMoveFileExW)(LPCTSTR lpExistingFileName,LPCTSTR lpNewFileName,DWORD dwFlags)=MoveFileExW;
BOOL WINAPI MyHookMoveFileExW(LPCTSTR lpExistingFileName,LPCTSTR lpNewFileName,DWORD dwFlags);

HANDLE (WINAPI *SysCreateFileW)(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
// pointer to security attributes
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to
// copy
)=CreateFileW;

HANDLE MyHookCreateFileW(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
// pointer to security attributes
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to
// copy
);

__declspec(dllexport) void ExportFunc(void)
{
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)SysDeleteFileW,MyHookDeleteFileW);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)SysMoveFileExW,MyHookMoveFileExW);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)SysCreateFileW,MyHookCreateFileW);

if(DetourTransactionCommit()==NO_ERROR)
{
MessageBox(NULL,_T("Attach Successfully!"),_T("Successful"),MB_OK);
}

// DetourTransactionBegin();
// DetourUpdateThread(GetCurrentThread());
// DetourAttach(&(PVOID&)SysDeleteFileW,MyHookDeleteFileW);

break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)SysDeleteFileW, MyHookDeleteFileW);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)SysMoveFileExW, MyHookMoveFileExW);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)SysCreateFileW, MyHookCreateFileW);

if(DetourTransactionCommit()==NO_ERROR)
{
MessageBox(NULL,_T("Detach Successfully!"),_T("Successful"),MB_OK);
}

break;

}

return true;
}

BOOL WINAPI MyHookDeleteFileW(LPCTSTR lpFileName)
{
MessageBox(NULL,_T("You Can Not Delete This File!"),_T("ERROR"),MB_OK);
return true;
}

BOOL WINAPI MyHookMoveFileExW(LPCTSTR lpExistingFileName,LPCTSTR lpNewFileName,DWORD dwFlags)
{
MessageBox(NULL,_T("You Can Not Move This File!"),_T("ERROR"),MB_OK);
return true;
}


HANDLE MyHookCreateFileW(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
// pointer to security attributes
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to
// copy
){
MessageBox(NULL,_T("You Can Not Create File!"),_T("ERROR"),MB_OK);
return NULL;

}
cnzdgs 2010-04-21
  • 打赏
  • 举报
回复
Hook DeleteFileW
SiGoYi 2010-04-21
  • 打赏
  • 举报
回复
up~~~~~~~~~~~~~~~~~~~~~~

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧