Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 294799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:24:00+00:00 2026-05-12T06:24:00+00:00

What I am trying to do is use the Detours library to hook into

  • 0

What I am trying to do is use the Detours library to hook into an applications WinSock2 send() and recv() functions (a packet logger).

While it does work for the send() function, it does not, however, work for the recv() function.

Here is my relevant code:

#include <cstdio>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <string>
#include <windows.h>
#include <detours.h>

#pragma comment( lib, "Ws2_32.lib" )
#pragma comment( lib, "detours.lib" )
#pragma comment( lib, "detoured.lib" )
#pragma comment( lib, "Mswsock.lib" )

std::ofstream Logger;

std::string NowToString() {
    time_t rawtime;
    tm *timeinfo = new tm();
    char buffer[32];

    time( &rawtime );
    localtime_s( timeinfo, &rawtime );

    strftime( buffer, 32, "%m/%d/%Y %I:%M:%S %p", timeinfo );

    delete timeinfo;

    return std::string( buffer );
}

std::string TimeToString() {
    time_t rawtime;
    tm *timeinfo = new tm();
    char buffer[32];

    time( &rawtime );
    localtime_s( timeinfo, &rawtime );

    strftime( buffer, 32, "%I:%M:%S %p", timeinfo );

    delete timeinfo;

    return std::string( buffer );
}

void LogPacket( const char *buf, int len ) {
    Logger << "        0  1  2  3  4  5  6  7   8  9  A  B  C  D  E  F\n";
    Logger << "       -- -- -- -- -- -- -- --  -- -- -- -- -- -- -- --\n";
    Logger << "0000   ";

    for ( int i = 0; i < len; ++i ) {
        if ( i != 0 && i % 16 == 0 ) {
            Logger << "  ";

            int line = ( i / 16 ) - 1;

            for ( int j = 0; j < 16; ++j ) {
                char c = buf[line * 16 + j];

                if ( c >= 32 && c <= 126 ) {
                    Logger << c;
                } else {
                    Logger << '.';
                }
            }

            Logger << "\n" << std::hex << std::setw( 4 ) << std::setfill( '0' ) << i << std::dec << std::setw( 0 ) << "   ";
        } else if ( i % 16 == 8 ) {
            Logger << ' ';
        }

        Logger << std::hex << std::setw( 2 ) << std::setfill( '0' ) << ( int( buf[i] ) & 0xFF ) << ' ';
        Logger << std::dec << std::setw( 0 );

        if ( i == len - 1 ) {
            int remaining = 16 - ( len % 16 );
            int fill = ( remaining * 3 ) + 2;

            if ( remaining >= 8 ) {
                ++fill;
            }

            for ( int j = 0; j < fill; ++j ) {
                Logger << ' ';
            }

            int line = ( i - ( ( len % 16 ) - 1 ) ) / 16 ;

            for ( int k = 0; k < ( len % 16 ); ++k ) {
                char c = buf[line * 16 + k];

                if ( c >= 32 && c <= 126 ) {
                    Logger << c;
                } else {
                    Logger << '.';
                }
            }
        }
    }

    Logger << "\n\n";
}

int ( WINAPI *Real_Send )( SOCKET s, const char *buf, int len, int flags ) = send;
int ( WINAPI *Real_Recv )( SOCKET s, char *buf, int len, int flags ) = recv;
int ( WINAPI *Real_RecvFrom )( SOCKET s, char *buf, int len, int flags, sockaddr *from, int *fromlen ) = recvfrom;
int ( WINAPI *Real_WSARecvEx )( SOCKET s, char *buf, int len, int *flags ) = WSARecvEx;

int WINAPI Mine_Send( SOCKET s, const char* buf, int len, int flags );
int WINAPI Mine_Recv( SOCKET s, char *buf, int len, int flags );
int WINAPI Mine_RecvFrom( SOCKET s, char *buf, int len, int flags, sockaddr *from, int *fromlen );
int WINAPI Mine_WSARecvEx( SOCKET s, char *buf, int len, int *flags );

