我正在使用 DLL 注入器来注入一个 dll,该 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) { }