From b7c93e12de557cb11aa30b41fefd35962a828be0 Mon Sep 17 00:00:00 2001 From: KimLS Date: Sat, 26 Oct 2024 10:03:57 -0700 Subject: [PATCH] Add some sanity checking for stream_parser so it's less likely to pickup other protocols as an everquest protocol. --- .../common/Daybreak/Connection.cs | 45 ++++++++++++------- utils/stream_parser/common/Daybreak/Parser.cs | 6 +++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/utils/stream_parser/common/Daybreak/Connection.cs b/utils/stream_parser/common/Daybreak/Connection.cs index 454d16efe..98fa93b5a 100644 --- a/utils/stream_parser/common/Daybreak/Connection.cs +++ b/utils/stream_parser/common/Daybreak/Connection.cs @@ -176,6 +176,11 @@ namespace StreamParser.Common.Daybreak case Opcode.SessionResponse: if (_connect_code == 0) { + if(data.Length != 21) + { + return; + } + _connect_code = BitConverter.ToUInt32(data.Slice(2, 4)); _encode_key = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data.Slice(6, 4))); _crc_bytes = data[10]; @@ -443,27 +448,33 @@ namespace StreamParser.Common.Daybreak private bool ValidateCRC(ReadOnlySpan p) { - if (_crc_bytes == 0) + try { - return true; - } + if (_crc_bytes == 0) + { + return true; + } - int actual = 0; - int calculated = _crc_generator.Calculate(p.Slice(0, p.Length - _crc_bytes), _encode_key); - switch (_crc_bytes) + int actual = 0; + int calculated = _crc_generator.Calculate(p.Slice(0, p.Length - _crc_bytes), _encode_key); + switch (_crc_bytes) + { + case 2: + actual = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(p.Slice(p.Length - 2, 2))) & 0xFFFF; + calculated = calculated & 0xFFFF; + break; + case 4: + actual = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(p.Slice(p.Length - 4, 4))); + break; + default: + return false; + } + + return actual == calculated; + } catch(Exception ex) { - case 2: - actual = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(p.Slice(p.Length - 2, 2))) & 0xFFFF; - calculated = calculated & 0xFFFF; - break; - case 4: - actual = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(p.Slice(p.Length - 4, 4))); - break; - default: - return false; + return false; } - - return actual == calculated; } private byte[] Decompress(byte[] p, int offset, int length) diff --git a/utils/stream_parser/common/Daybreak/Parser.cs b/utils/stream_parser/common/Daybreak/Parser.cs index 8c95dd6f9..be5d24e96 100644 --- a/utils/stream_parser/common/Daybreak/Parser.cs +++ b/utils/stream_parser/common/Daybreak/Parser.cs @@ -87,6 +87,12 @@ namespace StreamParser.Common.Daybreak } else if (data[0] == 0 && data[1] == Opcode.SessionRequest) { + if(data.Length != 24) + { + _logger.LogTrace("Tossing packet, {0} was not the right size for a SessionRequest", data.Length); + return; + } + c = new Connection(this, srcAddr, srcPort, dstAddr, dstPort); _connections.Add(c); c.ProcessPacket(srcAddr, srcPort, packetTime, data);