2

这就是我所拥有的:

std::string GetBytesAsHEX(const char *arr, int arr_size)
{
    BYTE ch = 0x00;
    char pseudo[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    std::string ret_val;

    for (int i = 0; i < arr_size; i++)
    {
        ch = (BYTE) (arr[i] & 0xF0);
        ch = (BYTE) (ch >> 4);
        ch = (BYTE) (ch & 0x0F);
        ret_val += pseudo[(int)ch];
        ch = (BYTE) (arr[i] & 0x0F);
        ret_val += pseudo[(int)ch];
        ret_val += ' ';
    }

    return ret_val;
}

int __stdcall Hooked_send(SOCKET s, const char *buf, int len, int flags)
{
    h_send.PreHook();
    //--------------

    int ret_val = send(s, buf, len, flags);

    if (LogPackets)
    {
        FILE *fptr = fopen("packet_log_hex.txt", "a");
        char header[128] = { 0 };
        sprintf(header, "\nSENT %i bytes: ", ret_val);
        fwrite(header, strlen(header), sizeof(char), fptr);
        fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);

        fclose(fptr);

        fptr = fopen("packet_log.txt", "ab");
        fwrite(buf, ret_val, sizeof(char), fptr);
        fclose(fptr);
    }

    //---------------
    h_send.PostHook();

    return ret_val;
}

int __stdcall Hooked_recv(SOCKET s, char *buf, int len, int flags)
{
    h_recv.PreHook(); //restore original recv address

    int ret_val = recv(s, buf, len, flags);

    if (ret_val > 0 && LogPackets)
    {
        FILE *fptr = fopen("packet_log_hex.txt", "a");
        char header[128] = { 0 };
        sprintf(header, "\nRECV %i bytes: ", ret_val);
        fwrite(header, strlen(header), sizeof(char), fptr);
        fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);

        fclose(fptr);

        fptr = fopen("packet_log.txt", "ab");
        fwrite(buf, ret_val, sizeof(char), fptr);
        fclose(fptr);
    }

    h_recv.PostHook(); //replace recv address with Hooked_recv

    return ret_val;
}

这就是我在文件 packet_log_hex.txt 中得到的

SENT 16 bytes: 55 47 0C 00 00 0 //this is way not 16 bytes... and why so weird termination?
RECV 32 bytes: 55 47 1C 00 00 00 10 00 03 00 00
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 00 0
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 0B 0
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 10 F
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 C5 E

packet_log.txt(这个包含纯字节)(从十六进制编辑器复制)

55 47 0C 00 00 00 00 00 02 00 00 00 01 00 03 02
55 47 1C 00 00 00 10 00 03 00 00 00 2D 04 00 00 50 07 F3 17 1A 37 34 48 81 D2 5E 13 73 21 37 A3
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 30 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 0B 00 00 00 12 00 00 00 40 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 10 FB 00 00 12 00 00 00 50 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 C5 EE 00 00 12 00 00 00 60 00 00 00

呃,你看到了。

4

1 回答 1

4

ret_val您在以下行中使用:

fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);

是您从recv()调用中收到的字节数。这不是您将接收到的字节转换为以空格分隔的十六进制字符串后需要写入文件的字节数。您的电话应该是:

std::string hex_str = GetBytesAsHEX(buf, ret_val);
fwrite(hex_str.c_str(), hex_str.length(), sizeof(char), fptr);

至于奇怪的终止,这是由同一件事引起的。通过简单地计算它们,您可以看到您在第一行中准确地写入了 16 个字节,但它是转换后的字符串的 16 个字节:

          111111
0123456789012345

55 47 0C 00 00 0
于 2012-05-31T13:20:41.363 回答