这就是我所拥有的:
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
呃,你看到了。