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.HardwareBreakpoint.cpp" />
|
||||||
<ClCompile Include="Debugger.Thread.Registers.cpp" />
|
<ClCompile Include="Debugger.Thread.Registers.cpp" />
|
||||||
<ClCompile Include="Debugger.Thread.Registers.GetSet.cpp" />
|
<ClCompile Include="Debugger.Thread.Registers.GetSet.cpp" />
|
||||||
|
<ClCompile Include="Static.BufferFile.cpp" />
|
||||||
<ClCompile Include="Static.File.cpp" />
|
<ClCompile Include="Static.File.cpp" />
|
||||||
<ClCompile Include="Static.Pe.cpp" />
|
<ClCompile Include="Static.Pe.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
@ -177,6 +178,7 @@
|
||||||
<ClInclude Include="Debugger.Thread.Registers.h" />
|
<ClInclude Include="Debugger.Thread.Registers.h" />
|
||||||
<ClInclude Include="Debugger.Thread.Registers.Register.h" />
|
<ClInclude Include="Debugger.Thread.Registers.Register.h" />
|
||||||
<ClInclude Include="GleeBug.h" />
|
<ClInclude Include="GleeBug.h" />
|
||||||
|
<ClInclude Include="Static.BufferFile.h" />
|
||||||
<ClInclude Include="Static.File.h" />
|
<ClInclude Include="Static.File.h" />
|
||||||
<ClInclude Include="Static.Global.h" />
|
<ClInclude Include="Static.Global.h" />
|
||||||
<ClInclude Include="Static.Pe.h" />
|
<ClInclude Include="Static.Pe.h" />
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,9 @@
|
||||||
<ClCompile Include="Static.Pe.cpp">
|
<ClCompile Include="Static.Pe.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Static.BufferFile.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Debugger.h">
|
<ClInclude Include="Debugger.h">
|
||||||
|
|
@ -112,5 +115,8 @@
|
||||||
<ClInclude Include="Static.Global.h">
|
<ClInclude Include="Static.Global.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Static.BufferFile.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</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 "MyDebugger.h"
|
||||||
#include "GleeBug/Static.File.h"
|
#include "GleeBug/Static.File.h"
|
||||||
#include "GleeBug/Static.Pe.h"
|
#include "GleeBug/Static.Pe.h"
|
||||||
|
#include "GleeBug/Static.BufferFile.h"
|
||||||
|
|
||||||
static void testDebugger()
|
static void testDebugger()
|
||||||
{
|
{
|
||||||
|
|
@ -44,73 +45,81 @@ static void testStatic()
|
||||||
wchar_t szFilePath[256] = L"c:\\!exclude\\pe\\mini.exe";
|
wchar_t szFilePath[256] = L"c:\\!exclude\\pe\\mini.exe";
|
||||||
#endif //_WIN64
|
#endif //_WIN64
|
||||||
using namespace GleeBug;
|
using namespace GleeBug;
|
||||||
File file(szFilePath, File::ReadOnly);
|
File diskFile(szFilePath, File::ReadOnly);
|
||||||
if (file.Open())
|
if (diskFile.Open())
|
||||||
{
|
{
|
||||||
Pe pe(file);
|
auto diskSize = diskFile.GetSize();
|
||||||
if (pe.ParseHeaders() == Pe::ErrorOk)
|
std::vector<uint8> diskData(diskSize);
|
||||||
|
if (diskFile.Read(0, diskData.data(), diskSize))
|
||||||
{
|
{
|
||||||
auto idh = pe.GetDosHeader();
|
BufferFile file(diskData.data(), diskSize);
|
||||||
printRegion("DOS Header:", idh);
|
Pe pe(file);
|
||||||
printf(" e_magic: %02X\n", idh->e_magic);
|
auto parseError = pe.ParseHeaders();
|
||||||
printf(" e_lfanew: %08X\n", idh->e_lfanew);
|
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();
|
auto afterDosData = pe.GetAfterDosData();
|
||||||
printRegion("After DOS Data", afterDosData);
|
printRegion("After DOS Data", afterDosData);
|
||||||
|
|
||||||
#ifdef _WIN64
|
#ifdef _WIN64
|
||||||
auto inth = pe.GetNtHeaders64();
|
auto inth = pe.GetNtHeaders64();
|
||||||
#else //x32
|
#else //x32
|
||||||
auto inth = pe.GetNtHeaders32();
|
auto inth = pe.GetNtHeaders32();
|
||||||
#endif //_WIN64
|
#endif //_WIN64
|
||||||
printRegion("NT Headers:", inth);
|
printRegion("NT Headers:", inth);
|
||||||
printf(" Signature: %08X\n", inth->Signature);
|
printf(" Signature: %08X\n", inth->Signature);
|
||||||
|
|
||||||
PIMAGE_FILE_HEADER ifh = &inth->FileHeader;
|
PIMAGE_FILE_HEADER ifh = &inth->FileHeader;
|
||||||
puts("\n File Header:");
|
puts("\n File Header:");
|
||||||
printf(" Machine : %04X\n", ifh->Machine);
|
printf(" Machine : %04X\n", ifh->Machine);
|
||||||
printf(" NumberOfSections: %04X\n", ifh->NumberOfSections);
|
printf(" NumberOfSections: %04X\n", ifh->NumberOfSections);
|
||||||
printf(" TimeDateStamp : %08X\n", ifh->TimeDateStamp);
|
printf(" TimeDateStamp : %08X\n", ifh->TimeDateStamp);
|
||||||
|
|
||||||
PIMAGE_OPTIONAL_HEADER ioh = &inth->OptionalHeader;
|
PIMAGE_OPTIONAL_HEADER ioh = &inth->OptionalHeader;
|
||||||
puts("\n Optional Header:");
|
puts("\n Optional Header:");
|
||||||
printf(" Magic : %04X\n", ioh->Magic);
|
printf(" Magic : %04X\n", ioh->Magic);
|
||||||
printf(" EntryPoint: %08X\n", ioh->AddressOfEntryPoint);
|
printf(" EntryPoint: %08X\n", ioh->AddressOfEntryPoint);
|
||||||
printf(" ImageBase : %p\n", ioh->ImageBase);
|
printf(" ImageBase : %p\n", ioh->ImageBase);
|
||||||
printf(" Subsystem : %04X\n", ioh->Subsystem);
|
printf(" Subsystem : %04X\n", ioh->Subsystem);
|
||||||
|
|
||||||
auto afterOptionalData = pe.GetAfterOptionalData();
|
auto afterOptionalData = pe.GetAfterOptionalData();
|
||||||
printRegion("After Optional Data", afterOptionalData);
|
printRegion("After Optional Data", afterOptionalData);
|
||||||
|
|
||||||
auto ish = pe.GetSectionHeaders();
|
auto ish = pe.GetSectionHeaders();
|
||||||
printRegion("Section Headers:", ish);
|
printRegion("Section Headers:", ish);
|
||||||
for (auto i = 0; i < ifh->NumberOfSections; i++)
|
for (auto i = 0; i < ifh->NumberOfSections; i++)
|
||||||
{
|
{
|
||||||
if (i)
|
if (i)
|
||||||
puts("");
|
puts("");
|
||||||
auto cur = ish.Data() + i;
|
auto cur = ish.Data() + i;
|
||||||
printf(" Section %d:\n", i + 1);
|
printf(" Section %d:\n", i + 1);
|
||||||
char name[9] = "";
|
char name[9] = "";
|
||||||
memcpy(name, cur->Name, sizeof(cur->Name));
|
memcpy(name, cur->Name, sizeof(cur->Name));
|
||||||
printf(" Name : %s\n", name);
|
printf(" Name : %s\n", name);
|
||||||
printf(" VSize: %08X\n", cur->Misc.VirtualSize);
|
printf(" VSize: %08X\n", cur->Misc.VirtualSize);
|
||||||
printf(" VAddr: %08X\n", cur->VirtualAddress);
|
printf(" VAddr: %08X\n", cur->VirtualAddress);
|
||||||
printf(" RSize: %08X\n", cur->SizeOfRawData);
|
printf(" RSize: %08X\n", cur->SizeOfRawData);
|
||||||
printf(" RAddr: %08X\n", cur->PointerToRawData);
|
printf(" RAddr: %08X\n", cur->PointerToRawData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
printf("Pe::ParseHeaders failed (%d)!\n", parseError);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
puts("Pe::ParseHeaders failed!");
|
puts("File::Read failed!");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
|
||||||
puts("File::Open failed!");
|
puts("File::Open failed!");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
testStatic();
|
testStatic();
|
||||||
|
puts("");
|
||||||
system("pause");
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue