c++ - Winsock recv hooking with Detours

标签 c++ winapi hook dll-injection

我有一个应用程序,它使用 Winsock 2.0 recv 函数,我可以通过 Redox Packet Editor 捕获输出,例如,它确认版本是 2.0。

我有这段代码来 Hook 函数:

#define _CRT_SECURE_NO_DEPRECATE
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <WinSock2.h>
#include <detours.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")


FILE *pSendLogFile;
FILE *pRecvLogFile;

int (WINAPI *pSend)(SOCKET s, const char* buf, int len, int flags) = send;
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags);
int (WINAPI *pRecv)(SOCKET s, char *buf, int len, int flags) = recv;
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags);


INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
    switch(Reason)
    {
        case DLL_PROCESS_ATTACH:
            DisableThreadLibraryCalls(hDLL);

            DetourTransactionBegin();
            DetourUpdateThread(GetCurrentThread());
            DetourAttach(&(PVOID&)pSend, MySend);
            if(DetourTransactionCommit() == NO_ERROR)
                MessageBox(0,"send() detoured successfully","asd",MB_OK);

            DetourTransactionBegin();
            DetourUpdateThread(GetCurrentThread());
            DetourAttach(&(PVOID&)pRecv, MyRecv);
            if(DetourTransactionCommit() == NO_ERROR)
                MessageBox(0,"recv() detoured successfully","asd",MB_OK);
            break;

    case DLL_PROCESS_DETACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
        break;
    }
    return TRUE;
}


int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
{
    MessageBox(0,"sent","sent",MB_OK);
    return pSend(s, buf, len, flags);
}

int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
{
    MessageBox(0,"recvd","recvd",MB_OK);
    return pRecv(s, buf, len, flags);
}

对于 send,一切正常,但对于 recv 我没有得到任何输出。我在另一个应用程序中尝试使用 1.1 版本的 Winsock,它工作正常。尝试 Hook WSARecv、WSARecvEx 但没有任何运气。

使用 WinAPIOverride32 检查应用程序,它清楚地表明它使用 recv 函数,并成功记录了使用情况。 Winsock 数据包编辑器也能很好地读取数据。

有什么想法吗?

最佳答案

您确定您挂接的是正确的 dll 吗?我会仔细检查程序实际使用了哪个 dll:WSOCK32.dll 或 ws2_32.dll。

编辑:

也许尝试这样的事情:

typedef int (WINAPI *SendPtr)(SOCKET s, const char* buf, int len, int flags);
HMODULE hLib = LoadLibrary("wsock32.dll");
SendPtr pSend = (SendPtr)GetProcAddress(hLib, "send");

然后将 pSend 与该值一起使用(recv 也是如此)。不要忘记最后调用 FreeLibrary。 如果您确定 dll 已经加载,那么最好使用 GetModuleHandle("wsock32.dll"),因为在这种情况下您不必调用 FreeLibrary。

关于c++ - Winsock recv hooking with Detours,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10065903/

相关文章:

c++ - 静态成员变量可以调用非静态成员函数吗?

c++ - 从 MATLAB 调用 C++ .dll 库

python - pygame 中的 pywin32 函数导致程序挂起/"python.exe is not responding"

linux - 在 C/C++ 中执行与 Windows API 的 SSH 连接?

git gitolite (v3) 预接收所有提交消息的钩子(Hook)

c++ - Windows:如何查询低级键盘 Hook 中修改键的状态?

c++ - 什么是 c printf %f 默认精度?

c++ - 虚拟继承中派生类的大小

.net - 长按电源键如何拒绝关机?

windows - 如何在 Vista 中使用 SetWindowsHookEx 并将管理应用程序与 UAC Hook ?