From fdaa58a6fa5bd32190a81dade2e82337d2d34d2e Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 11 Jan 2022 03:06:16 +0100 Subject: [PATCH] Improved malformed packet detection --- RNS/Link.py | 2 +- RNS/Packet.py | 42 ++++++++++++++++++++++++------------------ RNS/Transport.py | 4 +++- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/RNS/Link.py b/RNS/Link.py index 9cc4019..1978389 100644 --- a/RNS/Link.py +++ b/RNS/Link.py @@ -765,7 +765,7 @@ class Link: return plaintext except Exception as e: RNS.log("Decryption failed on link "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) - RNS.log(traceback.format_exc(), RNS.LOG_ERROR) + # RNS.log(traceback.format_exc(), RNS.LOG_ERROR) # TODO: Think long about implications here # self.teardown() diff --git a/RNS/Packet.py b/RNS/Packet.py index 77b1c9a..e196b3e 100755 --- a/RNS/Packet.py +++ b/RNS/Packet.py @@ -185,27 +185,33 @@ class Packet: def unpack(self): - self.flags = self.raw[0] - self.hops = self.raw[1] + try: + self.flags = self.raw[0] + self.hops = self.raw[1] - self.header_type = (self.flags & 0b11000000) >> 6 - self.transport_type = (self.flags & 0b00110000) >> 4 - self.destination_type = (self.flags & 0b00001100) >> 2 - self.packet_type = (self.flags & 0b00000011) + self.header_type = (self.flags & 0b11000000) >> 6 + self.transport_type = (self.flags & 0b00110000) >> 4 + self.destination_type = (self.flags & 0b00001100) >> 2 + self.packet_type = (self.flags & 0b00000011) - if self.header_type == Packet.HEADER_2: - self.transport_id = self.raw[2:12] - self.destination_hash = self.raw[12:22] - self.context = ord(self.raw[22:23]) - self.data = self.raw[23:] - else: - self.transport_id = None - self.destination_hash = self.raw[2:12] - self.context = ord(self.raw[12:13]) - self.data = self.raw[13:] + if self.header_type == Packet.HEADER_2: + self.transport_id = self.raw[2:12] + self.destination_hash = self.raw[12:22] + self.context = ord(self.raw[22:23]) + self.data = self.raw[23:] + else: + self.transport_id = None + self.destination_hash = self.raw[2:12] + self.context = ord(self.raw[12:13]) + self.data = self.raw[13:] - self.packed = False - self.update_hash() + self.packed = False + self.update_hash() + return True + + except Exception as e: + RNS.log("Received malformed packet, dropping it. The contained exception was: "+str(e), RNS.LOG_EXTREME) + return False def send(self): """ diff --git a/RNS/Transport.py b/RNS/Transport.py index 0d397fb..55fb8d9 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -585,7 +585,9 @@ class Transport: Transport.jobs_locked = True packet = RNS.Packet(None, raw) - packet.unpack() + if not packet.unpack(): + return + packet.receiving_interface = interface packet.hops += 1