From d82f8ac8b37114fe180a0e02f56289ae0f3141a2 Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 4 Oct 2023 15:50:24 +1100 Subject: [PATCH] Catch bad DNS queries and reduce exception logging --- app/Classes/Protocol/DNS.php | 9 ++++++++- app/Classes/Protocol/DNS/Query.php | 7 +++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Classes/Protocol/DNS.php b/app/Classes/Protocol/DNS.php index e6d9fbd..bbc7b79 100644 --- a/app/Classes/Protocol/DNS.php +++ b/app/Classes/Protocol/DNS.php @@ -104,7 +104,14 @@ final class DNS extends BaseProtocol { Log::debug(sprintf('%s:+ DNS Query',self::LOGKEY)); - $this->query = new BaseProtocol\DNS\Query($this->client->read(0,512)); + try { + $this->query = new BaseProtocol\DNS\Query($this->client->read(0,512)); + + } catch (\Exception $e) { + Log::error(sprintf('%s:! Ignoring bad DNS query (%s)',self::LOGKEY,$e->getMessage())); + + return FALSE; + } Log::info(sprintf('%s:= DNS Query from [%s] for [%s]',self::LOGKEY,$this->client->address_remote,$this->query->domain)); diff --git a/app/Classes/Protocol/DNS/Query.php b/app/Classes/Protocol/DNS/Query.php index 9a77ae8..02120d9 100644 --- a/app/Classes/Protocol/DNS/Query.php +++ b/app/Classes/Protocol/DNS/Query.php @@ -61,7 +61,7 @@ final class Query } 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; + throw $e; } $rx_ptr += 4; @@ -82,9 +82,8 @@ final class Query $rx_ptr += $this->additional->length; } - if (strlen($this->buf) !== $rx_ptr) { - dd(['query remaining'=>strlen($this->buf)-$rx_ptr,'hex'=>hex_dump(substr($this->buf,$rx_ptr))]); - } + if (strlen($this->buf) !== $rx_ptr) + throw new \Exception(sprintf('! DNS Buffer still has [%d]: %s',strlen($this->buf)-$rx_ptr,hex_dump(substr($this->buf,$rx_ptr)))); } public function __get($key)