PrismarineJS / bedrock-protocol

Minecraft Bedrock protocol library, with authentication and encryption
https://prismarinejs.github.io/minecraft-data/?v=bedrock_1.17.10&d=protocol
MIT License
303 stars 70 forks source link

Crash crafting using proxy on bedrock servers #515

Open bentonjj opened 1 month ago

bentonjj commented 1 month ago

Get the below crash crafting anything on Bedrock servers when connected with proxy. Tried 1.20.80 and 1.21.2 clients, even when using basic proxy script example code:

C:\bedrock\node_modules\protodef\src\compiler.js:114 throw e ^

Error: Read error for undefined : array size is abnormally large, not reading: 16777216 at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :342:37) at Object.ItemExtraDataWithoutBlockingTick (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :351:9) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :386:47) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :388:17) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :390:13) at Object.ItemLegacy (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :392:9) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1577:48) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1582:17) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1585:13) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1587:11)

Node.js v18.20.4

extremeheat commented 1 month ago

Can you reproduce this on another server or a vanilla server?

bentonjj commented 1 month ago

Yes, when using the standard bedrock server from minecraft.net still get the same crash, even in creative mode adding items to inventory:

C:\bedrock\node_modules\protodef\src\compiler.js:114

    throw e

    ^

Error: Read error for undefined : array size is abnormally large, not reading: 16777216

at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:342:37)

at Object.ItemExtraDataWithoutBlockingTick (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:351:9)

at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:386:47)

at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:388:17)

at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:390:13)

at Object.ItemLegacy (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:392:9)

at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:1606:48)

at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:1611:17)

at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:1614:13)

at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), <anonymous>:1616:11)

Node.js v18.20.4

I can get the proxy working again if I downgrade the minecraft-data package from 3.67.0 to 3.66.0 however I am limited to using the 1.20.80 and older client as if I upgrade to 1.21.2 I get a “Error: Unsupported version 1.21.2”. Updating minecraft-data to 3.67.0 causes the crash in all client versions.

bentonjj commented 1 month ago

The work around is to downgrade minecraft-data package to 3.66.0. Go to https://github.com/PrismarineJS/minecraft-data/releases and download the minecraft-data-3.66.0.zip file. Extract to node_modules\minecraft-data\minecraft-data. You will be limited to the 1.21.0 client and older as newer clients require minecraft-data 3.67.0 which causes the issue in all client versions.

extremeheat commented 1 month ago

Can you please set Error.stackTraceLimit = Infinity at the top of your program and post the stack trace again?

bentonjj commented 1 month ago

This is the code:

Error.stackTraceLimit = Infinity const { Relay } = require('bedrock-protocol') const relay = new Relay({version: '1.20.80', host: '10.10.0.10', port: 4000, destination: { host: '10.10.0.10', port: 3000 }}) relay.listen() relay.on('connect', player => { console.log('New connection', player.connection.address) player.on('serverbound', ({ name, params }) => { }) })

Client is version 1.20.81 and server from minecraft.net is version 1.20.81.01

This is the output after the crash adding any item to inventory:

New connection 10.10.0.10/55385 C:\bedrock\node_modules\protodef\src\compiler.js:114 throw e ^

