Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ bool addPassWithName(FunctionPassManager &FPM, StringRef &passName) {

bool addPassWithName(ModulePassManager &MPM, StringRef &passName) {
if (passName == "string-encryption") {
MPM.addPass(StringObfuscatorPass());
size_t maxSize = 0;
getEnvVar(EnvVarPrefix + "MAX_STRING_LENGTH").getAsInteger(10, maxSize);
MPM.addPass(StringObfuscatorPass{maxSize});
} else {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Or you can run the string encryption pass with:

Refer to the llvm::PassBuilder documentation for more information on each insertion point.

You can limit the length of strings that are obfuscated with the `LLVM_OBF_MAX_STRING_LENGTH` environment variable. A
value of 0 (the default) means no limit.

### With opt

[`opt`](https://llvm.org/docs/CommandGuide/opt.html) can be used to apply specific passes from LLRM-IR you
Expand Down
4 changes: 4 additions & 0 deletions flattening/Flattening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
#include "llvm/Analysis/LazyValueInfo.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#if LLVM_VERSION_MAJOR >= 22
#include "llvm/Plugins/PassPlugin.h"
#else
#include "llvm/Passes/PassPlugin.h"
#endif
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils.h"
#include "llvm/Transforms/Utils/LowerSwitch.h"
Expand Down
4 changes: 4 additions & 0 deletions split/SplitBasicBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#include "utils/Utils.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#if LLVM_VERSION_MAJOR >= 22
#include "llvm/Plugins/PassPlugin.h"
#else
#include "llvm/Passes/PassPlugin.h"
#endif

#include "utils/CryptoUtils.h"

Expand Down
29 changes: 14 additions & 15 deletions string/StringObfuscation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#if LLVM_VERSION_MAJOR >= 22
#include "llvm/Plugins/PassPlugin.h"
#else
#include "llvm/Passes/PassPlugin.h"
#endif
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils.h"
#include "llvm/Transforms/Utils/Cloning.h"
Expand All @@ -28,14 +32,8 @@ ConstantDataArray *StringObfuscatorPass::encodeStringDataArray(LLVMContext &ctx,
const char *str,
size_t size,
uint8_t key) {
// Check this is a valid string (not containing zeros)
if (str[size - 1] == '\0') {
if (strnlen(str, size) != size - 1)
return nullptr;
} else {
if (strnlen(str, size) != size)
return nullptr;
}
if (this->maxSize != 0 && size > this->maxSize)
return nullptr;

// Encode the data
char *encodedStr = (char *)malloc(size);
Expand Down Expand Up @@ -87,17 +85,18 @@ void StringObfuscatorPass::encodeStructString(LLVMContext &ctx,
}
}

StringObfuscatorPass::StringObfuscatorPass() {}
StringObfuscatorPass::StringObfuscatorPass(size_t maxSize) : maxSize{maxSize} {}

bool StringObfuscatorPass::encodeAllStrings(Module &M) {
auto &ctx = M.getContext();

// For each global variable
for (GlobalVariable &gv : M.globals()) {
if (!gv.isConstant() // constant
|| !gv.hasInitializer() // unitialized
|| gv.hasExternalLinkage() // external
|| gv.getSection() == "llvm.metadata") { // Intrinsic Global Variables
if (!gv.isConstant() // constant
|| !gv.hasInitializer() // unitialized
|| gv.hasExternalLinkage() // external
|| gv.getAlign().valueOrOne().value() != 1 // align == 1
|| gv.getSection() == "llvm.metadata") { // Intrinsic Global Variables
//|| gv.getSection().find("__objc_methname") != string::npos) { // TODO :
// is this necessary ?
continue;
Expand All @@ -109,7 +108,7 @@ bool StringObfuscatorPass::encodeAllStrings(Module &M) {
// Encode the value and update the variable
if (isa<ConstantDataArray>(initializer)) { // Global variable
auto array = cast<ConstantDataArray>(initializer);
if (array->isCString()) {
if (array->isString()) {
encodeGlobalString(ctx, &gv, array);
}
} else if (isa<ConstantStruct>(initializer)) { // Variable in a struct
Expand All @@ -118,7 +117,7 @@ bool StringObfuscatorPass::encodeAllStrings(Module &M) {
auto operand = cs->getOperand(i);
if (isa<ConstantDataArray>(operand)) {
auto array = cast<ConstantDataArray>(operand);
if (array->isCString()) {
if (array->isString()) {
encodeStructString(ctx, &gv, cs, array, i);
}
}
Expand Down
4 changes: 3 additions & 1 deletion string/StringObfuscation.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ struct GlobalStringVariable {
}
};


namespace llvm {
struct StringObfuscatorPass : public PassInfoMixin<StringObfuscatorPass> {
std::vector<GlobalStringVariable> globalStrings;
size_t maxSize;

StringObfuscatorPass();
StringObfuscatorPass(size_t maxSize);
ConstantDataArray *encodeStringDataArray(LLVMContext &ctx, const char *str,
size_t size, uint8_t key);
void encodeStructString(LLVMContext &ctx, GlobalVariable *gv,
Expand Down