老大,还是不行,救命!
我按你的意思,把代码改成:
var btNewBytes: array[0..7] of byte = ($0B8, $0, $0, $40, $0, $0FF, $0E0, 0);
function PatchAddress(hProcess: Thandle; OldFunc, NewFunc: Pointer): Integer;
var BeenDone: TList;
function PatchAddressInModule(hModule: THandle; OldFunc, NewFunc: Pointer):
Integer;
const
SIZE = 4;
var Dos: PImageDosHeader; NT: PImageNTHeaders;
ImportDesc: PImage_Import_Entry; rva: DWORD;
Func: PPointer; DLL: string; f: Pointer; written: DWORD;
mbi_thunk: TMemoryBasicInformation;
dwOldProtect: DWORD;
nCount: DWORD;
Buf: array[0..3] of byte;
ptextout: pdword;
dwOldBytes: array[0..7] of byte;
begin
Result := 0;
if hModule < 32 then exit;
Dos := Pointer(hModule);
if BeenDone.IndexOf(Dos) >= 0 then exit;
BeenDone.Add(Dos);
//OldFunc := FinalFunctionAddress(OldFunc);
if IsBadReadPtr(Dos, SizeOf(TImageDosHeader)) then exit;
if Dos.e_magic <> IMAGE_DOS_SIGNATURE then exit;
NT := Pointer(Integer(Dos) + dos._lfanew);
// if IsBadReadPtr(NT,SizeOf(TImageNtHeaders)) then exit;
RVA :=
NT^.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
if RVA = 0 then exit;
ImportDesc := pointer(integer(Dos) + RVA);
while (ImportDesc^.Name <> 0) do begin
DLL := PChar(Integer(Dos) + ImportDesc^.Name);
PatchAddressInModule(GetModuleHandle(PChar(DLL)), OldFunc, NewFunc);
Func := Pointer(Integer(DOS) + ImportDesc.LookupTable);
while Func^ <> nil do begin
f := FinalFunctionAddress(Func^);
if f = FinalFunctionAddress(OldFunc) then begin
//WriteProcessMemory(GetCurrentProcess, Func, @NewFunc, 4, written);
VirtualQuery(Func, mbi_thunk,
sizeof(TMemoryBasicInformation));
{更改内存属性}
if not VirtualProtect(Func, SIZE,
//PAGE_EXECUTE_WRITECOPY or
PAGE_EXECUTE_READWRITE, mbi_thunk.Protect) then
log('VirtualProtectEx Error!')
else
log('VirtualProtectEx OK');
ptextout := pdword(OldFunc);
[red] ////////////////////这里/////////////////////[/red] ReadProcessMemory(hProcess, ptextout, @dwOldBytes[0], sizeof(DWORD) * 2, written);
pdword(@btNewBytes[1])^ := DWORD(@NewFunc);
if not WriteProcessMemory(hProcess, pointer(ptextout), @btNewBytes[0], sizeof(DWORD)*2, written)
{把新函数地址覆盖它}
then
log('Write memory error!')
else begin
log('Write memory OK!');
end;
if VirtualProtect(Func, SIZE, mbi_thunk.Protect,
dwOldProtect) then {恢复内存属性}
log('res:VirtualProtectEx OK!')
else
log('res:VirtualProtectEx error!');
if Written > 0 then
Inc(Result);
{log('Dll:'+dll+',Oldfun:'+
int2hex(integer(func),8)+
',Newfun:'+
int2hex(integer(@Newfunc),8));}
end;
Inc(Func);
end;
Inc(ImportDesc);
end;
end;
begin
if hProcess = 0 then exit;
BeenDone := TList.Create;
try
Result := PatchAddressInModule(GetModuleHandle(nil), OldFunc, NewFunc);
finally
BeenDone.Free;
end;
end;
而我原来的代码(在2000/XP里面可以)
function FinalFunctionAddress(Code: Pointer): Pointer;
var
func: PImportCode;
begin
Result := Code;
if Code = nil then exit;
try
func := code;
if (func.JumpInstruction = $25FF) then
{指令二进制码FF 25 汇编指令jmp [...]}
Func := func.AddressOfPointerToFunction^;
result := Func;
except
Result := nil;
end;
end;
function PatchAddress(hProcess: Thandle; OldFunc, NewFunc: Pointer): Integer;
var BeenDone: TList;
function PatchAddressInModule(hModule: THandle; OldFunc, NewFunc: Pointer):
Integer;
const
SIZE = 4;
var Dos: PImageDosHeader; NT: PImageNTHeaders;
ImportDesc: PImage_Import_Entry; rva: DWORD;
Func: PPointer; DLL: string; f: Pointer; written: DWORD;
mbi_thunk: TMemoryBasicInformation;
dwOldProtect: DWORD;
nCount: DWORD;
Buf: array[0..3] of byte;
begin
Result := 0;
if hModule < 32 then exit;
Dos := Pointer(hModule);
if BeenDone.IndexOf(Dos) >= 0 then exit;
BeenDone.Add(Dos);
//OldFunc := FinalFunctionAddress(OldFunc);
if IsBadReadPtr(Dos, SizeOf(TImageDosHeader)) then exit;
if Dos.e_magic <> IMAGE_DOS_SIGNATURE then exit;
NT := Pointer(Integer(Dos) + dos._lfanew);
// if IsBadReadPtr(NT,SizeOf(TImageNtHeaders)) then exit;
RVA :=
NT^.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
if RVA = 0 then exit;
ImportDesc := pointer(integer(Dos) + RVA);
while (ImportDesc^.Name <> 0) do begin
DLL := PChar(Integer(Dos) + ImportDesc^.Name);
PatchAddressInModule(GetModuleHandle(PChar(DLL)), OldFunc, NewFunc);
Func := Pointer(Integer(DOS) + ImportDesc.LookupTable);
while Func^ <> nil do begin
f := FinalFunctionAddress(Func^);
if f = OldFunc then begin
//WriteProcessMemory(GetCurrentProcess, Func, @NewFunc, 4, written);
VirtualQuery(Func, mbi_thunk,
sizeof(TMemoryBasicInformation));
{更改内存属性}
if not VirtualProtect(Func, SIZE,
//PAGE_EXECUTE_WRITECOPY or
PAGE_EXECUTE_READWRITE, mbi_thunk.Protect) then
log('VirtualProtectEx Error!')
else
log('VirtualProtectEx OK');
ReadProcessMemory(hProcess, Func, @Buf, SIZE, written);
if not WriteProcessMemory(hProcess, Func, @NewFunc, SIZE, written)
{把新函数地址覆盖它}
then
log('Write memory error!')
else begin
{if Comparetext('regedit.exe', ExtractName(GetModuleName(0))) <> 0 then
WriteProcessMemory(hProcess, Func, @Buf, SIZE, written);}
log('Write memory OK!');
end;
if VirtualProtect(Func, SIZE, mbi_thunk.Protect,
dwOldProtect) then {恢复内存属性}
log('res:VirtualProtectEx OK!')
else
log('res:VirtualProtectEx error!');
if Written > 0 then
Inc(Result);
{log('Dll:'+dll+',Oldfun:'+
int2hex(integer(func),8)+
',Newfun:'+
int2hex(integer(@Newfunc),8));}
end;
Inc(Func);
end;
Inc(ImportDesc);
end;
end;
begin
if hProcess = 0 then exit;
BeenDone := TList.Create;
try
Result := PatchAddressInModule(GetModuleHandle(nil), OldFunc, NewFunc);
finally
BeenDone.Free;
end;
end;
能帮我看看吗???