gpt4 book ai didi

c++ - Hook sendto() 会导致崩溃,具体取决于代码顺序

转载 作者:可可西里 更新时间:2023-11-01 09:26:08 26 4
gpt4 key购买 nike

我正在使用 DLL 注入(inject)器注入(inject)一个 dll,它进入 IAT 并用我自己的替换系统调用 sendto()。

这是替换方法。

void replaceFunction(DWORD f, DWORD nf)
{
// Base address.
HMODULE hMod = GetModuleHandle(NULL);

// DOS Header.
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)hMod;

// NT Header.
PIMAGE_NT_HEADERS ntHeader = MakePtr(PIMAGE_NT_HEADERS, dosHeader, dosHeader->e_lfanew);

// Import Table descriptor.
PIMAGE_IMPORT_DESCRIPTOR importDesc = MakePtr(PIMAGE_IMPORT_DESCRIPTOR, dosHeader,ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);

// Make writeable.
removeReadOnly(MakePtr(PIMAGE_THUNK_DATA, hMod, importDesc->FirstThunk));

while(importDesc->Name)
{
PIMAGE_THUNK_DATA pThunk = MakePtr(PIMAGE_THUNK_DATA, dosHeader, importDesc->FirstThunk);

while (pThunk->u1.Function)
{
if(pThunk->u1.Function == f)
{
pThunk->u1.Function = nf;
}
pThunk++;
}

importDesc++;
}
}

调用者:

// Get the Function Address
DWORD f = (DWORD)GetProcAddress(GetModuleHandleA("ws2_32.dll"),"sendto");
DWORD nf = (DWORD)&my_sendto;

// Save real sendto address.
real_sendto = (int (*)(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen))f;

// Replace function.
replaceFunction(f, nf);

这个有效:

int my_sendto(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen)
{
CreateThread(NULL, 0, de_sendto, NULL, 0, NULL);
return real_sendto(s, buf, len, flags, to, tolen);
}

有效:

int my_sendto(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen)
{
int l = real_sendto(s, buf, len, flags, to, tolen);
CreateThread(NULL, 0, de_sendto, NULL, 0, NULL);
return l;
}

使用后一版本的 my_sendto() 后,主机应用程序将在调用 sendto() 时崩溃。

de_sendto 定义为:

DWORD WINAPI de_sendto(LPVOID args) { }

最佳答案

您的调用约定不正确。 C++默认的调用约定是__cdecl,而sendto的调用约定是__stdcall。将 my_sendto 的调用约定更改为 __stdcall 以修复崩溃。

关于c++ - Hook sendto() 会导致崩溃,具体取决于代码顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6949560/

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