1

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

EnderPoint
  • 15
  • 6
  • "it has put the authentication stuff after receiving the login success packet". The location of the authentication section on the page is somewhat misleading. Client authentication does happen before the encryption response. – Donut Sep 11 '22 at 14:53

1 Answers1

0

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.

Donut
  • 405
  • 1
  • 5
  • 12
  • omg why didnt i think of that, ill tick this after i get to test it later. Ty for your time and efforts!! – EnderPoint Sep 11 '22 at 16:23
  • https://pastebin.com/xJGQiFSP < im using that to decrypt the packet and im still getting a different packet each time i run it. i havnt been able to do the auth stuff cause apparently microsoft changed it and now it has a long procedure which ill figure out later. – EnderPoint Sep 12 '22 at 08:12
  • ok so i found the existence of cipherinputstream and cipheroutputstream imma try switching to those – EnderPoint Sep 12 '22 at 08:29
  • yeh im not gonnna need any answers anymore. I realized just authenticating with microsoft is a pain which is just the first step > https://wiki.vg/Microsoft_Authentication_Scheme it was fun while it lasted – EnderPoint Sep 12 '22 at 11:48