diff --git a/GleeBug/GleeBug.vcxproj b/GleeBug/GleeBug.vcxproj index 578546d..7744da8 100644 --- a/GleeBug/GleeBug.vcxproj +++ b/GleeBug/GleeBug.vcxproj @@ -163,6 +163,7 @@ + @@ -177,6 +178,7 @@ + diff --git a/GleeBug/GleeBug.vcxproj.filters b/GleeBug/GleeBug.vcxproj.filters index 8fd3aba..ec68345 100644 --- a/GleeBug/GleeBug.vcxproj.filters +++ b/GleeBug/GleeBug.vcxproj.filters @@ -68,6 +68,9 @@ Source Files + + Source Files + @@ -112,5 +115,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/GleeBug/Static.BufferFile.cpp b/GleeBug/Static.BufferFile.cpp new file mode 100644 index 0000000..8bd2cb8 --- /dev/null +++ b/GleeBug/Static.BufferFile.cpp @@ -0,0 +1,69 @@ +#include "Static.BufferFile.h" + +namespace GleeBug +{ + BufferFile::BufferFile(void* data, uint32 size) + : File(nullptr), + _data(data), + _size(size) + { + } + + bool BufferFile::Open() + { + return true; + } + + bool BufferFile::Create(bool) + { + return true; + } + + bool BufferFile::IsOpen() const + { + return !!_data; + } + + void BufferFile::Close() + { + } + + uint32 BufferFile::GetSize() const + { + return _size; + } + + bool BufferFile::Read(uint32 offset, void* data, uint32 size, uint32* bytesRead) const + { + if (offset >= _size) + return false; + auto readSize = size; + auto result = true; + if (offset + size > _size) + { + readSize = _size - offset; + result = false; + } + memcpy(data, (uint8*)_data + offset, readSize); + if (bytesRead) + *bytesRead = readSize; + return result; + } + + bool BufferFile::Write(uint32 offset, const void* data, uint32 size, uint32* bytesWritten) + { + if (offset >= _size) + return false; + auto writeSize = size; + auto result = true; + if (offset + size > _size) + { + writeSize = _size - offset; + result = false; + } + memcpy((uint8*)_data + offset, data, writeSize); + if (bytesWritten) + *bytesWritten = writeSize; + return result; + } +}; \ No newline at end of file diff --git a/GleeBug/Static.BufferFile.h b/GleeBug/Static.BufferFile.h new file mode 100644 index 0000000..590ce9e --- /dev/null +++ b/GleeBug/Static.BufferFile.h @@ -0,0 +1,67 @@ +#ifndef _STATIC_BUFFERFILE_H +#define _STATIC_BUFFERFILE_H + +#include "Static.File.h" + +namespace GleeBug +{ + class BufferFile : public File + { + public: + BufferFile(void* data, uint32 size); + + /** + \brief Opens an existing file. + \return true if the file was opened successfully, false otherwise. + */ + virtual bool Open() override; + + /** + \brief Creates a new file. + \param overwrite (Optional) true to overwrite, false to preserve fail if the file already exists. + \return true if the file was created, false otherwise. + */ + virtual bool Create(bool overwrite = true) override; + + /** + \brief Check if there is an open/created file. + */ + virtual bool IsOpen() const override; + + /** + \brief Closes the file. + */ + virtual void Close() override; + + /** + \brief Gets the size of the file. + */ + virtual uint32 GetSize() const override; + + /** + \brief Reads from the open file. + \param offset The offset to start reading from. + \param [out] data Destination buffer. + \param size The size to read. + \param [out] bytesRead (Optional) If set, returns the number of bytes read (even on failure). + \return true if the read was fully successful, false otherwise. + */ + virtual bool Read(uint32 offset, void* data, uint32 size, uint32* bytesRead = nullptr) const override; + + /** + \brief Writes to the open file. + \param offset The offset to start writing to. Everything after this offset will be truncated! + \param data The data to write. + \param size The size to write. + \param [out] bytesWritten (Optional) If set, returns the number of bytes written (even on failure) + \return true if the write was fully successful, false otherwise. + */ + virtual bool Write(uint32 offset, const void* data, uint32 size, uint32* bytesWritten = nullptr) override; + + private: + void* _data; + uint32 _size; + }; +}; + +#endif //_STATIC_BUFFERFILE_H \ No newline at end of file diff --git a/MyDebugger/main.cpp b/MyDebugger/main.cpp index eab78ff..f4d60ab 100644 --- a/MyDebugger/main.cpp +++ b/MyDebugger/main.cpp @@ -2,6 +2,7 @@ #include "MyDebugger.h" #include "GleeBug/Static.File.h" #include "GleeBug/Static.Pe.h" +#include "GleeBug/Static.BufferFile.h" static void testDebugger() { @@ -44,73 +45,81 @@ static void testStatic() wchar_t szFilePath[256] = L"c:\\!exclude\\pe\\mini.exe"; #endif //_WIN64 using namespace GleeBug; - File file(szFilePath, File::ReadOnly); - if (file.Open()) + File diskFile(szFilePath, File::ReadOnly); + if (diskFile.Open()) { - Pe pe(file); - if (pe.ParseHeaders() == Pe::ErrorOk) + auto diskSize = diskFile.GetSize(); + std::vector diskData(diskSize); + if (diskFile.Read(0, diskData.data(), diskSize)) { - auto idh = pe.GetDosHeader(); - printRegion("DOS Header:", idh); - printf(" e_magic: %02X\n", idh->e_magic); - printf(" e_lfanew: %08X\n", idh->e_lfanew); + BufferFile file(diskData.data(), diskSize); + Pe pe(file); + auto parseError = pe.ParseHeaders(); + if (parseError == Pe::ErrorOk) + { + auto idh = pe.GetDosHeader(); + printRegion("DOS Header:", idh); + printf(" e_magic: %02X\n", idh->e_magic); + printf(" e_lfanew: %08X\n", idh->e_lfanew); - auto afterDosData = pe.GetAfterDosData(); - printRegion("After DOS Data", afterDosData); + auto afterDosData = pe.GetAfterDosData(); + printRegion("After DOS Data", afterDosData); #ifdef _WIN64 - auto inth = pe.GetNtHeaders64(); + auto inth = pe.GetNtHeaders64(); #else //x32 - auto inth = pe.GetNtHeaders32(); + auto inth = pe.GetNtHeaders32(); #endif //_WIN64 - printRegion("NT Headers:", inth); - printf(" Signature: %08X\n", inth->Signature); + printRegion("NT Headers:", inth); + printf(" Signature: %08X\n", inth->Signature); - PIMAGE_FILE_HEADER ifh = &inth->FileHeader; - puts("\n File Header:"); - printf(" Machine : %04X\n", ifh->Machine); - printf(" NumberOfSections: %04X\n", ifh->NumberOfSections); - printf(" TimeDateStamp : %08X\n", ifh->TimeDateStamp); + PIMAGE_FILE_HEADER ifh = &inth->FileHeader; + puts("\n File Header:"); + printf(" Machine : %04X\n", ifh->Machine); + printf(" NumberOfSections: %04X\n", ifh->NumberOfSections); + printf(" TimeDateStamp : %08X\n", ifh->TimeDateStamp); - PIMAGE_OPTIONAL_HEADER ioh = &inth->OptionalHeader; - puts("\n Optional Header:"); - printf(" Magic : %04X\n", ioh->Magic); - printf(" EntryPoint: %08X\n", ioh->AddressOfEntryPoint); - printf(" ImageBase : %p\n", ioh->ImageBase); - printf(" Subsystem : %04X\n", ioh->Subsystem); + PIMAGE_OPTIONAL_HEADER ioh = &inth->OptionalHeader; + puts("\n Optional Header:"); + printf(" Magic : %04X\n", ioh->Magic); + printf(" EntryPoint: %08X\n", ioh->AddressOfEntryPoint); + printf(" ImageBase : %p\n", ioh->ImageBase); + printf(" Subsystem : %04X\n", ioh->Subsystem); - auto afterOptionalData = pe.GetAfterOptionalData(); - printRegion("After Optional Data", afterOptionalData); + auto afterOptionalData = pe.GetAfterOptionalData(); + printRegion("After Optional Data", afterOptionalData); - auto ish = pe.GetSectionHeaders(); - printRegion("Section Headers:", ish); - for (auto i = 0; i < ifh->NumberOfSections; i++) - { - if (i) - puts(""); - auto cur = ish.Data() + i; - printf(" Section %d:\n", i + 1); - char name[9] = ""; - memcpy(name, cur->Name, sizeof(cur->Name)); - printf(" Name : %s\n", name); - printf(" VSize: %08X\n", cur->Misc.VirtualSize); - printf(" VAddr: %08X\n", cur->VirtualAddress); - printf(" RSize: %08X\n", cur->SizeOfRawData); - printf(" RAddr: %08X\n", cur->PointerToRawData); + auto ish = pe.GetSectionHeaders(); + printRegion("Section Headers:", ish); + for (auto i = 0; i < ifh->NumberOfSections; i++) + { + if (i) + puts(""); + auto cur = ish.Data() + i; + printf(" Section %d:\n", i + 1); + char name[9] = ""; + memcpy(name, cur->Name, sizeof(cur->Name)); + printf(" Name : %s\n", name); + printf(" VSize: %08X\n", cur->Misc.VirtualSize); + printf(" VAddr: %08X\n", cur->VirtualAddress); + printf(" RSize: %08X\n", cur->SizeOfRawData); + printf(" RAddr: %08X\n", cur->PointerToRawData); + } } + else + printf("Pe::ParseHeaders failed (%d)!\n", parseError); } else - puts("Pe::ParseHeaders failed!"); + puts("File::Read failed!"); } else - { puts("File::Open failed!"); - } } int main() { testStatic(); + puts(""); system("pause"); return 0; } \ No newline at end of file