Question

I have a code snippet below that process a socket message, and I would like to know what should be the message sent in order not to result in a return.

Where SocketPacket is a class which stores the received socket, and DataLength would be the length of the received message, dataBuffer stores the message.

int num3;
byte num6 = 0;
SocketPacket workSocket;
int DataLength;

if (workSocket.dataBuffer[0] == 0x33)
{
    if (DataLength < 0xbb)
    {
        return false;
    }

    for (num3 = 0; num3 < 0xba; num3++)
    {
        num6 = (byte) (num6 + workSocket.dataBuffer[num3]);                        
    }

    // how to get pass this if condition??
    if (num6 != workSocket.dataBuffer[0xba])
    {
        return false;
    }
}

So,

  1. What would be the message to send to the server such to get pass the last if condition? (According to my understanding, the message should be at least 187 in length and the first digit should be "3:.........................")
  2. What are the 0xba, 0x33, 0xbb etc....? Hexadecimals? How should I re-construct the input message? Convert these to ASCII? or.... dec? Doesn't make any sense to me.......
  3. I tried to convert workSocket.dataBuffer[0 or 1 or any int] to a readable string. Convert.ToChar(workSocket.dataBuffer[0]) and workSocket.dataBuffer[0].toString() gives different results. Why is that?
Was it helpful?

Solution

Well, what you have there is a fixed-length message (a 187 bytes message). The first byte is a mark to identify the begining of the message then if the first byte is not 0x33 then your code doesn't process the bytes in the buffer.

Next, in the For statement you have a checksum. It is adding all the first 186 bytes in order to compare the result with the last byte (the precalculated checksum). It is to verify the message is okay (and it is useless by the way because protocols warranty the stream/datagram is okey).

So, about your questions:

What would be the message to send to the server such to get pass the last if condition?

Well, you need to send 187-bytes-length message (simply a byte[187]): the first one has to be 0x33, next the content and the last one has to be the checksum (you should calculate in the same way your snippet shows)

[0x33 | THE CONTENT | CHKSUM]
0     1            185     186

For example: the following buffer has a valid message (one that will pass the if condition). It simply begins with the mark byte (0x33) and the next 185 bytes are zero (I didn't assign values) then, the checksum is 0x33 + 0 + 0 + 0 + 0 ... 0 ... = 0x33

var buffer = new byte[187];
buffer[0] = 0x33;
buffer[186] = 0x33; 

What are the 0xba, 0x33, 0xbb etc....? Hexadecimals?

Yes, they are just numbers in hexadecimal.

I tried to convert (sic) gives different results. Why is that?

Sockets send/receive bytes (just numbers) but the real question is: why do you assume they have to be text? Probably they are text, yes but who knows. That is part of the agreements (the protocol) that both endpoints agreed and that allows them to exchange data. So, you have to know what those 185 bytes (187 - 1 byte for mark - 1 byte checksum) mean in order to be able to process them.

Now, what you are doing is a reverse engineering of a protocol and that is because it is clear you don't know the message format and I guess you don't know what the content meaning is, and even when you are right and the content is just text, you ignore the encoding used. Those are the things you need to focus on.

I hope this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top