Skip to content

Commit

Permalink
Semantic Refactor & Cleanups & ARM64 Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pinwhell committed Jan 27, 2024
1 parent b89c3a7 commit 5142986
Show file tree
Hide file tree
Showing 27 changed files with 245 additions and 90 deletions.
3 changes: 3 additions & 0 deletions include/OH/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class ConfigManager
JsonValueWrapper mConfigRoot;

bool mDumpDynamic;
bool mDumpRuntime;
bool mDumpEncrypt;
bool mIdentifierSalt;
bool mIdentifierHash;

std::string mMainCategory;
std::string mOutputName;
Expand Down
5 changes: 4 additions & 1 deletion include/OH/DumpTargetGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <unordered_map>
#include "IChild.h"
#include <OH/IJsonAccesor.h>

struct HeaderFileManager;

Expand All @@ -29,10 +30,12 @@ class DumpTargetGroup : public IDumpTarget, public IChild<TargetManager>

bool ReadAllTarget();

void HPPRuntimeResultWrite(IJsonAccesor* jsonAccesor);

void ReportHppIncludes();
void WriteHppStaticDeclsDefs();
void WriteHppDynDecls();
void WriteHppDynDefs();
void WriteHppCompileTimeDefs();
void MacroBegin();
void MacroEnd();
HeaderFileManager* getHppWriter();
Expand Down
1 change: 1 addition & 0 deletions include/OH/FileHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char>& output);
bool FileIsEmpty(const std::string& filePath);
}

2 changes: 1 addition & 1 deletion include/OH/FutureOffsetResultInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 6 additions & 1 deletion include/OH/IFutureResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ class IFutureResult : public IChild<SingleDumpTarget>
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();
Expand Down
15 changes: 12 additions & 3 deletions include/OH/IFutureResultInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include "ILValueRValueWrapper.h"
#include "INestedLValueRValueWrapper.h"
#include "IChild.h"
#include <OH/IJsonAccesor.h>

#define ERR_INVALID_OFFSET ((uint64_t)0xFFFFFFFFFFFFFFFF)
#define ERR_INVALID_OFFSET ((uint64_t)~0ull)

class IFutureResult;
struct HeaderFileManager;
Expand All @@ -19,13 +20,14 @@ class IFutureResultInfo : public IChild<IFutureResult>
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<ILValueRValueWrapper> mStaticResult;
std::unique_ptr<INestedLValueRValueWrapper> mDynamicResult; // Why nested? well, basicly we need to do, chainig struct
std::unique_ptr<INestedLValueRValueWrapper> 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;

Expand All @@ -48,7 +50,9 @@ class IFutureResultInfo : public IChild<IFutureResult>
virtual void ReportHppIncludes() {};
virtual void WriteHppStaticDeclsDefs();
virtual void WriteHppDynDecls();
virtual void WriteHppDynDefs();
virtual void WriteHppDef();

virtual void HppRuntimeDecryptionWrite(IJsonAccesor* jsonAccesor);

HeaderFileManager* getHppWriter();

Expand All @@ -62,4 +66,9 @@ class IFutureResultInfo : public IChild<IFutureResult>

virtual std::string getCppDataType() = 0;
virtual std::string getCppDefaultRvalue() = 0;




std::string getUniqueIdentifier();
};
3 changes: 3 additions & 0 deletions include/OH/IJsonAccesor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
};

9 changes: 4 additions & 5 deletions include/OH/ObfuscationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "JsonValueWrapper.h"
#include <unordered_set>
#include "IChild.h"
#include <OH/ConfigManager.h>

class TargetManager;

Expand All @@ -17,11 +18,11 @@ just like a book, where the pages are each "[identifier]" : {
class ObfuscationManager : public IChild<TargetManager>
{
private:
std::string mObfuscationInfoBookPath;
JsonValueWrapper mObfuscationInfoBookRoot;
bool mObfInfoMutationEnabled;
std::unordered_set<std::string> 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);
Expand All @@ -38,8 +39,6 @@ class ObfuscationManager : public IChild<TargetManager>
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);
};

5 changes: 4 additions & 1 deletion include/OH/SingleDumpTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "JsonValueWrapper.h"
#include "IChild.h"
#include "IBinaryFormat.h"
#include <OH/IJsonAccesor.h>

class DumpTargetGroup;
struct HeaderFileManager;
Expand Down Expand Up @@ -45,10 +46,12 @@ class SingleDumpTarget : public IDumpTarget, public IChild<DumpTargetGroup>

std::string getCategoryObjectName();

void HppRuntimeDecryptionWrite(IJsonAccesor* jsonAccesor);

void ReportHppIncludes();
void WriteHppStaticDeclsDefs();
void WriteHppDynDecls();
void WriteHppDynDefs();
void WriteHppCompileTimeDefs();
void BeginStruct();
void EndStruct();

Expand Down
16 changes: 15 additions & 1 deletion include/OH/TargetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ class TargetManager : public IChild<OffsetHunter>
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();

Expand Down
35 changes: 28 additions & 7 deletions samples/DummyLib/DummyOffsetMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)#
Expand All @@ -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)#
Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion samples/DummyLib/dummyConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion samples/DummyLib/dummyObfBook.json
Original file line number Diff line number Diff line change
@@ -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}}
23 changes: 6 additions & 17 deletions samples/DummyLib/dummyTargets.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[ // Targets

/*{ // Dump Target Group
{ // Dump Target Group

"macro" : "ARM32",
"targets" : [
Expand All @@ -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
]
1 change: 1 addition & 0 deletions samples/DummyLib/offsets_ARM32.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"1138065394":1844445070,"1775762011":4127170514,"500368777":4023873326}
2 changes: 1 addition & 1 deletion samples/DummyLib/offsets_ARM64.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"3482068232":8,"4119764849":8,"462494170":8}
{"3482068232":1333386728,"4119764849":3581899234,"462494170":4000739479}
3 changes: 3 additions & 0 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ bool ConfigManager::InitDumpInfo()
mDeclareGlobalDumpObj = mConfigRoot.get<bool>("declare_dump_global_obj", false);
mGlobalDumpObjName = mConfigRoot.get<std::string>("global_dump_obj_name", "g" + mMainCategory + "Offs");
mDumpEncrypt = mConfigRoot.get<bool>("dump_encrypt", false);
mDumpRuntime = mConfigRoot.get<bool>("dump_runtime", false);
mIdentifierSalt = mConfigRoot.get<bool>("identifier_salt", false);
mIdentifierHash = mConfigRoot.get<bool>("identifier_hash", false);

return true;
}
16 changes: 14 additions & 2 deletions src/DumpTargetGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -147,15 +159,15 @@ void DumpTargetGroup::WriteHppDynDecls()
MacroEnd();
}

void DumpTargetGroup::WriteHppDynDefs()
void DumpTargetGroup::WriteHppCompileTimeDefs()
{
if (mTargets.size() < 1)
return;

MacroBegin();

for (auto& kv : mTargets)
kv.first->WriteHppDynDefs();
kv.first->WriteHppCompileTimeDefs();

MacroEnd();
}
Expand Down
10 changes: 10 additions & 0 deletions src/FileHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ bool FileHelper::ReadFileBinary(const std::string& filePath, std::vector<unsigne
return true;
}

bool FileHelper::FileIsEmpty(const std::string& filePath)
{
std::string fileContent = "";

if (ReadFile(filePath, fileContent) == false)
return false;

return fileContent.empty();
}

Loading

0 comments on commit 5142986

Please sign in to comment.