Search code examples
javaserverstreamminecraftpacket

Minecraft server responding with weird packets after sending Encryption response


I am trying to make a java program to be able to login to a minecraft server. From this and help of some other related posts like this I have been able to deal with the Handshake, Login Start, Encryption Request and Encryption Response packets. Now, I expect the server to respond with a Login Success but instead it responds with a packet id of something random (i.e. a random number (now i may be doing something wrong trying to read it or mistaking it for something else)).

The code i use to read the packet:

// S->C Login Success
        int loginPacketSize = readVarInt(input); // packet size
        int loginPacketId = readVarInt(input); // packet id

        if(loginPacketId != 0x02) { // We want login success
            System.out.println("Bad packet id: " + loginPacketId);
            
            if(loginPacketId == 0x00) { // If it's a disconnect packet
                disconnected(input);
            }
        }

readVarInt method (i got this from this)

public static int readVarInt(DataInputStream in) throws IOException {
        int i = 0;
        int j = 0;
        while (true) {
            int k = in.readByte();
            i |= (k & 0x7F) << j++ * 7;
            if (j > 5) throw new RuntimeException("VarInt too big");
            if ((k & 0x80) != 128) break;
        }
        return i;
    }

this outputs something different every time it is run

Bad packet id: 134

Bad packet id: 3757

Bad packet id: 9673

more information on this can be found here

https://github.com/EnderPoint07/Fake-Minecraft-Client/blob/master/src/LoginToServer.java is all of the code of my program

edit: Ok so I ran this on my own server running from my computer to get the logs and the server logs this (EnderPoint_07 being my minecraft username):

[14:41:32 ERROR]: Username 'EnderPoint_07' tried to join with an invalid session [14:41:32 INFO]: /127.0.0.1:54157 lost connection: Failed to verify username!

can somebody explain to me why am i not receiving Login Success cause over at wiki.vg/Protocol_Encryption (i dont have rep to post more than 8 links) it has put the authentication stuff after receiving the login success packet


Solution

  • tldr authenticate before sending the encryption response and decrypt all packets from the server after sending the encryption response.

    The client must authenticate before sending the encryption response as shown here.

    C->S : Handshake State=2
    C->S : Login Start
    S->C : Encryption Key Request
    (Client Auth)
    C->S : Encryption Key Response
    (Server Auth, Both enable encryption)
    S->C : Login Success

    Follow the authentication process specified here.

    You're seeing a random garbage packet id because it is encrypted. All packets after the encryption response are encrypted and must be decrypted as specified here.

    Similarly, the client will also enable encryption upon sending Encryption Response. From this point forward, everything is encrypted. Note: the entire packet is encrypted, including the length fields and the packet's data. The Login Success packet is sent encrypted.