Files
BinaryObjectScanner/BurnOutSharp/FileType/MicrosoftCAB.cs

87 lines
2.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
using System.IO;
2022-05-01 17:41:50 -07:00
using BurnOutSharp.Interfaces;
2021-08-25 15:09:42 -07:00
using BurnOutSharp.Tools;
2022-05-14 21:25:41 -07:00
using WixToolset.Dtf.Compression.Cab;
namespace BurnOutSharp.FileType
{
2020-10-31 23:29:27 -07:00
// Specification available at http://download.microsoft.com/download/5/0/1/501ED102-E53F-4CE0-AA6B-B0F93629DDC6/Exchange/%5BMS-CAB%5D.pdf
public class MicrosoftCAB : IScannable
{
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public bool ShouldScan(byte[] magic)
{
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0x4d, 0x53, 0x43, 0x46 }))
return true;
return false;
}
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
2021-02-26 09:26:23 -08:00
{
if (!File.Exists(file))
return null;
using (var fs = File.OpenRead(file))
{
return Scan(scanner, fs, file);
}
}
// TODO: Add stream opening support
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
{
// If the cab file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
2022-05-14 21:25:41 -07:00
CabInfo cabfile = new CabInfo(file);
foreach (var sub in cabfile.GetFiles())
{
2022-05-14 21:25:41 -07:00
// If an individual entry fails
try
{
2022-05-14 21:25:41 -07:00
// The trim here is for some very odd and stubborn files
string tempFile = Path.Combine(tempPath, sub.Name.TrimEnd('.'));
sub.CopyTo(tempFile);
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
2020-10-28 22:51:33 -07:00
}
2020-10-28 22:51:33 -07:00
// Collect and format all found protections
2020-10-31 14:00:31 -07:00
var protections = scanner.GetProtections(tempPath);
2020-10-28 12:20:07 -07:00
2020-10-28 22:51:33 -07:00
// If temp directory cleanup fails
try
{
Directory.Delete(tempPath, true);
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
return null;
}
}
}