2025-04-12 17:03:04 +03:00
|
|
|
using X86Disassembler.PE;
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
namespace X86Disassembler;
|
|
|
|
|
|
|
|
internal class Program
|
2025-04-12 16:42:44 +03:00
|
|
|
{
|
2025-04-12 18:23:18 +03:00
|
|
|
// Path to the DLL file to disassemble
|
|
|
|
private const string DllPath = @"C:\Program Files (x86)\Nikita\Iron Strategy\Terrain.dll"; // Example path, replace with your target DLL
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
|
|
|
Console.WriteLine("X86 Disassembler and Decompiler");
|
|
|
|
Console.WriteLine("--------------------------------");
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine($"Loading file: {DllPath}");
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Load the DLL file
|
|
|
|
byte[] binaryData = File.ReadAllBytes(DllPath);
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine($"Successfully loaded {DllPath}");
|
|
|
|
Console.WriteLine($"File size: {binaryData.Length} bytes");
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Create the PE format parser
|
|
|
|
PEFormat peFile = new PEFormat(binaryData);
|
2025-04-12 17:05:23 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Parse the PE format
|
|
|
|
Console.WriteLine("\nParsing PE format...");
|
|
|
|
if (!peFile.Parse())
|
|
|
|
{
|
|
|
|
Console.WriteLine("Failed to parse PE file. Exiting.");
|
|
|
|
return;
|
|
|
|
}
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Display basic PE information
|
|
|
|
DisplayPEInfo(peFile);
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Display exported functions
|
|
|
|
DisplayExportedFunctions(peFile);
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Display imported functions
|
|
|
|
DisplayImportedFunctions(peFile);
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Find code sections for disassembly
|
|
|
|
var codeSections = peFile.GetCodeSections();
|
|
|
|
Console.WriteLine($"\nFound {codeSections.Count} code section(s):");
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
foreach (int sectionIndex in codeSections)
|
|
|
|
{
|
|
|
|
var section = peFile.SectionHeaders[sectionIndex];
|
|
|
|
Console.WriteLine($" - {section.Name}: Size={section.SizeOfRawData} bytes, RVA=0x{section.VirtualAddress:X8}");
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Get the section data for disassembly
|
|
|
|
byte[] sectionData = peFile.GetSectionData(sectionIndex);
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// TODO: Implement disassembling logic here
|
|
|
|
// This is where we would pass the section data to our disassembler
|
2025-04-12 16:42:44 +03:00
|
|
|
}
|
2025-04-12 18:23:18 +03:00
|
|
|
|
|
|
|
Console.WriteLine("\nPress any key to exit...");
|
|
|
|
Console.ReadKey();
|
|
|
|
}
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
private static void DisplayPEInfo(PEFormat peFile)
|
|
|
|
{
|
|
|
|
Console.WriteLine("\nPE File Information:");
|
|
|
|
Console.WriteLine($"Architecture: {(peFile.Is64Bit ? "64-bit" : "32-bit")}");
|
|
|
|
Console.WriteLine($"Entry Point: 0x{peFile.OptionalHeader.AddressOfEntryPoint:X8}");
|
|
|
|
Console.WriteLine($"Image Base: 0x{peFile.OptionalHeader.ImageBase:X}");
|
|
|
|
Console.WriteLine($"Number of Sections: {peFile.FileHeader.NumberOfSections}");
|
|
|
|
|
|
|
|
// Display section information
|
|
|
|
Console.WriteLine("\nSections:");
|
|
|
|
for (int i = 0; i < peFile.SectionHeaders.Count; i++)
|
2025-04-12 16:42:44 +03:00
|
|
|
{
|
2025-04-12 18:23:18 +03:00
|
|
|
var section = peFile.SectionHeaders[i];
|
|
|
|
string flags = "";
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
if ((section.Characteristics & 0x00000020) != 0) flags += "Code "; // IMAGE_SCN_CNT_CODE
|
|
|
|
if ((section.Characteristics & 0x20000000) != 0) flags += "Exec "; // IMAGE_SCN_MEM_EXECUTE
|
|
|
|
if ((section.Characteristics & 0x40000000) != 0) flags += "Read "; // IMAGE_SCN_MEM_READ
|
|
|
|
if ((section.Characteristics & 0x80000000) != 0) flags += "Write"; // IMAGE_SCN_MEM_WRITE
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine($" {i}: {section.Name,-8} VA=0x{section.VirtualAddress:X8} Size={section.SizeOfRawData,-8} [{flags}]");
|
2025-04-12 16:42:44 +03:00
|
|
|
}
|
2025-04-12 18:23:18 +03:00
|
|
|
}
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
private static void DisplayExportedFunctions(PEFormat peFile)
|
|
|
|
{
|
|
|
|
if (peFile.ExportDirectory == null)
|
2025-04-12 16:42:44 +03:00
|
|
|
{
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine("\nNo exported functions found.");
|
|
|
|
return;
|
|
|
|
}
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine("\nExported Functions:");
|
|
|
|
Console.WriteLine($"DLL Name: {peFile.ExportDirectory.DllName}");
|
|
|
|
Console.WriteLine($"Number of Functions: {peFile.ExportDirectory.NumberOfFunctions}");
|
|
|
|
Console.WriteLine($"Number of Names: {peFile.ExportDirectory.NumberOfNames}");
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Display all exported functions
|
|
|
|
for (int i = 0; i < peFile.ExportedFunctions.Count; i++)
|
|
|
|
{
|
|
|
|
var function = peFile.ExportedFunctions[i];
|
|
|
|
Console.WriteLine($" {i}: {function.Name} (Ordinal={function.Ordinal}, RVA=0x{function.Address:X8})");
|
2025-04-12 16:42:44 +03:00
|
|
|
}
|
2025-04-12 18:23:18 +03:00
|
|
|
}
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
private static void DisplayImportedFunctions(PEFormat peFile)
|
|
|
|
{
|
|
|
|
if (peFile.ImportDescriptors.Count == 0)
|
2025-04-12 16:42:44 +03:00
|
|
|
{
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine("\nNo imported functions found.");
|
|
|
|
return;
|
|
|
|
}
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine("\nImported Functions:");
|
|
|
|
Console.WriteLine($"Number of Imported DLLs: {peFile.ImportDescriptors.Count}");
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Display all imported DLLs and their functions
|
|
|
|
for (int i = 0; i < peFile.ImportDescriptors.Count; i++)
|
|
|
|
{
|
|
|
|
var descriptor = peFile.ImportDescriptors[i];
|
|
|
|
Console.WriteLine($" DLL: {descriptor.DllName}");
|
2025-04-12 16:42:44 +03:00
|
|
|
|
2025-04-12 18:23:18 +03:00
|
|
|
// Display all functions from this DLL
|
|
|
|
for (int j = 0; j < descriptor.Functions.Count; j++)
|
|
|
|
{
|
|
|
|
var function = descriptor.Functions[j];
|
|
|
|
if (function.IsOrdinal)
|
2025-04-12 16:42:44 +03:00
|
|
|
{
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine($" {j}: Ordinal {function.Ordinal}");
|
2025-04-12 16:42:44 +03:00
|
|
|
}
|
2025-04-12 18:23:18 +03:00
|
|
|
else
|
2025-04-12 16:42:44 +03:00
|
|
|
{
|
2025-04-12 18:23:18 +03:00
|
|
|
Console.WriteLine($" {j}: {function.Name} (Hint={function.Hint})");
|
2025-04-12 16:42:44 +03:00
|
|
|
}
|
|
|
|
}
|
2025-04-12 18:23:18 +03:00
|
|
|
|
|
|
|
if (i < peFile.ImportDescriptors.Count - 1)
|
|
|
|
{
|
|
|
|
Console.WriteLine(); // Add a blank line between DLLs for better readability
|
|
|
|
}
|
2025-04-12 16:42:44 +03:00
|
|
|
}
|
|
|
|
}
|
2025-04-12 18:23:18 +03:00
|
|
|
}
|