int WINAPI Mine_Send( SOCKET s, const char *buf, int len, int flags ) {
    Logger << TimeToString() << ": Client -> Server (Length: " << len << " bytes)\n\n";
    LogPacket( buf, len );
    Logger << std::endl;

    return Real_Send( s, buf, len, flags );
}

int WINAPI Mine_Recv( SOCKET s, char *buf, int len, int flags ) {
    Logger << TimeToString() << ": Server -> Client (Length: " << len << " bytes)\n\n";
    LogPacket( buf, len );
    Logger << std::endl;

    return Real_Recv( s, buf, len, flags );
}

int WINAPI Mine_RecvFrom( SOCKET s, char *buf, int len, int flags, sockaddr *from, int *fromlen ) {
    Logger << TimeToString() << ": Server -> Client (Length: " << len << " bytes)*\n\n";
    LogPacket( buf, len );
    Logger << std::endl;

    return Real_RecvFrom( s, buf, len, flags, from, fromlen );
}

int WINAPI Mine_WSARecvEx( SOCKET s, char *buf, int len, int *flags ) {
    Logger << TimeToString() << ": Server -> Client (Length: " << len << " bytes)**\n\n";
    LogPacket( buf, len );
    Logger << std::endl;

    return Real_WSARecvEx( s, buf, len, flags );
}

BOOL WINAPI DllMain( HINSTANCE, DWORD dwReason, LPVOID ) {
    switch ( dwReason ) {
        case DLL_PROCESS_ATTACH:    
            Logger.open( "C:\\Packets.txt", std::ios::out | std::ios::app | std::ios::ate );

            if ( Logger.tellp() > 0 ) {
                Logger << "\n\n\n";
            }

            Logger << "##\n## Logging Started (" << NowToString() << ")\n##\n\n\n";

            DetourTransactionBegin();
            DetourUpdateThread( GetCurrentThread() );
            DetourAttach( &(PVOID &)Real_Send, Mine_Send );
            DetourAttach( &(PVOID &)Real_Recv, Mine_Recv );
            DetourAttach( &(PVOID &)Real_RecvFrom, Mine_RecvFrom );
            DetourAttach( &(PVOID &)Real_WSARecvEx, Mine_WSARecvEx );
            DetourTransactionCommit();

            break;

        case DLL_PROCESS_DETACH:
            Logger << "##\n## Logging Stopped (" << NowToString() << ")\n##";
            Logger.close();

            DetourTransactionBegin();
            DetourUpdateThread( GetCurrentThread() );
            DetourDetach( &(PVOID &)Real_Send, Mine_Send );
            DetourDetach( &(PVOID &)Real_Recv, Mine_Recv );
            DetourDetach( &(PVOID &)Real_RecvFrom, Mine_RecvFrom );
            DetourDetach( &(PVOID &)Real_WSARecvEx, Mine_WSARecvEx );
            DetourTransactionCommit();

            break;
    }

    return TRUE;
}

Any ideas?

EDIT: So I’ve hooked recvfrom(), and WSARecvEx() as well, and it still doesn’t log the outgoing packets! I’ve updated my code with my exact code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:24:00+00:00Added an answer on May 12, 2026 at 6:24 am

    Well, a few months later I figured it out: I was hooking WinSock 2 functions when I should have been hooking WinSock 1.1’s send()/recv()!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying use a set of filter functions to run the appropriate routine,
I am trying use a javascript function while passing php variables in it. For
I am trying use a javascript function while passing php variables in it. For
I am relatively new to CMake, and I'm trying use the boost asio library
I am trying use this example http://www.sajithmr.me/jrecorder/example2.html for recording audio and send it to
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease
Hi I'm trying use a datepicker on a field I have. I'm trying to
Trying to use this method (gist of which is use self.method_name in the FunnyHelper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.