Files
BinaryObjectScanner/ExecutableTest/Program.cs

124 lines
4.7 KiB
C#
Raw Normal View History

using System.Text;
using BurnOutSharp.Wrappers;
using static BurnOutSharp.Builder.Extensions;
2022-11-08 11:35:39 -08:00
namespace ExecutableTest
{
public class ExecutableTest
{
public static void Main(string[] args)
{
// Invalid arguments means nothing to do
if (args == null || args.Length == 0)
{
Console.WriteLine("Please provide at least one file path");
Console.ReadLine();
return;
}
// Register the codepages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
2022-11-08 11:35:39 -08:00
// Loop through the args
foreach (string arg in args)
{
2022-11-08 13:07:48 -08:00
Console.WriteLine($"Checking possible path: {arg}");
2022-11-08 11:35:39 -08:00
// Check the file exists
if (!File.Exists(arg))
{
Console.WriteLine($"{arg} does not exist or is not a file, skipping...");
continue;
}
using (Stream stream = File.OpenRead(arg))
{
// Read the first 4 bytes
byte[] magic = stream.ReadBytes(2);
if (magic[0] != 'M' || magic[1] != 'Z')
{
Console.WriteLine("Not a recognized executable format, skipping...");
Console.WriteLine();
continue;
}
// Build the executable information
Console.WriteLine("Creating MS-DOS executable builder");
Console.WriteLine();
stream.Seek(0, SeekOrigin.Begin);
var msdos = MSDOS.Create(stream);
2022-11-08 11:35:39 -08:00
if (msdos == null)
{
Console.WriteLine("Something went wrong parsing MS-DOS executable");
Console.WriteLine();
continue;
}
// Print the executable info to screen
msdos.Print();
2022-11-08 11:35:39 -08:00
// Check for a valid new executable address
if (msdos.NewExeHeaderAddr >= stream.Length)
2022-11-08 11:35:39 -08:00
{
Console.WriteLine("New EXE header address invalid, skipping additional reading...");
Console.WriteLine();
continue;
}
2022-11-08 13:07:48 -08:00
// Try to read the executable info
stream.Seek(msdos.NewExeHeaderAddr, SeekOrigin.Begin);
2022-11-08 11:35:39 -08:00
magic = stream.ReadBytes(4);
// New Executable
if (magic[0] == 'N' && magic[1] == 'E')
{
2022-11-08 13:07:48 -08:00
stream.Seek(0, SeekOrigin.Begin);
var newExecutable = NewExecutable.Create(stream);
2022-11-08 13:07:48 -08:00
if (newExecutable == null)
{
Console.WriteLine("Something went wrong parsing New Executable");
Console.WriteLine();
continue;
}
2022-11-08 11:35:39 -08:00
2022-11-08 13:07:48 -08:00
// Print the executable info to screen
newExecutable.Print();
2022-11-08 11:35:39 -08:00
}
// Linear Executable
else if (magic[0] == 'L' && (magic[1] == 'E' || magic[1] == 'X'))
{
Console.WriteLine($"Linear executable found. No parsing currently available.");
Console.WriteLine();
continue;
}
// Portable Executable
else if (magic[0] == 'P' && magic[1] == 'E' && magic[2] == '\0' && magic[3] == '\0')
{
2022-11-08 15:11:18 -08:00
stream.Seek(0, SeekOrigin.Begin);
var portableExecutable = PortableExecutable.Create(stream);
2022-11-08 15:11:18 -08:00
if (portableExecutable == null)
{
Console.WriteLine("Something went wrong parsing Portable Executable");
Console.WriteLine();
continue;
}
// Print the executable info to screen
portableExecutable.Print();
2022-11-08 11:35:39 -08:00
}
// Unknown
else
{
Console.WriteLine($"Unrecognized header signature: {BitConverter.ToString(magic).Replace("-", string.Empty)}");
Console.WriteLine();
continue;
}
}
}
}
}
}