Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesadi committed Dec 25, 2023
1 parent 9b1492c commit e35772d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 39 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ jobs:
run: sudo apt install ninja-build -y
- name: Install z3
run: sudo apt install libz3-dev -y
- name: Install bitcoin dependencies
run: sudo apt install build-essential libtool autotools-dev automake pkg-config bsdmainutils python3 libevent-dev libboost-dev -y
- name: git submodule update --init
run: git submodule update --init
- name: make install
Expand All @@ -27,10 +25,10 @@ jobs:
run: ./tests/test-aes.sh
- name: Test jsoncpp
run: ./tests/test-jsoncpp.sh
- name: Test json
run: ./tests/test-json.sh
- name: Test openssl
run: ./tests/test-openssl.sh
- name: Test bitcoin
run: ./tests/test-bitcoin.sh
# Windows:

# runs-on: windows-latest
Expand Down
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[submodule "tests/openssl"]
path = tests/openssl
url = https://github.com/openssl/openssl.git
[submodule "tests/bitcoin"]
path = tests/bitcoin
url = https://github.com/bitcoin/bitcoin.git
[submodule "tests/jsoncpp"]
path = tests/jsoncpp
url = https://github.com/open-source-parsers/jsoncpp.git
[submodule "tests/json"]
path = tests/json
url = https://github.com/nlohmann/json.git
42 changes: 21 additions & 21 deletions llvm/lib/Transforms/Obfuscation/MBAObfuscation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;
using namespace llvm;
using namespace MBAUtils;

#define NUM_TERMS 10
#define NUM_COEFFS 10

PreservedAnalyses Pluto::MbaObfuscation::run(Function &F, FunctionAnalysisManager &AM) {
for (BasicBlock &BB : F) {
Expand Down Expand Up @@ -47,9 +47,9 @@ PreservedAnalyses Pluto::MbaObfuscation::run(Function &F, FunctionAnalysisManage
void Pluto::MbaObfuscation::substituteConstant(Instruction *I, int i) {
ConstantInt *val = dyn_cast<ConstantInt>(I->getOperand(i));
if (val && val->getBitWidth() <= 64) {
int64_t *terms = generateLinearMBA(NUM_TERMS);
terms[14] -= val->getValue().getZExtValue();
Value *mbaExpr = insertLinearMBA(terms, I);
int64_t *coeffs = generateLinearMBA(NUM_COEFFS);
coeffs[14] -= val->getValue().getZExtValue();
Value *mbaExpr = insertLinearMBA(coeffs, I);
if (val->getBitWidth() <= 32) {
mbaExpr = insertPolynomialMBA(mbaExpr, I);
}
Expand Down Expand Up @@ -87,33 +87,33 @@ void Pluto::MbaObfuscation::substitute(BinaryOperator *BI) {
}

Value *Pluto::MbaObfuscation::substituteAdd(BinaryOperator *BI) {
int64_t *terms = generateLinearMBA(NUM_TERMS);
terms[2] += 1;
terms[4] += 1;
return insertLinearMBA(terms, BI);
int64_t *coeffs = generateLinearMBA(NUM_COEFFS);
coeffs[2] += 1;
coeffs[4] += 1;
return insertLinearMBA(coeffs, BI);
}

Value *Pluto::MbaObfuscation::substituteSub(BinaryOperator *BI) {
int64_t *terms = generateLinearMBA(NUM_TERMS);
terms[2] += 1;
terms[4] -= 1;
return insertLinearMBA(terms, BI);
int64_t *coeffs = generateLinearMBA(NUM_COEFFS);
coeffs[2] += 1;
coeffs[4] -= 1;
return insertLinearMBA(coeffs, BI);
}

Value *Pluto::MbaObfuscation::substituteXor(BinaryOperator *BI) {
int64_t *terms = generateLinearMBA(NUM_TERMS);
terms[5] += 1;
return insertLinearMBA(terms, BI);
int64_t *coeffs = generateLinearMBA(NUM_COEFFS);
coeffs[5] += 1;
return insertLinearMBA(coeffs, BI);
}

Value *Pluto::MbaObfuscation::substituteAnd(BinaryOperator *BI) {
int64_t *terms = generateLinearMBA(NUM_TERMS);
terms[0] += 1;
return insertLinearMBA(terms, BI);
int64_t *coeffs = generateLinearMBA(NUM_COEFFS);
coeffs[0] += 1;
return insertLinearMBA(coeffs, BI);
}

Value *Pluto::MbaObfuscation::substituteOr(BinaryOperator *BI) {
int64_t *terms = generateLinearMBA(NUM_TERMS);
terms[6] += 1;
return insertLinearMBA(terms, BI);
int64_t *coeffs = generateLinearMBA(NUM_COEFFS);
coeffs[6] += 1;
return insertLinearMBA(coeffs, BI);
}
25 changes: 24 additions & 1 deletion llvm/lib/Transforms/Obfuscation/MBAUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include "llvm/Transforms/Obfuscation/CryptoUtils.h"
#include <algorithm>
#include <cstdint>
#include <queue>
#include <string>
#include <vector>

#define USE_CACHE

using namespace z3;
using namespace llvm;

Expand All @@ -30,6 +33,19 @@ static int8_t truthTables[15][4] = {
};

int64_t *MBAUtils::generateLinearMBA(int numExprs) {
#ifdef USE_CACHE
static std::queue<int64_t *> cached_coeffs;
if (cached_coeffs.size() && cryptoutils->get_range(10) < 8) {
int64_t *coeffs = cached_coeffs.front();
outs() << "[DEBUG] Use cached coefficients:";
for (int i = 0; i < 15; i++) {
outs() << " " << coeffs[i];
}
outs() << "\n";
cached_coeffs.pop();
return coeffs;
}
#endif
int *exprs = new int[numExprs];
int64_t *coeffs = new int64_t[15];
while (true) {
Expand All @@ -42,7 +58,7 @@ int64_t *MBAUtils::generateLinearMBA(int numExprs) {
X.push_back(c.int_const(name.c_str()));
}
for (int i = 0; i < numExprs; i++) {
exprs[i] = rand() % 15;
exprs[i] = cryptoutils->get_range(15);
}
for (int i = 0; i < 4; i++) {
expr equ = c.int_val(0);
Expand All @@ -65,6 +81,13 @@ int64_t *MBAUtils::generateLinearMBA(int numExprs) {
coeffs[exprs[i]] += m.eval(X[i]).as_int64();
}
delete[] exprs;
#ifdef USE_CACHE
if (cached_coeffs.size() < 10) {
int64_t *coeffs_copy = new int64_t[15];
std::copy(coeffs, coeffs + 15, coeffs_copy);
cached_coeffs.push(coeffs_copy);
}
#endif
return coeffs;
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/bitcoin
Submodule bitcoin deleted from 4b1196
1 change: 1 addition & 0 deletions tests/json
Submodule json added at a259ec
9 changes: 0 additions & 9 deletions tests/test-bitcoin.sh

This file was deleted.

12 changes: 12 additions & 0 deletions tests/test-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CXX=`pwd`/install/bin/clang++
CXX_FLAGS="-mllvm -passes=hlw,fla,mba"

cd tests/json
rm -rf build
mkdir -p build
cmake -B build \
-G "Ninja" \
-DCMAKE_CXX_COMPILER=$CXX \
-DCMAKE_CXX_FLAGS="$CXX_FLAGS"
ninja -j`nproc` -C build
ninja test

0 comments on commit e35772d

Please sign in to comment.