温索克与绕道挂钩

Winsock recv hooking with Detours

本文关键字:      更新时间:2023-10-16

我有一个应用程序,它使用 Winsock 2.0 recv 函数,我可以通过氧化还原数据包编辑器捕获输出,例如,它确认版本是 2.0。

我有这段代码来钩住函数:

#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,它工作正常。试图钩住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 也是如此)。最后不要忘记打电话给免费图书馆。如果您确定 dll 已经加载,那么最好使用 GetModuleHandle("wsock32.dll") 因为在这种情况下您不必调用 FreeLibrary。

你的问题源于试图写出一个空的(甚至是未初始化的缓冲区):

int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
{
    fopen_s(&pRecvLogFile, "C:\RecvLog.txt", "a+");
    fprintf(pRecvLogFile, "%sn", buf);
    fclose(pRecvLogFile);
    return pRecv(s, buf, len, flags); //you need to call recv first
}

而是做这样的事情:

int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
{
    int read = pRecv(s, buf, len, flags);
    if(read <= 0)
    {
        //read error/connection closed
        return read;
    }
    fopen_s(&pRecvLogFile, "C:\RecvLog.txt", "a+");
    fwrite(buf,sizeof(char),read,pRecvLogFile);
    fclose(pRecvLogFile);
    return read;
}

作为次要问题,您似乎假设发送或接收的数据纯粹基于字符串,但通常数据包可能在这里和那里包含零字节,这将过早地结束fprintf输出,您应该改用fwrite,传递发送/接收大小(这也意味着以二进制模式打开文件)。

我认为你当然应该使用GetProcAddress来获取要挂钩的地址。

像这样:

int (WINAPI *pRecv)(SOCKET s, char *buf, int len, int flags) = GetProcAddress(GetModuleHandle("ws2_32.dll"), "recv");

编译器可以想出从你的"recv"到加载dll的各种野生路线。因此,这两个地址可能会有所不同。要测试是否是这种情况,只需使用 dll 中的 recv。

您可能还想关注ReadFile/WriteFile。

并且还期望钩子不可靠。例如,目标可以随意移除钩子并执行更多操作。

相关文章:
  • 没有找到相关文章