gpt4 book ai didi

delphi xe3 dll注入(inject)64位dll到64位进程不起作用

转载 作者:行者123 更新时间:2023-12-03 15:52:43 25 4
gpt4 key购买 nike

我正在使用此代码将我的 64 位 dll 注入(inject)到 Windows 7 64 位上的 64 位进程中,CreateRemoteThread 返回 200 但 dll 仍然没有注入(inject),我用另一个源测试了我的 dll,它工作正常,Process Explorer 显示我的代码不起作用,这段代码可能有什么问题,我使用的是delphi XE3,并且我已经在64位目标平台上编译了代码。

function InjectDLL(dwPID: DWORD; DLLPath: pwidechar): integer;
var
dwThreadID: Cardinal;
hProc, hThread, hKernel: NativeUInt;
BytesWritten: NativeUInt;
pRemoteBuffer, pLoadLibrary: Pointer;
begin
try
hProc := OpenProcess(PROCESS_ALL_ACCESS, False, dwPID);
if hProc = 0 then
begin
Result := 0;
Exit;
end;
pRemoteBuffer := VirtualAllocEx(hProc, nil, Length(DLLPath) + 1, MEM_COMMIT,
PAGE_READWRITE);
if pRemoteBuffer = nil then
begin
Result := 0;
Exit;
end;
if WriteProcessMemory(hProc, Pointer(pRemoteBuffer), lpvoid(DLLPath),
Length(DLLPath) + 1, BytesWritten) = False then
begin
Result := 0;
Exit;
end;
hKernel := GetModuleHandle(pwidechar('kernel32.dll'));
pLoadLibrary := (GetProcAddress(hKernel, pansichar('LoadLibraryA')));
hThread := CreateRemoteThread(hProc, Pointer(nil), 0, Pointer(pLoadLibrary),
Pointer(pRemoteBuffer), 0, dwThreadID);

WaitForSingleObject(hThread, INFINITE);
VirtualFreeEx(hProc, Pointer(pRemoteBuffer), Length(DLLPath) + 1,
MEM_RELEASE);
CloseHandle(hThread);
CloseHandle(hProc);
// ShowMessage(IntToStr(hThread)+' '+ inttostr(dwThreadID));
Result := 1;
except
on d: exception do
begin
end;
end;
end;

最佳答案

您正在调用 LoadLibraryA,但向其传递 UTF-16 编码数据。切换到 LoadLibraryW 或将模块名称转换为 ANSI。

我会做前者。除了切换到 LoadLibraryW 之外,您还需要复制整个缓冲区。通过将 Length(DLLPath) + 1 的两个实例替换为 SizeOf(Char)*(Length(DLLPath) + 1) 来实现这一点。

更多评论:

  • 使用PROCESS_ALL_ACCESS是过度的。您只需要PROCESS_CREATE_THREAD或PROCESS_QUERY_INFORMATION或PROCESS_VM_OPERATION或PROCESS_VM_WRITE或PROCESS_VM_READ
  • GetProcAddress(hKernel, pansichar('LoadLibraryA')) 中的 PAnsiChar 转换看起来错误。因为 'LoadLibraryA' 是 UTF-16 编码的。只需使用 GetProcAddress(hKernel, 'LoadLibraryA') 即可。或者'LoadLibraryW'(如果您沿着这条路线走)。
  • 使用 NativeUInt 作为句柄是错误的。实际上并不重要,但您应该使用 THandle
  • 使用 MEM_RELEASE 时,必须传递 0 作为大小参数。

将所有这些放在一起,代码应如下所示:

function InjectDLL(dwPID: DWORD; DLLPath: PWideChar): integer;
var
dwThreadID: Cardinal;
hProc, hThread, hKernel: THandle;
BytesToWrite, BytesWritten: SIZE_T;
pRemoteBuffer, pLoadLibrary: Pointer;
begin
hProc := OpenProcess(PROCESS_CREATE_THREAD or PROCESS_QUERY_INFORMATION or PROCESS_VM_OPERATION or PROCESS_VM_WRITE or PROCESS_VM_READ, False, dwPID);
if hProc = 0 then
exit(0);
try
BytesToWrite := SizeOf(WideChar)*(Length(DLLPath) + 1);
pRemoteBuffer := VirtualAllocEx(hProc, nil, BytesToWrite, MEM_COMMIT, PAGE_READWRITE);
if pRemoteBuffer = nil then
exit(0);
try
if not WriteProcessMemory(hProc, pRemoteBuffer, DLLPath, BytesToWrite, BytesWritten) then
exit(0);
hKernel := GetModuleHandle('kernel32.dll');
pLoadLibrary := GetProcAddress(hKernel, 'LoadLibraryW');
hThread := CreateRemoteThread(hProc, nil, 0, pLoadLibrary, pRemoteBuffer, 0, dwThreadID);
try
WaitForSingleObject(hThread, INFINITE);
finally
CloseHandle(hThread);
end;
finally
VirtualFreeEx(hProc, pRemoteBuffer, 0, MEM_RELEASE);
end;
finally
CloseHandle(hProc);
end;
exit(1);
end;

就个人而言,我可能会传递一个字符串而不是PWideChar,但也许您这样做还有其他动机。

关于delphi xe3 dll注入(inject)64位dll到64位进程不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12670668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com