我想在文件打开(保存)之前获得文件的控制权(API hook) ( 积分: 300 )

  • 主题发起人 主题发起人 hcm0790
  • 开始时间 开始时间
H

hcm0790

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在文件打开(保存)之前获得文件的控制权,如xls或doc文件,希望当他们被打开时能够获得它们的文件名和内容,在对内容进行审核之后,再把控制权移交回相应的程式,如不能通过审核,则拒绝打开该文件。 时间紧,希望提供源码。分不够可再加,现金也行。
 
高人在哪呀
 
用ShellHook
 
可以用我写的 API Hook 控件,不需要任何相关知识.
 
白河愁: 谢谢你的回复, 请问如何取得你的API Hook控件, 有没使用说明, 若方便, 可发Email: hcm0790@163.com
 
白河愁: 谢谢你的回复, 请问如何取得你的API Hook控件, 有没使用说明, 若方便, 可发Email: hcm0790@163.com
 
我这里有一份API  HOOK的
 
(*&nbsp;-------------------------------------------&nbsp;*)<br>(*&nbsp;PermuteFunction功能&nbsp;:用&nbsp;NewFunc替代&nbsp;OldFunc&nbsp;*)<br>(*&nbsp;Windows&nbsp;Me&nbsp;+&nbsp;Delphi&nbsp;5.0&nbsp;*)<br>(*&nbsp;------------------------------------------&nbsp;*)<br>unit&nbsp;HookAPI;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;&nbsp;Windows,&nbsp;Classes&nbsp;;<br><br><br>type<br>&nbsp;&nbsp;TImportCode&nbsp;=&nbsp;packed&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;JumpInstruction:&nbsp;Word;<br>&nbsp;&nbsp;&nbsp;&nbsp;AddressOfPointerToFunction:&nbsp;^Pointer;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;PImportCode&nbsp;=&nbsp;^TImportCode;<br><br>type<br>&nbsp;&nbsp;&nbsp;PImage_Import_Entry&nbsp;=&nbsp;^Image_Import_Entry;<br>&nbsp;&nbsp;&nbsp;Image_Import_Entry&nbsp;=&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Characteristics&nbsp;:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeDateStamp&nbsp;:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MajorVersion&nbsp;:&nbsp;Word;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MinorVersion&nbsp;:&nbsp;Word;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LookupTable&nbsp;:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;Function&nbsp;TrueFunctionAddress(Code:&nbsp;Pointer):&nbsp;Pointer;<br>&nbsp;&nbsp;&nbsp;Function&nbsp;PermuteFunction(OldFunc,&nbsp;NewFunc:&nbsp;Pointer):&nbsp;Integer;<br><br>implementation<br><br><br>function&nbsp;TrueFunctionAddress(Code:&nbsp;Pointer):&nbsp;Pointer;<br>var&nbsp;func:&nbsp;PImportCode;<br>begin<br>Result&nbsp;:=&nbsp;Code;<br>if&nbsp;Code&nbsp;=&nbsp;nil&nbsp;then&nbsp;exit;<br>try<br>func&nbsp;:=&nbsp;code;<br>if&nbsp;(func.JumpInstruction=$25FF)&nbsp;then&nbsp;begin<br>Result&nbsp;:=&nbsp;func.AddressOfPointerToFunction^;<br>end;<br>except<br>Result&nbsp;:=&nbsp;nil;<br>end;<br>end;<br><br>Function&nbsp;PermuteFunction(OldFunc,&nbsp;NewFunc:&nbsp;Pointer):&nbsp;Integer;<br>var&nbsp;IsDone:&nbsp;TList;<br>Function&nbsp;PermuteAddrInModule(hModule:&nbsp;THandle;&nbsp;OldFunc,&nbsp;NewFunc:&nbsp;Pointer):&nbsp;Integer;<br>var<br>Dos&nbsp;:&nbsp;PImageDosHeader;<br>NT&nbsp;:&nbsp;PImageNTHeaders;<br>ImportDesc&nbsp;:&nbsp;PImage_Import_Entry;<br>RVA&nbsp;:&nbsp;DWORD;<br>Func&nbsp;:&nbsp;^Pointer;<br>DLL&nbsp;:&nbsp;String;<br>f&nbsp;:&nbsp;Pointer;<br>written&nbsp;:&nbsp;DWORD;<br>begin<br>Result&nbsp;:=&nbsp;0;<br>Dos&nbsp;:=&nbsp;Pointer(hModule);<br>if&nbsp;IsDone.IndexOf(Dos)&nbsp;&gt;=&nbsp;0&nbsp;then&nbsp;exit;<br>IsDone.Add(Dos);<br>OldFunc&nbsp;:=&nbsp;TrueFunctionAddress(OldFunc);<br>if&nbsp;IsBadReadPtr(Dos,SizeOf(TImageDosHeader))&nbsp;then&nbsp;exit;<br>if&nbsp;Dos.e_magic&nbsp;&lt;&gt;&nbsp;IMAGE_DOS_SIGNATURE&nbsp;then&nbsp;exit;<br>NT&nbsp;:=&nbsp;Pointer(Integer(Dos)&nbsp;+&nbsp;dos._lfanew);<br>RVA&nbsp;:=&nbsp;NT^.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;<br><br>if&nbsp;RVA&nbsp;=&nbsp;0&nbsp;then&nbsp;exit;<br>ImportDesc&nbsp;:=&nbsp;pointer(integer(Dos)+RVA);<br>While(ImportDesc^.Name&lt;&gt;0)&nbsp;do<br>begin<br>DLL&nbsp;:=&nbsp;PChar(Integer(Dos)&nbsp;+&nbsp;ImportDesc^.Name);<br>PermuteAddrInModule(GetModuleHandle(PChar(DLL)),OldFunc,NewFunc);<br>Func&nbsp;:=&nbsp;Pointer(Integer(DOS)&nbsp;+&nbsp;ImportDesc.LookupTable);<br>While&nbsp;Func^&nbsp;&lt;&gt;&nbsp;nil&nbsp;do<br>begin<br>f&nbsp;:=&nbsp;TrueFunctionAddress(Func^);<br>if&nbsp;f&nbsp;=&nbsp;OldFunc&nbsp;then<br>begin<br>WriteProcessMemory(GetCurrentProcess,Func,@NewFunc,4,written);<br>If&nbsp;Written&nbsp;&gt;&nbsp;0&nbsp;then&nbsp;Inc(Result);<br>end;<br>Inc(Func);<br>end;<br>Inc(ImportDesc);<br>end;<br>end;<br><br>begin<br>IsDone&nbsp;:=&nbsp;TList.Create;<br>try<br>Result&nbsp;:=&nbsp;PermuteAddrInModule(GetModuleHandle(nil),OldFunc,NewFunc);<br>finally<br>IsDone.Free;<br>end;<br>end;<br>end.
 
