C ++ Winsock recv hook Detours

时间:2015-06-21 14:03:58

标签: c++ dll hook winsock2 detours

我想写一个.dll。我的Targetapplication是一个游戏并使用winsock。 .dll应该在控制台中写入游戏(Targetapplication)通过winsock中的recv函数重现的所有内容。我在Visual Studio 2012 Professional中创建了一个C ++ Win32控制台应用程序,选择了.dll和Empty项目。

main.cpp中的我的代码

#include <Windows.h>
#include <detours.h>
#include <winsock2.h>
#include <iostream>

using namespace std;

#pragma comment (lib, "detours")

typedef int (WINAPI *MyRecv) (SOCKET, char, int, int);

MyRecv OrigRecv = NULL;

int WINAPI RecvDetour(SOCKET s, char *buf, int len, int flags)
{
    cout << *buf << "   -   " << len << endl;
    return OrigRecv(s, *buf, len, flags);
}


BOOL APIENTRY DllMain(HINSTANCE module, DWORD Reason, LPVOID reserved)
{
    switch (Reason)
    {
    case DLL_PROCESS_ATTACH:
        cout << "Starting.." << endl;
        OrigRecv = (MyRecv)DetourFunction((PBYTE)recv, (PBYTE)RecvDetour);
        break;
    case DLL_PROCESS_DETACH:
        break;
    }
}

我无法编译这个。有一些错误。有人在这段代码中看到错误吗?

非常感谢:)

1 个答案:

答案 0 :(得分:0)

MyRecv声明错误。第二个参数必须是char*而不是char

此外,您的绕行是在填充之前输出接收缓冲区。您需要先调用原始recv()函数,然后输出它接收的内容。此外,请务必考虑数据不会以空值终止。

typedef int (WINAPI *MyRecv) (SOCKET, char*, int, int);

MyRecv OrigRecv = NULL;

int WINAPI RecvDetour(SOCKET s, char *buf, int len, int flags)
{
    int ret = OrigRecv(s, buf, len, flags);
    if (ret > 0)
        cout.write(buf, ret) << "   -   " << ret << endl;
}

见前面这个问题:

Detour hook send/recv winsock

相关问题