Replace parts of Scan with Extract

This commit is contained in:
Matt Nadareski
2023-03-09 14:50:24 -05:00
parent 1dca51988a
commit 6e5d517e82
22 changed files with 124 additions and 449 deletions

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the BFPK file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.BFPK bfpk = BinaryObjectScanner.Wrappers.BFPK.Create(stream);
if (bfpk == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Extract all files
bfpk.ExtractAll(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -61,18 +61,11 @@ namespace BurnOutSharp.FileType
// If the BSP file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.BSP bsp = BinaryObjectScanner.Wrappers.BSP.Create(stream);
if (bsp == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Loop through and extract all files
bsp.ExtractAllLumps(tempPath);
bsp.ExtractAllTextures(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -63,25 +63,10 @@ namespace BurnOutSharp.FileType
// If the BZip2 file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (BZip2Stream bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true))
{
// If an individual entry fails
try
{
string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString());
using (FileStream fs = File.OpenWrite(tempFile))
{
bz2File.CopyTo(fs);
}
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -90,43 +90,10 @@ namespace BurnOutSharp.FileType
// If the MSI file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (CompoundFile msi = new CompoundFile(stream, CFSUpdateMode.ReadOnly, CFSConfiguration.Default))
{
msi.RootStorage.VisitEntries((e) =>
{
if (!e.IsStream)
return;
var str = msi.RootStorage.GetStream(e.Name);
if (str == null)
return;
byte[] strData = str.GetData();
if (strData == null)
return;
string decoded = DecodeStreamName(e.Name).TrimEnd('\0');
byte[] nameBytes = Encoding.UTF8.GetBytes(e.Name);
// UTF-8 encoding of 0x4840.
if (nameBytes[0] == 0xe4 && nameBytes[1] == 0xa1 && nameBytes[2] == 0x80)
decoded = decoded.Substring(3);
foreach (char c in Path.GetInvalidFileNameChars())
{
decoded = decoded.Replace(c, '_');
}
string filename = Path.Combine(tempPath, decoded);
using (Stream fs = File.OpenWrite(filename))
{
fs.Write(strData, 0, strData.Length);
}
}, recursive: true);
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the GCF file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.GCF gcf = BinaryObjectScanner.Wrappers.GCF.Create(stream);
if (gcf == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Loop through and extract all files
gcf.ExtractAll(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -67,29 +67,10 @@ namespace BurnOutSharp.FileType
// If the gzip file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (GZipArchive zipFile = GZipArchive.Open(stream))
{
foreach (var entry in zipFile.Entries)
{
// If an individual entry fails
try
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
entry.WriteToFile(tempFile);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -72,33 +72,10 @@ namespace BurnOutSharp.FileType
// If the archive itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
UnshieldSharp.Archive.InstallShieldArchiveV3 archive = new UnshieldSharp.Archive.InstallShieldArchiveV3(file);
foreach (CompressedFile cfile in archive.Files.Select(kvp => kvp.Value))
{
// If an individual entry fails
try
{
string tempFile = Path.Combine(tempPath, cfile.FullPath);
if (!Directory.Exists(Path.GetDirectoryName(tempFile)))
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
(byte[] fileContents, string error) = archive.Extract(cfile.FullPath);
if (!string.IsNullOrWhiteSpace(error))
continue;
using (FileStream fs = File.OpenWrite(tempFile))
{
fs.Write(fileContents, 0, fileContents.Length);
}
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -29,6 +29,21 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// 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 anything but the first file
if (!shouldScanCabinet)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
@@ -73,77 +88,35 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
{
// 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
{
// If the cab file itself fails
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);
// If temp directory cleanup fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
InstallShieldCabinet cabfile = InstallShieldCabinet.Open(file);
for (int i = 0; i < cabfile.FileCount; i++)
{
// If an individual entry fails
try
{
// Check if the file is valid first
if (!cabfile.FileIsValid(i))
continue;
string tempFile;
try
{
string filename = cabfile.FileName(i);
tempFile = Path.Combine(tempPath, filename);
}
catch
{
tempFile = Path.Combine(tempPath, $"BAD_FILENAME{i}");
}
cabfile.FileSave(i, tempFile);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);
// If temp directory cleanup fails
try
{
Directory.Delete(tempPath, true);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Remove temporary path references
StripFromKeys(protections, tempPath);
return protections;
Directory.Delete(tempPath, true);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Remove temporary path references
StripFromKeys(protections, tempPath);
return protections;
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
return null;

View File

@@ -94,44 +94,10 @@ namespace BurnOutSharp.FileType
// If the MPQ file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (MpqArchive mpqArchive = new MpqArchive(file, FileAccess.Read))
{
// Try to open the listfile
string listfile = null;
MpqFileStream listStream = mpqArchive.OpenFile("(listfile)");
// If we can't read the listfile, we just return
if (!listStream.CanRead)
return null;
// Read the listfile in for processing
using (StreamReader sr = new StreamReader(listStream))
{
listfile = sr.ReadToEnd();
}
// Split the listfile by newlines
string[] listfileLines = listfile.Replace("\r\n", "\n").Split('\n');
// Loop over each entry
foreach (string sub in listfileLines)
{
// If an individual entry fails
try
{
string tempFile = Path.Combine(tempPath, sub);
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
mpqArchive.ExtractFile(sub, tempFile);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -65,30 +65,10 @@ namespace BurnOutSharp.FileType
// If the cab file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Open the cab file
var cabFile = MicrosoftCabinet.Create(stream);
if (cabFile == null)
{
if (scanner.IncludeDebug) Console.WriteLine($"Error occurred while opening");
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
}
// If entry extraction fails
try
{
bool success = cabFile.ExtractAll(tempPath);
if (!success)
{
if (scanner.IncludeDebug) Console.WriteLine($"Error occurred during extraction of files");
}
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -76,30 +76,10 @@ namespace BurnOutSharp.FileType
// If the LZ file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
byte[] data = LZ.Decompress(stream);
// Create the temp filename
string tempFile = "temp.bin";
if (!string.IsNullOrEmpty(file))
{
string expandedFilePath = LZ.GetExpandedName(file, out _);
tempFile = Path.GetFileName(expandedFilePath).TrimEnd('\0');
if (tempFile.EndsWith(".ex"))
tempFile += "e";
else if (tempFile.EndsWith(".dl"))
tempFile += "l";
}
tempFile = Path.Combine(tempPath, tempFile);
// Write the file data to a temp file
using (Stream tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
tempStream.Write(data, 0, data.Length);
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the PAK file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.PAK pak = BinaryObjectScanner.Wrappers.PAK.Create(stream);
if (pak == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Loop through and extract all files
pak.ExtractAll(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the PFF file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.PFF bfpk = BinaryObjectScanner.Wrappers.PFF.Create(stream);
if (bfpk == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Extract all files
bfpk.ExtractAll(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -68,30 +68,10 @@ namespace BurnOutSharp.FileType
// If the zip file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (ZipArchive zipFile = ZipArchive.Open(stream))
{
foreach (var entry in zipFile.Entries)
{
// If an individual entry fails
try
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
entry.WriteToFile(tempFile);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the SGA file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.SGA sga = BinaryObjectScanner.Wrappers.SGA.Create(stream);
if (sga == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Loop through and extract all files
sga.ExtractAll(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -67,47 +67,28 @@ namespace BurnOutSharp.FileType
// If the 7-zip file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
using (SevenZipArchive sevenZipFile = SevenZipArchive.Open(stream))
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);
// If temp directory cleanup fails
try
{
foreach (var entry in sevenZipFile.Entries)
{
// If an individual entry fails
try
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
entry.WriteToFile(tempFile);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);
// If temp directory cleanup fails
try
{
Directory.Delete(tempPath, true);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Remove temporary path references
StripFromKeys(protections, tempPath);
return protections;
Directory.Delete(tempPath, true);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Remove temporary path references
StripFromKeys(protections, tempPath);
return protections;
}
catch (Exception ex)
{

View File

@@ -67,29 +67,10 @@ namespace BurnOutSharp.FileType
// If the tar file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (TarArchive tarFile = TarArchive.Open(stream))
{
foreach (var entry in tarFile.Entries)
{
// If an individual entry fails
try
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
entry.WriteToFile(tempFile);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the VBSP file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.VBSP vbsp = BinaryObjectScanner.Wrappers.VBSP.Create(stream);
if (vbsp == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Loop through and extract all files
vbsp.ExtractAllLumps(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the VPK file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.VPK vpk = BinaryObjectScanner.Wrappers.VPK.Create(stream);
if (vpk == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Loop through and extract all files
vpk.ExtractAll(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the WAD file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.WAD wad = BinaryObjectScanner.Wrappers.WAD.Create(stream);
if (wad == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Loop through and extract all files
wad.ExtractAllLumps(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -62,25 +62,10 @@ namespace BurnOutSharp.FileType
// If the xz file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (XZStream xzFile = new XZStream(stream))
{
// If an individual entry fails
try
{
string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString());
using (FileStream fs = File.OpenWrite(tempFile))
{
xzFile.CopyTo(fs);
}
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
}
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);

View File

@@ -60,17 +60,11 @@ namespace BurnOutSharp.FileType
// If the XZP file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
BinaryObjectScanner.Wrappers.XZP xzp = BinaryObjectScanner.Wrappers.XZP.Create(stream);
if (xzp == null)
// Extract and get the output path
string tempPath = Extract(stream, file);
if (tempPath == null)
return null;
// Loop through and extract all files
xzp.ExtractAll(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);