Skip to content

Commit

Permalink
Fix erro for build
Browse files Browse the repository at this point in the history
  • Loading branch information
SsageParuders committed May 22, 2023
1 parent 1f6c9af commit e8ce440
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ obfuscator-AutoRegistration
*/CMakeCache*
Obfuscation/Makefile
Obfuscation/libSsageObfuscator.so
.cache
12 changes: 6 additions & 6 deletions Obfuscation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
cmake_minimum_required(VERSION 3.13.4)
# SET (CMAKE_C_COMPILER_WORKS 1)
# SET (CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_C_COMPILER /root/android_clang_14_06/bin/clang) # 调用原生NDK的clang 来自AOSP
set(CMAKE_CXX_COMPILER /root/android_clang_14_06/bin/clang) # 调用原生NDK的clang++ 来自AOSP
set(CMAKE_C_COMPILER /home/ssage/Android/ndk-llvm/out/install/linux-x86/clang-dev/bin/clang) # 调用原生NDK的clang 来自AOSP
set(CMAKE_CXX_COMPILER /home/ssage/Android/ndk-llvm/out/install/linux-x86/clang-dev/bin/clang) # 调用原生NDK的clang++ 来自AOSP

project(SsagePass) # 项目名称
# we need LLVM_HOME in order not automatically set LLVM_DIR
set(ENV{LLVM_HOME} /root/android_clang_14_06) # 指定LLVM_HOME为来自原生NDK的
set(ENV{LLVM_DIR} /root/android_clang_14_06/lib64/cmake/llvm) # 指定cmake文件 如此可以连接到LLVM库
set(ENV{LLVM_HOME} /home/ssage/Android/ndk-llvm/out/install/linux-x86/clang-dev) # 指定LLVM_HOME为来自原生NDK的
set(ENV{LLVM_DIR} /home/ssage/Android/ndk-llvm/out/install/linux-x86/clang-dev/lib64/cmake/llvm) # 指定cmake文件 如此可以连接到LLVM库

find_package(LLVM REQUIRED CONFIG) # 寻找LLVM依赖包
add_definitions(${LLVM_DEFINITIONS}) # 预定义
include_directories(/root/android_clang_14_06/include) # LLVM的头文件
include_directories(/home/ssage/Android/ndk-llvm/out/install/linux-x86/clang-dev/include) # LLVM的头文件
include_directories("./include") # 包含 ./include 文件夹中的头文件 主要是为Pass项目服务
link_directories(${LLVM_LIBRARY_DIRS}) # LLVM依赖库

## 如果LLVM版本高于10 则需要启用C++14特性
set(CMAKE_CXX_STANDARD 14) # LLVM启用14特性
## 确保使用llvm的libc++ 而不是来自gcc的libstdc++
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") # libc++
include_directories(/root/android_clang_14_06/include/c++/v1) # libc++的头文件包含路径
include_directories(/home/ssage/Android/ndk-llvm/out/install/linux-x86/clang-dev/include/c++/v1) # libc++的头文件包含路径
set(CMAKE_SKIP_RPATH ON)