我做过hook&nbsp;createfile的<br>给你参考下&nbsp;&nbsp;希望可以帮到你
 
unit&nbsp;mess;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,Messages,SysUtils,Classes,HookAPI;<br><br>&nbsp;&nbsp;procedure&nbsp;API_Hookup;<br>&nbsp;&nbsp;procedure&nbsp;Un_API_Hook;<br><br>var<br>&nbsp;&nbsp;FuncMessageboxA,&nbsp;FuncMessageboxW:&nbsp;PImportCode;<br><br>implementation<br><br>type<br>&nbsp;&nbsp;TMessageA&nbsp;=&nbsp;function(hwn:&nbsp;hwnd;&nbsp;lptext:&nbsp;pchar;&nbsp;lpcapion:&nbsp;pchar;&nbsp;utype:&nbsp;cardinal):&nbsp;integer;&nbsp;stdcall;<br>&nbsp;&nbsp;TMessageW&nbsp;=&nbsp;function(hwn:&nbsp;hwnd;&nbsp;lptext:&nbsp;pwidechar;&nbsp;lpcapion:&nbsp;pwidechar;&nbsp;utype:&nbsp;cardinal):&nbsp;integer;&nbsp;stdcall;<br><br>&nbsp;&nbsp;TCreateFile&nbsp;=&nbsp;function(lpFileName:&nbsp;PChar;&nbsp;dwDesiredAccess,&nbsp;dwShareMode:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpSecurityAttributes:&nbsp;PSecurityAttributes;&nbsp;dwCreationDisposition,&nbsp;dwFlagsAndAttributes:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hTemplateFile:&nbsp;THandle):&nbsp;THandle;&nbsp;stdcall;<br>&nbsp;&nbsp;TCreateFileA&nbsp;=&nbsp;function(lpFileName:&nbsp;PAnsiChar;&nbsp;dwDesiredAccess,&nbsp;dwShareMode:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpSecurityAttributes:&nbsp;PSecurityAttributes;&nbsp;dwCreationDisposition,&nbsp;dwFlagsAndAttributes:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hTemplateFile:&nbsp;THandle):&nbsp;THandle;&nbsp;stdcall;<br>&nbsp;&nbsp;TCreateFileW&nbsp;=&nbsp;function(lpFileName:&nbsp;PWideChar;&nbsp;dwDesiredAccess,&nbsp;dwShareMode:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpSecurityAttributes:&nbsp;PSecurityAttributes;&nbsp;dwCreationDisposition,&nbsp;dwFlagsAndAttributes:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hTemplateFile:&nbsp;THandle):&nbsp;THandle;&nbsp;stdcall;<br><br>var<br>&nbsp;&nbsp;OldMessageBoxA:&nbsp;TMessageA;<br>&nbsp;&nbsp;OldMessageBoxW:&nbsp;TMessageW;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OldCreateFile:&nbsp;TCreateFile;<br>&nbsp;&nbsp;&nbsp;OldCreateFileA:&nbsp;TCreateFileA;<br>&nbsp;&nbsp;&nbsp;OldCreateFileW:&nbsp;TCreateFileW;<br><br>function&nbsp;MyBoxA(hwn:hwnd;lptext:pchar;lpcapion:pchar;utype:cardinal):&nbsp;integer;&nbsp;stdcall;<br>begin<br>&nbsp;&nbsp;result&nbsp;:=&nbsp;OldMessageBoxA(hwn,&nbsp;'Succes&nbsp;Hook&nbsp;A&nbsp;!',&nbsp;lpcapion,&nbsp;utype);<br>end;<br><br>function&nbsp;MyBoxw(hwn:hwnd;lptext:pwidechar;lpcapion:pwidechar;utype:cardinal):&nbsp;integer;&nbsp;stdcall;<br>begin<br>&nbsp;&nbsp;result&nbsp;:=&nbsp;OldMessageBoxW(hwn,&nbsp;'成功挂上W!',&nbsp;lpcapion,&nbsp;utype);<br>end;<br><br>function&nbsp;MyCreateFile(lpFileName:&nbsp;PChar;&nbsp;dwDesiredAccess,&nbsp;dwShareMode:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpSecurityAttributes:&nbsp;PSecurityAttributes;&nbsp;dwCreationDisposition,&nbsp;dwFlagsAndAttributes:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hTemplateFile:&nbsp;THandle):&nbsp;THandle;&nbsp;stdcall;<br>var<br>&nbsp;&nbsp;F:TextFile;<br>begin<br>&nbsp;&nbsp;&nbsp;oldCreateFile('c:/chenyi.txt',dwDesiredAccess,&nbsp;dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);<br>AssignFile(F,'c:/record.txt');<br>//writeln(f,'dads');<br>CloseFile(F);<br>&nbsp;&nbsp;&nbsp;result:=oldCreateFile(lpFileName,dwDesiredAccess,&nbsp;dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);<br>end;<br><br>function&nbsp;MyCreateFileA(lpFileName:&nbsp;PAnsiChar;&nbsp;dwDesiredAccess,&nbsp;dwShareMode:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpSecurityAttributes:&nbsp;PSecurityAttributes;&nbsp;dwCreationDisposition,&nbsp;dwFlagsAndAttributes:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hTemplateFile:&nbsp;THandle):&nbsp;THandle;&nbsp;stdcall;<br>var<br>&nbsp;&nbsp;F:TextFile;<br>begin<br>oldCreateFile('c:/chenyi.txt',dwDesiredAccess,&nbsp;dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);<br>AssignFile(F,'c:/chenyi.txt');<br>Writeln(f,lpFileName);<br>&nbsp;&nbsp;&nbsp;result:=oldCreateFileA(lpFileName,dwDesiredAccess,&nbsp;dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);<br>end;<br><br>function&nbsp;MyCreateFileW(lpFileName:&nbsp;PWideChar;&nbsp;dwDesiredAccess,&nbsp;dwShareMode:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpSecurityAttributes:&nbsp;PSecurityAttributes;&nbsp;dwCreationDisposition,&nbsp;dwFlagsAndAttributes:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hTemplateFile:&nbsp;THandle):&nbsp;THandle;&nbsp;stdcall;<br>begin<br>oldCreateFile('c:/chenyi.txt',dwDesiredAccess,&nbsp;dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);<br>&nbsp;&nbsp;&nbsp;result:=oldCreateFilew(lpFileName,dwDesiredAccess,&nbsp;dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);<br>end;<br><br><br>procedure&nbsp;API_Hookup;<br>begin<br>&nbsp;&nbsp;if&nbsp;@OldMessageBoxA&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@OldMessageBoxA&nbsp;:=&nbsp;TrueFunctionAddress(@messageboxA);<br>&nbsp;&nbsp;if&nbsp;@OldMessageBoxW&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@OldMessageBoxW&nbsp;:=&nbsp;TrueFunctionAddress(@messageboxW);<br><br>&nbsp;&nbsp;PermuteFunction(@OldMessageBoxA,&nbsp;@MyBoxA);<br>&nbsp;&nbsp;PermuteFunction(@OldMessageBoxW,&nbsp;@MyBoxW);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;@OldCreateFile&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@OldCreateFile&nbsp;:=&nbsp;TrueFunctionAddress(@CreateFile);<br>&nbsp;&nbsp;&nbsp;if&nbsp;@OldCreateFileA&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@OldCreateFileA&nbsp;:=&nbsp;TrueFunctionAddress(@CreateFileA);<br>&nbsp;&nbsp;&nbsp;if&nbsp;@OldCreateFileW&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@OldCreateFileW&nbsp;:=&nbsp;TrueFunctionAddress(@CreateFileW);<br><br>&nbsp;&nbsp;&nbsp;PermuteFunction(@OldCreateFile,&nbsp;@MyCreateFile);<br>&nbsp;&nbsp;&nbsp;PermuteFunction(@OldCreateFileA,&nbsp;@MyCreateFileA);<br>&nbsp;&nbsp;&nbsp;PermuteFunction(@OldCreateFileW,&nbsp;@MyCreateFileW);<br><br>end;<br><br>procedure&nbsp;Un_API_hook;<br>begin<br>&nbsp;&nbsp;If&nbsp;@OldMessageBoxA&nbsp;&lt;&gt;&nbsp;nil&nbsp;then&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PermuteFunction(@MyBoxA,&nbsp;@OldMessageboxA);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PermuteFunction(@MyBoxW,&nbsp;@OldMessageboxW);<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;@OldCreateFile&nbsp;&lt;&gt;&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PermuteFunction(@MyCreateFile,&nbsp;@OldCreateFile);<br>&nbsp;&nbsp;&nbsp;if&nbsp;@OldCreateFileA&nbsp;&lt;&gt;&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PermuteFunction(@MyCreateFileA,&nbsp;@OldCreateFileA);<br>&nbsp;&nbsp;&nbsp;if&nbsp;@OldCreateFileW&nbsp;&lt;&gt;&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PermuteFunction(@MyCreateFileW,&nbsp;@OldCreateFileW);<br>end;<br><br>initialization<br>&nbsp;&nbsp;FuncMessageboxA&nbsp;:=&nbsp;@MessageboxA;<br>&nbsp;&nbsp;FuncMessageboxW&nbsp;:=&nbsp;@MessageboxW;<br><br>finalization<br>&nbsp;&nbsp;Un_API_hook;<br><br>end.
 
我还有个API&nbsp;HOOK的类&nbsp;&nbsp;需要的话我发给你
 
暗夜中独舞:謝謝你的熱情幫助,&nbsp;請把相關資料發到&nbsp;hcm0790@163.com
 
后退
顶部