From 3ae6eae9e3b130d474f14bd10b4ef05b62fbe02c Mon Sep 17 00:00:00 2001 From: Safariminer Date: Mon, 1 Dec 2025 11:48:53 -0500 Subject: [PATCH] Initial Commit --- .gitignore | 6 + collabdbg.sln | 31 +++++ collabdbg/collabdbg.vcxproj | 139 +++++++++++++++++++++ collabdbg/collabdbg.vcxproj.filters | 30 +++++ collabdbg/collabdbg.vcxproj.user | 4 + collabdbg/collabdbg_PortableExecutable.cpp | 44 +++++++ collabdbg/collabdbg_PortableExecutable.h | 101 +++++++++++++++ collabdbg/main.cpp | 29 +++++ 8 files changed, 384 insertions(+) create mode 100644 .gitignore create mode 100644 collabdbg.sln create mode 100644 collabdbg/collabdbg.vcxproj create mode 100644 collabdbg/collabdbg.vcxproj.filters create mode 100644 collabdbg/collabdbg.vcxproj.user create mode 100644 collabdbg/collabdbg_PortableExecutable.cpp create mode 100644 collabdbg/collabdbg_PortableExecutable.h create mode 100644 collabdbg/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6c8f6d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.vs +x64 +x86 +*/x64 +*/x86 +*.ipch diff --git a/collabdbg.sln b/collabdbg.sln new file mode 100644 index 0000000..291e499 --- /dev/null +++ b/collabdbg.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36717.8 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "collabdbg", "collabdbg\collabdbg.vcxproj", "{87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}.Debug|x64.ActiveCfg = Debug|x64 + {87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}.Debug|x64.Build.0 = Debug|x64 + {87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}.Debug|x86.ActiveCfg = Debug|Win32 + {87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}.Debug|x86.Build.0 = Debug|Win32 + {87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}.Release|x64.ActiveCfg = Release|x64 + {87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}.Release|x64.Build.0 = Release|x64 + {87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}.Release|x86.ActiveCfg = Release|Win32 + {87BBF3DB-CC1E-48B4-9E32-9842FB6CE595}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C1FBA06C-003D-41BF-8633-90A39BBFC210} + EndGlobalSection +EndGlobal diff --git a/collabdbg/collabdbg.vcxproj b/collabdbg/collabdbg.vcxproj new file mode 100644 index 0000000..40df115 --- /dev/null +++ b/collabdbg/collabdbg.vcxproj @@ -0,0 +1,139 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {87bbf3db-cc1e-48b4-9e32-9842fb6ce595} + collabdbg + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpplatest + stdclatest + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpplatest + stdclatest + + + Console + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/collabdbg/collabdbg.vcxproj.filters b/collabdbg/collabdbg.vcxproj.filters new file mode 100644 index 0000000..aef3016 --- /dev/null +++ b/collabdbg/collabdbg.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/collabdbg/collabdbg.vcxproj.user b/collabdbg/collabdbg.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/collabdbg/collabdbg.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/collabdbg/collabdbg_PortableExecutable.cpp b/collabdbg/collabdbg_PortableExecutable.cpp new file mode 100644 index 0000000..50b30e6 --- /dev/null +++ b/collabdbg/collabdbg_PortableExecutable.cpp @@ -0,0 +1,44 @@ +#include "collabdbg_PortableExecutable.h" + +#include +#include +#include + +collabdbg::Microsoft::PortableExecutable::PortableExecutable() +{ +} + +collabdbg::Microsoft::PortableExecutable::PortableExecutable(std::string path) +{ + LoadFrom(path); +} + +void collabdbg::Microsoft::PortableExecutable::LoadFrom(std::string path) +{ + + std::ifstream fileHandle(path); + std::stringstream fileContentHandle; fileContentHandle << fileHandle.rdbuf(); + std::string fileContent = fileContentHandle.str(); + fileHandle.close(); // close file asap + + if (fileContent.size() <= 256) { + throw std::logic_error("File is too small to possibly be an executable of this type."); + } + + std::memcpy(&dosHeader, fileContent.data(), sizeof(DOSHeader)); + + if (dosHeader.magic[0] != 'M' || dosHeader.magic[1] != 'Z') { + throw std::logic_error("This executable lacks a DOS header."); + } + + if (fileContent.size() - sizeof(COFFHeader) < dosHeader.coffHeaderPointer) { + throw std::out_of_range("This executable's COFF header cannot possibly exist in the file."); + } + + std::memcpy(&coffHeader, fileContent.data() + dosHeader.coffHeaderPointer - 1, sizeof(COFFHeader)); + +} + +collabdbg::Microsoft::PortableExecutable::~PortableExecutable() +{ +} diff --git a/collabdbg/collabdbg_PortableExecutable.h b/collabdbg/collabdbg_PortableExecutable.h new file mode 100644 index 0000000..0375db3 --- /dev/null +++ b/collabdbg/collabdbg_PortableExecutable.h @@ -0,0 +1,101 @@ +#pragma once + +#include + +namespace collabdbg { + namespace Microsoft { + + struct DOSHeader { + char magic[2]; + unsigned short extraPageSize; + unsigned short pageCount; + unsigned short relocations; + unsigned short headerSizeInParagraphs; + unsigned short minimumAllocatedParagraphs; + unsigned short maximumAllocatedParagraphs; + unsigned short initialSSValue; + unsigned short initialRelativeSPValue; + unsigned short checksum; + unsigned short initialRelativeIPValue; + unsigned short initialCSValue; + unsigned short relocationsTablePointer; + unsigned short overlayNumber; + unsigned short reservedWords[4]; + unsigned short oemIdentifier; + unsigned short oemInformation; + unsigned short otherReservedWords[10]; + unsigned coffHeaderPointer; + + }; + + + enum ExeArchType : short { + EXE_ARCH_UNKNOWN = 0x0000, + EXE_ARCH_ALPHAAXP_OLD = 0x0183, + EXE_ARCH_ALPHAAXP = 0x0184, + EXE_ARCH_ALPHAAXP_64 = 0x0284, + EXE_ARCH_AM33 = 0x01D3, + EXE_ARCH_AMD64 = 0x8664, + EXE_ARCH_ARM = 0x01C0, + EXE_ARCH_ARM64 = 0xAA64, + EXE_ARCH_ARMNT = 0x01C4, + EXE_ARCH_CLR_PURE_MSIL = 0xC0EE, + EXE_ARCH_EBC = 0x0EBC, + EXE_ARCH_I386 = 0x014C, + EXE_ARCH_I860 = 0x014D, + EXE_ARCH_IA64 = 0x0200, + EXE_ARCH_LOONGARCH_32 = 0x6232, + EXE_ARCH_LOONGARCH_64 = 0x6264, + EXE_ARCH_M32R = 0x9041, + EXE_ARCH_MIPS_16 = 0x0266, + EXE_ARCH_MIPS_FPU = 0x0366, + EXE_ARCH_MIPS_FPU_16 = 0x0466, + EXE_ARCH_MOTOROLA_68000 = 0x0268, + EXE_ARCH_POWERPC = 0x01F0, + EXE_ARCH_POWERPC_FP = 0x01F1, + EXE_ARCH_POWERPC_64 = 0x01F2, + EXE_ARCH_R3000 = 0x0162, + EXE_ARCH_R4000 = 0x0166, + EXE_ARCH_R10000 = 0x0168, + EXE_ARCH_RISCV_32 = 0x5032, + EXE_ARCH_RISCV_64 = 0x5064, + EXE_ARCH_RISCV_128 = 0x5128, + EXE_ARCH_SH3 = 0x01A2, + EXE_ARCH_SH3DSP = 0x01A3, + EXE_ARCH_SH4 = 0x01A6, + EXE_ARCH_SH5 = 0x01A8, + EXE_ARCH_THUMB = 0x01C2, + EXE_ARCH_WCE_MIPS_V2 = 0x0169 + }; + + enum ExePeFormat : short { + EXE_FORMAT_ROM = 0x0107, + EXE_FORMAT_PE32 = 0x010B, + EXE_FORMAT_PE32_PLUS = 0x020B + }; + + struct COFFHeader { + char magic[4]; + ExeArchType architecture; + unsigned short sectionCount; + unsigned timedatestamp; + unsigned pointerToSymbolTable; + unsigned symbolTableCount; + unsigned short optionalHeaderSize; + unsigned short characteristics; + + }; + + class PortableExecutable { + DOSHeader dosHeader; + COFFHeader coffHeader; + public: + PortableExecutable(); + PortableExecutable(std::string path); // constructor shortcut to LoadFrom(path); + void LoadFrom(std::string path); + DOSHeader getDOSHeader() { return dosHeader; }; + COFFHeader getCOFFHeader() { return coffHeader; }; + ~PortableExecutable(); + }; + } +} \ No newline at end of file diff --git a/collabdbg/main.cpp b/collabdbg/main.cpp new file mode 100644 index 0000000..e4d5fec --- /dev/null +++ b/collabdbg/main.cpp @@ -0,0 +1,29 @@ +#include +#include +#include "collabdbg_PortableExecutable.h" + + +int main() { + + collabdbg::Microsoft::PortableExecutable pe; + + try{ + pe.LoadFrom("C:\\Windows\\write.exe"); + } + catch (std::logic_error e) { + std::println("LOGIC : {}", e.what()); + } + catch (std::out_of_range e) { + std::println("OUT OF RANGE : {}", e.what()); + } + catch (std::exception& e) { + std::println("GENERIC : {}", e.what()); + } + catch (...) { + std::println("UNKNOWN EXCEPTION TYPE! FATAL!"); + exit(-1); + } + + std::print("{}", pe.getCOFFHeader().architecture == collabdbg::Microsoft::EXE_ARCH_AMD64 ? "AMD64 Executable" : "Not the expected arch"); + +} \ No newline at end of file