H
hkcbz
Unregistered / Unconfirmed
GUEST, unregistred user!
unit APIHook;<br><br>interface<br><br>uses<br> Windows, Classes;<br> <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> TImportCode = packed record //定义一个跳转的结构<br> JumpInstruction: Word; //定义跳转指令jmp<br> AddressOfPointerToFunction: ^Pointer; //定义要跳转到的函数<br> end;<br> PImportCode = ^TImportCode;<br><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> Result := func.AddressOfPointerToFunction^;<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: PChar;<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(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<br> 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><br>2<br>unit HookAPI;<br><br>interface<br><br>uses<br> Windows;<br><br>function SetHookAPI(TargetModule, TargetProcchar; NewProc: Pointer; var OldProc: Pointer): integer; stdcall;<br>function UnHookAPI(NewProc, OldProc: Pointer): integer; stdcall;<br><br>type<br> TModuleList = array of cardinal;<br><br> TImportFunction = packed record //定义一个跳转的结构<br> JumpInstruction: Word; //定义跳转指令jmp<br> AddressOfPointerToFunction: ^Pointer; //定义要跳转到的函数<br> end;<br> <br> TImageImportEntry = record //定义一个入口结构<br> Characteristics: dword;<br> TimeDateStamp: dword;<br> MajorVersion: word;<br> MinorVersion: word;<br> Name: dword;<br> LookupTable: dword;<br> end;<br><br>implementation<br><br>function GetModuleList: TModuleList; stdcall;<br>var<br> Module, Base: pointer;<br> ModuleCount: integer;<br> lpModuleName: array [0..MAX_PATH] of char;<br> MemoryBasicInformation: TMemoryBasicInformation;<br>begin //枚举模块列表 貌似用HelpAPI弄个快照效果差不多<br> SetLength(Result, 10);<br> ModuleCount := 0;<br> Module := nil;<br> Base := nil;<br> while VirtualQueryEx(GetCurrentProcess, Module, MemoryBasicInformation, SizeOf(MemoryBasicInformation)) = SizeOf(MemoryBasicInformation) do<br> begin<br> if (MemoryBasicInformation.State = MEM_COMMIT) and (MemoryBasicInformation.AllocationBase <> Base) and (MemoryBasicInformation.AllocationBase = MemoryBasicInformation.BaseAddress) and (GetModuleFileName(dword(MemoryBasicInformation.AllocationBase), lpModuleName, MAX_PATH) > 0) then<br> begin<br> if ModuleCount = Length(Result) then SetLength(Result, ModuleCount * 2);<br> Result[ModuleCount] := dword(MemoryBasicInformation.AllocationBase);<br> Inc(ModuleCount);<br> end;<br> Base := MemoryBasicInformation.AllocationBase;<br> dword(Module) := dword(Module) + MemoryBasicInformation.RegionSize;<br> end;<br> SetLength(Result, ModuleCount);<br>end;<br><br>function FunctionAddress(Code: Pointer): Pointer;stdcall;<br>begin //获得函数真实地址<br> Result := Code;<br> if TImportFunction(Code^).JumpInstruction = $25FF then<br> Result := TImportFunction(Code^).AddressOfPointerToFunction^;<br>end;<br><br>function HookModules(ImageDosHeader: PImageDosHeader; TargetAddress, NewAddress: Pointer; var OldAddress: Pointer):integer;stdcall;<br>var<br> ImageNTHeaders : PImageNtHeaders;<br> ImageImportEntry: ^TImageImportEntry;<br> ImportCode: ^Pointer;<br> OldProtect: dword;<br> EndofImports: dword;<br>begin //修改一个模块来HookAPI<br> Result := 0;<br> OldAddress := FunctionAddress(TargetAddress);<br> <br> if ImageDosHeader.e_magic <> IMAGE_DOS_SIGNATURE then Exit;<br> ImageNTHeaders := Pointer(integer(ImageDosHeader) + ImageDosHeader._lfanew);;<br> if ImageNTHeaders <> nil then<br> begin<br> with ImageNTHeaders^.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] do<br> begin<br> ImageImportEntry := Pointer(dword(ImageDosHeader) + VirtualAddress);<br> EndofImports := VirtualAddress + Size;<br> end;<br> if ImageImportEntry <> nil then<br> begin<br> while ImageImportEntry^.Name <> 0 do<br> begin<br> if ImageImportEntry^.LookUpTable > EndofImports then break;<br> if ImageImportEntry^.LookUpTable <> 0 then<br> begin<br> ImportCode := Pointer(dword(ImageDosHeader) + ImageImportEntry^.LookUpTable);<br> while ImportCode^ <> nil do<br> begin<br> if (ImportCode^ = TargetAddress) and VirtualProtect(ImportCode, 4, PAGE_EXECUTE_READWRITE, @OldProtect) then ImportCode^ := NewAddress;<br> Inc(ImportCode);<br> end;<br> end;<br> Inc(ImageImportEntry);<br> end;<br> end;<br> end;<br>end;<br><br>function SetHookAPI(TargetModule, TargetProcchar; NewProc: Pointer; var OldProc: Pointer): integer; stdcall;<br>var<br> ModuleLoop,i: integer;<br> Modules: TModuleList;<br> Module: hModule;<br> Target: pointer;<br>begin //实际上就是枚举模块完 一个一个Hook<br> Result := 0;<br> Module := GetModuleHandle(pchar(TargetModule));<br> Modules := GetModuleList;<br> if Module = 0 then<br> begin<br> Module := LoadLibrary(pchar(TargetModule));<br> end;<br> Target := GetProcAddress(Module, pchar(TargetProc));<br> if Target = nil then Exit;<br> i := High(Modules);<br> for ModuleLoop := 0 to i do<br> begin<br> if (GetVersion and $80000000 = 0) or (Modules[ModuleLoop] < $80000000) then<br> begin<br> Result := HookModules(Pointer(Modules[ModuleLoop]), Target, NewProc, OldProc);<br> end;<br> end;<br>end;<br><br>function UnHookAPI(NewProc, OldProc: Pointer): integer; stdcall;<br>var<br> ModuleLoop: integer;<br> Modules: TModuleList;<br>begin //反Hook一遍就是了<br> Result := 0;<br> Modules := GetModuleList;<br> for ModuleLoop := 0 to High(Modules) do<br> begin<br> if (GetVersion and $80000000 = 0) or (Modules[ModuleLoop] < $80000000) then<br> begin<br> Result := HookModules(Pointer(Modules[ModuleLoop]), NewProc, OldProc, NewProc);<br> end;<br> end;<br>end;<br><br>end.<br><br>请大家一起分析一下,上面两个APIHOOK代码哪个更高效一些,哪个更简洁一些