From b89c3a70986e1d3ba4a628fccec8856c2e30d75c Mon Sep 17 00:00:00 2001 From: Pinwhell <60289470+pinwhell@users.noreply.github.com> Date: Sat, 27 Jan 2024 00:16:29 -0400 Subject: [PATCH] Semantic Refactor & Cleanup --- include/OH/ConfigManager.h | 17 +--- include/OH/IFutureResult.h | 1 + include/OH/TargetManager.h | 32 ++----- samples/DummyLib/DummyOffsetMgr.hpp | Bin 879 -> 870 bytes samples/DummyLib/dummyConfig.json | 4 +- samples/DummyLib/offsets_ARM64.json | 2 +- src/ConfigManager.cpp | 55 +----------- src/FutureOffsetResultInfo.cpp | 6 +- src/IFutureResult.cpp | 7 +- src/IFutureResultInfo.cpp | 6 +- src/OffsetHunter.cpp | 12 +-- src/StringHelper.cpp | 3 +- src/TargetManager.cpp | 128 +++++++++++----------------- 13 files changed, 83 insertions(+), 190 deletions(-) diff --git a/include/OH/ConfigManager.h b/include/OH/ConfigManager.h index 8700edf..512a65f 100644 --- a/include/OH/ConfigManager.h +++ b/include/OH/ConfigManager.h @@ -11,6 +11,7 @@ class ConfigManager JsonValueWrapper mConfigRoot; bool mDumpDynamic; + bool mDumpEncrypt; std::string mMainCategory; std::string mOutputName; @@ -21,23 +22,9 @@ class ConfigManager std::string mObfuscationBookPath; bool mDeclareGlobalDumpObj; - bool mObfuscationBookMutationEnabled; - - - void setConfigPath(const std::string& path); - - std::string getDumpTargetPath(); - std::string getMainCategoryName(); - std::string getHppOutputPath(); - std::string getDumpJsonLibName(); - std::string getGlobalDumpObjectName(); - std::string getObfuscationBookPath(); + bool mObfustationBookDoMutate; bool Init(); bool InitDynamicDumpInfo(); bool InitDumpInfo(); - - bool getObfuscationBookMutationEnabled(); - bool getDumpDynamic(); - bool getDeclareGlobalDumpObj(); }; \ No newline at end of file diff --git a/include/OH/IFutureResult.h b/include/OH/IFutureResult.h index c14aece..eca28f4 100644 --- a/include/OH/IFutureResult.h +++ b/include/OH/IFutureResult.h @@ -49,6 +49,7 @@ class IFutureResult : public IChild IJsonAccesor* getJsonAccesor(); bool getDumpDynamic(); + bool getDumpEncrypt(); void setMetadata(const JsonValueWrapper& metadata); JsonValueWrapper& getMetadata(); diff --git a/include/OH/TargetManager.h b/include/OH/TargetManager.h index eb69a34..f6ce8b2 100644 --- a/include/OH/TargetManager.h +++ b/include/OH/TargetManager.h @@ -8,6 +8,7 @@ #include "IChild.h" #include "IJsonAccesor.h" #include "ObfuscationManager.h" +#include class OffsetHunter; @@ -16,12 +17,6 @@ class TargetManager : public IChild private: std::unordered_map> mAllTargets; // For now just supporting DumpTargetGroup - std::string mDumpTargetsPath; - std::string mMainCategoryName; - std::string mObfuscationBookPath; - std::string mHppOutputPath; - std::string mGlobalDumpObjName; - std::string mDumpJsonLibName; std::string mDynamicJsonObjName; // by default "obj" std::string mDynamicOffsetSetterFuncName; // by default "Set" @@ -33,15 +28,13 @@ class TargetManager : public IChild std::unique_ptr mJsonAccesor; std::unique_ptr mObfucationManager; - bool mDumpDynamic; - bool mDeclareDumpObject; - bool mObfuscationBookMutationEnabled; + ConfigManager* mConfigMgr; bool SaveJson(); - bool SaveHpp(); + bool SaveHppCompileTime(); + bool SaveHppRuntime(); public: - TargetManager(); bool Init(); @@ -49,33 +42,22 @@ class TargetManager : public IChild void ComputeAll(); bool SaveResults(); + void setConfigManager(ConfigManager* configMgr); + ConfigManager* getConfigManager(); + void RemoveTarget(DumpTargetGroup* target); void AddTarget(std::unique_ptr& target); - void setDumpTargetPath(const std::string& path); - void setMainCategoryName(const std::string& mainCategoryName); - void setHppOutputPath(const std::string& outputPath); - bool ReadAllTargets(); bool HandleTargetGroupJson(const JsonValueWrapper& targetGroupRoot); HeaderFileManager* getHppWriter(); - void setObfuscationBookMutationEnabled(bool b); - - void setDumpDynamic(bool b); - void setDeclareGlobalDumpObj(bool b); - void setGlobalDumpObjectName(const std::string& globalObjName); - void setJsonAccesor(std::unique_ptr&& accesor); IJsonAccesor* getJsonAccesor(); - void setDumpJsonLibName(const std::string& dumpJsonLibName); - bool getDumpDynamic(); - void setDynamicOffsetSetterFuncName(const std::string& dynamicOffsetSetterFuncName); - void setObfuscationBookPath(const std::string& obfuscationBookPath); void WriteHppIncludes(); void WriteHppStaticDeclsDefs(); diff --git a/samples/DummyLib/DummyOffsetMgr.hpp b/samples/DummyLib/DummyOffsetMgr.hpp index 15fe908b731f77316075562dddc5a5af0cd5a29f..df1274e447d4fdad928a6a4ba9b32c4d9b316de8 100644 GIT binary patch literal 870 zcmb7D(Q3jl6n##<;$mU6dr(_Tt#%W-fy{vjtJ}*MQrlD`Z7OLx9K(NKs%v%HIrefN zZq7Z)IVV?HbAOTekg|YatWXxjc}U=9m8Gmzl;(19)DAKcQYHo%XM*Q}fJqdM7aTq% zCW4Sel#x*NrtWlfKbs7Pp7!IweHvS)hSB%%9c3<0EYp?q2<4J^E%;0T^An=OEwneb zgP_#{Z<*$C2y;@jKv5s4rkqU}zfPP%I~Sl^NUC=^SRpK=k#vT{+0LMY%KwP@kBl1Z zxqwi!|a)Qx7~Pa`q}e{3014nQRREWQ{=nn z(e+-}GHtVWnFym3h9dSIL&vm@wq+X~;}3>1evB&dl0$xd=?U6azhesyCL2LGD;MH= LvEK|?GY5YGVao#) literal 879 zcmb7@U2DQH6oz+O!T)ej7`h8>ntnyMK{t>&5Mgz@8JpBL)k>R6n+{?6-WjpgMh?WxrPWHcQV*CLW*&vxc^b2Jeza!y$8|U5Uck7hRVyh^GMB^9 z_XpDizH^xm9`yr8y@oxqM*YX}u-CJj#T)BopsS)LZTlYrW+joXS~)_JGJ#+4WXyp% z5k+tZ^3v=|&~Af0i?h&!3C&Be8n2!Laa^5uDSvjIYw>^DHAMU8b%hPeyujZ~iiYWS zWCzceIlI(TiN3^5x#~|J(pMaKU{l_3W0rDw7Lj*4Z%&xeYltWF=4q~bkB7muc0mdK zdktX?Yl@0>rS{Qs($_xYjV648f@BR7L=?T`iG?sv$a$C>vZ5kGGYl-FB3V^*q^Kq# zsDvy=bi&I6-|(<#Bg8O?QRHD=Cc0_LsGN6L0%?7`5^E3}LYJH-Z1*Q_roW#Hq)$ diff --git a/samples/DummyLib/dummyConfig.json b/samples/DummyLib/dummyConfig.json index b070ee0..47b244e 100644 --- a/samples/DummyLib/dummyConfig.json +++ b/samples/DummyLib/dummyConfig.json @@ -7,12 +7,12 @@ "enable_extern" : true, "extern_name" : "g_Offs", - + "dump_encrypt" : false, "dump_dynamic" : true, "dump_json_lib_name" : "jsoncpp", "declare_dump_global_obj": true, - "obf_book_mut_enabled" : false, + "obf_book_do_mutate" : false, "obf_book_path" : "dummyObfBook.json", } \ No newline at end of file diff --git a/samples/DummyLib/offsets_ARM64.json b/samples/DummyLib/offsets_ARM64.json index 7e75673..80ce717 100644 --- a/samples/DummyLib/offsets_ARM64.json +++ b/samples/DummyLib/offsets_ARM64.json @@ -1 +1 @@ -{"1353524362":2105391934,"2417577301":4261248983,"3612997897":3609688003} +{"3482068232":8,"4119764849":8,"462494170":8} diff --git a/src/ConfigManager.cpp b/src/ConfigManager.cpp index ed1aa9b..487053c 100644 --- a/src/ConfigManager.cpp +++ b/src/ConfigManager.cpp @@ -4,41 +4,6 @@ namespace fs = std::filesystem; -void ConfigManager::setConfigPath(const std::string& path) -{ - mConfigPath = path; -} - -std::string ConfigManager::getDumpTargetPath() -{ - return mDumpTargetPath; -} - -std::string ConfigManager::getMainCategoryName() -{ - return mMainCategory; -} - -std::string ConfigManager::getHppOutputPath() -{ - return mHppOutputPath; -} - -std::string ConfigManager::getDumpJsonLibName() -{ - return mDumpJsonLibName; -} - -std::string ConfigManager::getGlobalDumpObjectName() -{ - return mGlobalDumpObjName; -} - -std::string ConfigManager::getObfuscationBookPath() -{ - return mObfuscationBookPath; -} - bool ConfigManager::Init() { if (FileHelper::IsValidFilePath(mConfigPath, true, true) == false) @@ -77,7 +42,7 @@ bool ConfigManager::InitDynamicDumpInfo() mDumpJsonLibName = mConfigRoot.get("dump_json_lib_name", "jsoncpp"); - mObfuscationBookMutationEnabled = mConfigRoot.get("obf_book_mut_enabled", false); + mObfustationBookDoMutate = mConfigRoot.get("obf_book_do_mutate", false); mObfuscationBookPath = mConfigRoot.get("obf_book_path", mMainCategory + "_obf_book.json"); } @@ -97,21 +62,7 @@ bool ConfigManager::InitDumpInfo() mHppOutputPath = mConfigRoot.get("hpp_output_path", mOutputName + ".hpp"); mDeclareGlobalDumpObj = mConfigRoot.get("declare_dump_global_obj", false); mGlobalDumpObjName = mConfigRoot.get("global_dump_obj_name", "g" + mMainCategory + "Offs"); + mDumpEncrypt = mConfigRoot.get("dump_encrypt", false); return true; -} - -bool ConfigManager::getObfuscationBookMutationEnabled() -{ - return mObfuscationBookMutationEnabled; -} - -bool ConfigManager::getDumpDynamic() -{ - return mDumpDynamic; -} - -bool ConfigManager::getDeclareGlobalDumpObj() -{ - return mDeclareGlobalDumpObj; -} +} \ No newline at end of file diff --git a/src/FutureOffsetResultInfo.cpp b/src/FutureOffsetResultInfo.cpp index 74f27c8..6801937 100644 --- a/src/FutureOffsetResultInfo.cpp +++ b/src/FutureOffsetResultInfo.cpp @@ -123,9 +123,9 @@ void FutureOffsetResultInfo::HandleCommentPreprocess() if (getNeedShowComment() == false) return; - setComment(StringHelper::ReplacePlaceHolders(getComment(), [&](const std::string name) { - return "|_|" + name + "|_|"; - })); + //setComment(StringHelper::ReplacePlaceHolders(getComment(), [&](const std::string name) { + // return "|_|" + name + "|_|"; + // })); } diff --git a/src/IFutureResult.cpp b/src/IFutureResult.cpp index 55d158d..3a1f406 100644 --- a/src/IFutureResult.cpp +++ b/src/IFutureResult.cpp @@ -56,7 +56,12 @@ IJsonAccesor* IFutureResult::getJsonAccesor() bool IFutureResult::getDumpDynamic() { - return mTargetMgr->getDumpDynamic(); + return mTargetMgr->getConfigManager()->mDumpDynamic; +} + +bool IFutureResult::getDumpEncrypt() +{ + return mTargetMgr->getConfigManager()->mDumpEncrypt; } void IFutureResult::setMetadata(const JsonValueWrapper& metadata) diff --git a/src/IFutureResultInfo.cpp b/src/IFutureResultInfo.cpp index 425348d..9cd52f1 100644 --- a/src/IFutureResultInfo.cpp +++ b/src/IFutureResultInfo.cpp @@ -48,13 +48,13 @@ bool IFutureResultInfo::Init() mObfKey = getObfuscationManager()->getObfKey(mUIdentifierDynamic); mSaltKey = getObfuscationManager()->getSaltKey(mUIdentifierDynamic); - if (mSaltKey != 0) + if (mParent->getDumpEncrypt() && mSaltKey != 0) mUIdentifierDynamic += "_" + std::to_string(mSaltKey); mUIDHash = std::to_string((uint32_t)fnv1a_32(mUIdentifierDynamic.c_str(), mUIdentifierDynamic.size())); - mDynamicResult->setValue(mParent->getJsonAccesor()->genGetUInt(mUIDHash, mObfKey)); - } else mUIDHash = std::to_string((uint32_t)fnv1a_32(mUIdentifier.c_str(), mUIdentifier.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()));*/ mCanPickAnyResult = getMetadata().get("pick_any_result", false); diff --git a/src/OffsetHunter.cpp b/src/OffsetHunter.cpp index 9f1bdce..de6976b 100644 --- a/src/OffsetHunter.cpp +++ b/src/OffsetHunter.cpp @@ -18,15 +18,7 @@ bool OffsetHunter::Init() if (mConfigManager->Init() == false) return false; - mTargetManager->setDumpTargetPath(mConfigManager->getDumpTargetPath()); - mTargetManager->setMainCategoryName(mConfigManager->getMainCategoryName()); - mTargetManager->setHppOutputPath(mConfigManager->getHppOutputPath()); - mTargetManager->setDumpDynamic(mConfigManager->getDumpDynamic()); - mTargetManager->setDumpJsonLibName(mConfigManager->getDumpJsonLibName()); - mTargetManager->setDeclareGlobalDumpObj(mConfigManager->getDeclareGlobalDumpObj()); - mTargetManager->setGlobalDumpObjectName(mConfigManager->getGlobalDumpObjectName()); - mTargetManager->setObfuscationBookPath(mConfigManager->getObfuscationBookPath()); - mTargetManager->setObfuscationBookMutationEnabled(mConfigManager->getObfuscationBookMutationEnabled()); + mTargetManager->setConfigManager(mConfigManager.get()); if (mTargetManager->Init() == false) return false; @@ -57,7 +49,7 @@ void OffsetHunter::setConfigPath(const std::string& path) std::filesystem::current_path(parentPath); - mConfigManager->setConfigPath(absCfgPath.string()); + mConfigManager->mConfigPath = absCfgPath.string(); } CapstoneHelperProvider* OffsetHunter::getCapstoneHelperProvider() diff --git a/src/StringHelper.cpp b/src/StringHelper.cpp index 299474e..8439e98 100644 --- a/src/StringHelper.cpp +++ b/src/StringHelper.cpp @@ -76,7 +76,8 @@ std::string StringHelper::ReplacePlaceHolders(const std::string& input, std::fun acum += *c; } - result += acum; + if(acum.empty() == false) + result += acum; return result; } diff --git a/src/TargetManager.cpp b/src/TargetManager.cpp index 340c1b5..8f65476 100644 --- a/src/TargetManager.cpp +++ b/src/TargetManager.cpp @@ -19,20 +19,20 @@ TargetManager::TargetManager() bool TargetManager::Init() { - if (FileHelper::IsValidFilePath(mDumpTargetsPath, true, true) == false) + if (FileHelper::IsValidFilePath(mConfigMgr->mDumpTargetPath, true, true) == false) return false; - if (JsonHelper::File2Json(mDumpTargetsPath, mDumpTargetsRoot) == false) + if (JsonHelper::File2Json(mConfigMgr->mDumpTargetPath, mDumpTargetsRoot) == false) { printf("Unable to parse DUmp Targets\n"); return false; } - if (mDumpDynamic) + if (mConfigMgr->mDumpDynamic) { - if (JsonAccesorClassifier::Classify(mDumpJsonLibName, mJsonAccesor) == false) + if (JsonAccesorClassifier::Classify(mConfigMgr->mDumpJsonLibName, mJsonAccesor) == false) { - printf("\"%s\" Library mistyped or not supported\n", mDumpJsonLibName.c_str()); + printf("\"%s\" Library mistyped or not supported\n", mConfigMgr->mDumpJsonLibName.c_str()); return false; } @@ -40,11 +40,11 @@ bool TargetManager::Init() AddInclude(mJsonAccesor->getGlobalInclude()); } - if (getDumpDynamic()) + if (mConfigMgr->mDumpEncrypt) { - mObfucationManager->setPath(mObfuscationBookPath); + mObfucationManager->setPath(mConfigMgr->mObfuscationBookPath); mObfucationManager->setParent(this); - mObfucationManager->setObfInfoMutationEnabled(mObfuscationBookMutationEnabled); + mObfucationManager->setObfInfoMutationEnabled(mConfigMgr->mObfustationBookDoMutate); if (mObfucationManager->Init() == false) return false; @@ -56,13 +56,13 @@ bool TargetManager::Init() if (InitAllTargets() == false) return false; - if (mHppOutputPath.empty() == false) + if (mConfigMgr->mHppOutputPath.empty() == false) { - std::unique_ptr mHppOutputFile = std::make_unique(mHppOutputPath); + std::unique_ptr mHppOutputFile = std::make_unique(mConfigMgr->mHppOutputPath); if (mHppOutputFile->is_open() == false) { - printf("Unable to open/create \"%s\"\n", mHppOutputPath.c_str()); + printf("Unable to open/create \"%s\"\n", mConfigMgr->mHppOutputPath.c_str()); return false; } @@ -94,7 +94,7 @@ bool TargetManager::SaveResults() if (SaveHpp() == false) return false; - if (getDumpDynamic()) + if (mConfigMgr->mDumpDynamic) { if (SaveJson() == false) return false; @@ -124,7 +124,7 @@ bool TargetManager::SaveJson() return bSucess; } -bool TargetManager::SaveHpp() +bool TargetManager::SaveHppRuntime() { mHppWriter->AppendPragmaOnce(); @@ -132,7 +132,18 @@ bool TargetManager::SaveHpp() mHppWriter->AppendNextLine(); - mHppWriter->BeginStruct(mMainCategoryName); + +} + +bool TargetManager::SaveHppCompileTime() +{ + mHppWriter->AppendPragmaOnce(); + + WriteHppIncludes(); + + mHppWriter->AppendNextLine(); + + mHppWriter->BeginStruct(mConfigMgr->mMainCategory); /*Inside the struct*/ @@ -142,7 +153,7 @@ bool TargetManager::SaveHpp() // Generate Declaration-Definition here staticly - if (mDumpDynamic) + if (mConfigMgr->mDumpDynamic) { mHppWriter->AppendMacroElse(); /*Generate Declaration-only here*/ @@ -150,9 +161,17 @@ bool TargetManager::SaveHpp() WriteHppDynDecls(); mHppWriter->BeginFunction("void", mDynamicOffsetSetterFuncName, { "const " + mJsonAccesor->getJsonObjFullType() + "& " + mDynamicJsonObjName }); + + // Generating Decryption Safeguards + mHppWriter->AppendLineOfCode("static bool initialized = false;"); mHppWriter->AppendNextLine(); + mHppWriter->AppendLineOfCode("if(initialized) return;"); mHppWriter->AppendNextLine(); + /*Generate Definition-only here*/ - WriteHppDynDefs(); + WriteHppDynDefs(); + mHppWriter->AppendNextLine(); + + mHppWriter->AppendLineOfCode("initialized = true;"); mHppWriter->EndFunction(); @@ -163,14 +182,24 @@ bool TargetManager::SaveHpp() std::vector decls; - if (mDeclareDumpObject) - decls.push_back(StructDeclarationInfo(mGlobalDumpObjName, true, true)); + if (mConfigMgr->mDeclareGlobalDumpObj) + decls.push_back(StructDeclarationInfo(mConfigMgr->mGlobalDumpObjName, true, true)); - mHppWriter->EndStruct(mMainCategoryName, decls); + mHppWriter->EndStruct(mConfigMgr->mMainCategory, decls); return true; } +void TargetManager::setConfigManager(ConfigManager* configMgr) +{ + mConfigMgr = configMgr; +} + +ConfigManager* TargetManager::getConfigManager() +{ + return mConfigMgr; +} + void TargetManager::RemoveTarget(DumpTargetGroup* target) { if (mAllTargets.find(target) == mAllTargets.end()) @@ -189,26 +218,11 @@ void TargetManager::AddTarget(std::unique_ptr& target) mAllTargets[pDumpTarget] = std::move(target); } -void TargetManager::setDumpTargetPath(const std::string& path) -{ - mDumpTargetsPath = path; -} - -void TargetManager::setMainCategoryName(const std::string& mainCategoryName) -{ - mMainCategoryName = mainCategoryName; -} - -void TargetManager::setHppOutputPath(const std::string& outputPath) -{ - mHppOutputPath = outputPath; -} - bool TargetManager::ReadAllTargets() { if (mDumpTargetsRoot.isArray() == false) { - printf("Unexpected Format of the \"%s\"\n", mDumpTargetsPath.c_str()); + printf("Unexpected Format of the \"%s\"\n", mConfigMgr->mDumpTargetPath.c_str()); return false; } @@ -230,7 +244,7 @@ bool TargetManager::HandleTargetGroupJson(const JsonValueWrapper& targetGroupRoo if (JSON_ASSERT_STR_EMPTY(targetGroupRoot, "macro") == false) { - printf("\"macro\" Not present or empty in \"%s\" Targets config\n", mDumpTargetsPath.c_str()); + printf("\"macro\" Not present or empty in \"%s\" Targets config\n", mConfigMgr->mDumpTargetPath.c_str()); return false; } @@ -239,7 +253,7 @@ bool TargetManager::HandleTargetGroupJson(const JsonValueWrapper& targetGroupRoo targetGroup->setTargetManager(this); targetGroup->setDumpTargetDescJson(targetGroupRoot); targetGroup->setParent(this); - targetGroup->setTargetJsonPath(mDumpTargetsPath); + targetGroup->setTargetJsonPath(mConfigMgr->mDumpTargetPath); AddTarget(targetGroup); @@ -251,26 +265,6 @@ HeaderFileManager* TargetManager::getHppWriter() return mHppWriter.get(); } -void TargetManager::setObfuscationBookMutationEnabled(bool b) -{ - mObfuscationBookMutationEnabled = b; -} - -void TargetManager::setDumpDynamic(bool b) -{ - mDumpDynamic = b; -} - -void TargetManager::setDeclareGlobalDumpObj(bool b) -{ - mDeclareDumpObject = b; -} - -void TargetManager::setGlobalDumpObjectName(const std::string& globalObjName) -{ - mGlobalDumpObjName = globalObjName; -} - void TargetManager::setJsonAccesor(std::unique_ptr&& accesor) { mJsonAccesor = std::move(accesor); @@ -281,26 +275,6 @@ IJsonAccesor* TargetManager::getJsonAccesor() return mJsonAccesor.get(); } -void TargetManager::setDumpJsonLibName(const std::string& dumpJsonLibName) -{ - mDumpJsonLibName = dumpJsonLibName; -} - -bool TargetManager::getDumpDynamic() -{ - return mDumpDynamic; -} - -void TargetManager::setDynamicOffsetSetterFuncName(const std::string& dynamicOffsetSetterFuncName) -{ - mDynamicOffsetSetterFuncName = dynamicOffsetSetterFuncName; -} - -void TargetManager::setObfuscationBookPath(const std::string& obfuscationBookPath) -{ - mObfuscationBookPath = obfuscationBookPath; -} - void TargetManager::WriteHppIncludes() { for (const auto& kv : mAllTargets)