mirror of
https://github.com/SabreTools/BinaryObjectScanner.git
synced 2026-05-21 07:36:29 +00:00
Consolidate detectable Executable checks
This commit is contained in:
@@ -26,16 +26,5 @@ namespace BinaryObjectScanner.Test.FileType
|
|||||||
string? actual = detectable.Detect(stream, file, includeDebug: false);
|
string? actual = detectable.Detect(stream, file, includeDebug: false);
|
||||||
Assert.Null(actual);
|
Assert.Null(actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void DetectDict_EmptyStream_Empty()
|
|
||||||
{
|
|
||||||
Stream? stream = new MemoryStream();
|
|
||||||
string file = string.Empty;
|
|
||||||
var detectable = new Executable();
|
|
||||||
|
|
||||||
ProtectionDictionary actual = detectable.DetectDict(stream, file, includeDebug: false);
|
|
||||||
Assert.Empty(actual);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,28 +25,6 @@ namespace BinaryObjectScanner.FileType
|
|||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public string? Detect(Stream stream, string file, bool includeDebug)
|
public string? Detect(Stream stream, string file, bool includeDebug)
|
||||||
{
|
|
||||||
// Get all non-nested protections
|
|
||||||
var protections = DetectDict(stream, file, includeDebug);
|
|
||||||
if (protections.Count == 0)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// Create the internal list
|
|
||||||
var protectionList = new List<string>();
|
|
||||||
foreach (string key in protections.Keys)
|
|
||||||
{
|
|
||||||
protectionList.AddRange(protections[key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return string.Join(";", [.. protectionList]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IDetectable.Detect(Stream, string, bool)"/>
|
|
||||||
/// <remarks>
|
|
||||||
/// Ideally, we wouldn't need to circumvent the proper handling of file types just for Executable,
|
|
||||||
/// but due to the complexity of scanning, this is not currently possible.
|
|
||||||
/// </remarks>
|
|
||||||
public ProtectionDictionary DetectDict(Stream stream, string file, bool includeDebug)
|
|
||||||
{
|
{
|
||||||
// Create the output dictionary
|
// Create the output dictionary
|
||||||
var protections = new ProtectionDictionary();
|
var protections = new ProtectionDictionary();
|
||||||
@@ -57,12 +35,12 @@ namespace BinaryObjectScanner.FileType
|
|||||||
{
|
{
|
||||||
wrapper = WrapperFactory.CreateExecutableWrapper(stream);
|
wrapper = WrapperFactory.CreateExecutableWrapper(stream);
|
||||||
if (wrapper == null)
|
if (wrapper == null)
|
||||||
return protections;
|
return null;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
if (includeDebug) Console.Error.WriteLine(ex);
|
if (includeDebug) Console.Error.WriteLine(ex);
|
||||||
return protections;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only use generic content checks if we're in debug mode
|
// Only use generic content checks if we're in debug mode
|
||||||
@@ -101,7 +79,18 @@ namespace BinaryObjectScanner.FileType
|
|||||||
protections.Append(file, subProtections.Values);
|
protections.Append(file, subProtections.Values);
|
||||||
}
|
}
|
||||||
|
|
||||||
return protections;
|
// If there are no protections
|
||||||
|
if (protections.Count == 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Create the internal list
|
||||||
|
var protectionList = new List<string>();
|
||||||
|
foreach (string key in protections.Keys)
|
||||||
|
{
|
||||||
|
protectionList.AddRange(protections[key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return string.Join(";", [.. protectionList]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
|||||||
@@ -275,21 +275,10 @@ namespace BinaryObjectScanner
|
|||||||
|
|
||||||
// If we're scanning file contents
|
// If we're scanning file contents
|
||||||
if (detectable != null && _options.ScanContents)
|
if (detectable != null && _options.ScanContents)
|
||||||
{
|
|
||||||
// If we have an executable, it needs to bypass normal handling
|
|
||||||
if (detectable is Executable executable)
|
|
||||||
{
|
|
||||||
var subProtections = executable.DetectDict(stream, fileName, _options.IncludeDebug);
|
|
||||||
protections.Append(subProtections);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, use the default implementation
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
var subProtection = detectable.Detect(stream, fileName, _options.IncludeDebug);
|
var subProtection = detectable.Detect(stream, fileName, _options.IncludeDebug);
|
||||||
protections.Append(fileName, subProtection);
|
protections.Append(fileName, subProtection);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user