2020-09-10 21:10:32 -07:00
|
|
|
|
using System;
|
2021-07-18 09:44:23 -07:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
2022-05-01 17:41:50 -07:00
|
|
|
|
using BurnOutSharp.Interfaces;
|
2021-07-21 13:33:52 -07:00
|
|
|
|
using UnshieldSharp.Cabinet;
|
2022-12-15 00:13:24 -08:00
|
|
|
|
using static BurnOutSharp.Utilities.Dictionary;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
|
|
|
|
|
namespace BurnOutSharp.FileType
|
|
|
|
|
|
{
|
2022-12-08 21:32:52 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// InstallShield cabinet file
|
|
|
|
|
|
/// </summary>
|
2021-09-10 16:10:15 -07:00
|
|
|
|
public class InstallShieldCAB : IScannable
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2021-02-26 09:26:23 -08:00
|
|
|
|
/// <inheritdoc/>
|
2021-07-18 09:44:23 -07:00
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
|
2021-02-26 09:26:23 -08:00
|
|
|
|
{
|
|
|
|
|
|
if (!File.Exists(file))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
2022-12-22 22:03:32 -08:00
|
|
|
|
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
|
2021-02-26 09:26:23 -08:00
|
|
|
|
{
|
|
|
|
|
|
return Scan(scanner, fs, file);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-10 21:10:32 -07:00
|
|
|
|
// TODO: Add stream opening support
|
2021-02-26 09:26:23 -08:00
|
|
|
|
/// <inheritdoc/>
|
2021-07-18 09:44:23 -07:00
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Get the name of the first cabinet file or header
|
|
|
|
|
|
string directory = Path.GetDirectoryName(file);
|
|
|
|
|
|
string noExtension = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
|
string filenamePattern = Path.Combine(directory, noExtension);
|
|
|
|
|
|
filenamePattern = new Regex(@"\d+$").Replace(filenamePattern, string.Empty);
|
|
|
|
|
|
|
|
|
|
|
|
bool cabinetHeaderExists = File.Exists(Path.Combine(directory, filenamePattern + "1.hdr"));
|
|
|
|
|
|
bool shouldScanCabinet = cabinetHeaderExists
|
|
|
|
|
|
? file.Equals(Path.Combine(directory, filenamePattern + "1.hdr"), StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
|
: file.Equals(Path.Combine(directory, filenamePattern + "1.cab"), StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
|
|
// If we have the first file
|
|
|
|
|
|
if (shouldScanCabinet)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the cab file itself fails
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
|
|
|
|
|
Directory.CreateDirectory(tempPath);
|
|
|
|
|
|
|
2021-07-21 13:33:52 -07:00
|
|
|
|
InstallShieldCabinet cabfile = InstallShieldCabinet.Open(file);
|
2020-09-10 21:10:32 -07:00
|
|
|
|
for (int i = 0; i < cabfile.FileCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If an individual entry fails
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-01-15 17:44:25 -08:00
|
|
|
|
// Check if the file is valid first
|
|
|
|
|
|
if (!cabfile.FileIsValid(i))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2022-06-04 22:02:13 -07:00
|
|
|
|
string tempFile;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-01-15 17:45:11 -08:00
|
|
|
|
string filename = cabfile.FileName(i);
|
2022-06-04 22:02:13 -07:00
|
|
|
|
tempFile = Path.Combine(tempPath, filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
tempFile = Path.Combine(tempPath, $"BAD_FILENAME{i}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-28 12:19:10 -07:00
|
|
|
|
cabfile.FileSave(i, tempFile);
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
2022-05-15 20:58:27 -07:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scanner.IncludeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-28 12:19:10 -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:19:10 -07:00
|
|
|
|
|
2020-09-10 21:10:32 -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);
|
|
|
|
|
|
}
|
2020-10-31 13:20:54 -07:00
|
|
|
|
|
2020-10-31 14:14:35 -07:00
|
|
|
|
// Remove temporary path references
|
2022-12-15 00:13:24 -08:00
|
|
|
|
StripFromKeys(protections, tempPath);
|
2020-10-31 14:14:35 -07:00
|
|
|
|
|
2020-10-31 13:20:54 -07:00
|
|
|
|
return protections;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
2022-05-15 20:58:27 -07:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scanner.IncludeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-31 13:20:54 -07:00
|
|
|
|
return null;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|