我正在尝试挂钩 winsock send 和 recv 以读取进程的所有流量。我在目标进程中将以下代码作为 dll 注入
#include "dll.h"
#include <windows.h>
#include <winsock2.h>
#include <iostream>
#include <fstream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
DllClass::DllClass()
{
}
DllClass::~DllClass ()
{
}
BYTE hook[6];
BYTE hook2[6];
BYTE jmp[6] = { 0xe9,0x00, 0x00, 0x00, 0x00 ,0xc3 };
ofstream myfile;
ofstream myfile2;
DWORD HookFunction(LPCSTR lpModule, LPCSTR lpFuncName, LPVOID lpFunction, unsigned char *lpBackup)
{
DWORD dwAddr = (DWORD)GetProcAddress(GetModuleHandle(lpModule), lpFuncName);
ReadProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, lpBackup, 6, 0);
DWORD dwCalc = ((DWORD)lpFunction - dwAddr - 5);
memcpy(&jmp[1], &dwCalc, 4);
WriteProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, jmp, 6, 0);
return dwAddr;
}
BOOL UnHookFunction(LPCSTR lpModule, LPCSTR lpFuncName, unsigned char *lpBackup)
{
DWORD dwAddr = (DWORD)GetProcAddress(GetModuleHandle(lpModule), lpFuncName);
if (WriteProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, lpBackup, 6, 0))
return TRUE;
return FALSE;
}
int nSend(SOCKET s, const char *buf, int len,int flags){
UnHookFunction("ws2_32.dll", "send", hook);
int result = send(s,buf,len,flags);
myfile.open ("C:\\tmp\\log.txt",ios::app | ios::binary);
myfile << buf;
myfile.close();
HookFunction("ws2_32.dll", "send", (LPVOID*) nSend, hook);
return result;
}
int nRecv(SOCKET s, char* buf, int len, int flags)
{
UnHookFunction("ws2_32.dll", "recv", hook2);
DWORD tmp;
len = recv(s, buf, len, flags);
if (len > 0)
{
myfile2.open ("C:\\tmp\\log.txt",ios::app | ios::binary);
myfile2 << buf;
myfile2.close();
}
HookFunction("ws2_32.dll", "recv", (LPVOID*) nRecv, hook2);
return len;
}
void fun(){ // <-- this is called after the DLL has been injected
HookFunction("ws2_32.dll", "send", (LPVOID*) nSend, hook);
HookFunction("ws2_32.dll", "recv", (LPVOID*) nRecv, hook2);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
这在某些情况下有效,在某些情况下无效。如果我将它注入filezilla ftp,它就像一个魅力,并将发送或接收的所有内容写入文件。
但是在几乎所有其他程序(Internet Explorer,firefox usw。)上,它只是将一些字节写入文件,然后进程崩溃......
有谁知道出了什么问题?