Files
BinaryObjectScanner/BurnOutSharp/FileType/MSI.cs

75 lines
2.1 KiB
C#
Raw Normal View History

2020-10-28 22:51:33 -07:00
using System;
using System.Collections.Concurrent;
2020-10-28 22:51:33 -07:00
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;
2021-03-02 13:09:15 -08:00
using WixToolset.Dtf.WindowsInstaller;
2020-10-28 22:51:33 -07:00
namespace BurnOutSharp.FileType
{
public class MSI : IScannable
2020-10-28 22:51:33 -07:00
{
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public bool ShouldScan(byte[] magic)
2020-10-28 22:51:33 -07:00
{
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }))
2020-10-28 22:51:33 -07:00
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);
}
}
2020-10-28 22:51:33 -07:00
// 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)
2020-10-28 22:51:33 -07:00
{
// If the MSI file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (Database msidb = new Database(file, DatabaseOpenMode.ReadOnly))
{
msidb.ExportAll(tempPath);
}
// Collect and format all found protections
2020-10-31 14:00:31 -07:00
var protections = scanner.GetProtections(tempPath);
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;
2020-10-28 22:51:33 -07:00
}
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
return null;
2020-10-28 22:51:33 -07:00
}
}
}