Skip to content

Commit 2559c3c

Browse files
committed
chat message signing
fix Player List Packet serialization issues
1 parent 0ca9c70 commit 2559c3c

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

src/main/java/systems/kinau/fishingbot/bot/Player.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import systems.kinau.fishingbot.network.protocol.ProtocolConstants;
1818
import systems.kinau.fishingbot.network.protocol.play.*;
1919
import systems.kinau.fishingbot.network.protocol.play.PacketOutEntityAction.EntityAction;
20+
import systems.kinau.fishingbot.network.utils.CryptManager;
2021
import systems.kinau.fishingbot.utils.ItemUtils;
2122
import systems.kinau.fishingbot.utils.LocationUtils;
2223
import systems.kinau.fishingbot.utils.StringUtils;
2324

2425
import java.util.HashMap;
2526
import java.util.Map;
27+
import java.util.Optional;
2628
import java.util.UUID;
2729
import java.util.concurrent.TimeUnit;
2830
import java.util.function.Consumer;
@@ -48,6 +50,7 @@ public class Player implements Listener {
4850
@Getter @Setter private Slot heldItem;
4951
@Getter @Setter private Inventory inventory;
5052
@Getter private final Map<Integer, Inventory> openedInventories = new HashMap<>();
53+
@Getter @Setter private Optional<CryptManager.MessageSignature> lastUsedSignature = Optional.empty();
5154

5255
@Getter @Setter private UUID uuid;
5356

src/main/java/systems/kinau/fishingbot/network/protocol/play/PacketInChatPlayer.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import systems.kinau.fishingbot.event.play.ChatEvent;
1919
import systems.kinau.fishingbot.network.protocol.NetworkHandler;
2020
import systems.kinau.fishingbot.network.protocol.Packet;
21+
import systems.kinau.fishingbot.network.protocol.ProtocolConstants;
2122
import systems.kinau.fishingbot.network.utils.ByteArrayDataInputWrapper;
2223
import systems.kinau.fishingbot.utils.TextComponent;
2324

@@ -37,21 +38,25 @@ public void write(ByteArrayDataOutput out, int protocolId) {
3738

3839
@Override
3940
public void read(ByteArrayDataInputWrapper in, NetworkHandler networkHandler, int length, int protocolId) {
40-
this.text = readString(in);
41-
try {
42-
JSONObject object = (JSONObject) PARSER.parse(text);
41+
if (protocolId >= ProtocolConstants.MINECRAFT_1_19) {
4342

43+
} else {
44+
this.text = readString(in);
4445
try {
45-
this.text = TextComponent.toPlainText(object);
46+
JSONObject object = (JSONObject) PARSER.parse(text);
47+
48+
try {
49+
this.text = TextComponent.toPlainText(object);
50+
} catch (Exception ignored) {
51+
//Ignored
52+
}
53+
54+
//TODO: Handle this correctly. This packet represents the normal chat packet up to 1.18.2 and the vanilla server player chat packet in 1.19 and higher
55+
56+
FishingBot.getInstance().getCurrentBot().getEventManager().callEvent(new ChatEvent(getText(), getSender()));
4657
} catch (Exception ignored) {
4758
//Ignored
4859
}
49-
50-
//TODO: Handle this correctly. This packet represents the normal chat packet up to 1.18.2 and the vanilla server player chat packet in 1.19 and higher
51-
52-
FishingBot.getInstance().getCurrentBot().getEventManager().callEvent(new ChatEvent(getText(), getSender()));
53-
} catch (Exception ignored) {
54-
//Ignored
5560
}
5661
}
5762
}

src/main/java/systems/kinau/fishingbot/network/protocol/play/PacketInPlayerListItem.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import systems.kinau.fishingbot.event.play.UpdatePlayerListEvent;
1313
import systems.kinau.fishingbot.network.protocol.NetworkHandler;
1414
import systems.kinau.fishingbot.network.protocol.Packet;
15+
import systems.kinau.fishingbot.network.protocol.ProtocolConstants;
1516
import systems.kinau.fishingbot.network.utils.ByteArrayDataInputWrapper;
1617

1718
import java.io.IOException;
@@ -49,6 +50,15 @@ public void read(ByteArrayDataInputWrapper in, NetworkHandler networkHandler, in
4950
int ping = readVarInt(in);
5051
if (in.readBoolean()) // has display name
5152
readString(in); // display name
53+
if (protocolId >= ProtocolConstants.MINECRAFT_1_19) {
54+
if (in.readBoolean()) {
55+
in.readLong(); // expiration
56+
int size = readVarInt(in); // pubkey length
57+
in.readFully(new byte[size]); // pubkey
58+
size = readVarInt(in); // sig length
59+
in.readFully(new byte[size]); // sig
60+
}
61+
}
5262
if (uuid.equals(FishingBot.getInstance().getCurrentBot().getPlayer().getUuid()))
5363
FishingBot.getInstance().getCurrentBot().getEventManager().callEvent(new PingChangeEvent(ping));
5464
} else if (action == 1) {

src/main/java/systems/kinau/fishingbot/network/protocol/play/PacketOutChatCommand.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import lombok.Getter;
1111
import systems.kinau.fishingbot.network.protocol.NetworkHandler;
1212
import systems.kinau.fishingbot.network.protocol.Packet;
13+
import systems.kinau.fishingbot.network.protocol.ProtocolConstants;
1314
import systems.kinau.fishingbot.network.utils.ByteArrayDataInputWrapper;
1415

1516
@AllArgsConstructor
@@ -22,11 +23,16 @@ public class PacketOutChatCommand extends Packet {
2223
public void write(ByteArrayDataOutput out, int protocolId) {
2324
writeString(getCommand(), out);
2425
// this is most likely very illegal, but it seems like the server does not care about the signature
26+
// UPDATE: It's not working in a signed context (only no argument commands work)
2527

2628
out.writeLong(System.currentTimeMillis()); // timestamp
2729
out.writeLong(System.currentTimeMillis()); // arg sig salt
2830
writeVarInt(0, out); // arg sig map
2931
out.writeBoolean(false); // signed preview
32+
if (protocolId >= ProtocolConstants.MINECRAFT_1_19_1) {
33+
writeVarInt(0, out); // acknowledgements lastSeen (LastSeenMessageList.write(buf))
34+
out.writeBoolean(false); // acknowledgements lastReceived (Optional<LastSeenMessageList.Entry>)
35+
}
3036
}
3137

3238
@Override

src/main/java/systems/kinau/fishingbot/network/protocol/play/PacketOutChatMessage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import systems.kinau.fishingbot.network.utils.ByteArrayDataInputWrapper;
1717
import systems.kinau.fishingbot.network.utils.CryptManager;
1818

19+
import java.util.Optional;
1920
import java.util.UUID;
2021

2122
@AllArgsConstructor
@@ -50,6 +51,8 @@ public void write(ByteArrayDataOutput out, int protocolId) {
5051
writeVarInt(signature.getSignature().length, out);
5152
out.write(signature.getSignature());
5253
out.writeBoolean(false);
54+
55+
FishingBot.getInstance().getCurrentBot().getPlayer().setLastUsedSignature(Optional.of(signature));
5356
}
5457

5558
if (protocolId >= ProtocolConstants.MINECRAFT_1_19_1) {

src/main/java/systems/kinau/fishingbot/network/utils/CryptManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.security.spec.X509EncodedKeySpec;
3636
import java.time.Instant;
3737
import java.util.Objects;
38+
import java.util.Optional;
3839
import java.util.Random;
3940
import java.util.UUID;
4041
import java.util.function.Consumer;
@@ -221,6 +222,7 @@ public static MessageSignature signChatMessage(AuthData.ProfileKeys keys, UUID s
221222
HashingOutputStream hashingOutputStream = new HashingOutputStream(Hashing.sha256(), new OutputStream() {
222223
@Override
223224
public void write(int b) throws IOException {
225+
// empty
224226
}
225227
});
226228

@@ -238,6 +240,9 @@ public void write(int b) throws IOException {
238240
byte[] messageBody = hashingOutputStream.hash().asBytes();
239241
return new MessageSignature(sign(keys, signature -> {
240242
try {
243+
Optional<MessageSignature> prevSignature = FishingBot.getInstance().getCurrentBot().getPlayer().getLastUsedSignature();
244+
if (prevSignature.isPresent())
245+
signature.update(prevSignature.get().getSignature());
241246
signature.update(messageHeader);
242247
signature.update(messageBody);
243248
} catch (SignatureException e) {

0 commit comments

Comments
 (0)