From 8e5446e198643bc9e6c9e053da3b52e2133a31ee Mon Sep 17 00:00:00 2001 From: momo5502 Date: Fri, 3 Mar 2017 11:33:13 +0100 Subject: [PATCH] [Proxy] Cleaner interface disassembly --- src/Steam/Proxy.cpp | 16 ++++++++-------- src/Steam/Proxy.hpp | 40 +++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index 9578011a..cf968356 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -39,7 +39,7 @@ namespace Steam return this->methodCache[method]; } - auto methodData = Interface::lookupMethod(method); + auto methodData = this->lookupMethod(method); this->methodCache[method] = methodData; return methodData; } @@ -48,16 +48,16 @@ namespace Steam { if (!::Utils::Memory::IsBadReadPtr(this->interfacePtr)) { - unsigned char** vftbl = *static_cast(this->interfacePtr); + auto* vftbl = this->interfacePtr->vftbl; - while (!::Utils::Memory::IsBadReadPtr(vftbl) && !::Utils::Memory::IsBadCodePtr((FARPROC(*vftbl)))) + while (!::Utils::Memory::IsBadReadPtr(vftbl) && !::Utils::Memory::IsBadCodePtr(vftbl->func)) { std::string name; uint16_t params; if (this->getMethodData(*vftbl, &name, ¶ms) && name == method) { - return{ *vftbl, params }; + return{ vftbl->data, params }; } ++vftbl; @@ -67,17 +67,17 @@ namespace Steam return { nullptr, 0 }; } - bool Interface::getMethodData(unsigned char* methodPtr, std::string* name, uint16_t* params) + bool Interface::getMethodData(VInterface::VMethod method, std::string* name, uint16_t* params) { name->clear(); *params = 0; - if (::Utils::Memory::IsBadCodePtr(methodPtr)) return false; + if (::Utils::Memory::IsBadCodePtr(method.data)) return false; ud_t ud; ud_init(&ud); ud_set_mode(&ud, 32); - ud_set_pc(&ud, reinterpret_cast(methodPtr)); - ud_set_input_buffer(&ud, reinterpret_cast(methodPtr), INT32_MAX); + ud_set_pc(&ud, method.value); + ud_set_input_buffer(&ud, method.data, INT32_MAX); while (true) { diff --git a/src/Steam/Proxy.hpp b/src/Steam/Proxy.hpp index f0a95d53..0d1ab502 100644 --- a/src/Steam/Proxy.hpp +++ b/src/Steam/Proxy.hpp @@ -146,7 +146,7 @@ namespace Steam { public: Interface() : interfacePtr(nullptr) {} - Interface(void* _interfacePtr) : interfacePtr(_interfacePtr) {} + Interface(void* _interfacePtr) : interfacePtr(static_cast(_interfacePtr)) {} template T invoke(std::string methodName, Args... args) @@ -195,56 +195,66 @@ namespace Steam template struct AddSizes : std::integral_constant::value + (sizeof(void*) - 1)) & ~(sizeof(void*) - 1))> {}; - void* interfacePtr; + struct VInterface + { + union VMethod + { + unsigned char* data; + unsigned int value; + FARPROC func; + }* vftbl; + }; + + VInterface* interfacePtr; std::unordered_map> methodCache; std::pair getMethod(std::string method); std::pair lookupMethod(std::string method); - bool getMethodData(unsigned char* methodPtr, std::string* name, uint16_t* params); + bool getMethodData(VInterface::VMethod method, std::string* name, uint16_t* params); }; class KeyValuesBuilder { private: - std::stringstream m_buffer; + std::string buffer; inline void packBytes(const void* bytes, size_t size) { - m_buffer << std::string(reinterpret_cast(bytes), size); + this->buffer.append(reinterpret_cast(bytes), size); } inline void packDataType(uint8_t type) { - packBytes(&type, 1); + this->packBytes(&type, 1); } inline void packNullTerminated(const char* string) { - packBytes(string, strlen(string) + 1); + this->packBytes(string, strlen(string) + 1); } public: inline void packString(const char* key, const char* value) { - packDataType(1); - packNullTerminated(key); - packNullTerminated(value); + this->packDataType(1); + this->packNullTerminated(key); + this->packNullTerminated(value); } inline void packUint64(const char* key, uint64_t value) { - packDataType(7); - packNullTerminated(key); - packBytes(&value, sizeof(value)); + this->packDataType(7); + this->packNullTerminated(key); + this->packBytes(&value, sizeof(value)); } inline void packEnd() { - packDataType(8); + this->packDataType(8); } inline std::string getString() { - return m_buffer.str(); + return this->buffer; } };