From 4c3c6e757ce7da4baed8fb31ac16bc6392adec11 Mon Sep 17 00:00:00 2001 From: atom0s Date: Sun, 28 May 2023 02:07:05 -0700 Subject: [PATCH] API: (PE32) Adjusted 'Parse' method to exit early if the DOS stub is invalid. (Avoids attempting to read an invalid NT header block.) API: (PE64) Adjusted 'Parse' method to exit early if the DOS stub is invalid. (Avoids attempting to read an invalid NT header block.) --- Steamless.API/PE32/Pe32File.cs | 10 ++++++---- Steamless.API/PE64/Pe64File.cs | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Steamless.API/PE32/Pe32File.cs b/Steamless.API/PE32/Pe32File.cs index 1d849dc..b33259c 100644 --- a/Steamless.API/PE32/Pe32File.cs +++ b/Steamless.API/PE32/Pe32File.cs @@ -85,12 +85,14 @@ namespace Steamless.API.PE32 if (this.FileData.Length < (Marshal.SizeOf(typeof(NativeApi32.ImageDosHeader32)) + Marshal.SizeOf(typeof(NativeApi32.ImageNtHeaders32)))) return false; - // Read the file headers.. + // Read the file DOS header.. this.DosHeader = Pe32Helpers.GetStructure(this.FileData); - this.NtHeaders = Pe32Helpers.GetStructure(this.FileData, this.DosHeader.e_lfanew); + if (!this.DosHeader.IsValid) + return false; - // Validate the headers.. - if (!this.DosHeader.IsValid || !this.NtHeaders.IsValid) + // Read the file NT headers.. + this.NtHeaders = Pe32Helpers.GetStructure(this.FileData, this.DosHeader.e_lfanew); + if (!this.NtHeaders.IsValid) return false; // Read and store the dos header if it exists.. diff --git a/Steamless.API/PE64/Pe64File.cs b/Steamless.API/PE64/Pe64File.cs index 366e0a2..a586b43 100644 --- a/Steamless.API/PE64/Pe64File.cs +++ b/Steamless.API/PE64/Pe64File.cs @@ -85,12 +85,14 @@ namespace Steamless.API.PE64 if (this.FileData.Length < (Marshal.SizeOf(typeof(NativeApi64.ImageDosHeader64)) + Marshal.SizeOf(typeof(NativeApi64.ImageNtHeaders64)))) return false; - // Read the file headers.. + // Read the file DOS header.. this.DosHeader = Pe64Helpers.GetStructure(this.FileData); - this.NtHeaders = Pe64Helpers.GetStructure(this.FileData, this.DosHeader.e_lfanew); + if (!this.DosHeader.IsValid) + return false; - // Validate the headers.. - if (!this.DosHeader.IsValid || !this.NtHeaders.IsValid) + // Read the file NT headers.. + this.NtHeaders = Pe64Helpers.GetStructure(this.FileData, this.DosHeader.e_lfanew); + if (!this.NtHeaders.IsValid) return false; // Read and store the dos header if it exists..