Add some sanity checking for stream_parser so it's less likely to pickup other protocols as an everquest protocol.

This commit is contained in:
KimLS 2024-10-26 10:03:57 -07:00
parent c1651b7dca
commit b7c93e12de
2 changed files with 34 additions and 17 deletions

View File

@ -176,6 +176,11 @@ namespace StreamParser.Common.Daybreak
case Opcode.SessionResponse: case Opcode.SessionResponse:
if (_connect_code == 0) if (_connect_code == 0)
{ {
if(data.Length != 21)
{
return;
}
_connect_code = BitConverter.ToUInt32(data.Slice(2, 4)); _connect_code = BitConverter.ToUInt32(data.Slice(2, 4));
_encode_key = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data.Slice(6, 4))); _encode_key = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data.Slice(6, 4)));
_crc_bytes = data[10]; _crc_bytes = data[10];
@ -443,27 +448,33 @@ namespace StreamParser.Common.Daybreak
private bool ValidateCRC(ReadOnlySpan<byte> p) private bool ValidateCRC(ReadOnlySpan<byte> p)
{ {
if (_crc_bytes == 0) try
{ {
return true; if (_crc_bytes == 0)
} {
return true;
}
int actual = 0; int actual = 0;
int calculated = _crc_generator.Calculate(p.Slice(0, p.Length - _crc_bytes), _encode_key); int calculated = _crc_generator.Calculate(p.Slice(0, p.Length - _crc_bytes), _encode_key);
switch (_crc_bytes) 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: return false;
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;
} }
private byte[] Decompress(byte[] p, int offset, int length) private byte[] Decompress(byte[] p, int offset, int length)

View File

@ -87,6 +87,12 @@ namespace StreamParser.Common.Daybreak
} }
else if (data[0] == 0 && data[1] == Opcode.SessionRequest) 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); c = new Connection(this, srcAddr, srcPort, dstAddr, dstPort);
_connections.Add(c); _connections.Add(c);
c.ProcessPacket(srcAddr, srcPort, packetTime, data); c.ProcessPacket(srcAddr, srcPort, packetTime, data);