Compare commits

..
2 Commits
Author SHA1 Message Date
erikbrockoandClaude Opus 5.5 004aa41732 Pad a short RSA puzzle answer instead of failing the connect
An answer with a leading zero byte comes out of toByteArray() as 63 bytes,
and copying it started one byte too far, failing about one connect in 256.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 20:17:49 +00:00
erikbrockoandClaude Opus 5.5 0549b6bf02 Sign myTeamSpeak identity into clientinit
Adds an authenticator hook that appends the myTeamSpeak parameters to
clientinit/updatemytsid, and the per-connection challenge they sign.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:04:55 +00:00
4 changed files with 72 additions and 11 deletions
@@ -159,16 +159,7 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
BigInteger x = new BigInteger(1, serverReplyStep3.getX());
BigInteger n = new BigInteger(1, serverReplyStep3.getN());
byte[] y = new byte[64];
byte[] solution =
x.modPow(BigInteger.valueOf(2L).pow(serverReplyStep3.getLevel()), n).toByteArray();
System.arraycopy(
solution, Math.abs(solution.length - 64),
y, solution.length < 64 ? 64 - solution.length : 0,
solution.length < 64 ? solution.length : 64
);
byte[] y = unsigned64(x.modPow(BigInteger.valueOf(2L).pow(serverReplyStep3.getLevel()), n));
// Build response
PacketBody8Init1.Step4 step4 = new PacketBody8Init1.Step4();
@@ -355,8 +346,22 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
)
);
Ts3Debugging.debug(clientinit.build());
getClient().addMyTeamSpeakParameters(clientinit);
Ts3Debugging.debug("Sending clientinit");
getClient().writePacket(new PacketBody2Command(ProtocolRole.CLIENT, clientinit));
}
/**
* The puzzle's answer as 64 unsigned big-endian bytes. {@link BigInteger#toByteArray()}
* adds a sign byte when the top bit is set and drops leading zero bytes, so it can be
* 65 bytes or fewer than 64.
*/
static byte[] unsigned64(BigInteger value) {
byte[] bytes = value.toByteArray();
byte[] y = new byte[64];
int length = Math.min(bytes.length, 64);
System.arraycopy(bytes, bytes.length - length, y, 64 - length, length);
return y;
}
}
@@ -32,6 +32,12 @@ public class PacketTransformation {
return fakeMac;
}
/** Connection binding used by myTeamSpeak, in the AES-128/SHA-256 protocol mode. */
public byte[] getMyTeamSpeakChallenge() {
return Ts3Crypt.hash256(ByteBuffer.allocate(6 + ivStruct.length)
.put(new byte[] {0x30, 8, 0, 0, 0, 0}).put(ivStruct).array());
}
public Pair<byte[], byte[]> computeParameters(PacketHeader header) {
ByteBuffer temporaryByteBuffer = ByteBuffer
.allocate(ivStruct.length == 20 ? 26 : 70)
@@ -3,6 +3,7 @@ package com.github.manevolent.ts3j.protocol.socket.client;
import com.github.manevolent.ts3j.command.CommandException;
import com.github.manevolent.ts3j.command.*;
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
import com.github.manevolent.ts3j.identity.Identity;
import com.github.manevolent.ts3j.protocol.NetworkPacket;
import com.github.manevolent.ts3j.protocol.Packet;
@@ -36,6 +37,7 @@ import java.nio.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Server/client common abstract client socket, used to interact with the UDP connection. Provides most of the protocol
@@ -165,6 +167,21 @@ public abstract class AbstractTeamspeakClientSocket
setTransformation(new PacketTransformation(parameters.getIvStruct(), parameters.getFakeSignature()));
}
private volatile Function<byte[], Map<String, String>> myTeamSpeakAuthenticator;
/** Set before connecting; invoked after the handshake with this connection's challenge. */
public void setMyTeamSpeakAuthenticator(Function<byte[], Map<String, String>> authenticator) {
myTeamSpeakAuthenticator = authenticator;
}
public void addMyTeamSpeakParameters(Command command) {
Function<byte[], Map<String, String>> authenticator = myTeamSpeakAuthenticator;
if (authenticator == null) return;
Map<String, String> parameters = authenticator.apply(transformation.getMyTeamSpeakChallenge());
for (Map.Entry<String, String> entry : parameters.entrySet())
command.add(new CommandSingleParameter(entry.getKey(), entry.getValue()));
}
protected abstract NetworkPacket readNetworkPacket(int timeout) throws IOException;
protected abstract void writeNetworkPacket(NetworkPacket packet) throws IOException;
@@ -0,0 +1,33 @@
package com.github.manevolent.ts3j.protocol.packet.handler.client;
import junit.framework.TestCase;
import java.math.BigInteger;
import java.util.Arrays;
public class PuzzleAnswerTest extends TestCase {
private static byte[] bytes(int length, int first) {
byte[] b = new byte[length];
Arrays.fill(b, (byte) 0x11);
b[0] = (byte) first;
return b;
}
/** Top bit set: toByteArray() gives 65 bytes, the first a sign byte. */
public void testDropsTheSignByte() {
byte[] answer = bytes(64, 0x80);
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
}
public void testKeepsSixtyFourBytes() {
byte[] answer = bytes(64, 0x7f);
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
}
/** A leading zero byte: toByteArray() gives 63 bytes, which used to fail the connect. */
public void testPadsAShortAnswerWithZeros() {
byte[] answer = bytes(64, 0x00);
answer[1] = 0x42;
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
}
}