From 51429862fc5559c05f930c10758f805d6ded5128 Mon Sep 17 00:00:00 2001 From: Pinwhell <60289470+pinwhell@users.noreply.github.com> Date: Sat, 27 Jan 2024 15:19:14 -0400 Subject: [PATCH] Semantic Refactor & Cleanups & ARM64 Integration --- include/OH/ConfigManager.h | 3 ++ include/OH/DumpTargetGroup.h | 5 ++- include/OH/FileHelper.h | 1 + include/OH/FutureOffsetResultInfo.h | 2 +- include/OH/IFutureResult.h | 7 +++- include/OH/IFutureResultInfo.h | 15 +++++-- include/OH/IJsonAccesor.h | 3 ++ include/OH/ObfuscationManager.h | 9 ++-- include/OH/SingleDumpTarget.h | 5 ++- include/OH/TargetManager.h | 16 +++++++- samples/DummyLib/DummyOffsetMgr.hpp | 35 ++++++++++++---- samples/DummyLib/dummyConfig.json | 5 ++- samples/DummyLib/dummyObfBook.json | 2 +- samples/DummyLib/dummyTargets.json | 23 +++-------- samples/DummyLib/offsets_ARM32.json | 1 + samples/DummyLib/offsets_ARM64.json | 2 +- src/ConfigManager.cpp | 3 ++ src/DumpTargetGroup.cpp | 16 +++++++- src/FileHelper.cpp | 10 +++++ src/FutureOffsetResultInfo.cpp | 4 +- src/HPPManager.cpp | 4 ++ src/IFutureResult.cpp | 24 ++++++++++- src/IFutureResultInfo.cpp | 64 ++++++++++++++++++++--------- src/IJsonAccesor.cpp | 10 +++++ src/ObfuscationManager.cpp | 30 ++++++++------ src/SingleDumpTarget.cpp | 10 ++++- src/TargetManager.cpp | 26 ++++++++---- 27 files changed, 245 insertions(+), 90 deletions(-) create mode 100644 samples/DummyLib/offsets_ARM32.json diff --git a/include/OH/ConfigManager.h b/include/OH/ConfigManager.h index 512a65f..f2d8140 100644 --- a/include/OH/ConfigManager.h +++ b/include/OH/ConfigManager.h @@ -11,7 +11,10 @@ class ConfigManager JsonValueWrapper mConfigRoot; bool mDumpDynamic; + bool mDumpRuntime; bool mDumpEncrypt; + bool mIdentifierSalt; + bool mIdentifierHash; std::string mMainCategory; std::string mOutputName; diff --git a/include/OH/DumpTargetGroup.h b/include/OH/DumpTargetGroup.h index 832199f..3aa8603 100644 --- a/include/OH/DumpTargetGroup.h +++ b/include/OH/DumpTargetGroup.h @@ -5,6 +5,7 @@ #include #include #include "IChild.h" +#include struct HeaderFileManager; @@ -29,10 +30,12 @@ class DumpTargetGroup : public IDumpTarget, public IChild bool ReadAllTarget(); + void HPPRuntimeResultWrite(IJsonAccesor* jsonAccesor); + void ReportHppIncludes(); void WriteHppStaticDeclsDefs(); void WriteHppDynDecls(); - void WriteHppDynDefs(); + void WriteHppCompileTimeDefs(); void MacroBegin(); void MacroEnd(); HeaderFileManager* getHppWriter(); diff --git a/include/OH/FileHelper.h b/include/OH/FileHelper.h index 2fabb41..cac63ed 100644 --- a/include/OH/FileHelper.h +++ b/include/OH/FileHelper.h @@ -9,5 +9,6 @@ namespace FileHelper bool IsValidFilePath(const std::string& filePath, bool logPathUnacesible = false, bool logPathIsNotRegularFile = false); bool ReadFile(const std::string& filePath, std::string& output); bool ReadFileBinary(const std::string& filePath, std::vector& output); + bool FileIsEmpty(const std::string& filePath); } diff --git a/include/OH/FutureOffsetResultInfo.h b/include/OH/FutureOffsetResultInfo.h index 3772e13..006485c 100644 --- a/include/OH/FutureOffsetResultInfo.h +++ b/include/OH/FutureOffsetResultInfo.h @@ -19,7 +19,7 @@ class FutureOffsetResultInfo : public IFutureResultInfo { void ReportHppIncludes() override; void WriteHppStaticDeclsDefs() override; void WriteHppDynDecls() override; - void WriteHppDynDefs() override; + void WriteHppDef() override; void OnParentTargetFinish() override; diff --git a/include/OH/IFutureResult.h b/include/OH/IFutureResult.h index eca28f4..ea1058b 100644 --- a/include/OH/IFutureResult.h +++ b/include/OH/IFutureResult.h @@ -42,14 +42,19 @@ class IFutureResult : public IChild void ReportHppIncludes(); void WriteHppStaticDeclsDefs(); // This structs arround need to be refactored to handle general stuffs, not just offsets, void WriteHppDynDecls(); // Code structure is done, just refactoring names, and key specific structures - void WriteHppDynDefs(); + void WriteHppCompileTimeDefs(); + + void HppRuntimeDecryptionWrite(IJsonAccesor* jsonAccesor); void setTargetManager(TargetManager* pTarget); TargetManager* getTargetManager(); IJsonAccesor* getJsonAccesor(); bool getDumpDynamic(); + bool getDumpRuntime(); bool getDumpEncrypt(); + bool getIdentifierSalt(); + bool getIdentifierHash(); void setMetadata(const JsonValueWrapper& metadata); JsonValueWrapper& getMetadata(); diff --git a/include/OH/IFutureResultInfo.h b/include/OH/IFutureResultInfo.h index 81a7e4b..3f5b375 100644 --- a/include/OH/IFutureResultInfo.h +++ b/include/OH/IFutureResultInfo.h @@ -6,8 +6,9 @@ #include "ILValueRValueWrapper.h" #include "INestedLValueRValueWrapper.h" #include "IChild.h" +#include -#define ERR_INVALID_OFFSET ((uint64_t)0xFFFFFFFFFFFFFFFF) +#define ERR_INVALID_OFFSET ((uint64_t)~0ull) class IFutureResult; struct HeaderFileManager; @@ -19,13 +20,14 @@ class IFutureResultInfo : public IChild std::string mName; std::string mUIdentifier; std::string mUIdentifierDynamic; + std::string mUIdentifierDynamicSalted; std::string mUIDHash; std::string mComment; // If there is no comment available then this will be empty bool mCanPickAnyResult; protected: std::unique_ptr mStaticResult; - std::unique_ptr mDynamicResult; // Why nested? well, basicly we need to do, chainig struct + std::unique_ptr mStructMemberAccessor; // Why nested? well, basicly we need to do, chainig struct // objects to be able to modify/acess the desired offset // for ex. mA.mB.mC = 0xXYZ; @@ -48,7 +50,9 @@ class IFutureResultInfo : public IChild virtual void ReportHppIncludes() {}; virtual void WriteHppStaticDeclsDefs(); virtual void WriteHppDynDecls(); - virtual void WriteHppDynDefs(); + virtual void WriteHppDef(); + + virtual void HppRuntimeDecryptionWrite(IJsonAccesor* jsonAccesor); HeaderFileManager* getHppWriter(); @@ -62,4 +66,9 @@ class IFutureResultInfo : public IChild virtual std::string getCppDataType() = 0; virtual std::string getCppDefaultRvalue() = 0; + + + + + std::string getUniqueIdentifier(); }; diff --git a/include/OH/IJsonAccesor.h b/include/OH/IJsonAccesor.h index 62a10fa..ffd950f 100644 --- a/include/OH/IJsonAccesor.h +++ b/include/OH/IJsonAccesor.h @@ -12,6 +12,7 @@ class IJsonAccesor { public: virtual std::string genGetInt(const std::string& key, uint32_t xorend = 0x0); virtual std::string genGetUInt(const std::string& key, uint32_t xorend = 0x0); + virtual std::string genAssign(const std::string& key, const std::string& what); std::string genJsonAccess(const std::string& key); @@ -23,5 +24,7 @@ class IJsonAccesor { virtual std::string getJsonObjFullType(); // for example in the scenario of JsonCPP library // it will return "Json::Value" + + std::string getJsonObjectName(); }; diff --git a/include/OH/ObfuscationManager.h b/include/OH/ObfuscationManager.h index af91a2c..78cdbd3 100644 --- a/include/OH/ObfuscationManager.h +++ b/include/OH/ObfuscationManager.h @@ -3,6 +3,7 @@ #include "JsonValueWrapper.h" #include #include "IChild.h" +#include class TargetManager; @@ -17,11 +18,11 @@ just like a book, where the pages are each "[identifier]" : { class ObfuscationManager : public IChild { private: - std::string mObfuscationInfoBookPath; JsonValueWrapper mObfuscationInfoBookRoot; - bool mObfInfoMutationEnabled; std::unordered_set mMutatedUIDs; + ConfigManager* mConfigMgr; + bool getObfInfoPage(const std::string& uId, JsonValueWrapper& outPage); bool getObfInfoPageUpdateMutation(const std::string& uId, JsonValueWrapper& outPage); void UpdateObfInfoPage(const std::string& uId, JsonValueWrapper& page); @@ -38,8 +39,6 @@ class ObfuscationManager : public IChild uint32_t getSaltKey(const std::string& uId); uint32_t getObfKey(const std::string& uId); - void setPath(const std::string& path); - - void setObfInfoMutationEnabled(bool b); + void setConfigManager(ConfigManager* cfgMgr); }; diff --git a/include/OH/SingleDumpTarget.h b/include/OH/SingleDumpTarget.h index 6883b3c..ddcdc5f 100644 --- a/include/OH/SingleDumpTarget.h +++ b/include/OH/SingleDumpTarget.h @@ -7,6 +7,7 @@ #include "JsonValueWrapper.h" #include "IChild.h" #include "IBinaryFormat.h" +#include class DumpTargetGroup; struct HeaderFileManager; @@ -45,10 +46,12 @@ class SingleDumpTarget : public IDumpTarget, public IChild std::string getCategoryObjectName(); + void HppRuntimeDecryptionWrite(IJsonAccesor* jsonAccesor); + void ReportHppIncludes(); void WriteHppStaticDeclsDefs(); void WriteHppDynDecls(); - void WriteHppDynDefs(); + void WriteHppCompileTimeDefs(); void BeginStruct(); void EndStruct(); diff --git a/include/OH/TargetManager.h b/include/OH/TargetManager.h index f6ce8b2..3c5e753 100644 --- a/include/OH/TargetManager.h +++ b/include/OH/TargetManager.h @@ -62,7 +62,21 @@ class TargetManager : public IChild void WriteHppIncludes(); void WriteHppStaticDeclsDefs(); void WriteHppDynDecls(); - void WriteHppDynDefs(); + + + /*Will Generate Compiletime/Runtime Conditional Definitions for all targets*/ + /*Where each target have its sub-fields, ej.*/ + + /* +#ifdef TARGET + mTarget.mSubField1 = ...; + mTarget.mSubField2 = ...; + ... +#endif + */ + + void WriteHppCompileTimeDefs(); + void TargetsWriteCompileTime(); CapstoneHelperProvider* getCapstoneHelperProvider(); ObfuscationManager* getObfuscationManager(); diff --git a/samples/DummyLib/DummyOffsetMgr.hpp b/samples/DummyLib/DummyOffsetMgr.hpp index df1274e..9bb7888 100644 --- a/samples/DummyLib/DummyOffsetMgr.hpp +++ b/samples/DummyLib/DummyOffsetMgr.hpp @@ -5,6 +5,14 @@ struct OffMgr { #if defined(STATIC_OFFS) +#if defined(ARM32) + struct BinArm32A { + uintptr_t name1 = 0x8; // Should be 0x8 #(Hi)# + uintptr_t name2 = 0x8; // Should be 0x8 + uintptr_t name3 = 0x8; // Should be 0x8 + } mBinArm32A; +#endif + #if defined(ARM64) struct BinArm64A { uintptr_t name1 = 0x8; // Should be 0x8 #(Hi)# @@ -15,6 +23,14 @@ struct OffMgr { #else +#if defined(ARM32) + struct BinArm32A { + uintptr_t name1; // Should be 0x8 #(Hi)# + uintptr_t name2; // Should be 0x8 + uintptr_t name3; // Should be 0x8 + } mBinArm32A; +#endif + #if defined(ARM64) struct BinArm64A { uintptr_t name1; // Should be 0x8 #(Hi)# @@ -24,18 +40,23 @@ struct OffMgr { #endif void Set(const Json::Value& obj) { - static bool decrypted = false; + static bool initialized = false; - if(decrypted) return; + if(initialized) return; -#if defined(ARM64) - mBinArm64A.name1 = obj["462494170"].asUInt(); // Should be 0x8 #(Hi)# - mBinArm64A.name2 = obj["4119764849"].asUInt(); // Should be 0x8 - mBinArm64A.name3 = obj["3482068232"].asUInt(); // Should be 0x8 +#if defined(ARM32) + mBinArm32A.name1 = obj["500368777"].asUInt() ^ 4023873318; // Should be 0x8 #(Hi)# + mBinArm32A.name2 = obj["1138065394"].asUInt() ^ 1844445062; // Should be 0x8 + mBinArm32A.name3 = obj["1775762011"].asUInt() ^ 4127170522; // Should be 0x8 #endif +#if defined(ARM64) + mBinArm64A.name1 = obj["462494170"].asUInt() ^ 4000739487; // Should be 0x8 #(Hi)# + mBinArm64A.name2 = obj["4119764849"].asUInt() ^ 3581899242; // Should be 0x8 + mBinArm64A.name3 = obj["3482068232"].asUInt() ^ 1333386720; // Should be 0x8 +#endif - decrypted = true; + initialized = true; } #endif diff --git a/samples/DummyLib/dummyConfig.json b/samples/DummyLib/dummyConfig.json index 47b244e..2a93ca9 100644 --- a/samples/DummyLib/dummyConfig.json +++ b/samples/DummyLib/dummyConfig.json @@ -7,8 +7,11 @@ "enable_extern" : true, "extern_name" : "g_Offs", - "dump_encrypt" : false, + "dump_encrypt" : true, "dump_dynamic" : true, + "dump_runtime" : false, + "identifier_salt" : false, + "identifier_hash" : true, "dump_json_lib_name" : "jsoncpp", "declare_dump_global_obj": true, diff --git a/samples/DummyLib/dummyObfBook.json b/samples/DummyLib/dummyObfBook.json index 4a7be05..169e4c4 100644 --- a/samples/DummyLib/dummyObfBook.json +++ b/samples/DummyLib/dummyObfBook.json @@ -1 +1 @@ -{"mBinArm32A.name1":{"obf_key":1985833994,"salt_key":4283495952},"mBinArm32A.name2":{"obf_key":1610241857,"salt_key":2078276588},"mBinArm32A.name3":{"obf_key":3214868459,"salt_key":1450015593},"mBinArm32B.name1":{"obf_key":2130704125,"salt_key":1458880288},"mBinArm32B.name2":{"obf_key":2608821920,"salt_key":4122959116},"mBinArm32B.name3":{"obf_key":2142748451,"salt_key":2330853172},"mBinArm64A.name1":{"obf_key":2105391926,"salt_key":2071949210},"mBinArm64A.name2":{"obf_key":4261248991,"salt_key":1331658730},"mBinArm64A.name3":{"obf_key":3609688011,"salt_key":4252450174},"mBinArm64B.name1":{"obf_key":257130281,"salt_key":3212802292},"mBinArm64B.name2":{"obf_key":4134368048,"salt_key":2104442799},"mBinArm64B.name3":{"obf_key":4287559064,"salt_key":4282247254}} +{"mBinArm32A.name1":{"obf_key":4023873318,"salt_key":4285004300},"mBinArm32A.name2":{"obf_key":1844445062,"salt_key":2137501527},"mBinArm32A.name3":{"obf_key":4127170522,"salt_key":4126660006},"mBinArm64A.name1":{"obf_key":4000739487,"salt_key":4292867548},"mBinArm64A.name2":{"obf_key":3581899242,"salt_key":3480856371},"mBinArm64A.name3":{"obf_key":1333386720,"salt_key":4020186553}} diff --git a/samples/DummyLib/dummyTargets.json b/samples/DummyLib/dummyTargets.json index 81a0791..2c37bf4 100644 --- a/samples/DummyLib/dummyTargets.json +++ b/samples/DummyLib/dummyTargets.json @@ -1,6 +1,6 @@ [ // Targets - /*{ // Dump Target Group + { // Dump Target Group "macro" : "ARM32", "targets" : [ @@ -9,29 +9,18 @@ "bin_path" : "libs/armeabi-v7a/libdummy.so", "dataset_path" : "DummyLib.json" } // Single Dump Target - - , - { // Single Dump Target - "name" : "BinArm32B", - "bin_path" : "libs/armeabi-v7a/libdummy.so", - "dataset_path" : "DummyLib.json" - } // Single Dump Target ] - - } // Dump Target Group - - - - ,*/ - { + }, // Dump Target Group + + { // Dump Target Group "macro" : "ARM64", "targets" : [ { // Single Dump Target "name" : "BinArm64A", "bin_path" : "libs/arm64-v8a/libdummy.so", "dataset_path" : "DummyLib64.json" - } + } // Single Dump Target ] - } + } // Dump Target Group ] \ No newline at end of file diff --git a/samples/DummyLib/offsets_ARM32.json b/samples/DummyLib/offsets_ARM32.json new file mode 100644 index 0000000..3fddc9d --- /dev/null +++ b/samples/DummyLib/offsets_ARM32.json @@ -0,0 +1 @@ +{"1138065394":1844445070,"1775762011":4127170514,"500368777":4023873326} diff --git a/samples/DummyLib/offsets_ARM64.json b/samples/DummyLib/offsets_ARM64.json index 80ce717..e480624 100644 --- a/samples/DummyLib/offsets_ARM64.json +++ b/samples/DummyLib/offsets_ARM64.json @@ -1 +1 @@ -{"3482068232":8,"4119764849":8,"462494170":8} +{"3482068232":1333386728,"4119764849":3581899234,"462494170":4000739479} diff --git a/src/ConfigManager.cpp b/src/ConfigManager.cpp index 487053c..8d1d62c 100644 --- a/src/ConfigManager.cpp +++ b/src/ConfigManager.cpp @@ -63,6 +63,9 @@ bool ConfigManager::InitDumpInfo() mDeclareGlobalDumpObj = mConfigRoot.get("declare_dump_global_obj", false); mGlobalDumpObjName = mConfigRoot.get("global_dump_obj_name", "g" + mMainCategory + "Offs"); mDumpEncrypt = mConfigRoot.get("dump_encrypt", false); + mDumpRuntime = mConfigRoot.get("dump_runtime", false); + mIdentifierSalt = mConfigRoot.get("identifier_salt", false); + mIdentifierHash = mConfigRoot.get("identifier_hash", false); return true; } \ No newline at end of file diff --git a/src/DumpTargetGroup.cpp b/src/DumpTargetGroup.cpp index d917fcf..27719f3 100644 --- a/src/DumpTargetGroup.cpp +++ b/src/DumpTargetGroup.cpp @@ -112,6 +112,18 @@ bool DumpTargetGroup::ReadAllTarget() return true; } +void DumpTargetGroup::HPPRuntimeResultWrite(IJsonAccesor* jsonAccesor) +{ + getHppWriter()->BeginFunction("void", getMacro() + "Decrypt", { + jsonAccesor->getJsonObjFullType() + "& " + jsonAccesor->getJsonObjectName() + }); + + for (auto& target : mTargets) + target.first->HppRuntimeDecryptionWrite(jsonAccesor); + + getHppWriter()->EndFunction(); +} + void DumpTargetGroup::ReportHppIncludes() { if (mTargets.size() < 1) @@ -147,7 +159,7 @@ void DumpTargetGroup::WriteHppDynDecls() MacroEnd(); } -void DumpTargetGroup::WriteHppDynDefs() +void DumpTargetGroup::WriteHppCompileTimeDefs() { if (mTargets.size() < 1) return; @@ -155,7 +167,7 @@ void DumpTargetGroup::WriteHppDynDefs() MacroBegin(); for (auto& kv : mTargets) - kv.first->WriteHppDynDefs(); + kv.first->WriteHppCompileTimeDefs(); MacroEnd(); } diff --git a/src/FileHelper.cpp b/src/FileHelper.cpp index 5a30d4b..1ffb4ee 100644 --- a/src/FileHelper.cpp +++ b/src/FileHelper.cpp @@ -70,3 +70,13 @@ bool FileHelper::ReadFileBinary(const std::string& filePath, std::vectorgetConfigManager()->mDumpDynamic; } +bool IFutureResult::getDumpRuntime() +{ + return mTargetMgr->getConfigManager()->mDumpRuntime; +} + bool IFutureResult::getDumpEncrypt() { return mTargetMgr->getConfigManager()->mDumpEncrypt; } +bool IFutureResult::getIdentifierSalt() +{ + return mTargetMgr->getConfigManager()->mIdentifierSalt; +} + +bool IFutureResult::getIdentifierHash() +{ + return mTargetMgr->getConfigManager()->mIdentifierHash; +} + void IFutureResult::setMetadata(const JsonValueWrapper& metadata) { mMetadata = metadata; @@ -110,9 +125,14 @@ void IFutureResult::WriteHppDynDecls() mpFutureResultInfo->WriteHppDynDecls(); } -void IFutureResult::WriteHppDynDefs() +void IFutureResult::WriteHppCompileTimeDefs() +{ + mpFutureResultInfo->WriteHppDef(); +} + +void IFutureResult::HppRuntimeDecryptionWrite(IJsonAccesor* jsonAccesor) { - mpFutureResultInfo->WriteHppDynDefs(); + mpFutureResultInfo->HppRuntimeDecryptionWrite(jsonAccesor); } void IFutureResult::WriteHppStaticDeclsDefs() diff --git a/src/IFutureResultInfo.cpp b/src/IFutureResultInfo.cpp index 9cd52f1..38373eb 100644 --- a/src/IFutureResultInfo.cpp +++ b/src/IFutureResultInfo.cpp @@ -10,9 +10,11 @@ #include IFutureResultInfo::IFutureResultInfo() + : mSaltKey(0) + , mObfKey(0) { mStaticResult = std::make_unique(); // Will be used for Declaring-defining the static result - mDynamicResult = std::make_unique(); // Will be used for declaring and defining the dynamic result + mStructMemberAccessor = std::make_unique(); // Will be used for declaring and defining the dynamic result mName = ""; mComment = ""; @@ -34,27 +36,27 @@ bool IFutureResultInfo::Init() mStaticResult->setName(mName); mStaticResult->setValue(getCppDefaultRvalue()); - if (mParent->getDumpDynamic()) - { - mDynamicResult->setType(getCppDataType()); - mDynamicResult->PushParentName(mParent->getParent()->getCategoryObjectName()); - mDynamicResult->setName(mName); - - mUIdentifierDynamic = mDynamicResult->getFullName(); // This will chain all, and will get the full name - // for example: mA.mB.mC.mD - // so this way can get a unique identifier for this variable + mStructMemberAccessor->setType(getCppDataType()); + mStructMemberAccessor->PushParentName(mParent->getParent()->getCategoryObjectName()); + mStructMemberAccessor->setName(mName); + mUIdentifierDynamic = mStructMemberAccessor->getFullName(); // This will chain all, and will get the full name + // for example: mA.mB.mC.mD + // so this way can get a unique identifier for this variable - mObfKey = getObfuscationManager()->getObfKey(mUIdentifierDynamic); + if (mParent->getIdentifierSalt()) + { mSaltKey = getObfuscationManager()->getSaltKey(mUIdentifierDynamic); - - if (mParent->getDumpEncrypt() && mSaltKey != 0) - mUIdentifierDynamic += "_" + std::to_string(mSaltKey); - + mUIdentifierDynamicSalted = mUIdentifierDynamic + "_" + std::to_string(mSaltKey); + mUIDHash = std::to_string((uint32_t)fnv1a_32(mUIdentifierDynamicSalted.c_str(), mUIdentifierDynamicSalted.size())); + } else mUIDHash = std::to_string((uint32_t)fnv1a_32(mUIdentifierDynamic.c_str(), mUIdentifierDynamic.size())); - mDynamicResult->setValue(mParent->getJsonAccesor()->genGetUInt(mUIDHash, mParent->getDumpEncrypt() ? mObfKey : 0x0)); - }/* else mUIDHash = std::to_string((uint32_t)fnv1a_32(mUIdentifier.c_str(), mUIdentifier.size()));*/ + if (mParent->getDumpEncrypt()) + mObfKey = getObfuscationManager()->getObfKey(mUIdentifierDynamic); + + if(mParent->getDumpDynamic() || mParent->getDumpRuntime()) + mStructMemberAccessor->setValue(mParent->getJsonAccesor()->genGetUInt(getUniqueIdentifier(), mParent->getDumpEncrypt() ? mObfKey : 0x0)); mCanPickAnyResult = getMetadata().get("pick_any_result", false); @@ -110,7 +112,21 @@ void IFutureResultInfo::WriteHppDynDecls() if (mParent->ResultWasSucessfull() == false) return; - getHppWriter()->AppendLineOfCode(mDynamicResult->ComputeDeclaration(), true, getNeedShowComment() == false); + getHppWriter()->AppendLineOfCode(mStructMemberAccessor->ComputeDeclaration(), true, getNeedShowComment() == false); + + if (getNeedShowComment()) + { + getHppWriter()->AppendTab(); + getHppWriter()->AppendComment(mComment); + } +} + +void IFutureResultInfo::WriteHppDef() +{ + if (mParent->ResultWasSucessfull() == false) + return; + + getHppWriter()->AppendLineOfCode(mStructMemberAccessor->ComputeDefinition(), true, getNeedShowComment() == false); if (getNeedShowComment()) { @@ -119,12 +135,15 @@ void IFutureResultInfo::WriteHppDynDecls() } } -void IFutureResultInfo::WriteHppDynDefs() +void IFutureResultInfo::HppRuntimeDecryptionWrite(IJsonAccesor* jsonAccesor) { if (mParent->ResultWasSucessfull() == false) return; - getHppWriter()->AppendLineOfCode(mDynamicResult->ComputeDefinition(), true, getNeedShowComment() == false); + if(mParent->getIdentifierHash()) + getHppWriter()->AppendLineOfCode("/* " + (mParent->getIdentifierSalt() ? mUIdentifierDynamicSalted : mUIdentifierDynamic) + " */\t", true, false); + + getHppWriter()->AppendLineOfCode(jsonAccesor->genAssign(getUniqueIdentifier(), jsonAccesor->genGetUInt(getUniqueIdentifier(), mObfKey)), mParent->getIdentifierHash() == false, getNeedShowComment() == false); if (getNeedShowComment()) { @@ -156,4 +175,9 @@ ObfuscationManager* IFutureResultInfo::getObfuscationManager() JsonValueWrapper& IFutureResultInfo::getMetadata() { return mParent->getMetadata(); +} + +std::string IFutureResultInfo::getUniqueIdentifier() +{ + return mParent->getIdentifierHash() ? getUIDHashStr() : (mParent->getIdentifierSalt() ? mUIdentifierDynamicSalted : mUIdentifierDynamic); } \ No newline at end of file diff --git a/src/IJsonAccesor.cpp b/src/IJsonAccesor.cpp index d90e49c..b905742 100644 --- a/src/IJsonAccesor.cpp +++ b/src/IJsonAccesor.cpp @@ -10,6 +10,11 @@ std::string IJsonAccesor::genGetUInt(const std::string& key, uint32_t xorend) return genJsonAccess(key) + genXorend(xorend); } +std::string IJsonAccesor::genAssign(const std::string& key, const std::string& what) +{ + return genJsonAccess(key) + " = " + what + ";"; +} + std::string IJsonAccesor::genJsonAccess(const std::string& key) { return mJsonObjName + "[\"" + key + "\"]"; @@ -34,3 +39,8 @@ std::string IJsonAccesor::getJsonObjFullType() { return ""; } + +std::string IJsonAccesor::getJsonObjectName() +{ + return mJsonObjName; +} diff --git a/src/ObfuscationManager.cpp b/src/ObfuscationManager.cpp index 10b86b4..ef931cb 100644 --- a/src/ObfuscationManager.cpp +++ b/src/ObfuscationManager.cpp @@ -5,6 +5,9 @@ bool ObfuscationManager::Init() { + if (mConfigMgr == nullptr) + return false; + if (Import() == false) return false; @@ -13,12 +16,15 @@ bool ObfuscationManager::Init() bool ObfuscationManager::Import() { - if (FileHelper::FileExist(mObfuscationInfoBookPath)) + if (mConfigMgr == nullptr) + return false; + + if (FileHelper::FileExist(mConfigMgr->mObfuscationBookPath)) { - if (FileHelper::IsValidFilePath(mObfuscationInfoBookPath, true, true) == false) + if (FileHelper::IsValidFilePath(mConfigMgr->mObfuscationBookPath, true, true) == false) return false; - if (JsonHelper::File2Json(mObfuscationInfoBookPath, mObfuscationInfoBookRoot) == false) + if (FileHelper::FileIsEmpty(mConfigMgr->mObfuscationBookPath) == false && JsonHelper::File2Json(mConfigMgr->mObfuscationBookPath, mObfuscationInfoBookRoot) == false) return false; } @@ -27,7 +33,10 @@ bool ObfuscationManager::Import() bool ObfuscationManager::Export() { - return JsonHelper::Json2File(mObfuscationInfoBookRoot, mObfuscationInfoBookPath); + if (mConfigMgr == nullptr) + return false; + + return JsonHelper::Json2File(mObfuscationInfoBookRoot, mConfigMgr->mObfuscationBookPath); } bool ObfuscationManager::getObfInfoPage(const std::string& uId, JsonValueWrapper& outPage) @@ -49,7 +58,7 @@ bool ObfuscationManager::getObfInfoPage(const std::string& uId, JsonValueWrapper bool ObfuscationManager::getObfInfoPageUpdateMutation(const std::string& uId, JsonValueWrapper& outPage) { - if (mObfInfoMutationEnabled) + if (mConfigMgr->mObfustationBookDoMutate) MutatePage(uId); return getObfInfoPage(uId, outPage); @@ -101,12 +110,7 @@ uint32_t ObfuscationManager::getObfKey(const std::string& uId) return page.get("obf_key", 0); } -void ObfuscationManager::setPath(const std::string& path) +void ObfuscationManager::setConfigManager(ConfigManager* cfgMgr) { - mObfuscationInfoBookPath = path; -} - -void ObfuscationManager::setObfInfoMutationEnabled(bool b) -{ - mObfInfoMutationEnabled = b; -} + mConfigMgr = cfgMgr; +} \ No newline at end of file diff --git a/src/SingleDumpTarget.cpp b/src/SingleDumpTarget.cpp index ab5780d..a4bf9bd 100644 --- a/src/SingleDumpTarget.cpp +++ b/src/SingleDumpTarget.cpp @@ -161,6 +161,12 @@ std::string SingleDumpTarget::getCategoryObjectName() return mCategoryObjName; } +void SingleDumpTarget::HppRuntimeDecryptionWrite(IJsonAccesor* jsonAccesor) +{ + for (auto& futureResult : mFutureResults) + futureResult.first->HppRuntimeDecryptionWrite(jsonAccesor); +} + void SingleDumpTarget::ReportHppIncludes() { for (auto& currOff : mFutureResults) @@ -187,10 +193,10 @@ void SingleDumpTarget::WriteHppDynDecls() EndStruct(); } -void SingleDumpTarget::WriteHppDynDefs() +void SingleDumpTarget::WriteHppCompileTimeDefs() { for (auto& currOff : mFutureResults) - currOff.first->WriteHppDynDefs(); + currOff.first->WriteHppCompileTimeDefs(); } void SingleDumpTarget::BeginStruct() diff --git a/src/TargetManager.cpp b/src/TargetManager.cpp index 8f65476..0d76a09 100644 --- a/src/TargetManager.cpp +++ b/src/TargetManager.cpp @@ -28,7 +28,7 @@ bool TargetManager::Init() return false; } - if (mConfigMgr->mDumpDynamic) + if (mConfigMgr->mDumpDynamic || mConfigMgr->mDumpRuntime) { if (JsonAccesorClassifier::Classify(mConfigMgr->mDumpJsonLibName, mJsonAccesor) == false) { @@ -40,11 +40,10 @@ bool TargetManager::Init() AddInclude(mJsonAccesor->getGlobalInclude()); } - if (mConfigMgr->mDumpEncrypt) + if (mConfigMgr->mDumpEncrypt || mConfigMgr->mIdentifierSalt) { - mObfucationManager->setPath(mConfigMgr->mObfuscationBookPath); + mObfucationManager->setConfigManager(mConfigMgr); mObfucationManager->setParent(this); - mObfucationManager->setObfInfoMutationEnabled(mConfigMgr->mObfustationBookDoMutate); if (mObfucationManager->Init() == false) return false; @@ -91,7 +90,10 @@ void TargetManager::ComputeAll() bool TargetManager::SaveResults() { - if (SaveHpp() == false) + if (!mConfigMgr->mDumpRuntime && SaveHppCompileTime() == false) + return false; + + if (mConfigMgr->mDumpRuntime && SaveHppRuntime() == false) return false; if (mConfigMgr->mDumpDynamic) @@ -132,7 +134,14 @@ bool TargetManager::SaveHppRuntime() mHppWriter->AppendNextLine(); + for (const auto& kv : mAllTargets) + { + kv.second->HPPRuntimeResultWrite(mJsonAccesor.get()); + + mHppWriter->AppendNextLine(); + } + return true; } bool TargetManager::SaveHppCompileTime() @@ -168,8 +177,7 @@ bool TargetManager::SaveHppCompileTime() /*Generate Definition-only here*/ - WriteHppDynDefs(); - mHppWriter->AppendNextLine(); + WriteHppCompileTimeDefs(); mHppWriter->AppendLineOfCode("initialized = true;"); @@ -296,10 +304,10 @@ void TargetManager::WriteHppDynDecls() kv.second->WriteHppDynDecls(); } -void TargetManager::WriteHppDynDefs() +void TargetManager::WriteHppCompileTimeDefs() { for (const auto& kv : mAllTargets) - kv.second->WriteHppDynDefs(); + kv.second->WriteHppCompileTimeDefs(); } CapstoneHelperProvider* TargetManager::getCapstoneHelperProvider()