This is what i have:
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;
}
and this is what i get in file 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 (this one contain pure bytes) (copied from hex editor)
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
uhh, you see that.
The
ret_valyou are using in the following line:is the number of bytes you have received from the
recv()call. This is not the number of bytes you need to write to the file after you have converted the received bytes to a hex string separated by spaces. Your call should probably be:As for the weird termination, this is caused by the same thing. You can see that you are writing exactly 16 bytes in the first line by simply counting them, but it is 16 bytes of the converted string: