Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacharyPL committed Aug 22, 2024
2 parents a12799a + e4c1dd6 commit 5a17125
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
57 changes: 53 additions & 4 deletions payload/import/mkw/net/eventHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ class EventHandler

static_assert(sizeof(EventInfo) == 0x01);

struct ItemUsedEvent {
bool isValid(u8 itemObject, u8 playerAid) const
{
using namespace mkw::Item;

ItemObject item = static_cast<ItemObject>(itemObject);

switch (item) {
case ItemObject::ThunderCloud: {
return this->playerAid == playerAid;
}
default: {
return true;
}
}
}

/* 0x00 */ u8 _00[0x02 - 0x00];
/* 0x02 */ u8 playerAid;
};

static_assert(sizeof(ItemUsedEvent) == 0x03);

bool containsInvalidItemObject() const
{
for (size_t n = 0; n < sizeof(eventInfo); n++) {
Expand All @@ -94,9 +117,9 @@ class EventHandler
return false;
}

bool isValid(u8 packetSize) const
bool isValid(u8 packetSize, u8 playerAid) const
{
u32 expectedPacketSize = sizeof(eventInfo);
size_t expectedPacketSize = sizeof(eventInfo);

for (size_t n = 0; n < sizeof(eventInfo); n++) {
if (!eventInfo[n].isValid()) {
Expand All @@ -106,11 +129,37 @@ class EventHandler
expectedPacketSize += eventInfo[n].getEventDataSize();
}

return expectedPacketSize == packetSize;
if (expectedPacketSize != packetSize) {
return false;
}

expectedPacketSize = 0;
for (size_t n = 0; n < sizeof(eventInfo); n++) {
EventInfo info = eventInfo[n];
u8 itemObject = info.itemObject;
const u8* data = eventData + expectedPacketSize;

switch (info.eventType) {
case EventInfo::EventType::ItemUsed: {
const ItemUsedEvent* itemUsedEvent =
reinterpret_cast<const ItemUsedEvent*>(data);

if (!itemUsedEvent->isValid(itemObject, playerAid)) {
return false;
}
}
default: {
}
}

expectedPacketSize += info.getEventDataSize();
}

return true;
}

/* 0x00 */ EventInfo eventInfo[0x18];
/* 0x18 */ u8 _18[0xF8 - 0x18];
/* 0x18 */ u8 eventData[0xF8 - 0x18];
};

static_assert(sizeof(Packet) == 0xF8);
Expand Down
2 changes: 1 addition & 1 deletion payload/import/mkw/net/selectHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class SelectHandler

// Support modifications that allow for clients to be connected to more
// than 11 peers at once.
for (u32 n = 0; n < sizeof(aidsStillLoading) * 8; n++) {
for (size_t n = 0; n < sizeof(aidsStillLoading) * 8; n++) {
if (((aidsStillLoading >> n) & 1) == 0) {
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions payload/wwfcSecurity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ static bool IsHeaderPacketDataValid(
const void* /* packet */, u8 /* packetSize */, u8 /* playerAid */
)
{
// This packet is validated by the function 'IsRacePacketValid'
return true;
}

Expand Down Expand Up @@ -497,9 +498,8 @@ IsItemPacketDataValid(const void* packet, u8 packetSize, u8 /* playerAid */)
return true;
}

static bool IsEventPacketDataValid(
const void* packet, u8 packetSize, u8 /* playerAid */
)
static bool
IsEventPacketDataValid(const void* packet, u8 packetSize, u8 playerAid)
{
using namespace mkw::System;

Expand All @@ -522,7 +522,7 @@ static bool IsEventPacketDataValid(
return true;
}

if (!eventPacket->isValid(packetSize)) {
if (!eventPacket->isValid(packetSize, playerAid)) {
return false;
}

Expand Down

0 comments on commit 5a17125

Please sign in to comment.