add_library( SsageObfuscator MODULE # 生成动态库文件
Expand Down
72 changes: 37 additions & 35 deletions Obfuscation/src/StringEncryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ static cl::opt<bool> OnlyStr("onlystr", cl::init(true),
cl::desc("Encrypt string variable only"));

PreservedAnalyses StringEncryptionPass::run(Module &M, ModuleAnalysisManager& AM) {
for (Module::iterator iter = M.begin(); iter != M.end(); iter++) {
Function *F = &(*iter);
if (toObfuscate(flag, F, "strenc")){
for (Module::iterator iter = M.begin(); iter != M.end(); iter++) {
Function *F = &(*iter);
outs() << "\033[1;32m[StringEncryption] Function : " << F->getName() << "\033[0m\n"; // 打印一下被混淆函数的symbol
}
Function *F = &(*iter);
outs() << "\033[1;32m[StringEncryption] Function : " << F->getName() << "\033[0m\n"; // 打印一下被混淆函数的symbol
INIT_CONTEXT(M);
vector<GlobalVariable *> GVs;
for (GlobalVariable &GV : M.getGlobalList()) {
Expand All @@ -24,48 +24,50 @@ PreservedAnalyses StringEncryptionPass::run(Module &M, ModuleAnalysisManager& AM
for (int i = 0; i < ObfuTimes; i++) {
for (GlobalVariable *GV : GVs) {
// Only encrypt globals of integer and array
if (!GV->getValueType()->isIntegerTy() &&
!GV->getValueType()->isArrayTy()) {
if (!GV->getValueType()->isIntegerTy() && !GV->getValueType()->isArrayTy()) {
continue;
}
if (GV->hasInitializer() && GV->getInitializer() &&
(GV->getName().contains(".str") || !OnlyStr)
// Do not encrypt globals having a section named "llvm.metadata"
&& !GV->getSection().equals("llvm.metadata")
&& GV->getSection().find(StringRef("__objc")) == string::npos && GV->getName().find("OBJC") == string::npos) {
if (GV->hasInitializer() && GV->getInitializer() && (GV->getName().contains(".str") || !OnlyStr)
// Do not encrypt globals having a section named
// "llvm.metadata"
&& !GV->getSection().equals("llvm.metadata") &&
GV->getSection().find(StringRef("__objc")) == string::npos &&
GV->getName().find("OBJC") == string::npos) {
Constant *initializer = GV->getInitializer();
ConstantInt *intData = dyn_cast<ConstantInt>(initializer);
ConstantDataArray *arrData = dyn_cast<ConstantDataArray>(initializer);

if (arrData) {
uint32_t eleSize = arrData->getElementByteSize();
uint32_t eleNum = arrData->getNumElements();
uint32_t arrLen = eleNum * eleSize;
char *data = const_cast<char *>(arrData->getRawDataValues().data());
char *dataCopy = new char[arrLen];
memcpy(dataCopy, data, arrLen);
uint64_t key = cryptoutils->get_uint64_t();
// A simple xor encryption
for (uint32_t i = 0; i < arrLen; i++) {
dataCopy[i] ^= ((char *)&key)[i % eleSize];
}
GV->setInitializer(ConstantDataArray::getRaw(
StringRef(dataCopy, arrLen), eleNum, arrData->getElementType()));
GV->setConstant(false);
insertArrayDecryption(M, {GV, key, eleNum});
uint32_t eleSize = arrData->getElementByteSize();
uint32_t eleNum = arrData->getNumElements();
uint32_t arrLen = eleNum * eleSize;
char *data = const_cast<char *>(arrData->getRawDataValues().data());
char *dataCopy = new char[arrLen];
memcpy(dataCopy, data, arrLen);
uint64_t key = cryptoutils->get_uint64_t();
// A simple xor encryption
for (uint32_t i = 0; i < arrLen; i++) {
dataCopy[i] ^= ((char *)&key)[i % eleSize];
}
GV->setInitializer(ConstantDataArray::getRaw(
StringRef(dataCopy, arrLen), eleNum,
arrData->getElementType()));
GV->setConstant(false);
insertArrayDecryption(M, {GV, key, eleNum});
} else if (intData) {
uint64_t key = cryptoutils->get_uint64_t();
ConstantInt *enc =
CONST(intData->getType(), key ^ intData->getZExtValue());
GV->setInitializer(enc);
GV->setConstant(false);
insertIntDecryption(M, {GV, key, 1LL});
uint64_t key = cryptoutils->get_uint64_t();
ConstantInt *enc = CONST(intData->getType(),
key ^ intData->getZExtValue());
GV->setInitializer(enc);
GV->setConstant(false);
insertIntDecryption(M, {GV, key, 1LL});
}
}
}
}
}
} else {
return PreservedAnalyses::all();
}

}
return PreservedAnalyses::all();
}
/*
Expand Down

0 comments on commit e8ce440

Please sign in to comment.