From 4c27eb415c5a5370418b66ce998292ae8545c37c Mon Sep 17 00:00:00 2001 From: Ray Koopa Date: Sun, 12 Jul 2020 21:27:32 +0200 Subject: [PATCH] Read packets more strictly to prevent crashes from bogus data. --- src/tool/Syroot.Worms.Worms2.GameServer/Packet.cs | 15 ++++++++++----- .../Syroot.Worms.Worms2.GameServer.csproj | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/tool/Syroot.Worms.Worms2.GameServer/Packet.cs b/src/tool/Syroot.Worms.Worms2.GameServer/Packet.cs index f66f724..b6d7f73 100644 --- a/src/tool/Syroot.Worms.Worms2.GameServer/Packet.cs +++ b/src/tool/Syroot.Worms.Worms2.GameServer/Packet.cs @@ -11,6 +11,10 @@ namespace Syroot.Worms.Worms2.GameServer /// internal class Packet { + // ---- CONSTANTS ---------------------------------------------------------------------------------------------- + + private const int _maxDataSize = 0x1000; + // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------ /// @@ -138,8 +142,8 @@ namespace Syroot.Worms.Worms2.GameServer internal void Receive(Stream stream) { int dataLength = 0; - Code = (PacketCode)stream.ReadInt32(); - Flags flags = (Flags)stream.ReadInt32(); + Code = stream.ReadEnum(true); + Flags flags = stream.ReadEnum(true); if (flags.HasFlag(Flags.Value0)) Value0 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value1)) Value1 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value2)) Value2 = stream.ReadInt32(); @@ -147,7 +151,8 @@ namespace Syroot.Worms.Worms2.GameServer if (flags.HasFlag(Flags.Value4)) Value4 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value10)) Value10 = stream.ReadInt32(); if (flags.HasFlag(Flags.DataLength)) dataLength = stream.ReadInt32(); - if (flags.HasFlag(Flags.Data) && dataLength != 0) Data = stream.ReadFixedString(dataLength, Encodings.Windows1252); + if (flags.HasFlag(Flags.Data) && dataLength >= 0 && dataLength <= _maxDataSize) + Data = stream.ReadFixedString(dataLength, Encodings.Windows1252); if (flags.HasFlag(Flags.Error)) Error = stream.ReadInt32(); if (flags.HasFlag(Flags.Name)) Name = stream.ReadFixedString(20, Encodings.Windows1252); if (flags.HasFlag(Flags.Session)) Session = stream.ReadStruct(); @@ -159,8 +164,8 @@ namespace Syroot.Worms.Worms2.GameServer /// The to write the packet data to. internal void Send(Stream stream) { - stream.WriteInt32((int)Code); - stream.WriteInt32((int)GetFlags()); + stream.WriteEnum(Code); + stream.WriteEnum(GetFlags()); if (Value0.HasValue) stream.WriteInt32(Value0.Value); if (Value1.HasValue) stream.WriteInt32(Value1.Value); if (Value2.HasValue) stream.WriteInt32(Value2.Value); diff --git a/src/tool/Syroot.Worms.Worms2.GameServer/Syroot.Worms.Worms2.GameServer.csproj b/src/tool/Syroot.Worms.Worms2.GameServer/Syroot.Worms.Worms2.GameServer.csproj index 99247e9..ac75ecb 100644 --- a/src/tool/Syroot.Worms.Worms2.GameServer/Syroot.Worms.Worms2.GameServer.csproj +++ b/src/tool/Syroot.Worms.Worms2.GameServer/Syroot.Worms.Worms2.GameServer.csproj @@ -7,7 +7,7 @@ Syroot (c) Syroot, licensed under MIT Worms 2 Game Server - 1.0.0 + 1.0.1