Skip to content

Commit

Permalink
Move obfuscator code to a library
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexodia committed Oct 11, 2024
1 parent a5708e6 commit ff74c2c
Show file tree
Hide file tree
Showing 16 changed files with 1,432 additions and 1,195 deletions.
89 changes: 82 additions & 7 deletions obfuscator/CMakeLists.txt

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

36 changes: 33 additions & 3 deletions 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 = "root"

[variables]
INSTALL_GTEST = false

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

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

[target.linux-pe]
type = "interface"
include-directories = ["src/linux-pe"]

[target.obfuscator]
type = "executable"
sources = ["src/main.cpp"]
type = "static"
alias = "riscvm::obfuscator"
sources = ["src/obfuscator/*.cpp"]
headers = ["src/obfuscator/*.hpp"]
include-directories = ["include"]
compile-features = ["cxx_std_20"]
link-libraries = ["zasm::zasm"]
link-libraries = ["zasm::zasm", "linux-pe"]

[target.obfuscate]
condition = "root"
type = "executable"
sources = ["src/obfuscate.cpp"]
link-libraries = ["riscvm::obfuscator"]

[target.tests]
type = "executable"
condition = "build-tests"
sources = ["src/tests/*.cpp"]
link-libraries = ["riscvm::obfuscator", "gtest_main"]
8 changes: 8 additions & 0 deletions obfuscator/include/obfuscator/analyze.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <obfuscator/context.hpp>

namespace obfuscator
{
bool analyze(Context& ctx, bool verbose = false);
}
43 changes: 43 additions & 0 deletions obfuscator/include/obfuscator/context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <deque>
#include <zasm/zasm.hpp>

namespace obfuscator
{

std::string formatFlagsMask(uint32_t mask);
std::string formatRegsMask(uint64_t mask);
std::vector<zasm::x86::Gp> maskToRegs(uint64_t mask);
uint32_t regMask(const zasm::Reg& reg);

struct InstructionData
{
uint64_t address = 0;
zasm::InstructionDetail detail = {};
zasm::InstrCPUFlags flagsModified = 0;
zasm::InstrCPUFlags flagsTested = 0;
uint32_t regsWritten = 0;
uint32_t regsRead = 0;

zasm::InstrCPUFlags flagsLive = 0;
uint32_t regsLive = 0;
};

struct Context
{
zasm::Program& program;

explicit Context(zasm::Program& program) : program(program)
{
}

InstructionData* addInstructionData(
zasm::Node* node, uint64_t address, zasm::MachineMode mode, const zasm::InstructionDetail& detail
);

private:
std::deque<InstructionData> instructionDataPool;
};

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

#include <obfuscator/context.hpp>

namespace obfuscator
{
bool disassemble(Context& ctx, const uint64_t functionStart, const std::vector<uint8_t>& code, bool verbose = false);
}
30 changes: 30 additions & 0 deletions obfuscator/include/obfuscator/msvc-secure.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <cstring>
#include <cstdio>
#include <cerrno>

// NOTE: These are source-compatible stubs for some MSVC-specific functions.

#ifndef _WIN32
template <size_t Count, class... Args> int sprintf_s(char (&Dest)[Count], const char* fmt, Args... args)
{
return snprintf(Dest, Count, fmt, args...);
}

inline size_t strcpy_s(char* dst, size_t size, const char* src)
{
return strlcpy(dst, src, size);
}

inline int fopen_s(FILE** fp, const char* filename, const char* mode)
{
*fp = fopen(filename, mode);
return errno;
}

static void __debugbreak()
{
__builtin_debugtrap();
}
#endif // _WIN32
8 changes: 8 additions & 0 deletions obfuscator/include/obfuscator/obfuscate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <obfuscator/context.hpp>

namespace obfuscator
{
bool obfuscate(Context& ctx);
}
12 changes: 12 additions & 0 deletions obfuscator/include/obfuscator/utility.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <cstdint>
#include <vector>
#include <span>
#include <string_view>

namespace obfuscator
{
bool loadFile(const std::string& path, std::vector<uint8_t>& data);
bool findFunction(const std::span<uint8_t>& pe, std::string_view name, uint64_t& address, std::vector<uint8_t>& code);
} // namespace obfuscator
Loading

0 comments on commit ff74c2c

Please sign in to comment.