From 6b6fdd5bb5371bc40d48407b2b269f19a29b9a0d Mon Sep 17 00:00:00 2001 From: Matthijs Lavrijsen Date: Sat, 10 Oct 2020 02:15:04 +0200 Subject: [PATCH] Emulator: implement StaticFileLoad and GetPE32Data --- GleeBug/GleeBug.vcxproj | 2 ++ GleeBug/GleeBug.vcxproj.filters | 6 ++++++ StaticEngine/Emulator.h | 13 ++++++++++++- StaticEngine/TitanEngineEmulator.cpp | 12 +++++++++++- TitanEngineEmulator/Emulator.h | 11 +++++++++++ TitanEngineEmulator/TitanEngineEmulator.cpp | 10 ++++++++++ 6 files changed, 52 insertions(+), 2 deletions(-) diff --git a/GleeBug/GleeBug.vcxproj b/GleeBug/GleeBug.vcxproj index de60782..caa7900 100644 --- a/GleeBug/GleeBug.vcxproj +++ b/GleeBug/GleeBug.vcxproj @@ -171,6 +171,7 @@ + _CRT_SECURE_NO_WARNINGS;_USING_V110_SDK71_;%(PreprocessorDefinitions) @@ -203,6 +204,7 @@ + diff --git a/GleeBug/GleeBug.vcxproj.filters b/GleeBug/GleeBug.vcxproj.filters index d778bb0..9f8c1ea 100644 --- a/GleeBug/GleeBug.vcxproj.filters +++ b/GleeBug/GleeBug.vcxproj.filters @@ -104,6 +104,9 @@ Source Files\Zydis + + Source Files + @@ -193,6 +196,9 @@ Header Files + + Header Files + diff --git a/StaticEngine/Emulator.h b/StaticEngine/Emulator.h index 7647407..31a349f 100644 --- a/StaticEngine/Emulator.h +++ b/StaticEngine/Emulator.h @@ -5,7 +5,8 @@ #include "ntdll.h" #include "FileMap.h" #include -#include +#include +#include #pragma comment(lib, "psapi.lib") @@ -467,6 +468,11 @@ public: std::unordered_map mappedFiles; //PE + bool StaticFileLoad(const char* szFileName, DWORD DesiredAccess, bool SimulateLoad, LPHANDLE FileHandle, LPDWORD LoadedSize, LPHANDLE FileMap, PULONG_PTR FileMapVA) + { + return StaticFileLoadW(Utf8ToUtf16(szFileName).c_str(), DesiredAccess, SimulateLoad, FileHandle, LoadedSize, FileMap, FileMapVA); + } + bool StaticFileLoadW(const wchar_t* szFileName, DWORD DesiredAccess, bool SimulateLoad, LPHANDLE FileHandle, LPDWORD LoadedSize, LPHANDLE FileMap, PULONG_PTR FileMapVA) { auto file = new ::FileMap; @@ -594,6 +600,11 @@ public: : GetPE32DataW_impl(found->second.pe->GetNtHeaders32(), WhichSection, WhichData, sections); } + ULONG_PTR GetPE32Data(const char* szFileName, DWORD WhichSection, DWORD WhichData) + { + return GetPE32DataW(Utf8ToUtf16(szFileName).c_str(), WhichSection, WhichData); + } + ULONG_PTR GetPE32DataW(const wchar_t* szFileName, DWORD WhichSection, DWORD WhichData) { FileMap file; diff --git a/StaticEngine/TitanEngineEmulator.cpp b/StaticEngine/TitanEngineEmulator.cpp index 8d799dd..6d7955d 100644 --- a/StaticEngine/TitanEngineEmulator.cpp +++ b/StaticEngine/TitanEngineEmulator.cpp @@ -1,4 +1,4 @@ -#include +#include #include "Emulator.h" Emulator emu; @@ -172,6 +172,11 @@ __declspec(dllexport) void TITCALL Getx87FPURegisters(x87FPURegister_t x87FPUReg } //PE +__declspec(dllexport) bool TITCALL StaticFileLoad(const char* szFileName, DWORD DesiredAccess, bool SimulateLoad, LPHANDLE FileHandle, LPDWORD LoadedSize, LPHANDLE FileMap, PULONG_PTR FileMapVA) +{ + return emu.StaticFileLoad(szFileName, DesiredAccess, SimulateLoad, FileHandle, LoadedSize, FileMap, FileMapVA); +} + __declspec(dllexport) bool TITCALL StaticFileLoadW(const wchar_t* szFileName, DWORD DesiredAccess, bool SimulateLoad, LPHANDLE FileHandle, LPDWORD LoadedSize, LPHANDLE FileMap, PULONG_PTR FileMapVA) { return emu.StaticFileLoadW(szFileName, DesiredAccess, SimulateLoad, FileHandle, LoadedSize, FileMap, FileMapVA); @@ -202,6 +207,11 @@ __declspec(dllexport) ULONG_PTR TITCALL GetPE32DataFromMappedFile(ULONG_PTR File return emu.GetPE32DataFromMappedFile(FileMapVA, WhichSection, WhichData); } +__declspec(dllexport) ULONG_PTR TITCALL GetPE32Data(const char* szFileName, DWORD WhichSection, DWORD WhichData) +{ + return emu.GetPE32Data(szFileName, WhichSection, WhichData); +} + __declspec(dllexport) ULONG_PTR TITCALL GetPE32DataW(const wchar_t* szFileName, DWORD WhichSection, DWORD WhichData) { return emu.GetPE32DataW(szFileName, WhichSection, WhichData); diff --git a/TitanEngineEmulator/Emulator.h b/TitanEngineEmulator/Emulator.h index 9bbcafe..67d08ff 100644 --- a/TitanEngineEmulator/Emulator.h +++ b/TitanEngineEmulator/Emulator.h @@ -2,6 +2,7 @@ #include #include #include +#include #include "TitanEngine.h" #include "FileMap.h" #include "PEB.h" @@ -531,6 +532,11 @@ public: std::unordered_map mappedFiles; //PE + bool StaticFileLoad(const char* szFileName, DWORD DesiredAccess, bool SimulateLoad, LPHANDLE FileHandle, LPDWORD LoadedSize, LPHANDLE FileMap, PULONG_PTR FileMapVA) + { + return StaticFileLoadW(Utf8ToUtf16(szFileName).c_str(), DesiredAccess, SimulateLoad, FileHandle, LoadedSize, FileMap, FileMapVA); + } + bool StaticFileLoadW(const wchar_t* szFileName, DWORD DesiredAccess, bool SimulateLoad, LPHANDLE FileHandle, LPDWORD LoadedSize, LPHANDLE FileMap, PULONG_PTR FileMapVA) { auto file = new ::FileMap; @@ -655,6 +661,11 @@ public: : GetPE32DataW_impl(found->second.pe->GetNtHeaders32(), WhichSection, WhichData, sections); } + ULONG_PTR GetPE32Data(const char* szFileName, DWORD WhichSection, DWORD WhichData) + { + return GetPE32DataW(Utf8ToUtf16(szFileName).c_str(), WhichSection, WhichData); + } + ULONG_PTR GetPE32DataW(const wchar_t* szFileName, DWORD WhichSection, DWORD WhichData) { FileMap file; diff --git a/TitanEngineEmulator/TitanEngineEmulator.cpp b/TitanEngineEmulator/TitanEngineEmulator.cpp index 83ddb51..d6dd248 100644 --- a/TitanEngineEmulator/TitanEngineEmulator.cpp +++ b/TitanEngineEmulator/TitanEngineEmulator.cpp @@ -172,6 +172,11 @@ __declspec(dllexport) void TITCALL Getx87FPURegisters(x87FPURegister_t x87FPUReg } //PE +__declspec(dllexport) bool TITCALL StaticFileLoad(const char* szFileName, DWORD DesiredAccess, bool SimulateLoad, LPHANDLE FileHandle, LPDWORD LoadedSize, LPHANDLE FileMap, PULONG_PTR FileMapVA) +{ + return emu.StaticFileLoad(szFileName, DesiredAccess, SimulateLoad, FileHandle, LoadedSize, FileMap, FileMapVA); +} + __declspec(dllexport) bool TITCALL StaticFileLoadW(const wchar_t* szFileName, DWORD DesiredAccess, bool SimulateLoad, LPHANDLE FileHandle, LPDWORD LoadedSize, LPHANDLE FileMap, PULONG_PTR FileMapVA) { return emu.StaticFileLoadW(szFileName, DesiredAccess, SimulateLoad, FileHandle, LoadedSize, FileMap, FileMapVA); @@ -202,6 +207,11 @@ __declspec(dllexport) ULONG_PTR TITCALL GetPE32DataFromMappedFile(ULONG_PTR File return emu.GetPE32DataFromMappedFile(FileMapVA, WhichSection, WhichData); } +__declspec(dllexport) ULONG_PTR TITCALL GetPE32Data(const char* szFileName, DWORD WhichSection, DWORD WhichData) +{ + return emu.GetPE32Data(szFileName, WhichSection, WhichData); +} + __declspec(dllexport) ULONG_PTR TITCALL GetPE32DataW(const wchar_t* szFileName, DWORD WhichSection, DWORD WhichData) { return emu.GetPE32DataW(szFileName, WhichSection, WhichData);