mirror of https://github.com/x64dbg/GleeBug
added Static.BufferFile to read/write buffers
This commit is contained in:
parent
c56f2daebd
commit
399503b268
|
|
@ -163,6 +163,7 @@
|
|||
<ClCompile Include="Debugger.Thread.HardwareBreakpoint.cpp" />
|
||||
<ClCompile Include="Debugger.Thread.Registers.cpp" />
|
||||
<ClCompile Include="Debugger.Thread.Registers.GetSet.cpp" />
|
||||
<ClCompile Include="Static.BufferFile.cpp" />
|
||||
<ClCompile Include="Static.File.cpp" />
|
||||
<ClCompile Include="Static.Pe.cpp" />
|
||||
</ItemGroup>
|
||||
|
|
@ -177,6 +178,7 @@
|
|||
<ClInclude Include="Debugger.Thread.Registers.h" />
|
||||
<ClInclude Include="Debugger.Thread.Registers.Register.h" />
|
||||
<ClInclude Include="GleeBug.h" />
|
||||
<ClInclude Include="Static.BufferFile.h" />
|
||||
<ClInclude Include="Static.File.h" />
|
||||
<ClInclude Include="Static.Global.h" />
|
||||
<ClInclude Include="Static.Pe.h" />
|
||||
|
|
|
|||
|
|
@ -68,6 +68,9 @@
|
|||
<ClCompile Include="Static.Pe.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Static.BufferFile.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Debugger.h">
|
||||
|
|
@ -112,5 +115,8 @@
|
|||
<ClInclude Include="Static.Global.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Static.BufferFile.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
@ -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
|
||||
|
|
@ -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<uint8> 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;
|
||||
}
|
||||
Loading…
Reference in New Issue