Read packets more strictly to prevent crashes from bogus data.

This commit is contained in:
Ray Koopa 2020-07-12 21:27:32 +02:00
parent 48e6988083
commit 4c27eb415c
2 changed files with 11 additions and 6 deletions

View File

@ -11,6 +11,10 @@ namespace Syroot.Worms.Worms2.GameServer
/// </summary>
internal class Packet
{
// ---- CONSTANTS ----------------------------------------------------------------------------------------------
private const int _maxDataSize = 0x1000;
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
/// <summary>
@ -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<PacketCode>(true);
Flags flags = stream.ReadEnum<Flags>(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<SessionInfo>();
@ -159,8 +164,8 @@ namespace Syroot.Worms.Worms2.GameServer
/// <param name="stream">The <see cref="Stream"/> to write the packet data to.</param>
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);

View File

@ -7,7 +7,7 @@
<Authors>Syroot</Authors>
<Copyright>(c) Syroot, licensed under MIT</Copyright>
<Description>Worms 2 Game Server</Description>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
</PropertyGroup>
<!-- References -->