Error: Read error for undefined : array size is abnormally large, not reading: 16777216 at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :342:37) at Object.ItemExtraDataWithoutBlockingTick (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :351:9) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :386:47) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :388:17) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :390:13) at Object.ItemLegacy (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :392:9) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1577:48) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1582:17) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1585:13) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1587:11) at Object.ItemStackRequest (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :1592:9) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :4267:46) at Object.packet_item_stack_request (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :4272:9) at eval (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :2029:76) at mcpe_packet (eval at compile (C:\bedrock\node_modules\protodef\src\compiler.js:258:12), :2092:9) at CompiledProtodef.read (C:\bedrock\node_modules\protodef\src\compiler.js:70:12) at e.message (C:\bedrock\node_modules\protodef\src\compiler.js:111:49) at tryCatch (C:\bedrock\node_modules\protodef\src\utils.js:50:16) at CompiledProtodef.parsePacketBuffer (C:\bedrock\node_modules\protodef\src\compiler.js:111:29) at Parser.parsePacketBuffer (C:\bedrock\node_modules\protodef\src\serializer.js:68:23) at RelayPlayer.readPacket (C:\bedrock\node_modules\bedrock-protocol\src\relay.js:133:44) at RelayPlayer.onDecryptedPacket (C:\bedrock\node_modules\bedrock-protocol\src\connection.js:163:12) at Decipheriv.verify (C:\bedrock\node_modules\bedrock-protocol\src\transforms\encryption.js:92:12) at Decipheriv.emit (node:events:519:28) at addChunk (node:internal/streams/readable:559:12) at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) at Readable.push (node:internal/streams/readable:390:5) at Decipheriv._transform (node:internal/crypto/cipher:159:8) at Transform._write (node:internal/streams/transform:171:8) at writeOrBuffer (node:internal/streams/writable:570:12) at _write (node:internal/streams/writable:499:10) at Writable.write (node:internal/streams/writable:508:10) at RelayPlayer.decrypt (C:\bedrock\node_modules\bedrock-protocol\src\transforms\encryption.js:98:21) at RelayPlayer.handle (C:\bedrock\node_modules\bedrock-protocol\src\connection.js:170:14) at C:\bedrock\node_modules\bedrock-protocol\src\server.js:105:35 at process.processTicksAndRejections (node:internal/process/task_queues:77:11)

Node.js v20.16.0

extremeheat commented 1 month ago

OK, please add some logging like this:

const fs = require('fs')

relay.on('connect', (player) => {

  const file = fs.createWriteStream('log.txt')
  player.downInLog = function (...e) {
    file.write(JSON.stringify(e) + '\n')
  }

})

and provide the log.txt file (either here or or http://gist.github.com). That should contain needed packet buffer information.

bentonjj commented 1 month ago

Sorry program simply exits when I connect and does not create any log file. This is what I'm using:

Error.stackTraceLimit = Infinity const { Relay } = require('bedrock-protocol') const relay = new Relay({version: '1.20.80', host: '10.10.0.10', port: 4000, destination: { host: '10.10.0.10', port: 3000 }}) relay.listen() relay.on('connect', player => { const file = fs.createWriteStream('log.txt') player.downInLog = function (...e) {file.write(JSON.stringify(e) + '\n')} console.log('New connection', player.connection.address) player.on('serverbound', ({ name, params }) => { }) })

bentonjj commented 1 month ago

This is the log:

log.txt

This is the code I'm using:

BigInt.prototype.toJSON = function() { return this.toString() } const { Relay } = require('bedrock-protocol') const relay = new Relay({version: '1.20.80', host: '10.10.0.10', port: 4000, destination: { host: '10.10.0.10', port: 3000 }}) relay.listen() const fs = require('fs')

relay.on('connect', (player) => {

const file = fs.createWriteStream('log.txt') player.downInLog = function (...e) { file.write(JSON.stringify(e) + '\n') } })

I had to add the first line to resolve a "TypeError: Do not know how to serialize a BigInt"

bentonjj commented 1 month ago

You can pick up items, take items from chests, and use /give command with no crash. Crash happens when crafting an item on crafting table or adding item to inventory from creative mode.

extremeheat commented 1 month ago

Looking into the crafting issue, seems a bit odd that the buffer is using craft_creative as opposed to auto which was updated.

I can separately confirm the issue with the movement correction packet, that is missing a field since 1.20.80.

extremeheat commented 1 month ago

Ah wow, crafting issue seems to be caused by a bug in protodef compiler's codegen. Anonymous containers with switches have bad code generation.

extremeheat commented 1 month ago

Will be fixed by https://github.com/ProtoDef-io/node-protodef/pull/159

extremeheat commented 1 month ago

Would be great if people could test out the above PR.

extremeheat commented 4 weeks ago

Movement correction issue fixed upstream with #522.

But again if anyone could test above PR in ProtoDef that would be great.