From 7024136919d60141cd3fe5c2670add8cc5e1d712 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 15 Jul 2021 09:57:06 -0700 Subject: [PATCH] Make large file parsing safer (fixes #44) --- BurnOutSharp/FileType/Executable.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/BurnOutSharp/FileType/Executable.cs b/BurnOutSharp/FileType/Executable.cs index 198fc83f..55e13da5 100644 --- a/BurnOutSharp/FileType/Executable.cs +++ b/BurnOutSharp/FileType/Executable.cs @@ -63,20 +63,28 @@ namespace BurnOutSharp.FileType /// public Dictionary> Scan(Scanner scanner, Stream stream, string file) { + // Files can be protected in multiple ways + var protections = new Dictionary>(); + // Load the current file content byte[] fileContent = null; - using (BinaryReader br = new BinaryReader(stream, Encoding.Default, true)) + try { - fileContent = br.ReadBytes((int)stream.Length); + using (BinaryReader br = new BinaryReader(stream, Encoding.Default, true)) + { + fileContent = br.ReadBytes((int)stream.Length); + } + } + catch + { + Utilities.AppendToDictionary(protections, file, "[File too large to be scanned]"); + return protections; } // If we can, seek to the beginning of the stream if (stream.CanSeek) stream.Seek(0, SeekOrigin.Begin); - // Files can be protected in multiple ways - var protections = new Dictionary>(); - // Iterate through all content checks foreach (var contentCheckClass in contentCheckClasses) {