Skip to content

Commit

Permalink
Refactored obfuscator into a library
Browse files Browse the repository at this point in the history
  • Loading branch information
oopsmishap committed Sep 15, 2024
1 parent 7683ab6 commit f2e6bde
Show file tree
Hide file tree
Showing 60 changed files with 1,862 additions and 1,242 deletions.
104 changes: 103 additions & 1 deletion obfuscator/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion obfuscator/cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,42 @@
name = "obfuscator"
msvc-runtime = "static"

[options]
BUILD_TESTS = true
ENABLE_LOGGING = true

[fetch-content.zasm]
git = "https://github.com/zyantific/zasm"
tag = "a18fa34251b161cc18e4c0fc385dee7cea104cfd"

[fetch-content.GTest]
git = "https://github.com/google/googletest"
tag = "v1.15.2"
condition = "build-tests"

[target.linux_pe]
type = "interface"
include-directories = ["thirdparty/linux-pe"]
compile-features = ["cxx_std_20"]

[target.obfuscator_lib]
type = "static"
sources = ["lib/src/**.cpp"]
headers = ["lib/include/**.hpp"]
include-directories = ["lib/include"]
compile-features = ["cxx_std_20"]
link-libraries = ["zasm::zasm", "linux_pe"]
ENABLE_LOGGING.compile-definitions = ["ENABLE_LOGGING"]

[target.obfuscator]
type = "executable"
sources = ["src/main.cpp"]
compile-features = ["cxx_std_20"]
link-libraries = ["zasm::zasm"]
link-libraries = ["obfuscator_lib"]

[target.obfuscator_tests]
type = "executable"
condition = "build-tests"
sources = ["tests/**.cpp"]
headers = ["tests/**.hpp"]
link-libraries = ["obfuscator_lib", "GTest::gtest_main"]
24 changes: 24 additions & 0 deletions obfuscator/lib/include/analyzer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <zasm/zasm.hpp>
#include "cfg.hpp"

namespace ObfuscatorLib
{

using namespace zasm;

class Analyzer
{
public:
Analyzer(Program& program);

bool analyze(bool verbose = false);
CFG& getCFG();

private:
Program& program_;
CFG cfg_;
};

} // namespace ObfuscatorLib
59 changes: 59 additions & 0 deletions obfuscator/lib/include/cfg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include <map>
#include <set>
#include <vector>
#include <zasm/zasm.hpp>

namespace ObfuscatorLib
{

using namespace zasm;

struct BasicBlock
{
uint64_t address = 0;
Label label;
Node* begin = nullptr;
Node* end = nullptr;

uint32_t regsUse = 0;
uint32_t regsDef = 0;
uint32_t regsLiveIn = 0;
uint32_t regsLiveOut = 0;

uint32_t flagsUse = 0;
uint32_t flagsDef = 0;
uint32_t flagsLiveIn = 0;
uint32_t flagsLiveOut = 0;
};

class CFG
{
public:
CFG();

bool create(Program& program, Label entryLabel);
void computeLiveness();

std::map<Label::Id, BasicBlock>& getBasicBlocks();

void printResults(Program& program);
void printDot(Program& program);

private:
BasicBlock& getBlock(Label label, LabelData& data);
BasicBlock& getBlock(Label::Id id);
void addEdge(Label from, Label to);
std::set<Label::Id> getSuccessors(Label::Id labelId);
std::set<Label::Id> getPredecessors(Label::Id labelId);

private:
std::map<Label::Id, BasicBlock> blocks_;
std::map<Label::Id, std::set<Label::Id>> predecessors_;
std::map<Label::Id, std::set<Label::Id>> successors_;
std::set<Label::Id> exits_;
Label::Id entry_;
};

} // namespace ObfuscatorLib
26 changes: 26 additions & 0 deletions obfuscator/lib/include/context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <deque>
#include <vector>
#include <zasm/zasm.hpp>
#include "instruction_data.hpp"


namespace ObfuscatorLib
{

using namespace zasm;

class Context
{
public:
Context(Program& program);

void addInstructionData(Node* node, uintptr_t address, const InstructionDetail& detail);

private:
Program& program_;
std::deque<InstructionData> instructionDataPool_;
};

} // namespace ObfuscatorLib
27 changes: 27 additions & 0 deletions obfuscator/lib/include/disassembler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "context.hpp"
#include <string>
#include <vector>
#include <zasm/zasm.hpp>

namespace ObfuscatorLib
{

using namespace zasm;

class Disassembler
{
public:
Disassembler(Program& program, Context& context);

bool disassemble(
const std::string& functionName, uintptr_t address, const std::vector<uint8_t>& code, bool verbose = false
);

private:
Context& ctx_;
Program& program_;
};

} // namespace ObfuscatorLib
24 changes: 24 additions & 0 deletions obfuscator/lib/include/instruction_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <zasm/zasm.hpp>

namespace ObfuscatorLib
{

using namespace zasm;

struct InstructionData
{
uint64_t address = 0;
uint32_t flagsModified = 0;
uint32_t flagsTested = 0;
uint32_t regsWritten = 0;
uint32_t regsRead = 0;

uint32_t regsLive = 0;
uint32_t flagsLive = 0;

zasm::InstructionDetail detail;
};

} // namespace ObfuscatorLib
21 changes: 21 additions & 0 deletions obfuscator/lib/include/logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <cstdio>

namespace ObfuscatorLib
{

class Logger
{
public:
Logger();
~Logger();

static void log(const char* format, ...);
static void logLine(const char* format, ...);
static void logError(const char* format, ...);
static void logWarning(const char* format, ...);
static void logInfo(const char* format, ...);
};

} // namespace ObfuscatorLib
26 changes: 26 additions & 0 deletions obfuscator/lib/include/obfuscator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <string>
#include <vector>

namespace ObfuscatorLib
{

class Obfuscator
{
public:
Obfuscator();
~Obfuscator();

bool loadPEFile(const std::string& filePath, const std::string& functionName, bool verbose = false);
bool disassembleFunction(const std::string& functionName, bool verbose = false);
bool analyzeFunction(bool verbose = false);
bool obfuscateFunction(bool verbose = false);
bool serialize(std::vector<uint8_t>& outputCode, bool verbose = false);

private:
class Impl;
Impl* impl_;
};

} // namespace ObfuscatorLib
Loading

0 comments on commit f2e6bde

Please sign in to comment.