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) { }