From bd0d708274b0bf6d3b1ae1721718b84b9e8474b7 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 26 Nov 2013 17:23:45 +0100 Subject: [PATCH] Fixed Packet of Death --- src/network/CustomPacketHandler.php | 10 +++++++--- src/network/MinecraftInterface.php | 3 +++ src/network/Packet.php | 8 ++++++-- src/utils/Utils.php | 4 ++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/network/CustomPacketHandler.php b/src/network/CustomPacketHandler.php index 88916b415..8d9108f9a 100644 --- a/src/network/CustomPacketHandler.php +++ b/src/network/CustomPacketHandler.php @@ -40,6 +40,10 @@ class CustomPacketHandler{ } return $data; } + + private function feof(){ + return !isset($this->raw{$this->offset}); + } public function __construct($pid, $raw = "", $data = array(), $create = false){ $this->raw = $raw; @@ -469,7 +473,7 @@ class CustomPacketHandler{ $this->data["radius"] = Utils::readFloat($this->get(4)); $this->data["count"] = Utils::readInt($this->get(4)); $this->data["records"] = array(); - for($r = 0; $r < $this->data["count"]; ++$r){ + for($r = 0; $r < $this->data["count"] and !$this->feof(); ++$r){ $this->data["records"][] = new Vector3(Utils::readByte($this->get(1)), Utils::readByte($this->get(1)), Utils::readByte($this->get(1))); } }else{ @@ -707,7 +711,7 @@ class CustomPacketHandler{ $this->data["windowid"] = ord($this->get(1)); $this->data["count"] = Utils::readShort($this->get(2), false); $this->data["slots"] = array(); - for($s = 0; $s < $this->data["count"]; ++$s){ + for($s = 0; $s < $this->data["count"] and !$this->feof(); ++$s){ $this->data["slots"][$s] = Utils::readSlot($this); } if($this->data["windowid"] === 1){ //Armor is also sent @@ -791,7 +795,7 @@ class CustomPacketHandler{ $this->data["windowid"] = ord($this->get(1)); $this->data["count"] = Utils::readShort($this->get(2), false); $this->data["slots"] = array(); - for($s = 0; $s < $this->data["count"]; ++$s){ + for($s = 0; $s < $this->data["count"] and !$this->feof(); ++$s){ $this->data["slots"][$s] = Utils::readSlot($this); } }else{ diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index ae582dd3f..2b0a2ea3f 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -65,6 +65,9 @@ class MinecraftInterface{ if($len === false){ return $pk; } + echo "received packet [".strtoupper(bin2hex(Utils::writeInt(crc32($buf))))."]".PHP_EOL; + global $lastPacketSent; + $lastPacketSent = bin2hex($buf); $this->bandwidth[0] += $len; $this->parsePacket($buf, $source, $port); return ($pk !== false ? $pk : $this->popPacket()); diff --git a/src/network/Packet.php b/src/network/Packet.php index 6e09ad4a1..578e94685 100644 --- a/src/network/Packet.php +++ b/src/network/Packet.php @@ -173,6 +173,10 @@ class Packet{ $this->offset += $len; return $data; } + + private function feof(){ + return !isset($this->raw{$this->offset}); + } protected function addRaw($str){ $this->raw .= $str; @@ -203,10 +207,10 @@ class Packet{ case 0xa0: $cnt = Utils::readShort($this->get(2), false); $this->data[$field] = array(); - for($i = 0; $i < $cnt; ++$i){ + for($i = 0; $i < $cnt and !$this->feof(); ++$i){ if(Utils::readBool($this->get(1)) === false){ $start = Utils::readTriad(strrev($this->get(3))); - $end = Utils::readTriad(strrev($this->get(3))); + $end = min(Utils::readTriad(strrev($this->get(3))), $start + 4096); for($c = $start; $c <= $end; ++$c){ $this->data[$field][] = $c; } diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 159e71570..1409d0ac6 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -162,7 +162,7 @@ class Utils{ $m = array(); $b = ord($value{$offset}); ++$offset; - while($b !== 127){ + while($b !== 127 and isset($value{$offset})){ $bottom = $b & 0x1F; $type = $b >> 5; switch($type){ @@ -220,7 +220,7 @@ class Utils{ public static function readDataArray($str, $len = 10, &$offset = null){ $data = array(); $offset = 0; - for($i = 1; $i <= $len; ++$i){ + for($i = 1; $i <= $len and isset($str{$offset}); ++$i){ $l = Utils::readTriad(substr($str, $offset, 3)); $offset += 3; $data[] = substr($str, $offset, $l);