同意 xusong168
监视的是函数!即createfile! 不用hook是不行的!
//////////////////////////////////
贴一个我自己整理的hook的函数库
////////////////////////////////////////////////////////////////////
umyapi.pas
unit umyapi;
interface
uses tlhelp32,psapi,windows,SysUtils;
type
tasmjump=packed record
mov_eax:BYTE;//0xB8
address
ointer;
jump_eax:WORD;//0xE0FF
end;
tdllinfo=packed record
hModule:HMODULE;
lpNewBaseOfDll
ointer;
modinfo:_MODULEINFO;
end;
var
jmpcode:tasmjump;
procedure hookapi(pDllInfo:tDLLINFO ;name
CHAR;hackfunc
dword; var pNewFunc
ointer);
procedure InitDll(var pdllinfo:tdllinfo;name
char);
implementation
procedure hookapi(pDllInfo:tDLLINFO ;name
CHAR;hackfunc
dword; var pNewFunc
ointer);
var
mbi:TMemoryBasicInformation;
dwOrigFunc
ointer;
dw
ointer;
tp:dword;
begin
dw:=nil;
dwOrigFunc :=GetProcAddress(pdllinfo.hModule,name);
VirtualQuery(dwOrigFunc,mbi,sizeof(TMemoryBasicInformation));
VirtualProtect(mbi.BaseAddress,mbi.RegionSize,PAGE_EXECUTE_READWRITE,dw);
jmpcode.mov_eax := byte($B8);
jmpcode.address :=hackfunc;
jmpcode.jump_eax :=word($E0FF);
tp:=dword(dwOrigFunc) - dword(pDllInfo.modinfo.lpBaseOfDll) + dword(pDllInfo.lpNewBaseOfDll);
pNewFunc :=pointer(tp);
CopyMemory(dwOrigFunc, @jmpcode,sizeof(tASMJUMP));//修改原函数入口处内容
end;
procedure InitDll(var pdllinfo:tdllinfo;name
char);
var
dw
ointer;
me:TMODULEENTRY32;
handle:thandle;
nofind:bool;
begin
dw:=nil;
pDllInfo.hModule := GetModuleHandle(name);
pDllInfo.modinfo.lpBaseOfDll:=pointer(getmodulehandle(name));
handle:=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,0);
nofind:=Module32First(handle,me);
while nofind do
begin
if me.hModule=getmodulehandle(name) then
begin
pDllInfo.modinfo.SizeOfImage:=me.modBaseSize;
break;
end else
nofind:=module32next(handle,me)
end;
getmem(
pDllInfo.lpNewBaseOfDll,
pDllInfo.modinfo.SizeOfImage);
VirtualProtect(pDllInfo.modinfo.lpBaseOfDll,pDllInfo.modinfo.SizeOfImage,PAGE_EXECUTE_READWRITE,dw);
VirtualProtect(pDllInfo.lpNewBaseOfDll,pDllInfo.modinfo.SizeOfImage,PAGE_EXECUTE_READWRITE,dw);
copyMemory(pDllInfo.lpNewBaseOfDll, pDllInfo.modinfo.lpBaseOfDll, pDllInfo.modinfo.SizeOfImage);
end;
end.
用法:
function hook后的函数(...)
begin
result:=原来的函数地址(...);
end;
var Fdll:tdllinfo;
initDll(Fdll,'acreatefile所在的dll);
hookapi(Fdll,'CreatefileA',@hook后的函数,@原来的函数地址);
把这个
dll注射到你要监视的程序中就行了
///////////////////////////////////////////////
再附上个注入的程序吧!配套的,o(∩_∩)o...
program inject2;
uses
windows,tlhelp32,SysUtils;
var
pfnThreadRtn:function(dr
char):LongWord;
function inject3(pid
WORD;dll
char):integer;
var
pszLibFileRemote
ointer;
hRemoteProcess,hRemoteThread:THANDLE;
CurPath:array[0..255]of char;
len:integer;
wCurPath
WCHAR;
dw:dword;
begin
pszLibFileRemote:=nil;
hRemoteProcess:=0;
hRemoteThread:=0;
hRemoteProcess := OpenProcess(
PROCESS_QUERY_INFORMATION or // Required by Alpha
PROCESS_CREATE_THREAD or // For CreateRemoteThread
PROCESS_VM_OPERATION or // For VirtualAllocEx/VirtualFreeEx
PROCESS_VM_WRITE, // For WriteProcessMemory
FALSE, pid);
fillchar(CurPath, 0, sizeof(CurPath));
strcat(CurPath, dll);
len :=(strlen(CurPath)+1)*2;
MultiByteToWideChar(CP_ACP,0,CurPath,-1,wCurPath,256);
pszLibFileRemote :=VirtualAllocEx(hRemoteProcess, nil, len, MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(hRemoteProcess,pszLibFileRemote,wCurPath,len, dw);
pfnThreadRtn :=GetProcAddress(GetModuleHandle('Kernel32.dll'), 'LoadLibraryW');
hRemoteThread := CreateRemoteThread(hRemoteProcess,nil,0,@pfnThreadRtn,pszLibFileRemote,0,dw);
end;
begin
inject3(916,'hooknow.dll');
///////////////////////////////////////////////////////////////