From b7c1c97cf7e3748b2ff87774ba70e573b8cf5b9c Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 20 Sep 2023 22:26:35 +1000 Subject: [PATCH] Catch DNS Query that fail unpack() --- app/Classes/Protocol/DNS/Query.php | 16 +++++++++++++--- app/Classes/Sock/SocketClient.php | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/Classes/Protocol/DNS/Query.php b/app/Classes/Protocol/DNS/Query.php index f9003fc..ac9d18b 100644 --- a/app/Classes/Protocol/DNS/Query.php +++ b/app/Classes/Protocol/DNS/Query.php @@ -32,7 +32,8 @@ final class Query 'arcount' => [0x05,'n',1], // Resource Records in the addition records section ]; - public function __construct(string $buf) { + public function __construct(string $buf) + { $this->buf = $buf; $rx_ptr = 0; @@ -54,7 +55,15 @@ final class Query } // Get the query type/class - $result = unpack('ntype/nclass',substr($this->buf,$rx_ptr,4)); + try { + $result = unpack('ntype/nclass',substr($this->buf,$rx_ptr,4)); + + } catch (\Exception $e) { + Log::error(sprintf('%s:! Unpack failed: Buffer: [%s] (%d), RXPTR [%d]',self::LOGKEY,hex_dump($this->buf),strlen($this->buf),$rx_ptr)); + + return; + } + $rx_ptr += 4; $this->type = $result['type']; $this->class = $result['class']; @@ -96,7 +105,8 @@ final class Query } } - public static function header_len() { + public static function header_len() + { return collect(self::header)->sum(function($item) { return $item[2]*2; }); } diff --git a/app/Classes/Sock/SocketClient.php b/app/Classes/Sock/SocketClient.php index 11996f0..8a8626d 100644 --- a/app/Classes/Sock/SocketClient.php +++ b/app/Classes/Sock/SocketClient.php @@ -320,7 +320,7 @@ final class SocketClient { } } catch (\Exception $e) { - Log::error(sprintf('%s: - socket_recv Exception [%s]',self::LOGKEY,$e->getMessage())); + Log::error(sprintf('%s:! socket_recv Exception [%s]',self::LOGKEY,$e->getMessage())); throw new SocketException($x=socket_last_error($this->connection),socket_strerror($x)); }